diff --git a/src/mol-state/state.ts b/src/mol-state/state.ts index 0176e2fefabd1e7463675e81492dbe851afc3402..1d2b4cebefbe4f96df42271dacec03828001c0ee 100644 --- a/src/mol-state/state.ts +++ b/src/mol-state/state.ts @@ -165,16 +165,14 @@ namespace State { return findState.roots; } + type FindDeletesCtx = { newTree: StateTree, cells: State.Cells, deletes: Ref[] } + function _visitCheckDelete(n: ImmutableTree.Node<any>, _: any, ctx: FindDeletesCtx) { + if (!ctx.newTree.nodes.has(n.ref) && ctx.cells.has(n.ref)) ctx.deletes.push(n.ref); + } function findDeletes(ctx: UpdateContext): Ref[] { - // TODO: do this in some sort of "tree order"? - const deletes: Ref[] = []; - const keys = ctx.cells.keys(); - while (true) { - const key = keys.next(); - if (key.done) break; - if (!ctx.tree.nodes.has(key.value)) deletes.push(key.value); - } - return deletes; + const deleteCtx: FindDeletesCtx = { newTree: ctx.tree, cells: ctx.cells, deletes: [] }; + ImmutableTree.doPostOrder(ctx.oldTree, ctx.oldTree.nodes.get(ctx.oldTree.rootRef), deleteCtx, _visitCheckDelete); + return deleteCtx.deletes; } function setObjectState(ctx: UpdateContext, ref: Ref, status: StateObjectCell.Status, errorText?: string) {