refactor: use dependency inject.

This commit is contained in:
Ansolve 2023-11-27 19:27:55 +08:00
parent e050c8b166
commit dd74345242
No known key found for this signature in database
GPG Key ID: 2F2873AC9538CC2D
1 changed files with 18 additions and 6 deletions

View File

@ -12,16 +12,28 @@ export class Announce {
/**
* @type {EventTarget}
*/
#handler;
#eventTarget;
/**
* @type {WeakMap<function(any): void, AnnounceSubscriber>}
*/
#records;
constructor() {
this.#handler = new EventTarget();
this.#records = new WeakMap();
/**
* @type {FunctionConstructor}
*/
#SubscriberType;
/**
*
* @param {EventTarget} eventTarget
* @param {WeakMap<function(any): void, AnnounceSubscriber>} records
* @param {FunctionConstructor} [SubscriberType]
*/
constructor(eventTarget, records, SubscriberType = AnnounceSubscriber) {
this.#eventTarget = eventTarget;
this.#records = records;
this.#SubscriberType = SubscriberType;
}
/**
@ -35,7 +47,7 @@ export class Announce {
* @returns {T}
*/
publish(name, values) {
this.#handler.dispatchEvent(new CustomEvent(name, {
this.#eventTarget.dispatchEvent(new CustomEvent(name, {
detail: [values, name]
}));
return values;
@ -58,7 +70,7 @@ export class Announce {
if (this.#records.has(method))
subscriber = this.#records.get(method);
else {
subscriber = new AnnounceSubscriber(method, this.#handler);
subscriber = new this.#SubscriberType(method, this.#eventTarget);
this.#records.set(method, subscriber);
}
subscriber.subscribe(name);