object.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. import { ParamDefinition } from 'mol-util/param-definition';
  9. export { StateObject, StateObjectCell }
  10. interface StateObject<D = any, T extends StateObject.Type = StateObject.Type<any>> {
  11. readonly id: UUID,
  12. readonly type: T,
  13. readonly data: D,
  14. readonly label: string,
  15. readonly description?: string,
  16. }
  17. namespace StateObject {
  18. export function factory<T extends Type>() {
  19. return <D = { }>(type: T) => create<D, T>(type);
  20. }
  21. export type Type<Cls extends string = string> = { name: string, typeClass: Cls }
  22. export type Ctor<T extends StateObject = StateObject> = { new(...args: any[]): T, type: any }
  23. export type From<C extends Ctor> = C extends Ctor<infer T> ? T : never
  24. export function create<Data, T extends Type>(type: T) {
  25. return class O implements StateObject<Data, T> {
  26. static type = type;
  27. static is(obj?: StateObject): obj is O { return !!obj && type === obj.type; }
  28. id = UUID.create22();
  29. type = type;
  30. label: string;
  31. description?: string;
  32. constructor(public data: Data, props?: { label: string, description?: string }) {
  33. this.label = props && props.label || type.name;
  34. this.description = props && props.description;
  35. }
  36. }
  37. }
  38. /** A special object indicating a transformer result has no value. */
  39. export const Null: StateObject<any, any> = {
  40. id: UUID.create22(),
  41. type: { name: 'Null', typeClass: 'Null' },
  42. data: void 0,
  43. label: 'Null'
  44. };
  45. }
  46. interface StateObjectCell {
  47. transform: Transform,
  48. // Which object was used as a parent to create data in this cell
  49. sourceRef: Transform.Ref | undefined,
  50. version: string
  51. status: StateObjectCell.Status,
  52. params: {
  53. definition: ParamDefinition.Params,
  54. values: any
  55. } | undefined;
  56. errorText?: string,
  57. obj?: StateObject
  58. }
  59. namespace StateObjectCell {
  60. export type Status = 'ok' | 'error' | 'pending' | 'processing'
  61. export interface State {
  62. isHidden: boolean,
  63. isCollapsed: boolean
  64. }
  65. export const DefaultState: State = { isHidden: false, isCollapsed: false };
  66. export function areStatesEqual(a: State, b: State) {
  67. return a.isHidden !== b.isHidden || a.isCollapsed !== b.isCollapsed;
  68. }
  69. export function isStateChange(a: State, b?: Partial<State>) {
  70. if (!b) return false;
  71. if (typeof b.isCollapsed !== 'undefined' && a.isCollapsed !== b.isCollapsed) return true;
  72. if (typeof b.isHidden !== 'undefined' && a.isHidden !== b.isHidden) return true;
  73. return false;
  74. }
  75. }