state.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // import { State, StateObject, StateTree, Transformer } from '../mol-state';
  2. // import { Task } from '../mol-task';
  3. // import * as util from 'util';
  4. // export type TypeClass = 'root' | 'shape' | 'prop'
  5. // export interface ObjProps { label: string }
  6. // export interface TypeInfo { name: string, class: TypeClass }
  7. // const _obj = StateObject.factory<TypeInfo, ObjProps>()
  8. // const _transform = Transformer.factory('test');
  9. // export class Root extends _obj({ name: 'Root', class: 'root' }) { }
  10. // export class Square extends _obj<{ a: number }>({ name: 'Square', class: 'shape' }) { }
  11. // export class Circle extends _obj<{ r: number }>({ name: 'Circle', class: 'shape' }) { }
  12. // export class Area extends _obj<{ volume: number }>({ name: 'Area', class: 'prop' }) { }
  13. // export const CreateSquare = _transform<Root, Square, { a: number }>({
  14. // name: 'create-square',
  15. // from: [Root],
  16. // to: [Square],
  17. // apply({ params: p }) {
  18. // return new Square({ label: `Square a=${p.a}` }, p);
  19. // },
  20. // update({ b, newParams: p }) {
  21. // b.props.label = `Square a=${p.a}`
  22. // b.data.a = p.a;
  23. // return Transformer.UpdateResult.Updated;
  24. // }
  25. // });
  26. // export const CreateCircle = _transform<Root, Circle, { r: number }>({
  27. // name: 'create-circle',
  28. // from: [Root],
  29. // to: [Square],
  30. // apply({ params: p }) {
  31. // return new Circle({ label: `Circle r=${p.r}` }, p);
  32. // },
  33. // update({ b, newParams: p }) {
  34. // b.props.label = `Circle r=${p.r}`
  35. // b.data.r = p.r;
  36. // return Transformer.UpdateResult.Updated;
  37. // }
  38. // });
  39. // export const CaclArea = _transform<Square | Circle, Area, {}>({
  40. // name: 'calc-area',
  41. // from: [Square, Circle],
  42. // to: [Area],
  43. // apply({ a }) {
  44. // if (a instanceof Square) return new Area({ label: 'Area' }, { volume: a.data.a * a.data.a });
  45. // else if (a instanceof Circle) return new Area({ label: 'Area' }, { volume: a.data.r * a.data.r * Math.PI });
  46. // throw new Error('Unknown object type.');
  47. // },
  48. // update({ a, b }) {
  49. // if (a instanceof Square) b.data.volume = a.data.a * a.data.a;
  50. // else if (a instanceof Circle) b.data.volume = a.data.r * a.data.r * Math.PI;
  51. // else throw new Error('Unknown object type.');
  52. // return Transformer.UpdateResult.Updated;
  53. // }
  54. // });
  55. // export async function runTask<A>(t: A | Task<A>): Promise<A> {
  56. // if ((t as any).run) return await (t as Task<A>).run();
  57. // return t as A;
  58. // }
  59. // function hookEvents(state: State) {
  60. // state.events.object.created.subscribe(e => console.log('created:', e.ref));
  61. // state.events.object.removed.subscribe(e => console.log('removed:', e.ref));
  62. // state.events.object.replaced.subscribe(e => console.log('replaced:', e.ref));
  63. // state.events.object.cellState.subscribe(e => console.log('stateChanged:', e.ref, e.cell.status));
  64. // state.events.object.updated.subscribe(e => console.log('updated:', e.ref));
  65. // }
  66. // export async function testState() {
  67. // const state = State.create(new Root({ label: 'Root' }, { }));
  68. // hookEvents(state);
  69. // const tree = state.tree;
  70. // const builder = tree.build();
  71. // builder.toRoot<Root>()
  72. // .apply(CreateSquare, { a: 10 }, { ref: 'square' })
  73. // .apply(CaclArea);
  74. // const tree1 = builder.getTree();
  75. // printTTree(tree1);
  76. // const tree2 = StateTree.updateParams<typeof CreateSquare>(tree1, 'square', { a: 15 });
  77. // printTTree(tree1);
  78. // printTTree(tree2);
  79. // await state.update(tree1).run();
  80. // console.log('----------------');
  81. // console.log(util.inspect(state.cells, true, 3, true));
  82. // console.log('----------------');
  83. // const jsonString = JSON.stringify(StateTree.toJSON(tree2), null, 2);
  84. // const jsonData = JSON.parse(jsonString);
  85. // printTTree(tree2);
  86. // console.log(jsonString);
  87. // const treeFromJson = StateTree.fromJSON(jsonData);
  88. // printTTree(treeFromJson);
  89. // console.log('----------------');
  90. // await state.update(treeFromJson).run();
  91. // console.log(util.inspect(state.cells, true, 3, true));
  92. // console.log('----------------');
  93. // const sel = state.select('square');
  94. // console.log(sel);
  95. // }
  96. // testState();
  97. // //test();
  98. // export function printTTree(tree: StateTree) {
  99. // let lines: string[] = [];
  100. // function print(offset: string, ref: any) {
  101. // const t = tree.nodes.get(ref)!;
  102. // const tr = t;
  103. // const name = tr.transformer.id;
  104. // lines.push(`${offset}|_ (${ref}) ${name} ${tr.params ? JSON.stringify(tr.params) : ''}, v${t.version}`);
  105. // offset += ' ';
  106. // tree.children.get(ref).forEach(c => print(offset, c!));
  107. // }
  108. // print('', tree.root.ref);
  109. // console.log(lines.join('\n'));
  110. // }