Skip to content

Commit 10cefab

Browse files
committed
Bug fixed on createStore using function patch
1 parent cbca65b commit 10cefab

4 files changed

Lines changed: 36 additions & 18 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dop",
3-
"version": "1.5.3",
3+
"version": "1.5.4",
44
"main": "dist/dop.js",
55
"browser": "dist/dop.umd.js",
66
"module": "src/index.js",

src/api/createStoreFactory.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,33 @@ export default function createStoreFactory(applyPatchFunction) {
1414
listeners.delete(listener)
1515
}
1616

17-
function applyPatch(patch_original) {
18-
const { mutations, unpatch } = applyPatchFunction(
19-
api.state,
20-
patch_original
21-
)
17+
function applyPatch(patch_or_fn) {
18+
const applied = applyPatchFunction(api.state, patch_or_fn)
2219

2320
const outputs = Array.from(listeners.entries()).map(
2421
([listener, filter]) => {
25-
const mts = isFunction(filter)
26-
? mutations.filter(filter)
27-
: mutations.slice(0)
22+
const mutations = isFunction(filter)
23+
? applied.mutations.filter(filter)
24+
: applied.mutations.slice(0)
2825

2926
const { patch, unpatch } =
30-
createPatchAndUnpatchFromMutations(mts, patch_original)
27+
createPatchAndUnpatchFromMutations(
28+
mutations,
29+
applied.patch
30+
)
31+
3132
return {
3233
listener,
3334
patch,
3435
unpatch,
35-
mutations: mts,
36+
mutations,
3637
}
3738
}
3839
)
3940

40-
outputs.mutations = mutations
41-
outputs.unpatch = unpatch
41+
outputs.mutations = applied.mutations
42+
outputs.patch = applied.patch
43+
outputs.unpatch = applied.unpatch
4244

4345
return outputs
4446
}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import Swap from './types/Swap.js'
1616
import Multi from './types/Multi.js'
1717

1818
// Deno does not support json imports: https://stackoverflow.com/questions/61907155/deno-import-json-file-as-module
19-
const version = '1.5.2'
19+
const version = '1.5.4'
2020

2121
function factory() {
2222
const patchers = []

test/create_store.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ test('applyPatch', function (t) {
6969
t.deepEqual(outputs[0].unpatch, { prop: false })
7070
t.true(Array.isArray(outputs[0].mutations))
7171

72-
const { unpatch, mutations } = outputs
73-
t.deepEqual(unpatch, { prop: false })
74-
t.true(Array.isArray(mutations))
75-
t.deepEqual(mutations, outputs[0].mutations)
72+
t.deepEqual(outputs.patch, patch)
73+
t.deepEqual(outputs.unpatch, { prop: false })
74+
t.deepEqual(outputs.mutations, outputs[0].mutations)
75+
t.true(Array.isArray(outputs.mutations))
7676
})
7777

7878
test('applyPatch with function', function (t) {
@@ -145,3 +145,19 @@ test('filtered patches by createPatchAndUnpatchFromMutations must be the same as
145145
t.deepEqual(output[0].patch, {})
146146
t.deepEqual(output[1].patch, patch)
147147
})
148+
149+
test('Matching subscriber patch and original patch', function (t) {
150+
const store = createStore({
151+
['one']: {},
152+
})
153+
store.subscribe((patch) => {
154+
t.deepEqual(patch, { one: { two: TYPE.Replace({ three: true }) } })
155+
})
156+
157+
const listeners = store.applyPatch((d) => {
158+
d['one']['two'] = { three: true }
159+
})
160+
161+
t.deepEqual(listeners[0].patch, listeners.patch)
162+
t.deepEqual(store.state, { one: { two: { three: true } } })
163+
})

0 commit comments

Comments
 (0)