Skip to content

Commit

Permalink
Added useEvent hook
Browse files Browse the repository at this point in the history
  • Loading branch information
neki-dev committed Oct 19, 2023
1 parent c45baf3 commit d7b4b09
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ useSceneUpdate(
)
```

#### Subscribe to event
```ts
useEvent(
emitter: Phaser.Scene | Phaser.GameObject,
event: string,
callback: () => void,
depends: any[]
)
```

#### Position relative to camera
```ts
useRelativePosition(position: {
Expand Down
1 change: 1 addition & 0 deletions dist/hooks/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export * from './use-match-media';
export * from './use-mobile-platform';
export * from './use-click';
export * from './use-click-outside';
export * from './use-event';
export * from './use-interaction';
11 changes: 11 additions & 0 deletions dist/hooks/use-event.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference types="node" />
import EventEmitter from 'events';
/**
* Subscribe to event.
*
* @param emitter - Events emitter
* @param event - Event
* @param callback - Event callback
* @param depends - Callback dependencies
*/
export declare function useEvent(emitter: EventEmitter, event: string, callback: () => void, depends: any[]): void;
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "phaser-react-ui",
"description": "React interface render for Phaser engine",
"version": "1.12.1",
"version": "1.13.0",
"keywords": [
"phaser",
"interface",
Expand Down
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export * from './use-match-media';
export * from './use-mobile-platform';
export * from './use-click';
export * from './use-click-outside';
export * from './use-event';
export * from './use-interaction';
25 changes: 25 additions & 0 deletions src/hooks/use-event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import EventEmitter from 'events';
import { useEffect } from 'react';

/**
* Subscribe to event.
*
* @param emitter - Events emitter
* @param event - Event
* @param callback - Event callback
* @param depends - Callback dependencies
*/
export function useEvent(
emitter: EventEmitter,
event: string,
callback: () => void,
depends: any[],
) {
useEffect(() => {
emitter.on(event, callback);

return () => {
emitter.off(event, callback);
};
}, [event, ...depends]);
}

0 comments on commit d7b4b09

Please sign in to comment.