UNPKG

molstar

Version:

A comprehensive macromolecular library.

80 lines (79 loc) 2.65 kB
/** * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal <david.sehnal@gmail.com> */ import { __extends } from "tslib"; import { shallowMergeArray } from '../mol-util/object'; import { RxEventHelper } from '../mol-util/rx-event-helper'; import { arraySetRemove } from '../mol-util/array'; var PluginComponent = /** @class */ (function () { function PluginComponent() { this.subs = void 0; } PluginComponent.prototype.subscribe = function (obs, action) { var _this = this; if (typeof this.subs === 'undefined') this.subs = []; var sub = obs.subscribe(action); this.subs.push(sub); return { unsubscribe: function () { if (sub && _this.subs && arraySetRemove(_this.subs, sub)) { sub.unsubscribe(); sub = void 0; } } }; }; Object.defineProperty(PluginComponent.prototype, "ev", { get: function () { return this._ev || (this._ev = RxEventHelper.create()); }, enumerable: false, configurable: true }); PluginComponent.prototype.dispose = function () { if (this._ev) this._ev.dispose(); if (this.subs) { for (var _i = 0, _a = this.subs; _i < _a.length; _i++) { var s = _a[_i]; s.unsubscribe(); } this.subs = void 0; } }; return PluginComponent; }()); export { PluginComponent }; var StatefulPluginComponent = /** @class */ (function (_super) { __extends(StatefulPluginComponent, _super); function StatefulPluginComponent(initialState) { var _this = _super.call(this) || this; _this._state = initialState; return _this; } StatefulPluginComponent.prototype.updateState = function () { var states = []; for (var _i = 0; _i < arguments.length; _i++) { states[_i] = arguments[_i]; } var latest = this.state; var s = shallowMergeArray(latest, states); if (s !== latest) { this._state = s; return true; } return false; }; Object.defineProperty(StatefulPluginComponent.prototype, "state", { get: function () { return this._state; }, enumerable: false, configurable: true }); return StatefulPluginComponent; }(PluginComponent)); export { StatefulPluginComponent };