diff --git a/README.md b/README.md index 8fa6526..7bd71ad 100644 --- a/README.md +++ b/README.md @@ -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: { diff --git a/dist/hooks/index.d.ts b/dist/hooks/index.d.ts index ba5f59e..e27309a 100644 --- a/dist/hooks/index.d.ts +++ b/dist/hooks/index.d.ts @@ -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'; diff --git a/dist/hooks/use-event.d.ts b/dist/hooks/use-event.d.ts new file mode 100644 index 0000000..d54fd98 --- /dev/null +++ b/dist/hooks/use-event.d.ts @@ -0,0 +1,11 @@ +/// +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; diff --git a/dist/index.js b/dist/index.js index c238800..9b4049d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -(()=>{"use strict";var e={610:function(e,t,r){var n=this&&this.__createBinding||(Object.create?function(e,t,r,n){void 0===n&&(n=r);var o=Object.getOwnPropertyDescriptor(t,r);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,n,o)}:function(e,t,r,n){void 0===n&&(n=r),e[n]=t[r]}),o=this&&this.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||n(t,e,r)};Object.defineProperty(t,"__esModule",{value:!0}),o(r(159),t),o(r(644),t),o(r(418),t)},644:function(e,t,r){var n=this&&this.__createBinding||(Object.create?function(e,t,r,n){void 0===n&&(n=r);var o=Object.getOwnPropertyDescriptor(t,r);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,n,o)}:function(e,t,r,n){void 0===n&&(n=r),e[n]=t[r]}),o=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),i=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)"default"!==r&&Object.prototype.hasOwnProperty.call(e,r)&&n(t,e,r);return o(t,e),t},u=this&&this.__rest||function(e,t){var r={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(n=Object.getOwnPropertySymbols(e);o{Object.defineProperty(t,"__esModule",{value:!0}),t.SceneProvider=t.SceneContext=void 0;var n=r(497);t.SceneContext=(0,n.createContext)(null),t.SceneProvider=t.SceneContext.Provider},362:function(e,t,r){var n=this&&this.__createBinding||(Object.create?function(e,t,r,n){void 0===n&&(n=r);var o=Object.getOwnPropertyDescriptor(t,r);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,n,o)}:function(e,t,r,n){void 0===n&&(n=r),e[n]=t[r]}),o=this&&this.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||n(t,e,r)};Object.defineProperty(t,"__esModule",{value:!0}),o(r(386),t),o(r(664),t),o(r(531),t),o(r(692),t),o(r(465),t),o(r(870),t),o(r(666),t),o(r(457),t),o(r(13),t),o(r(90),t),o(r(253),t),o(r(519),t)},253:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.useClickOutside=void 0;var n=r(497),o=r(13);t.useClickOutside=function(e,t,r){var i=(0,o.useMobilePlatform)(),u=(0,n.useCallback)((function(r){e.current&&(r.composedPath().includes(e.current)||t())}),r);(0,n.useEffect)((function(){var e=i?"touchend":"mouseup";return document.addEventListener(e,u),function(){document.removeEventListener(e,u)}}),[i,u])}},90:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.useClick=void 0;var n=r(497),o=r(13);t.useClick=function(e,t,r,i){var u=(0,o.useMobilePlatform)(),a=(0,n.useCallback)((function(e){r(e),e.stopPropagation(),e.preventDefault()}),i);(0,n.useEffect)((function(){var r,n=e.current;if(n)return r=u?"up"===t?"touchend":"touchstart":"up"===t?"mouseup":"mousedown",n.addEventListener(r,a),function(){n.removeEventListener(r,a)}}),[t,u,a])}},664:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.useCurrentScene=void 0;var n=r(497),o=r(794);t.useCurrentScene=function(){var e=(0,n.useContext)(o.SceneContext);if(!e)throw Error("Undefined scene context");return e}},386:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.useGame=void 0;var n=r(497),o=r(794);t.useGame=function(){var e=(0,n.useContext)(o.SceneContext);if(!e)throw Error("Undefined scene context");return e.game}},519:(e,t,r)=>{function n(e,t){if(e){if("string"==typeof e)return o(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?o(e,t):void 0}}function o(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r{function n(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r{Object.defineProperty(t,"__esModule",{value:!0}),t.useMobilePlatform=void 0;var n=r(386);t.useMobilePlatform=function(){return!(0,n.useGame)().device.os.desktop}},465:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.useRelativePosition=void 0;var n=r(497),o=r(664),i=r(692);t.useRelativePosition=function(e){var t=e.x,r=e.y,u=e.camera,a=(0,o.useCurrentScene)(),c=null!=u?u:a.cameras.main,l=(0,n.useRef)(null);return(0,i.useSceneUpdate)(a,(function(){if(l.current){var e=Math.round((t-c.worldView.x)*c.zoom),n=Math.round((r-c.worldView.y)*c.zoom);l.current.style.transform="translate(".concat(e,"px, ").concat(n,"px)")}}),[t,r,c]),l}},870:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.useRelativeScale=void 0;var n=r(497),o=r(386);t.useRelativeScale=function(e){var t=e.target,r=e.min,i=e.max,u=e.round,a=(0,o.useGame)(),c=(0,n.useRef)(null),l=(0,n.useCallback)((function(){if(c.current){var e=a.canvas.parentElement.clientWidth/t;"number"==typeof i&&(e=Math.min(e,i)),"number"==typeof r&&(e=Math.max(e,r)),u&&(e=Math.round(10*e)/10),c.current.style.removeProperty("transform"),c.current.style.removeProperty("transformOrigin"),c.current.style.removeProperty("width"),c.current.style.removeProperty("height");var n=c.current.clientWidth,o=c.current.clientHeight;c.current.style.transform="scale(".concat(e,")"),c.current.style.transformOrigin="0 0",c.current.style.width="".concat(n/e,"px"),c.current.style.height="".concat(o/e,"px")}}),[t,r,i,u]);return(0,n.useEffect)((function(){return l(),window.addEventListener("resize",l),function(){window.removeEventListener("resize",l)}}),[l]),c}},692:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.useSceneUpdate=void 0;var n=r(497);t.useSceneUpdate=function(e,t,r){(0,n.useEffect)((function(){return t(),e.events.on("update",t),function(){e.events.off("update",t)}}),r)}},531:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.useScene=void 0;var n=r(497),o=r(386);t.useScene=function(e){var t=(0,o.useGame)();return(0,n.useMemo)((function(){return t.scene.getScene(e)}),[e])}},666:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.useTexture=void 0;var n=r(497),o=r(386);t.useTexture=function(e){var t=(0,o.useGame)();return(0,n.useMemo)((function(){return t.textures.get(e).getSourceImage()}),[e])}},820:function(e,t,r){var n=this&&this.__createBinding||(Object.create?function(e,t,r,n){void 0===n&&(n=r);var o=Object.getOwnPropertyDescriptor(t,r);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,n,o)}:function(e,t,r,n){void 0===n&&(n=r),e[n]=t[r]}),o=this&&this.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||n(t,e,r)};Object.defineProperty(t,"__esModule",{value:!0}),o(r(362),t),o(r(130),t),o(r(904),t),o(r(610),t)},130:function(e,t,r){function n(e){return n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},n(e)}function o(e,t){for(var r=0;r{function r(e,t){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!r){if(Array.isArray(e)||(r=function(e,t){if(e){if("string"==typeof e)return n(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(e,t):void 0}}(e))||t&&e&&"number"==typeof e.length){r&&(e=r);var o=0,i=function(){};return{s:i,n:function(){return o>=e.length?{done:!0}:{done:!1,value:e[o++]}},e:function(e){throw e},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var u,a=!0,c=!1;return{s:function(){r=r.call(e)},n:function(){var e=r.next();return a=e.done,e},e:function(e){c=!0,u=e},f:function(){try{a||null==r.return||r.return()}finally{if(c)throw u}}}}function n(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r{e.exports=require("phaser")},497:e=>{e.exports=require("react")},183:e=>{e.exports=require("react-dom/client")}},t={},r=function r(n){var o=t[n];if(void 0!==o)return o.exports;var i=t[n]={exports:{}};return e[n].call(i.exports,i,i.exports,r),i.exports}(820);module.exports=r})(); \ No newline at end of file +(()=>{"use strict";var e={610:function(e,t,r){var n=this&&this.__createBinding||(Object.create?function(e,t,r,n){void 0===n&&(n=r);var o=Object.getOwnPropertyDescriptor(t,r);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,n,o)}:function(e,t,r,n){void 0===n&&(n=r),e[n]=t[r]}),o=this&&this.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||n(t,e,r)};Object.defineProperty(t,"__esModule",{value:!0}),o(r(159),t),o(r(644),t),o(r(418),t)},644:function(e,t,r){var n=this&&this.__createBinding||(Object.create?function(e,t,r,n){void 0===n&&(n=r);var o=Object.getOwnPropertyDescriptor(t,r);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,n,o)}:function(e,t,r,n){void 0===n&&(n=r),e[n]=t[r]}),o=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),i=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)"default"!==r&&Object.prototype.hasOwnProperty.call(e,r)&&n(t,e,r);return o(t,e),t},u=this&&this.__rest||function(e,t){var r={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(n=Object.getOwnPropertySymbols(e);o{Object.defineProperty(t,"__esModule",{value:!0}),t.SceneProvider=t.SceneContext=void 0;var n=r(497);t.SceneContext=(0,n.createContext)(null),t.SceneProvider=t.SceneContext.Provider},362:function(e,t,r){var n=this&&this.__createBinding||(Object.create?function(e,t,r,n){void 0===n&&(n=r);var o=Object.getOwnPropertyDescriptor(t,r);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,n,o)}:function(e,t,r,n){void 0===n&&(n=r),e[n]=t[r]}),o=this&&this.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||n(t,e,r)};Object.defineProperty(t,"__esModule",{value:!0}),o(r(386),t),o(r(664),t),o(r(531),t),o(r(692),t),o(r(465),t),o(r(870),t),o(r(666),t),o(r(457),t),o(r(13),t),o(r(90),t),o(r(253),t),o(r(711),t),o(r(519),t)},253:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.useClickOutside=void 0;var n=r(497),o=r(13);t.useClickOutside=function(e,t,r){var i=(0,o.useMobilePlatform)(),u=(0,n.useCallback)((function(r){e.current&&(r.composedPath().includes(e.current)||t())}),r);(0,n.useEffect)((function(){var e=i?"touchend":"mouseup";return document.addEventListener(e,u),function(){document.removeEventListener(e,u)}}),[i,u])}},90:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.useClick=void 0;var n=r(497),o=r(13);t.useClick=function(e,t,r,i){var u=(0,o.useMobilePlatform)(),a=(0,n.useCallback)((function(e){r(e),e.stopPropagation(),e.preventDefault()}),i);(0,n.useEffect)((function(){var r,n=e.current;if(n)return r=u?"up"===t?"touchend":"touchstart":"up"===t?"mouseup":"mousedown",n.addEventListener(r,a),function(){n.removeEventListener(r,a)}}),[t,u,a])}},664:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.useCurrentScene=void 0;var n=r(497),o=r(794);t.useCurrentScene=function(){var e=(0,n.useContext)(o.SceneContext);if(!e)throw Error("Undefined scene context");return e}},711:(e,t,r)=>{function n(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r{Object.defineProperty(t,"__esModule",{value:!0}),t.useGame=void 0;var n=r(497),o=r(794);t.useGame=function(){var e=(0,n.useContext)(o.SceneContext);if(!e)throw Error("Undefined scene context");return e.game}},519:(e,t,r)=>{function n(e,t){if(e){if("string"==typeof e)return o(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?o(e,t):void 0}}function o(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r{function n(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r{Object.defineProperty(t,"__esModule",{value:!0}),t.useMobilePlatform=void 0;var n=r(386);t.useMobilePlatform=function(){return!(0,n.useGame)().device.os.desktop}},465:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.useRelativePosition=void 0;var n=r(497),o=r(664),i=r(692);t.useRelativePosition=function(e){var t=e.x,r=e.y,u=e.camera,a=(0,o.useCurrentScene)(),c=null!=u?u:a.cameras.main,l=(0,n.useRef)(null);return(0,i.useSceneUpdate)(a,(function(){if(l.current){var e=Math.round((t-c.worldView.x)*c.zoom),n=Math.round((r-c.worldView.y)*c.zoom);l.current.style.transform="translate(".concat(e,"px, ").concat(n,"px)")}}),[t,r,c]),l}},870:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.useRelativeScale=void 0;var n=r(497),o=r(386);t.useRelativeScale=function(e){var t=e.target,r=e.min,i=e.max,u=e.round,a=(0,o.useGame)(),c=(0,n.useRef)(null),l=(0,n.useCallback)((function(){if(c.current){var e=a.canvas.parentElement.clientWidth/t;"number"==typeof i&&(e=Math.min(e,i)),"number"==typeof r&&(e=Math.max(e,r)),u&&(e=Math.round(10*e)/10),c.current.style.removeProperty("transform"),c.current.style.removeProperty("transformOrigin"),c.current.style.removeProperty("width"),c.current.style.removeProperty("height");var n=c.current.clientWidth,o=c.current.clientHeight;c.current.style.transform="scale(".concat(e,")"),c.current.style.transformOrigin="0 0",c.current.style.width="".concat(n/e,"px"),c.current.style.height="".concat(o/e,"px")}}),[t,r,i,u]);return(0,n.useEffect)((function(){return l(),window.addEventListener("resize",l),function(){window.removeEventListener("resize",l)}}),[l]),c}},692:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.useSceneUpdate=void 0;var n=r(497);t.useSceneUpdate=function(e,t,r){(0,n.useEffect)((function(){return t(),e.events.on("update",t),function(){e.events.off("update",t)}}),r)}},531:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.useScene=void 0;var n=r(497),o=r(386);t.useScene=function(e){var t=(0,o.useGame)();return(0,n.useMemo)((function(){return t.scene.getScene(e)}),[e])}},666:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.useTexture=void 0;var n=r(497),o=r(386);t.useTexture=function(e){var t=(0,o.useGame)();return(0,n.useMemo)((function(){return t.textures.get(e).getSourceImage()}),[e])}},820:function(e,t,r){var n=this&&this.__createBinding||(Object.create?function(e,t,r,n){void 0===n&&(n=r);var o=Object.getOwnPropertyDescriptor(t,r);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,n,o)}:function(e,t,r,n){void 0===n&&(n=r),e[n]=t[r]}),o=this&&this.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||n(t,e,r)};Object.defineProperty(t,"__esModule",{value:!0}),o(r(362),t),o(r(130),t),o(r(904),t),o(r(610),t)},130:function(e,t,r){function n(e){return n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},n(e)}function o(e,t){for(var r=0;r{function r(e,t){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!r){if(Array.isArray(e)||(r=function(e,t){if(e){if("string"==typeof e)return n(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(e,t):void 0}}(e))||t&&e&&"number"==typeof e.length){r&&(e=r);var o=0,i=function(){};return{s:i,n:function(){return o>=e.length?{done:!0}:{done:!1,value:e[o++]}},e:function(e){throw e},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var u,a=!0,c=!1;return{s:function(){r=r.call(e)},n:function(){var e=r.next();return a=e.done,e},e:function(e){c=!0,u=e},f:function(){try{a||null==r.return||r.return()}finally{if(c)throw u}}}}function n(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r{e.exports=require("phaser")},497:e=>{e.exports=require("react")},183:e=>{e.exports=require("react-dom/client")}},t={},r=function r(n){var o=t[n];if(void 0!==o)return o.exports;var i=t[n]={exports:{}};return e[n].call(i.exports,i,i.exports,r),i.exports}(820);module.exports=r})(); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 0697fdc..74c1789 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "phaser-react-ui", - "version": "1.12.1", + "version": "1.13.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "phaser-react-ui", - "version": "1.12.1", + "version": "1.13.0", "license": "MIT", "devDependencies": { "@babel/core": "7.22.5", diff --git a/package.json b/package.json index 7e87f6b..65417b5 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/hooks/index.ts b/src/hooks/index.ts index ba5f59e..e27309a 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -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'; diff --git a/src/hooks/use-event.ts b/src/hooks/use-event.ts new file mode 100644 index 0000000..1acbba6 --- /dev/null +++ b/src/hooks/use-event.ts @@ -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]); +}