Skip to content
Snippets Groups Projects
Commit 45822b2f authored by David Sehnal's avatar David Sehnal
Browse files

mol-state: StateBuilder.To.insert

parent ae1920fa
No related branches found
No related tags found
No related merge requests found
...@@ -69,6 +69,27 @@ namespace StateBuilder { ...@@ -69,6 +69,27 @@ namespace StateBuilder {
return new To(this.state, t.ref, this.root); return new To(this.state, t.ref, this.root);
} }
/**
* Inserts a new transform that does not change the object type and move the original children to it.
*/
insert<T extends StateTransformer<A, A, any>>(tr: T, params?: StateTransformer.Params<T>, options?: Partial<StateTransform.Options>, initialCellState?: Partial<StateObjectCell.State>): To<StateTransformer.To<T>> {
// cache the children
const children = this.state.tree.children.get(this.ref).toArray();
// add the new node
const t = tr.apply(this.ref, params, options);
this.state.tree.add(t, initialCellState);
// move the original children to the new node
for (const c of children) {
this.state.tree.changeParent(c, t.ref);
}
this.editInfo.count++;
this.editInfo.lastUpdate = t.ref;
return new To(this.state, t.ref, this.root);
}
update<T extends StateTransformer<any, A, any>>(transformer: T, params: (old: StateTransformer.Params<T>) => StateTransformer.Params<T>): Root update<T extends StateTransformer<any, A, any>>(transformer: T, params: (old: StateTransformer.Params<T>) => StateTransformer.Params<T>): Root
update(params: any): Root update(params: any): Root
update<T extends StateTransformer<any, A, any>>(paramsOrTransformer: T, provider?: (old: StateTransformer.Params<T>) => StateTransformer.Params<T>) { update<T extends StateTransformer<any, A, any>>(paramsOrTransformer: T, provider?: (old: StateTransformer.Params<T>) => StateTransformer.Params<T>) {
......
...@@ -48,10 +48,14 @@ namespace Transform { ...@@ -48,10 +48,14 @@ namespace Transform {
} }
} }
export function withParams<T>(t: Transform, params: any): Transform { export function withParams(t: Transform, params: any): Transform {
return { ...t, params, version: UUID.create22() }; return { ...t, params, version: UUID.create22() };
} }
export function withParent(t: Transform, parent: Ref): Transform {
return { ...t, parent, version: UUID.create22() };
}
export function createRoot(props?: Props): Transform { export function createRoot(props?: Props): Transform {
return create(RootRef, StateTransformer.ROOT, {}, { ref: RootRef, props }); return create(RootRef, StateTransformer.ROOT, {}, { ref: RootRef, props });
} }
......
...@@ -90,6 +90,14 @@ class TransientTree implements StateTree { ...@@ -90,6 +90,14 @@ class TransientTree implements StateTree {
this.childMutations.set(parent, set); this.childMutations.set(parent, set);
} }
changeParent(ref: StateTransform.Ref, newParent: StateTransform.Ref) {
ensurePresent(this.transforms, ref);
const old = this.transforms.get(ref);
this.removeChild(old.parent, ref);
this.addChild(newParent, ref);
}
add(transform: StateTransform, initialState?: Partial<StateObjectCell.State>) { add(transform: StateTransform, initialState?: Partial<StateObjectCell.State>) {
const ref = transform.ref; const ref = transform.ref;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment