object.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 { UUID } from 'mol-util';
  7. import { Transform } from './transform';
  8. export { StateObject, StateObjectCell }
  9. interface StateObject<P = any, D = any, T = any> {
  10. readonly id: UUID,
  11. readonly type: StateObject.Type<T>,
  12. readonly props: P,
  13. readonly data: D
  14. }
  15. namespace StateObject {
  16. export function factory<Type, CommonProps>() {
  17. return <D = { }, P = {}>(type: Type) => create<P & CommonProps, D, Type>(type);
  18. }
  19. export type Type<I = unknown> = I
  20. export type Ctor = { new(...args: any[]): StateObject, type: any }
  21. export function create<Props, Data, Type>(type: Type) {
  22. return class implements StateObject<Props, Data, Type> {
  23. static type = type;
  24. static is(obj?: StateObject): obj is StateObject<Props, Data, Type> { return !!obj && type === obj.type; }
  25. id = UUID.create();
  26. type = type;
  27. constructor(public props: Props, public data: Data) { }
  28. }
  29. }
  30. }
  31. interface StateObjectCell {
  32. ref: Transform.Ref,
  33. version: string
  34. status: StateObjectCell.Status,
  35. state: unknown,
  36. errorText?: string,
  37. obj?: StateObject
  38. }
  39. namespace StateObjectCell {
  40. export type Status = 'ok' | 'error' | 'pending' | 'processing'
  41. export interface State {
  42. isVisible: boolean,
  43. isHidden: boolean,
  44. isBound: boolean,
  45. isExpanded: boolean
  46. }
  47. }