state.ts 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /**
  2. * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { StateObject, StateObjectCell, StateObjectSelector } from './object';
  7. import { StateTree } from './tree';
  8. import { StateTransform } from './transform';
  9. import { StateTransformer } from './transformer';
  10. import { RuntimeContext, Task } from '../mol-task';
  11. import { StateSelection } from './state/selection';
  12. import { RxEventHelper } from '../mol-util/rx-event-helper';
  13. import { StateBuilder } from './state/builder';
  14. import { StateAction } from './action';
  15. import { StateActionManager } from './action/manager';
  16. import { TransientTree } from './tree/transient';
  17. import { LogEntry } from '../mol-util/log-entry';
  18. import { now, formatTimespan } from '../mol-util/now';
  19. import { ParamDefinition } from '../mol-util/param-definition';
  20. import { StateTreeSpine } from './tree/spine';
  21. import { AsyncQueue } from '../mol-util/async-queue';
  22. import { isProductionMode } from '../mol-util/debug';
  23. import { arraySetAdd, arraySetRemove } from '../mol-util/array';
  24. import { UniqueArray } from '../mol-data/generic';
  25. import { assignIfUndefined } from '../mol-util/object';
  26. export { State };
  27. class State {
  28. private _tree: TransientTree;
  29. protected errorFree = true;
  30. private ev = RxEventHelper.create();
  31. readonly globalContext: unknown = void 0;
  32. readonly events = {
  33. cell: {
  34. stateUpdated: this.ev<State.ObjectEvent & { cell: StateObjectCell }>(),
  35. created: this.ev<State.ObjectEvent & { cell: StateObjectCell }>(),
  36. removed: this.ev<State.ObjectEvent & { parent: StateTransform.Ref }>(),
  37. },
  38. object: {
  39. updated: this.ev<State.ObjectEvent & { action: 'in-place' | 'recreate', obj: StateObject, oldObj?: StateObject, oldData?: any }>(),
  40. created: this.ev<State.ObjectEvent & { obj: StateObject }>(),
  41. removed: this.ev<State.ObjectEvent & { obj?: StateObject }>()
  42. },
  43. log: this.ev<LogEntry>(),
  44. changed: this.ev<{ state: State, inTransaction: boolean }>(),
  45. historyUpdated: this.ev<{ state: State }>()
  46. };
  47. readonly behaviors = {
  48. currentObject: this.ev.behavior<State.ObjectEvent>({ state: this, ref: StateTransform.RootRef }),
  49. isUpdating: this.ev.behavior<boolean>(false),
  50. };
  51. readonly actions = new StateActionManager();
  52. readonly runTask: <T>(task: Task<T>) => Promise<T>;
  53. get tree(): StateTree { return this._tree; }
  54. get transforms() { return (this._tree as StateTree).transforms; }
  55. get current() { return this.behaviors.currentObject.value.ref; }
  56. get root() { return this.cells.get((this._tree as StateTree).root.ref)!; }
  57. build() { return new StateBuilder.Root(this.tree, this); }
  58. readonly cells: State.Cells = new Map();
  59. private spine = new StateTreeSpine.Impl(this.cells);
  60. private historyCapacity = 5;
  61. private history: [StateTree, string][] = [];
  62. private addHistory(tree: StateTree, label?: string) {
  63. if (this.historyCapacity === 0) return;
  64. this.history.unshift([tree, label || 'Update']);
  65. if (this.history.length > this.historyCapacity) this.history.pop();
  66. this.events.historyUpdated.next({ state: this });
  67. }
  68. private clearHistory() {
  69. if (this.history.length === 0) return;
  70. this.history = [];
  71. this.events.historyUpdated.next({ state: this });
  72. }
  73. get latestUndoLabel() {
  74. return this.history.length > 0 ? this.history[0][1] : void 0;
  75. }
  76. get canUndo() {
  77. return this.history.length > 0;
  78. }
  79. private undoingHistory = false;
  80. undo() {
  81. return Task.create('Undo', async ctx => {
  82. const e = this.history.shift();
  83. if (!e) return;
  84. this.events.historyUpdated.next({ state: this });
  85. this.undoingHistory = true;
  86. try {
  87. await this.updateTree(e[0], { canUndo: false }).runInContext(ctx);
  88. } finally {
  89. this.undoingHistory = false;
  90. }
  91. });
  92. }
  93. getSnapshot(): State.Snapshot {
  94. return { tree: StateTree.toJSON(this._tree) };
  95. }
  96. setSnapshot(snapshot: State.Snapshot) {
  97. const tree = StateTree.fromJSON(snapshot.tree);
  98. return this.updateTree(tree);
  99. }
  100. setCurrent(ref: StateTransform.Ref) {
  101. this.behaviors.currentObject.next({ state: this, ref });
  102. }
  103. updateCellState(ref: StateTransform.Ref, stateOrProvider: ((old: StateTransform.State) => Partial<StateTransform.State>) | Partial<StateTransform.State>) {
  104. const cell = this.cells.get(ref);
  105. if (!cell) return;
  106. const update = typeof stateOrProvider === 'function' ? stateOrProvider(cell.state) : stateOrProvider;
  107. if (StateTransform.assignState(cell.state, update)) {
  108. cell.transform = this._tree.assignState(cell.transform.ref, update);
  109. this.events.cell.stateUpdated.next({ state: this, ref, cell });
  110. }
  111. }
  112. dispose() {
  113. this.ev.dispose();
  114. this.actions.dispose();
  115. }
  116. /**
  117. * Select Cells using the provided selector.
  118. * @example state.query(StateSelection.Generators.byRef('test').ancestorOfType([type]))
  119. * @example state.query('test')
  120. */
  121. select<C extends StateObjectCell>(selector: StateSelection.Selector<C>) {
  122. return StateSelection.select(selector, this);
  123. }
  124. /**
  125. * Select Cells by building a query generated on the fly.
  126. * @example state.select(q => q.byRef('test').subtree())
  127. */
  128. selectQ<C extends StateObjectCell>(selector: (q: typeof StateSelection.Generators) => StateSelection.Selector<C>) {
  129. if (typeof selector === 'string') return StateSelection.select(selector, this);
  130. return StateSelection.select(selector(StateSelection.Generators), this);
  131. }
  132. /**
  133. * Creates a Task that applies the specified StateAction (i.e. must use run* on the result)
  134. * If no ref is specified, apply to root.
  135. */
  136. applyAction<A extends StateAction>(action: A, params: StateAction.Params<A>, ref: StateTransform.Ref = StateTransform.RootRef): Task<void> {
  137. return Task.create('Apply Action', ctx => {
  138. const cell = this.cells.get(ref);
  139. if (!cell) throw new Error(`'${ref}' does not exist.`);
  140. if (cell.status !== 'ok') throw new Error(`Action cannot be applied to a cell with status '${cell.status}'`);
  141. return runTask(action.definition.run({ ref, cell, a: cell.obj!, params, state: this }, this.globalContext), ctx);
  142. });
  143. }
  144. private inTransaction = false;
  145. private inTransactionError = false;
  146. /** Apply series of updates to the state. If any of them fail, revert to the original state. */
  147. transaction(edits: (ctx: RuntimeContext) => Promise<void> | void, options?: { canUndo?: string | boolean, rethrowErrors?: boolean }) {
  148. return Task.create('State Transaction', async ctx => {
  149. const isNested = this.inTransaction;
  150. // if (!isNested) this.changedInTransaction = false;
  151. const snapshot = this._tree.asImmutable();
  152. let restored = false;
  153. try {
  154. if (!isNested) this.behaviors.isUpdating.next(true);
  155. this.inTransaction = true;
  156. this.inTransactionError = false;
  157. await edits(ctx);
  158. if (this.inTransactionError) {
  159. restored = true;
  160. await this.updateTree(snapshot).runInContext(ctx);
  161. }
  162. } catch (e) {
  163. if (!restored) {
  164. restored = true;
  165. await this.updateTree(snapshot).runInContext(ctx);
  166. this.events.log.next(LogEntry.error('' + e));
  167. }
  168. if (isNested) {
  169. this.inTransactionError = true;
  170. throw e;
  171. }
  172. if (options?.rethrowErrors) throw e;
  173. } finally {
  174. if (!isNested) {
  175. this.inTransaction = false;
  176. this.events.changed.next({ state: this, inTransaction: false });
  177. this.behaviors.isUpdating.next(false);
  178. if (!restored) {
  179. if (options?.canUndo) this.addHistory(snapshot, typeof options.canUndo === 'string' ? options.canUndo : void 0);
  180. else this.clearHistory();
  181. }
  182. }
  183. }
  184. });
  185. }
  186. private _inUpdate = false;
  187. /**
  188. * Determines whether the state is currently "inside" updateTree function.
  189. * This is different from "isUpdating" which wraps entire transactions.
  190. */
  191. get inUpdate() { return this._inUpdate; }
  192. /**
  193. * Queues up a reconciliation of the existing state tree.
  194. *
  195. * If the tree is StateBuilder.To<T>, the corresponding StateObject is returned by the task.
  196. * @param tree Tree instance or a tree builder instance
  197. * @param doNotReportTiming Indicates whether to log timing of the individual transforms
  198. */
  199. updateTree<T extends StateObject>(tree: StateBuilder.To<T, any>, options?: Partial<State.UpdateOptions>): Task<StateObjectSelector<T>>
  200. updateTree(tree: StateTree | StateBuilder, options?: Partial<State.UpdateOptions>): Task<void>
  201. updateTree(tree: StateTree | StateBuilder, options?: Partial<State.UpdateOptions>): Task<any> {
  202. const params: UpdateParams = { tree, options };
  203. return Task.create('Update Tree', async taskCtx => {
  204. const removed = await this.updateQueue.enqueue(params);
  205. if (!removed) return;
  206. this._inUpdate = true;
  207. const snapshot = options?.canUndo ? this._tree.asImmutable() : void 0;
  208. let reverted = false;
  209. if (!this.inTransaction) this.behaviors.isUpdating.next(true);
  210. try {
  211. if (StateBuilder.is(tree)) {
  212. if (tree.editInfo.applied) throw new Error('This builder has already been applied. Create a new builder for further state updates');
  213. tree.editInfo.applied = true;
  214. }
  215. this.reverted = false;
  216. const ret = options && (options.revertIfAborted || options.revertOnError)
  217. ? await this._revertibleTreeUpdate(taskCtx, params, options)
  218. : await this._updateTree(taskCtx, params);
  219. reverted = this.reverted;
  220. if (ret.ctx.hadError) this.inTransactionError = true;
  221. if (!ret.cell) return;
  222. return new StateObjectSelector(ret.cell.transform.ref, this);
  223. } finally {
  224. this._inUpdate = false;
  225. this.updateQueue.handled(params);
  226. if (!this.inTransaction) {
  227. this.behaviors.isUpdating.next(false);
  228. if (!options?.canUndo) {
  229. if (!this.undoingHistory) this.clearHistory();
  230. } else if (!reverted) {
  231. this.addHistory(snapshot!, typeof options.canUndo === 'string' ? options.canUndo : void 0);
  232. }
  233. }
  234. }
  235. }, () => {
  236. this.updateQueue.remove(params);
  237. });
  238. }
  239. private reverted = false;
  240. private updateQueue = new AsyncQueue<UpdateParams>();
  241. private async _revertibleTreeUpdate(taskCtx: RuntimeContext, params: UpdateParams, options: Partial<State.UpdateOptions>) {
  242. const old = this.tree;
  243. const ret = await this._updateTree(taskCtx, params);
  244. let revert = ((ret.ctx.hadError || ret.ctx.wasAborted) && options.revertOnError) || (ret.ctx.wasAborted && options.revertIfAborted);
  245. if (revert) {
  246. this.reverted = true;
  247. return await this._updateTree(taskCtx, { tree: old, options: params.options });
  248. }
  249. return ret;
  250. }
  251. private async _updateTree(taskCtx: RuntimeContext, params: UpdateParams) {
  252. let updated = false;
  253. const ctx = this.updateTreeAndCreateCtx(params.tree, taskCtx, params.options);
  254. try {
  255. updated = await update(ctx);
  256. if (StateBuilder.isTo(params.tree)) {
  257. const cell = this.select(params.tree.ref)[0];
  258. return { ctx, cell };
  259. }
  260. return { ctx };
  261. } finally {
  262. this.spine.current = undefined;
  263. if (updated) this.events.changed.next({ state: this, inTransaction: this.inTransaction });
  264. }
  265. }
  266. private updateTreeAndCreateCtx(tree: StateTree | StateBuilder, taskCtx: RuntimeContext, options: Partial<State.UpdateOptions> | undefined) {
  267. const _tree = (StateBuilder.is(tree) ? tree.getTree() : tree).asTransient();
  268. const oldTree = this._tree;
  269. this._tree = _tree;
  270. const ctx: UpdateContext = {
  271. parent: this,
  272. editInfo: StateBuilder.is(tree) ? tree.editInfo : void 0,
  273. errorFree: this.errorFree,
  274. taskCtx,
  275. oldTree,
  276. tree: _tree,
  277. cells: this.cells as Map<StateTransform.Ref, StateObjectCell>,
  278. spine: this.spine,
  279. results: [],
  280. options: { ...StateUpdateDefaultOptions, ...options },
  281. changed: false,
  282. hadError: false,
  283. wasAborted: false,
  284. newCurrent: void 0
  285. };
  286. this.errorFree = true;
  287. return ctx;
  288. }
  289. constructor(rootObject: StateObject, params: State.Params) {
  290. this._tree = StateTree.createEmpty(StateTransform.createRoot(params && params.rootState)).asTransient();
  291. const tree = this._tree;
  292. const root = tree.root;
  293. this.runTask = params.runTask;
  294. if (params?.historyCapacity !== void 0) this.historyCapacity = params.historyCapacity;
  295. (this.cells as Map<StateTransform.Ref, StateObjectCell>).set(root.ref, {
  296. parent: this,
  297. transform: root,
  298. sourceRef: void 0,
  299. obj: rootObject,
  300. status: 'ok',
  301. state: { ...root.state },
  302. errorText: void 0,
  303. params: {
  304. definition: {},
  305. values: {}
  306. },
  307. dependencies: { dependentBy: [], dependsOn: [] },
  308. cache: { }
  309. });
  310. this.globalContext = params && params.globalContext;
  311. }
  312. }
  313. namespace State {
  314. export interface Params {
  315. runTask<T>(task: Task<T>): Promise<T>,
  316. globalContext?: unknown,
  317. rootState?: StateTransform.State,
  318. historyCapacity?: number
  319. }
  320. export function create(rootObject: StateObject, params: Params) {
  321. return new State(rootObject, params);
  322. }
  323. export type Cells = ReadonlyMap<StateTransform.Ref, StateObjectCell>
  324. export type Tree = StateTree
  325. export type Builder = StateBuilder
  326. export interface ObjectEvent {
  327. state: State,
  328. ref: Ref
  329. }
  330. export namespace ObjectEvent {
  331. export function isCell(e: ObjectEvent, cell?: StateObjectCell) {
  332. return !!cell && e.ref === cell.transform.ref && e.state === cell.parent;
  333. }
  334. }
  335. export interface Snapshot {
  336. readonly tree: StateTree.Serialized
  337. }
  338. export interface UpdateOptions {
  339. doNotLogTiming: boolean,
  340. doNotUpdateCurrent: boolean,
  341. revertIfAborted: boolean,
  342. revertOnError: boolean,
  343. canUndo: boolean | string
  344. }
  345. }
  346. const StateUpdateDefaultOptions: State.UpdateOptions = {
  347. doNotLogTiming: false,
  348. doNotUpdateCurrent: true,
  349. revertIfAborted: false,
  350. revertOnError: false,
  351. canUndo: false
  352. };
  353. type Ref = StateTransform.Ref
  354. type UpdateParams = { tree: StateTree | StateBuilder, options?: Partial<State.UpdateOptions> }
  355. interface UpdateContext {
  356. parent: State,
  357. editInfo: StateBuilder.EditInfo | undefined
  358. errorFree: boolean,
  359. taskCtx: RuntimeContext,
  360. oldTree: StateTree,
  361. tree: TransientTree,
  362. cells: Map<StateTransform.Ref, StateObjectCell>,
  363. spine: StateTreeSpine.Impl,
  364. results: UpdateNodeResult[],
  365. // suppress timing messages
  366. options: State.UpdateOptions,
  367. changed: boolean,
  368. hadError: boolean,
  369. wasAborted: boolean,
  370. newCurrent?: Ref
  371. }
  372. async function update(ctx: UpdateContext) {
  373. // if only a single node was added/updated, we can skip potentially expensive diffing
  374. const fastTrack = !!(ctx.editInfo && ctx.editInfo.count === 1 && ctx.editInfo.lastUpdate && ctx.editInfo.sourceTree === ctx.oldTree);
  375. let deletes: StateTransform.Ref[], deletedObjects: (StateObject | undefined)[] = [], roots: StateTransform.Ref[];
  376. if (fastTrack) {
  377. deletes = [];
  378. roots = [ctx.editInfo!.lastUpdate!];
  379. } else {
  380. // find all nodes that will definitely be deleted.
  381. // this is done in "post order", meaning that leaves will be deleted first.
  382. deletes = findDeletes(ctx);
  383. const current = ctx.parent.current;
  384. let hasCurrent = false;
  385. for (const d of deletes) {
  386. if (d === current) {
  387. hasCurrent = true;
  388. break;
  389. }
  390. }
  391. if (hasCurrent) {
  392. const newCurrent = findNewCurrent(ctx.oldTree, current, deletes, ctx.cells);
  393. ctx.parent.setCurrent(newCurrent);
  394. }
  395. for (let i = deletes.length - 1; i >= 0; i--) {
  396. const cell = ctx.cells.get(deletes[i]);
  397. if (cell) {
  398. dispose(cell.transform, cell.obj, cell?.transform.params, cell.cache, ctx.parent.globalContext);
  399. }
  400. }
  401. for (const d of deletes) {
  402. const cell = ctx.cells.get(d);
  403. if (cell) {
  404. cell.parent = void 0;
  405. unlinkCell(cell);
  406. }
  407. const obj = cell && cell.obj;
  408. ctx.cells.delete(d);
  409. deletedObjects.push(obj);
  410. }
  411. // Find roots where transform version changed or where nodes will be added.
  412. roots = findUpdateRoots(ctx.cells, ctx.tree);
  413. }
  414. // Init empty cells where not present
  415. // this is done in "pre order", meaning that "parents" will be created 1st.
  416. const init = initCells(ctx, roots);
  417. // Notify additions of new cells.
  418. for (const cell of init.added) {
  419. ctx.parent.events.cell.created.next({ state: ctx.parent, ref: cell.transform.ref, cell });
  420. }
  421. for (let i = 0; i < deletes.length; i++) {
  422. const d = deletes[i];
  423. const parent = ctx.oldTree.transforms.get(d).parent;
  424. ctx.parent.events.object.removed.next({ state: ctx.parent, ref: d, obj: deletedObjects[i] });
  425. ctx.parent.events.cell.removed.next({ state: ctx.parent, ref: d, parent: parent });
  426. }
  427. if (deletedObjects.length) deletedObjects = [];
  428. if (init.dependent) {
  429. for (const cell of init.dependent) {
  430. roots.push(cell.transform.ref);
  431. }
  432. }
  433. // Set status of cells that will be updated to 'pending'.
  434. initCellStatus(ctx, roots);
  435. // Sequentially update all the subtrees.
  436. for (const root of roots) {
  437. await updateSubtree(ctx, root);
  438. }
  439. // Sync cell states
  440. if (!ctx.editInfo) {
  441. syncNewStates(ctx);
  442. }
  443. let newCurrent: StateTransform.Ref | undefined = ctx.newCurrent;
  444. // Raise object updated events
  445. for (const update of ctx.results) {
  446. if (update.action === 'created') {
  447. ctx.parent.events.object.created.next({ state: ctx.parent, ref: update.ref, obj: update.obj! });
  448. if (!ctx.newCurrent) {
  449. const transform = ctx.tree.transforms.get(update.ref);
  450. if (!transform.state.isGhost && update.obj !== StateObject.Null) newCurrent = update.ref;
  451. }
  452. } else if (update.action === 'updated') {
  453. ctx.parent.events.object.updated.next({ state: ctx.parent, ref: update.ref, action: 'in-place', obj: update.obj, oldData: update.oldData });
  454. } else if (update.action === 'replaced') {
  455. ctx.parent.events.object.updated.next({ state: ctx.parent, ref: update.ref, action: 'recreate', obj: update.obj, oldObj: update.oldObj });
  456. }
  457. }
  458. if (newCurrent) {
  459. if (!ctx.options.doNotUpdateCurrent) ctx.parent.setCurrent(newCurrent);
  460. } else {
  461. // check if old current or its parent hasn't become null
  462. const current = ctx.parent.current;
  463. const currentCell = ctx.cells.get(current);
  464. if (currentCell && (currentCell.obj === StateObject.Null
  465. || (currentCell.status === 'error' && currentCell.errorText === ParentNullErrorText))) {
  466. newCurrent = findNewCurrent(ctx.oldTree, current, [], ctx.cells);
  467. ctx.parent.setCurrent(newCurrent);
  468. }
  469. }
  470. return deletes.length > 0 || roots.length > 0 || ctx.changed;
  471. }
  472. function findUpdateRoots(cells: Map<StateTransform.Ref, StateObjectCell>, tree: StateTree) {
  473. const findState = { roots: [] as Ref[], cells };
  474. StateTree.doPreOrder(tree, tree.root, findState, findUpdateRootsVisitor);
  475. return findState.roots;
  476. }
  477. function findUpdateRootsVisitor(n: StateTransform, _: any, s: { roots: Ref[], cells: Map<Ref, StateObjectCell> }) {
  478. const cell = s.cells.get(n.ref);
  479. if (!cell || cell.transform.version !== n.version) {
  480. s.roots.push(n.ref);
  481. return false;
  482. }
  483. if (cell.status === 'error') return false;
  484. // nothing below a Null object can be an update root
  485. if (cell && cell.obj === StateObject.Null) return false;
  486. return true;
  487. }
  488. type FindDeletesCtx = { newTree: StateTree, cells: State.Cells, deletes: Ref[] }
  489. function checkDeleteVisitor(n: StateTransform, _: any, ctx: FindDeletesCtx) {
  490. if (!ctx.newTree.transforms.has(n.ref) && ctx.cells.has(n.ref)) ctx.deletes.push(n.ref);
  491. }
  492. function findDeletes(ctx: UpdateContext): Ref[] {
  493. const deleteCtx: FindDeletesCtx = { newTree: ctx.tree, cells: ctx.cells, deletes: [] };
  494. StateTree.doPostOrder(ctx.oldTree, ctx.oldTree.root, deleteCtx, checkDeleteVisitor);
  495. return deleteCtx.deletes;
  496. }
  497. function syncNewStatesVisitor(n: StateTransform, tree: StateTree, ctx: UpdateContext) {
  498. const cell = ctx.cells.get(n.ref);
  499. if (!cell || !StateTransform.syncState(cell.state, n.state)) return;
  500. ctx.parent.events.cell.stateUpdated.next({ state: ctx.parent, ref: n.ref, cell });
  501. }
  502. function syncNewStates(ctx: UpdateContext) {
  503. StateTree.doPreOrder(ctx.tree, ctx.tree.root, ctx, syncNewStatesVisitor);
  504. }
  505. function setCellStatus(ctx: UpdateContext, ref: Ref, status: StateObjectCell.Status, errorText?: string) {
  506. const cell = ctx.cells.get(ref)!;
  507. const changed = cell.status !== status;
  508. cell.status = status;
  509. cell.errorText = errorText;
  510. if (changed) ctx.parent.events.cell.stateUpdated.next({ state: ctx.parent, ref, cell });
  511. }
  512. function initCellStatusVisitor(t: StateTransform, _: any, ctx: UpdateContext) {
  513. ctx.cells.get(t.ref)!.transform = t;
  514. setCellStatus(ctx, t.ref, 'pending');
  515. }
  516. function initCellStatus(ctx: UpdateContext, roots: Ref[]) {
  517. for (const root of roots) {
  518. StateTree.doPreOrder(ctx.tree, ctx.tree.transforms.get(root), ctx, initCellStatusVisitor);
  519. }
  520. }
  521. function unlinkCell(cell: StateObjectCell) {
  522. for (const other of cell.dependencies.dependsOn) {
  523. arraySetRemove(other.dependencies.dependentBy, cell);
  524. }
  525. }
  526. type InitCellsCtx = { ctx: UpdateContext, visited: Set<Ref>, added: StateObjectCell[] }
  527. function addCellsVisitor(transform: StateTransform, _: any, { ctx, added, visited }: InitCellsCtx) {
  528. visited.add(transform.ref);
  529. if (ctx.cells.has(transform.ref)) {
  530. return;
  531. }
  532. const cell: StateObjectCell = {
  533. parent: ctx.parent,
  534. transform,
  535. sourceRef: void 0,
  536. status: 'pending',
  537. state: { ...transform.state },
  538. errorText: void 0,
  539. params: void 0,
  540. dependencies: { dependentBy: [], dependsOn: [] },
  541. cache: void 0
  542. };
  543. ctx.cells.set(transform.ref, cell);
  544. added.push(cell);
  545. }
  546. // type LinkCellsCtx = { ctx: UpdateContext, visited: Set<Ref>, dependent: UniqueArray<Ref, StateObjectCell> }
  547. function linkCells(target: StateObjectCell, ctx: UpdateContext) {
  548. if (!target.transform.dependsOn) return;
  549. for (const ref of target.transform.dependsOn) {
  550. const t = ctx.tree.transforms.get(ref);
  551. if (!t) {
  552. throw new Error(`Cannot depend on a non-existent transform.`);
  553. }
  554. const cell = ctx.cells.get(ref)!;
  555. arraySetAdd(target.dependencies.dependsOn, cell);
  556. arraySetAdd(cell.dependencies.dependentBy, target);
  557. }
  558. }
  559. function initCells(ctx: UpdateContext, roots: Ref[]) {
  560. const initCtx: InitCellsCtx = { ctx, visited: new Set(), added: [] };
  561. // Add new cells
  562. for (const root of roots) {
  563. StateTree.doPreOrder(ctx.tree, ctx.tree.transforms.get(root), initCtx, addCellsVisitor);
  564. }
  565. // Update links for newly added cells
  566. for (const cell of initCtx.added) {
  567. linkCells(cell, ctx);
  568. }
  569. let dependent: UniqueArray<Ref, StateObjectCell>;
  570. // Find dependent cells
  571. initCtx.visited.forEach(ref => {
  572. const cell = ctx.cells.get(ref)!;
  573. for (const by of cell.dependencies.dependentBy) {
  574. if (initCtx.visited.has(by.transform.ref)) continue;
  575. if (!dependent) dependent = UniqueArray.create();
  576. UniqueArray.add(dependent, by.transform.ref, by);
  577. }
  578. });
  579. // TODO: check if dependent cells are all "proper roots"
  580. return { added: initCtx.added, dependent: dependent! ? dependent!.array : void 0 };
  581. }
  582. function findNewCurrent(tree: StateTree, start: Ref, deletes: Ref[], cells: Map<Ref, StateObjectCell>) {
  583. const deleteSet = new Set(deletes);
  584. return _findNewCurrent(tree, start, deleteSet, cells);
  585. }
  586. function _findNewCurrent(tree: StateTree, ref: Ref, deletes: Set<Ref>, cells: Map<Ref, StateObjectCell>): Ref {
  587. if (ref === StateTransform.RootRef) return ref;
  588. const node = tree.transforms.get(ref)!;
  589. const siblings = tree.children.get(node.parent)!.values();
  590. let prevCandidate: Ref | undefined = void 0, seenRef = false;
  591. while (true) {
  592. const s = siblings.next();
  593. if (s.done) break;
  594. if (deletes.has(s.value)) continue;
  595. const cell = cells.get(s.value);
  596. if (!cell || cell.status === 'error' || cell.obj === StateObject.Null) {
  597. continue;
  598. }
  599. const t = tree.transforms.get(s.value);
  600. if (t.state.isGhost) continue;
  601. if (s.value === ref) {
  602. seenRef = true;
  603. if (!deletes.has(ref)) prevCandidate = ref;
  604. continue;
  605. }
  606. if (seenRef) return t.ref;
  607. prevCandidate = t.ref;
  608. }
  609. if (prevCandidate) return prevCandidate;
  610. return _findNewCurrent(tree, node.parent, deletes, cells);
  611. }
  612. /** Set status and error text of the cell. Remove all existing objects in the subtree. */
  613. function doError(ctx: UpdateContext, ref: Ref, errorObject: any | undefined, silent: boolean) {
  614. if (!silent) {
  615. ctx.hadError = true;
  616. (ctx.parent as any as { errorFree: boolean }).errorFree = false;
  617. }
  618. const cell = ctx.cells.get(ref)!;
  619. if (errorObject) {
  620. ctx.wasAborted = ctx.wasAborted || Task.isAbort(errorObject);
  621. const message = '' + errorObject;
  622. setCellStatus(ctx, ref, 'error', message);
  623. if (!silent) ctx.parent.events.log.next({ type: 'error', timestamp: new Date(), message });
  624. } else {
  625. cell.params = void 0;
  626. }
  627. if (cell.obj) {
  628. const obj = cell.obj;
  629. cell.obj = void 0;
  630. cell.cache = void 0;
  631. ctx.parent.events.object.removed.next({ state: ctx.parent, ref, obj });
  632. }
  633. // remove the objects in the child nodes if they exist
  634. const children = ctx.tree.children.get(ref).values();
  635. while (true) {
  636. const next = children.next();
  637. if (next.done) return;
  638. doError(ctx, next.value, void 0, silent);
  639. }
  640. }
  641. type UpdateNodeResult =
  642. | { ref: Ref, action: 'created', obj: StateObject }
  643. | { ref: Ref, action: 'updated', oldData?: any, obj: StateObject }
  644. | { ref: Ref, action: 'replaced', oldObj?: StateObject, obj: StateObject }
  645. | { action: 'none' }
  646. const ParentNullErrorText = 'Parent is null';
  647. async function updateSubtree(ctx: UpdateContext, root: Ref) {
  648. setCellStatus(ctx, root, 'processing');
  649. let isNull = false;
  650. try {
  651. const start = now();
  652. const update = await updateNode(ctx, root);
  653. const time = now() - start;
  654. if (update.action !== 'none') ctx.changed = true;
  655. setCellStatus(ctx, root, 'ok');
  656. ctx.results.push(update);
  657. if (update.action === 'created') {
  658. isNull = update.obj === StateObject.Null;
  659. if (!isNull && !ctx.options.doNotLogTiming) ctx.parent.events.log.next(LogEntry.info(`Created ${update.obj.label} in ${formatTimespan(time)}.`));
  660. } else if (update.action === 'updated') {
  661. isNull = update.obj === StateObject.Null;
  662. if (!isNull && !ctx.options.doNotLogTiming) ctx.parent.events.log.next(LogEntry.info(`Updated ${update.obj.label} in ${formatTimespan(time)}.`));
  663. } else if (update.action === 'replaced') {
  664. isNull = update.obj === StateObject.Null;
  665. if (!isNull && !ctx.options.doNotLogTiming) ctx.parent.events.log.next(LogEntry.info(`Updated ${update.obj.label} in ${formatTimespan(time)}.`));
  666. }
  667. } catch (e) {
  668. ctx.changed = true;
  669. if (!ctx.hadError) ctx.newCurrent = root;
  670. doError(ctx, root, e, false);
  671. if (!isProductionMode) console.error(e);
  672. return;
  673. }
  674. const children = ctx.tree.children.get(root).values();
  675. while (true) {
  676. const next = children.next();
  677. if (next.done) return;
  678. if (isNull) doError(ctx, next.value, void 0, true);
  679. else await updateSubtree(ctx, next.value);
  680. }
  681. }
  682. function resolveParams(ctx: UpdateContext, transform: StateTransform, src: StateObject) {
  683. const prms = transform.transformer.definition.params;
  684. const definition = prms ? prms(src, ctx.parent.globalContext) : {};
  685. const defaultValues = ParamDefinition.getDefaultValues(definition);
  686. (transform.params as any) = transform.params
  687. ? assignIfUndefined(transform.params, defaultValues)
  688. : defaultValues;
  689. return { definition, values: transform.params };
  690. }
  691. async function updateNode(ctx: UpdateContext, currentRef: Ref): Promise<UpdateNodeResult> {
  692. const { oldTree, tree } = ctx;
  693. const current = ctx.cells.get(currentRef)!;
  694. const transform = current.transform;
  695. // special case for Root
  696. if (current.transform.ref === StateTransform.RootRef) {
  697. return { action: 'none' };
  698. }
  699. let parentCell = transform.transformer.definition.from.length === 0
  700. ? ctx.cells.get(current.transform.parent)
  701. : StateSelection.findAncestorOfType(tree, ctx.cells, currentRef, transform.transformer.definition.from);
  702. if (!parentCell) {
  703. throw new Error(`No suitable parent found for '${currentRef}'`);
  704. }
  705. ctx.spine.current = current;
  706. const parent = parentCell.obj!;
  707. current.sourceRef = parentCell.transform.ref;
  708. const params = resolveParams(ctx, transform, parent);
  709. if (!oldTree.transforms.has(currentRef) || !current.params) {
  710. current.params = params;
  711. const obj = await createObject(ctx, current, transform.transformer, parent, params.values);
  712. updateTag(obj, transform);
  713. current.obj = obj;
  714. return { ref: currentRef, action: 'created', obj };
  715. } else {
  716. const oldParams = current.params.values;
  717. const oldCache = current.cache;
  718. const oldData = current.obj?.data;
  719. const newParams = params.values;
  720. current.params = params;
  721. const updateKind = !!current.obj && current.obj !== StateObject.Null
  722. ? await updateObject(ctx, current, transform.transformer, parent, current.obj!, oldParams, newParams)
  723. : StateTransformer.UpdateResult.Recreate;
  724. switch (updateKind) {
  725. case StateTransformer.UpdateResult.Recreate: {
  726. const oldObj = current.obj;
  727. dispose(transform, oldObj, oldParams, oldCache, ctx.parent.globalContext);
  728. const newObj = await createObject(ctx, current, transform.transformer, parent, newParams);
  729. updateTag(newObj, transform);
  730. current.obj = newObj;
  731. return { ref: currentRef, action: 'replaced', oldObj, obj: newObj };
  732. }
  733. case StateTransformer.UpdateResult.Updated:
  734. updateTag(current.obj, transform);
  735. return { ref: currentRef, action: 'updated', oldData, obj: current.obj! };
  736. case StateTransformer.UpdateResult.Null: {
  737. dispose(transform, current.obj, oldParams, oldCache, ctx.parent.globalContext);
  738. current.obj = StateObject.Null;
  739. return { ref: currentRef, action: 'updated', obj: current.obj! };
  740. }
  741. default:
  742. return { action: 'none' };
  743. }
  744. }
  745. }
  746. function dispose(transform: StateTransform, b: StateObject | undefined, params: any, cache: any, globalContext: any) {
  747. transform.transformer.definition.dispose?.({
  748. b: b !== StateObject.Null ? b : void 0,
  749. params,
  750. cache
  751. }, globalContext);
  752. }
  753. function updateTag(obj: StateObject | undefined, transform: StateTransform) {
  754. if (!obj || obj === StateObject.Null) return;
  755. (obj.tags as string[] | undefined) = transform.tags;
  756. }
  757. function runTask<T>(t: T | Task<T>, ctx: RuntimeContext) {
  758. if (typeof (t as any).runInContext === 'function') return (t as Task<T>).runInContext(ctx);
  759. return t as T;
  760. }
  761. function resolveDependencies(cell: StateObjectCell) {
  762. if (cell.dependencies.dependsOn.length === 0) return void 0;
  763. const deps = Object.create(null);
  764. for (const dep of cell.dependencies.dependsOn) {
  765. if (!dep.obj) {
  766. throw new Error('Unresolved dependency.');
  767. }
  768. deps[dep.transform.ref] = dep.obj;
  769. }
  770. return deps;
  771. }
  772. function createObject(ctx: UpdateContext, cell: StateObjectCell, transformer: StateTransformer, a: StateObject, params: any) {
  773. if (!cell.cache) cell.cache = Object.create(null);
  774. return runTask(transformer.definition.apply({ a, params, cache: cell.cache, spine: ctx.spine, dependencies: resolveDependencies(cell) }, ctx.parent.globalContext), ctx.taskCtx);
  775. }
  776. async function updateObject(ctx: UpdateContext, cell: StateObjectCell, transformer: StateTransformer, a: StateObject, b: StateObject, oldParams: any, newParams: any) {
  777. if (!transformer.definition.update) {
  778. return StateTransformer.UpdateResult.Recreate;
  779. }
  780. if (!cell.cache) cell.cache = Object.create(null);
  781. return runTask(transformer.definition.update({ a, oldParams, b, newParams, cache: cell.cache, spine: ctx.spine, dependencies: resolveDependencies(cell) }, ctx.parent.globalContext), ctx.taskCtx);
  782. }