Skip to content

Commit 4017b9b

Browse files
committed
Dirty inner array solution
1 parent 4310342 commit 4017b9b

File tree

8 files changed

+317
-391
lines changed

8 files changed

+317
-391
lines changed

src/api/applyPatchFactory.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ export default function applyPatchFactory(patchers) {
99
const patch_root = { '': patch } // a trick to allow top level patches
1010
const unpatch_root = { '': {} }
1111

12+
function registerMutation(path, object, prop, old_value) {
13+
// console.log(path, object, prop, old_value)
14+
setDeep(unpatch_root, path.slice(0), old_value)
15+
mutations.push({
16+
old_value,
17+
object,
18+
prop,
19+
path: path.slice(1),
20+
})
21+
}
22+
1223
forEachObject(
1324
patch_root,
1425
target_root,
@@ -19,13 +30,8 @@ export default function applyPatchFactory(patchers) {
1930
if (
2031
!had_prop ||
2132
(patch_value !== target_value &&
22-
!(
23-
isPlainObject(patch_value) &&
24-
isPlainObject(target_value)
25-
))
33+
!(isPlainObject(patch_value) && isPlain(target_value)))
2634
) {
27-
target[prop] = patch_value
28-
2935
// Applying patches
3036
const old_value = patchers.reduce(
3137
(old_value, p) =>
@@ -36,20 +42,16 @@ export default function applyPatchFactory(patchers) {
3642
path,
3743
old_value,
3844
had_prop,
45+
unpatch_root,
3946
applyPatch,
47+
registerMutation,
4048
}),
4149
target_value
4250
)
4351

4452
// We register the mutation if old_value is different to the new value
4553
if (target[prop] !== old_value) {
46-
setDeep(unpatch_root, path.slice(0), old_value)
47-
mutations.push({
48-
old_value,
49-
object: target,
50-
prop,
51-
path: path.slice(1),
52-
})
54+
registerMutation(path, target, prop, old_value)
5355
}
5456

5557
return false // we don't go deeper

src/index.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import createStoreFactory from './api/createStoreFactory'
77
import applyPatchFactory from './api/applyPatchFactory'
88

99
import Function from './types/Function'
10-
import Plain from './types/Plain'
10+
import Primitives from './types/Primitives'
1111
import Delete from './types/Delete'
1212
import Replace from './types/Replace'
13-
import Inner from './types/Inner'
1413
import Splice from './types/Splice'
1514
import Swap from './types/Swap'
1615

@@ -32,10 +31,9 @@ function factory() {
3231
}
3332

3433
addType(Function)
35-
addType(Plain)
34+
addType(Primitives)
3635
addType(Delete)
3736
addType(Replace)
38-
addType(Inner)
3937
addType(Splice)
4038
addType(Swap)
4139

@@ -52,7 +50,6 @@ function factory() {
5250
TYPE: {
5351
Delete,
5452
Replace,
55-
Inner,
5653
Splice,
5754
Swap,
5855
},

src/types/Delete.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ Delete.patch = function ({ patch, target, prop, old_value, had_prop }) {
1212
if (patch[prop] instanceof Delete || patch[prop] === Delete) {
1313
delete target[prop]
1414
}
15-
if (!had_prop) {
16-
old_value = new Delete()
17-
}
1815
return old_value
1916
}
2017

src/types/Inner.js

Lines changed: 0 additions & 68 deletions
This file was deleted.

src/types/Plain.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/types/Primitives.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { isArray, isPlainObject } from '../util/is'
2+
import merge from '../util/merge'
3+
import Replace from './Replace'
4+
import Delete from './Delete'
5+
6+
export default function Primitives() {}
7+
8+
Primitives.patch = function ({
9+
patch,
10+
target,
11+
prop,
12+
old_value,
13+
path,
14+
had_prop,
15+
unpatch_root,
16+
applyPatch,
17+
registerMutation,
18+
}) {
19+
const patch_value = patch[prop]
20+
21+
if (!had_prop) {
22+
if (isArray(target)) {
23+
const path_length = path.slice(0, path.length - 1).concat('length')
24+
// console.log(
25+
// path_length.join('.'),
26+
// unpatch_root,
27+
// getDeep(unpatch_root, path_length.slice(0))
28+
// )
29+
if (!getDeep(unpatch_root, path_length.slice(0))) {
30+
registerMutation(path_length, target, 'length', target.length)
31+
}
32+
} else {
33+
old_value = new Delete()
34+
}
35+
}
36+
37+
if (isPlainObject(patch_value)) {
38+
// console.log({ path, had_prop, target })
39+
target[prop] = applyPatch({}, patch_value).result
40+
}
41+
42+
// New array
43+
else if (isArray(patch_value)) {
44+
target[prop] = merge([], patch_value)
45+
return isPlainObject(old_value) ? Replace(old_value) : old_value
46+
}
47+
48+
// Any other
49+
else {
50+
target[prop] = patch_value
51+
}
52+
53+
return old_value
54+
}
55+
56+
function getDeep(object, path) {
57+
if (path.length === 0) {
58+
return true
59+
}
60+
const prop = path.shift()
61+
return object.hasOwnProperty(prop) ? getDeep(object[prop], path) : false
62+
}

0 commit comments

Comments
 (0)