component.ts 954 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { shallowMergeArray } from '../mol-util/object';
  7. import { RxEventHelper } from '../mol-util/rx-event-helper';
  8. export class PluginComponent<State> {
  9. private _ev: RxEventHelper | undefined;
  10. protected get ev() {
  11. return this._ev || (this._ev = RxEventHelper.create());
  12. }
  13. private _state: State;
  14. protected updateState(...states: Partial<State>[]): boolean {
  15. const latest = this.state;
  16. const s = shallowMergeArray(latest, states);
  17. if (s !== latest) {
  18. this._state = s;
  19. return true;
  20. }
  21. return false;
  22. }
  23. get state() {
  24. return this._state;
  25. }
  26. dispose() {
  27. if (this._ev) this._ev.dispose();
  28. }
  29. constructor(initialState: State) {
  30. this._state = initialState;
  31. }
  32. }