Skip to content

Commit

Permalink
fix: no add each hook (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
JerrysShan authored Jul 28, 2022
1 parent 22fe1a0 commit d9d5ee2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/loader/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class LoaderFactory {
}
await this.loaderEmitter.emitBeforeEach(curLoader, item);
const result = await this.loadItem(item);
await this.loaderEmitter.emitAfterEach(curLoader, result);
await this.loaderEmitter.emitAfterEach(curLoader, item, result);
}
if (prevLoader) {
await this.loaderEmitter.emitAfter(prevLoader);
Expand Down
14 changes: 11 additions & 3 deletions src/loader/loader_event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ export interface LoaderEventListener {

beforeEach?: (item: ManifestItem) => void;

afterEach?: (loadContent: any) => void;
afterEach?: (item: ManifestItem, loadContent: any) => void;
}

export default class LoaderEventEmitter {
private listeners: Record<string, Record<'before' | 'after', CallableFunction[]>> = {};
private listeners: Record<string, Record<keyof LoaderEventListener, CallableFunction[]>> = {};

addListener(eventName, listener: LoaderEventListener) {
if (!this.listeners[eventName]) {
this.listeners[eventName] = { before: [], after: [] };
this.listeners[eventName] = { before: [], after: [], beforeEach: [], afterEach: [] };
}

if (listener.before) {
Expand All @@ -24,6 +24,14 @@ export default class LoaderEventEmitter {
if (listener.after) {
this.listeners[eventName].after.push(listener.after);
}

if (listener.beforeEach) {
this.listeners[eventName].beforeEach.push(listener.beforeEach);
}

if (listener.afterEach) {
this.listeners[eventName].afterEach.push(listener.afterEach);
}
}

removeListener(eventName: string, stage?: keyof LoaderEventListener) {
Expand Down

0 comments on commit d9d5ee2

Please sign in to comment.