-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix] build issues, added mixing dependencies. (#17)
- Loading branch information
1 parent
8c26482
commit 1526311
Showing
2 changed files
with
31 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,34 @@ | ||
export type DispatcherHandler<T> = (data?: T) => Promise<void> | ||
export type DispatcherHandler<T> = (data?: T) => Promise<void>; | ||
|
||
export class Dispatcher<DispatchData> { | ||
private subHandlers: Record<string, DispatcherHandler<DispatchData>[]> = {} | ||
private subHandlers: Record<string, DispatcherHandler<DispatchData>[]> = {}; | ||
|
||
public regist (key: string, handler: DispatcherHandler<DispatchData>) { | ||
if (Reflect.has(this.subHandlers, key) && typeof Array.isArray(this.subHandlers[key])) { | ||
this.subHandlers[key].push(handler) | ||
public regist(key: string, handler: DispatcherHandler<DispatchData>) { | ||
if ( | ||
Reflect.has(this.subHandlers, key) && | ||
typeof Array.isArray(this.subHandlers[key]) | ||
) { | ||
this.subHandlers[key].push(handler); | ||
} else { | ||
this.subHandlers[key] = [handler] | ||
this.subHandlers[key] = [handler]; | ||
} | ||
} | ||
|
||
public batchRegist (list: { key: string; handler: DispatcherHandler<DispatchData> }[]) { | ||
list.forEach((item) => this.regist(item.key, item.handler)) | ||
public batchRegist( | ||
list: { key: string; handler: DispatcherHandler<DispatchData> }[] | ||
) { | ||
list.forEach((item) => this.regist(item.key, item.handler)); | ||
} | ||
|
||
public async dispatch (key: string, data: DispatchData) { | ||
const handlers = this.subHandlers[key] | ||
public async dispatch(key: string, data: DispatchData) { | ||
const handlers = this.subHandlers[key]; | ||
|
||
if (!handlers) return | ||
if (!handlers) return; | ||
|
||
await Promise.all(handlers.map((handler) => handler(data))).catch((e) => { | ||
logger.info(e.stack.toString()) | ||
// logger.info(e.stack.toString()) | ||
|
||
throw e | ||
}) | ||
throw e; | ||
}); | ||
} | ||
} | ||
} |