-
-
Notifications
You must be signed in to change notification settings - Fork 115
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
3a273b7
commit 4248f55
Showing
9 changed files
with
210 additions
and
223 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,44 @@ | ||
|
||
class EventManager { | ||
constructor() { | ||
this._registry = {}; | ||
} | ||
|
||
unsubscribeAll() { | ||
this._registry = {}; | ||
} | ||
|
||
subscribeMulti(names, handler) { | ||
names.forEach((name) => { | ||
this.subscribe(name, handler); | ||
}); | ||
} | ||
|
||
subscribe(name, handler) { | ||
if (!name || !handler) throw new Error("name and handler are required."); | ||
if (!this._registry[name]) this._registry[name] = []; | ||
this._registry[name].push(handler); | ||
} | ||
|
||
unsubscribe(name, handler) { | ||
if (!this._registry[name]) return; | ||
const index = this._registry[name].indexOf(handler); | ||
if (index <= -1) return; | ||
this._registry[name].splice(index, 1); | ||
} | ||
|
||
publish(name, ...args) { | ||
if (!this._registry[name]) return; | ||
const handlers = this._registry[name]; | ||
handlers.forEach((handler) => { | ||
handler(...args); | ||
}); | ||
} | ||
|
||
async publishWithResult(name, ...args) { | ||
if (!this._registry[name]) return true; | ||
const handlers = this._registry[name]; | ||
if (handlers.length <= 0) return true; | ||
return await Promise.all(handlers.map((handler) => handler(...args))); | ||
} | ||
constructor() { | ||
this._registry = {}; | ||
} | ||
|
||
unsubscribeAll() { | ||
this._registry = {}; | ||
} | ||
|
||
subscribeMulti(names, handler) { | ||
names.forEach((name) => { | ||
this.subscribe(name, handler); | ||
}); | ||
} | ||
|
||
subscribe(name, handler) { | ||
if (!name || !handler) throw new Error("name and handler are required."); | ||
if (!this._registry[name]) this._registry[name] = []; | ||
this._registry[name].push(handler); | ||
} | ||
|
||
unsubscribe(name, handler) { | ||
if (!this._registry[name]) return; | ||
const index = this._registry[name].indexOf(handler); | ||
if (index <= -1) return; | ||
this._registry[name].splice(index, 1); | ||
} | ||
|
||
publish(name, ...args) { | ||
if (!this._registry[name]) return; | ||
const handlers = this._registry[name]; | ||
handlers.forEach((handler) => { | ||
handler(...args); | ||
}); | ||
} | ||
|
||
async publishWithResult(name, ...args) { | ||
if (!this._registry[name]) return true; | ||
const handlers = this._registry[name]; | ||
if (handlers.length <= 0) return true; | ||
return await Promise.all(handlers.map((handler) => handler(...args))); | ||
} | ||
export default EventManager; | ||
} | ||
export default EventManager; |
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
Oops, something went wrong.