-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba604a1
commit 4522ae7
Showing
5 changed files
with
112 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
export interface LoaderEventListener { | ||
before?: CallableFunction; | ||
after?: CallableFunction; | ||
} | ||
|
||
export default class LoaderEventEmitter { | ||
private listeners: Record<string, Record<'before' | 'after', CallableFunction[]>> = {}; | ||
|
||
addListener(eventName, listener: LoaderEventListener) { | ||
if (!this.listeners[eventName]) { | ||
this.listeners[eventName] = { before: [], after: [] }; | ||
} | ||
|
||
if (listener.before) { | ||
this.listeners[eventName].before.push(listener.before); | ||
} | ||
|
||
if (listener.after) { | ||
this.listeners[eventName].after.push(listener.after); | ||
} | ||
} | ||
|
||
removeListener(eventName: string, stage: keyof LoaderEventListener) { | ||
if (!this.listeners[eventName]) { | ||
return; | ||
} | ||
if (stage) { | ||
this.listeners[eventName][stage] = []; | ||
return; | ||
} | ||
|
||
delete this.listeners[eventName]; | ||
} | ||
|
||
async emitBefore(eventName, ...args) { | ||
const { before = [] } = this.listeners[eventName] ?? {}; | ||
if (!before || before.length === 0) { | ||
return; | ||
} | ||
for (const listener of before) { | ||
await listener(...args); | ||
} | ||
} | ||
|
||
async emitAfter(eventName, ...args) { | ||
const { after = [] } = this.listeners[eventName] ?? {}; | ||
if (!after || after.length === 0) { | ||
return; | ||
} | ||
|
||
for (const listener of after) { | ||
await listener(...args); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters