refactor: use dependency inject.
This commit is contained in:
parent
e050c8b166
commit
dd74345242
|
@ -12,16 +12,28 @@ export class Announce {
|
||||||
/**
|
/**
|
||||||
* @type {EventTarget}
|
* @type {EventTarget}
|
||||||
*/
|
*/
|
||||||
#handler;
|
#eventTarget;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {WeakMap<function(any): void, AnnounceSubscriber>}
|
* @type {WeakMap<function(any): void, AnnounceSubscriber>}
|
||||||
*/
|
*/
|
||||||
#records;
|
#records;
|
||||||
|
|
||||||
constructor() {
|
/**
|
||||||
this.#handler = new EventTarget();
|
* @type {FunctionConstructor}
|
||||||
this.#records = new WeakMap();
|
*/
|
||||||
|
#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}
|
* @returns {T}
|
||||||
*/
|
*/
|
||||||
publish(name, values) {
|
publish(name, values) {
|
||||||
this.#handler.dispatchEvent(new CustomEvent(name, {
|
this.#eventTarget.dispatchEvent(new CustomEvent(name, {
|
||||||
detail: [values, name]
|
detail: [values, name]
|
||||||
}));
|
}));
|
||||||
return values;
|
return values;
|
||||||
|
@ -58,7 +70,7 @@ export class Announce {
|
||||||
if (this.#records.has(method))
|
if (this.#records.has(method))
|
||||||
subscriber = this.#records.get(method);
|
subscriber = this.#records.get(method);
|
||||||
else {
|
else {
|
||||||
subscriber = new AnnounceSubscriber(method, this.#handler);
|
subscriber = new this.#SubscriberType(method, this.#eventTarget);
|
||||||
this.#records.set(method, subscriber);
|
this.#records.set(method, subscriber);
|
||||||
}
|
}
|
||||||
subscriber.subscribe(name);
|
subscriber.subscribe(name);
|
||||||
|
|
Loading…
Reference in New Issue