mirror of
https://github.com/Instadapp/assembly.git
synced 2024-07-29 22:37:06 +00:00
25 lines
576 B
TypeScript
25 lines
576 B
TypeScript
import { onBeforeUnmount } from "@nuxtjs/composition-api";
|
|
import { TinyEmitter } from "tiny-emitter";
|
|
|
|
const eventEmitter = new TinyEmitter();
|
|
|
|
export default function useEventBus() {
|
|
const eventHandlers = [];
|
|
|
|
onBeforeUnmount(() =>
|
|
eventHandlers.forEach(eventHandler =>
|
|
eventEmitter.off(eventHandler.event, eventHandler.handler)
|
|
)
|
|
);
|
|
|
|
return {
|
|
onEvent(event, handler) {
|
|
eventHandlers.push({ event, handler });
|
|
eventEmitter.on(event, handler);
|
|
},
|
|
emitEvent(event, payload) {
|
|
eventEmitter.emit(event, payload);
|
|
}
|
|
};
|
|
}
|