Skip to content

Commit

Permalink
Updated useEvent hook
Browse files Browse the repository at this point in the history
  • Loading branch information
neki-dev committed Oct 28, 2023
1 parent 85631c0 commit 7c959e4
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dist/hooks/use-event.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ import type Phaser from 'phaser';
* @param callback - Event callback
* @param depends - Callback dependencies
*/
export declare function useEvent(emitter: Phaser.Events.EventEmitter, event: string, callback: (...args: any[]) => void, depends: any[]): void;
export declare function useEvent(emitter: Phaser.Events.EventEmitter | null, event: string, callback: (...args: any[]) => 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.14.2",
"version": "1.14.3",
"keywords": [
"phaser",
"interface",
Expand Down
8 changes: 6 additions & 2 deletions src/hooks/use-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ import { useEffect } from 'react';
* @param depends - Callback dependencies
*/
export function useEvent(
emitter: Phaser.Events.EventEmitter,
emitter: Phaser.Events.EventEmitter | null,
event: string,
callback: (...args: any[]) => void,
depends: any[],
) {
useEffect(() => {
if (!emitter) {
return;
}

emitter.on(event, callback);

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

0 comments on commit 7c959e4

Please sign in to comment.