controller.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * Adapted from LiteMol
  5. * Copyright (c) 2016 - now David Sehnal, licensed under Apache 2.0, See LICENSE file for more info.
  6. */
  7. import { BehaviorSubject } from 'rxjs';
  8. import { merge } from 'mol-util';
  9. import { Context } from '../context/context'
  10. import { LayoutRegion } from './layout';
  11. export class Controller<State> {
  12. private _state = new BehaviorSubject<State>(<any>void 0);
  13. private _latestState: State = <any>void 0;
  14. get dispatcher() {
  15. return this.context.dispatcher;
  16. }
  17. setState(...states: Partial<State>[]) {
  18. let s = merge(this._latestState, ...states);
  19. if (s !== this._latestState) {
  20. this._latestState = s;
  21. this._state.next(s);
  22. }
  23. }
  24. get state() {
  25. return this._state;
  26. }
  27. get latestState() {
  28. return this._latestState;
  29. }
  30. constructor(public context: Context, initialState: State) {
  31. this._latestState = initialState;
  32. }
  33. }
  34. export interface ControllerInfo {
  35. key: string;
  36. controller: Controller<any>;
  37. view: any;
  38. region: LayoutRegion;
  39. isStatic?: boolean;
  40. }