context.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 { StateObject } from './object';
  7. import { Transform } from './transform';
  8. import { RxEventHelper } from 'mol-util/rx-event-helper';
  9. export { StateContext }
  10. class StateContext {
  11. private ev = RxEventHelper.create();
  12. readonly events = {
  13. object: {
  14. stateChanged: this.ev<{ ref: Transform.Ref }>(),
  15. propsChanged: this.ev<{ ref: Transform.Ref, newProps: unknown }>(),
  16. updated: this.ev<{ ref: Transform.Ref, obj?: StateObject }>(),
  17. replaced: this.ev<{ ref: Transform.Ref, oldObj?: StateObject, newObj?: StateObject }>(),
  18. created: this.ev<{ ref: Transform.Ref, obj: StateObject }>(),
  19. removed: this.ev<{ ref: Transform.Ref, obj?: StateObject }>(),
  20. currentChanged: this.ev<{ ref: Transform.Ref }>()
  21. },
  22. warn: this.ev<string>(),
  23. updated: this.ev<void>()
  24. };
  25. readonly behaviors = {
  26. currentObject: this.ev.behavior<{ ref: Transform.Ref }>(void 0 as any)
  27. };
  28. readonly globalContext: unknown;
  29. readonly defaultObjectProps: unknown;
  30. dispose() {
  31. this.ev.dispose();
  32. }
  33. constructor(params: { globalContext: unknown, defaultObjectProps: unknown, rootRef: Transform.Ref }) {
  34. this.globalContext = params.globalContext;
  35. this.defaultObjectProps = params.defaultObjectProps;
  36. this.behaviors.currentObject.next({ ref: params.rootRef });
  37. }
  38. }