UNPKG

mvdom

Version:

deprecated - Moved to dom-native package

63 lines 2.2 kB
import { hub } from './hub'; const onHubEventByConstructor = new Map(); export function onHub(hubName, topic, label) { return function (target, propertyKey, descriptor) { const clazz = target.constructor; let onEvents = onHubEventByConstructor.get(clazz); if (onEvents == null) { onEvents = []; onHubEventByConstructor.set(clazz, onEvents); } const onEvent = { methodName: propertyKey, hubName, topic, label }; onEvents.push(onEvent); }; } export function bindOnHubDecorators() { let clazz = this.constructor; const topClazz = clazz; const fnNameBoundSet = new Set(); const opts = { ...this._nsObj, ctx: this }; while (clazz !== HTMLElement) { const onEvents = onHubEventByConstructor.get(clazz); if (onEvents) { for (const onEvent of onEvents) { const fnName = onEvent.methodName; if (!fnNameBoundSet.has(fnName)) { const fn = this[fnName]; const h = hub(onEvent.hubName); h.sub(onEvent.topic, onEvent.label, fn, opts); fnNameBoundSet.add(fnName); } } } clazz = Object.getPrototypeOf(clazz); } } export function unbindOnHubDecorators() { let clazz = this.constructor; const fnNameBoundSet = new Set(); const unboundHubNames = new Set(); const nsObj = this._nsObj; while (clazz !== HTMLElement) { const onEvents = onHubEventByConstructor.get(clazz); if (onEvents) { for (const onEvent of onEvents) { const hubName = onEvent.hubName; const fnName = onEvent.methodName; if (!fnNameBoundSet.has(fnName) && !unboundHubNames.has(hubName)) { const h = hub(hubName); h.unsub(nsObj); unboundHubNames.add(hubName); fnNameBoundSet.add(fnName); } } } clazz = Object.getPrototypeOf(clazz); } } //# sourceMappingURL=ts-decorator-on-hub.js.map