Skip to content

Commit

Permalink
Ready for ignoring same value mutations (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladnicula authored Dec 5, 2023
1 parent 70c45d6 commit d71961b
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 10 deletions.
2 changes: 1 addition & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,5 @@ export declare const select: <T extends object, MP extends SelectorMappingBase<T
dispose: () => void;
};
export declare const inversePatch: (patch: JSONPatchEnhanced) => JSONPatchEnhanced;
export declare const LIB_VERSION = "2.0.4beta";
export declare const LIB_VERSION = "2.0.5beta";
export {};
2 changes: 1 addition & 1 deletion dist/index.main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.module.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "proxy-live-document",
"version": "2.0.4beta",
"version": "2.0.5beta",
"description": "Mutable, observable, patchable JSON state wrapped in Smart Domains",
"types": "dist/index.d.ts",
"main": "dist/index.main.js",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -859,4 +859,4 @@ export const inversePatch = (patch: JSONPatchEnhanced): JSONPatchEnhanced => {
}
}

export const LIB_VERSION = '2.0.4beta'
export const LIB_VERSION = '2.0.5beta'
5 changes: 5 additions & 0 deletions src/mutation-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ export const createMutaitonInMutationTree = (
newValue: unknown,
opCount: number
) => {
// don't create mutation if old and new value are the same
if ( oldValue === newValue ) {
return
}
// 1. check if this node contains an operation
if ( "op" in mutationNode ) {
// if the new value is NO_VALUE, it means we are deleting
Expand Down Expand Up @@ -308,6 +312,7 @@ const recursiveApplyChanges = (mutationNode: MutationTreeNode) => {


export const getPatchesFromMutationTree = (mutationNode: MutationTreeNode) => {

const patches: Array<JSONPatchEnhanced & {opCount : number }> = []
accumulatePatchesFromMutationTree(mutationNode, patches)
return patches.sort((a, b) => a.opCount - b.opCount).map((patch) => {
Expand Down
5 changes: 3 additions & 2 deletions tests/mutate-from-patches.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,12 @@ describe('mutate from patches', () => {
expect(observableCallback).toHaveBeenCalledTimes(1)

const patches3 = mutate(doc, (modifiable) => {
modifiable.nodes['id1'].operationThatChangesInternalTypeTheRightWay('changed-via-method')
modifiable.nodes['id1'].operationThatChangesInternalTypeTheRightWay('ceva?')
})

mutateFromPatches(otherDoc, patches3!)
expect(node2).toHaveProperty('type', 'changed-via-method')
mutateFromPatches(otherDoc, patches3!)
expect(node2).toHaveProperty('type', 'ceva?')
expect(observableCallback).toHaveBeenCalledTimes(2)
})

Expand Down
23 changes: 21 additions & 2 deletions tests/mutate-patch-order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ describe.only('patch order from mutations', () => {
name: 'c',
parentId: 'a'
}
const patches = mutate(document, (modifiable) => {
const docClone = JSON.parse(JSON.stringify(document))
const patches = mutate(docClone, (modifiable) => {
// order of the operations is important
// the const parent here is needed before
// the change is made
Expand All @@ -41,10 +42,28 @@ describe.only('patch order from mutations', () => {
parent.children[newNodeC.id] = true
})

console.log('patches', patches)
// console.log('patches', patches)
expect(patches).toHaveLength(2)
expect(patches![0].path).toEqual('/nodes/c')
expect(patches![1].path).toEqual('/nodes/a/children/c')
})

it('does not generate patch if the old and new value are the same', () => {
const docClone = JSON.parse(JSON.stringify(document))

const patches = mutate(docClone, (modifiable) => {
modifiable.nodes.a.name = 'a'
})
expect(patches).toHaveLength(0)
})

it('does not generate patch if delete key does not exist', () => {
const docClone = JSON.parse(JSON.stringify(document))

const patches = mutate(docClone, (modifiable) => {
delete modifiable.nodes.x
})
expect(patches).toHaveLength(0)
})

})
2 changes: 1 addition & 1 deletion tests/tasklistapp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ describe('Task List project test suite', () => {
const myNewTask = new Task(`3213421451212`, 'Buy bread')
doc.addTask(myNewTask)
})
expect(taskListProject.taskHierarchy.root).toEqual(patches![1].value)
expect(taskListProject.taskHierarchy.root['3213421451212']).toEqual(patches![1].value)
})

})

0 comments on commit d71961b

Please sign in to comment.