UNPKG

reaper-osc

Version:
64 lines 2.51 kB
"use strict"; /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ /* eslint-disable @typescript-eslint/ban-types */ /* eslint-disable @typescript-eslint/no-explicit-any */ Object.defineProperty(exports, "__esModule", { value: true }); exports.notifyOnPropertyChanged = exports.notify = void 0; const ste_simple_events_1 = require("ste-simple-events"); /** * Changes to the property will cause an event containing the name of the property to be fired by {@link INotifyPropertyChanged.onPropertyChanged}. * Initializing the property will trigger the event. * * @remarks * Requires {@link notifyOnPropertyChanged} decorator on the class * * @param overrideName Use to specify a different property name in the event. Useful for notifying changes to a get-only property from a change to a private backing file */ function notify(overrideName) { return (target, propertyKey) => { // Create a new prop to hold the value const valueKey = `_${propertyKey}Notify`; // Replace the decorated prop with getter/setter that handles the notifications Object.defineProperty(target, propertyKey, { set(value) { const oldValue = this[propertyKey]; if (value === oldValue) { return; } this[valueKey] = value; if (this._propertyChanged === undefined) { return; } const propertyName = overrideName ? overrideName : propertyKey; this._propertyChanged.dispatch(propertyName); }, get() { return this[valueKey]; }, }); }; } exports.notify = notify; /** * Adds an implementation of {@link INotifyPropertyChanged.onPropertyChanged} to the class. * * @remarks * Use in conjunction with {@link INotifyPropertyChanged} and decorating properties with {@link notify} */ function notifyOnPropertyChanged(constructor) { return class extends constructor { constructor() { super(...arguments); this._propertyChanged = new ste_simple_events_1.SimpleEventDispatcher(); } onPropertyChanged(property, callback) { return this._propertyChanged.sub(prop => { if (property === prop) { callback(); } }); } }; } exports.notifyOnPropertyChanged = notifyOnPropertyChanged; //# sourceMappingURL=Notify.js.map