Skip to content

Commit

Permalink
use applyPatches internally instead of a dedicated actions
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiamanzati committed Jul 13, 2017
1 parent 385598b commit 63080c8
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 26 deletions.
7 changes: 0 additions & 7 deletions src/core/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export class Node {
private readonly disposers: (() => void)[] = []
applyPatches: (patches: IReversibleJsonPatch[]) => void
applySnapshot: (snapshot: any) => void
replaceChild: (subpath: string, newValue: any) => void

constructor(
type: IType<any, any>,
Expand Down Expand Up @@ -56,12 +55,6 @@ export class Node {
this.applySnapshot = createActionInvoker("@APPLY_SNAPSHOT", (snapshot: any) => {
return this.type.applySnapshot(this, snapshot)
}).bind(this.storedValue)
this.replaceChild = createActionInvoker(
"@REPLACE_NODE",
(subpath: string, newValue: any) => {
return this.type.replaceChild(this, subpath, newValue)
}
).bind(this.storedValue)

// optimization: don't keep the snapshot by default alive with a reaction by default
// in prod mode. This saves lot of GC overhead (important for e.g. React Native)
Expand Down
4 changes: 0 additions & 4 deletions src/types/complex-types/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,6 @@ export class ArrayType<S, T> extends ComplexType<S[], IObservableArray<T>> {
removeChild(node: Node, subpath: string) {
node.storedValue.splice(parseInt(subpath, 10), 1)
}

replaceChild(node: Node, subpath: string, newValue: any) {
node.storedValue[parseInt(subpath, 10)] = newValue
}
}

export function array<S, T>(subtype: IType<S, T>): IComplexType<S[], IObservableArray<T>> {
Expand Down
4 changes: 0 additions & 4 deletions src/types/complex-types/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,6 @@ export class MapType<S, T> extends ComplexType<{ [key: string]: S }, IExtendedOb
removeChild(node: Node, subpath: string) {
;(node.storedValue as ObservableMap<any>).delete(subpath)
}

replaceChild(node: Node, subpath: string, newValue: any) {
;(node.storedValue as ObservableMap<any>).set(subpath, newValue)
}
}

export function map<S, T>(
Expand Down
4 changes: 0 additions & 4 deletions src/types/complex-types/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,6 @@ export class ObjectType extends ComplexType<any, any> {
removeChild(node: Node, subpath: string) {
node.storedValue[subpath] = null
}

replaceChild(node: Node, subpath: string, newValue: any) {
node.storedValue[subpath] = newValue
}
}

export type IModelProperties<T> = { [K in keyof T]: IType<any, T[K]> | T[K] }
Expand Down
6 changes: 0 additions & 6 deletions src/types/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export interface IType<S, T> {
getChildNode(node: Node, key: string): Node
getChildType(key: string): IType<any, any>
removeChild(node: Node, subpath: string): void
replaceChild(node: Node, subpath: string, newValue: any): void
isAssignableFrom(type: IType<any, any>): boolean
}

Expand Down Expand Up @@ -69,7 +68,6 @@ export abstract class ComplexType<S, T> implements IType<S, T> {
abstract applyPatchLocally(node: Node, subpath: string, patch: IJsonPatch): void
abstract getChildType(key: string): IType<any, any>
abstract removeChild(node: Node, subpath: string): void
abstract replaceChild(node: Node, subpath: string, newValue: any): void
abstract isValidSnapshot(value: any, context: IContext): IValidationResult

isAssignableFrom(type: IType<any, any>): boolean {
Expand Down Expand Up @@ -192,10 +190,6 @@ export abstract class Type<S, T> extends ComplexType<S, T> implements IType<S, T
removeChild(node: Node, subpath: string): void {
return fail(`No child '${subpath}' available in type: ${this.name}`)
}

replaceChild(node: Node, subpath: string, newValue: any): void {
return fail(`No child '${subpath}' available in type: ${this.name}`)
}
}

import { EMPTY_ARRAY, fail, isMutable } from "../utils"
Expand Down
8 changes: 7 additions & 1 deletion src/types/utility-types/lazy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ export class Lazy<S, T, TR> extends Type<S, T | TR> {
if (!node.parent) return
if (!this.loadedType) return

node.parent.replaceChild(node.subpath, type.create(node.snapshot))
node.parent.applyPatches([
{
op: "replace",
path: node.subpath,
value: node.snapshot
}
])
})
})
)
Expand Down

0 comments on commit 63080c8

Please sign in to comment.