UNPKG

@freemework/common

Version:

Common library of the Freemework Project.

58 lines 2.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FChannelEventMixin = void 0; const FChannelEventBase_js_1 = require("./FChannelEventBase.js"); /** * @example * // * // Following example shown how to use the Mixin class. * // SomeEventSource is a general class that extends from another class, so it * // is impossible to inherit FChannelEventImpl (due to not supported class * // multiple inheritance). By the way we may use Mixin approach... * // * class SomeEventSource extends Something { * protected onAddFirstHandler(): void { } // define in needed * protected onRemoveLastHandler(): void { } // define in needed * * public async someActivity(executionContext: FExecutionContext): Promise<void> { * const eventData: BroadcastMessage = {}; * await this.notify(executionContext, { data: eventData }); // Call all registered callbacks. * // Here we guaranteed all consumers processed the message without exceptions. * } * } * interface SomeEventSource extends FChannelEventMixin<ApplicationPageContext> { } * FChannelEventMixin.applyMixin(SomeEventSource); */ class FChannelEventMixin extends FChannelEventBase_js_1.FChannelEventBase { static applyMixin(targetClass) { Object.getOwnPropertyNames(FChannelEventBase_js_1.FChannelEventBase.prototype).forEach(name => { const propertyDescriptor = Object.getOwnPropertyDescriptor(FChannelEventBase_js_1.FChannelEventBase.prototype, name); if (name === "constructor") { // Skip constructor return; } if (name === "onAddFirstHandler" || name === "onRemoveLastHandler") { // Add NOP methods into mixed only if targetClass does not implement its if (propertyDescriptor !== undefined) { const existingPropertyDescriptor = Object.getOwnPropertyDescriptor(targetClass.prototype, name); if (existingPropertyDescriptor === undefined) { Object.defineProperty(targetClass.prototype, name, propertyDescriptor); } } return; } if (propertyDescriptor !== undefined) { Object.defineProperty(targetClass.prototype, name, propertyDescriptor); } }); } constructor() { // Never called, due mixin // Private constructor has two kinds of responsibility // 1) Restrict to extends the mixin // 2) Restrict to make instances of the mixin super(); } } exports.FChannelEventMixin = FChannelEventMixin; //# sourceMappingURL=FChannelEventMixin.js.map