Skip to content

Commit 994add3

Browse files
committed
store runs produce
1 parent 5d6918a commit 994add3

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

src/store/schema.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,9 @@ export function createSchema<const O extends FxMap = FxMap>(
249249
Array.isArray(ctx.updater) ? ctx.updater : [ctx.updater]
250250
) as StoreUpdater<SliceFromSchema<O>>[];
251251

252-
const [nextState, patches, _] = produceWithPatches<SliceFromSchema<O>>(
253-
store.getState(),
254-
(draft: Draft<SliceFromSchema<O>>) => {
255-
upds.forEach((updater) => updater(draft));
256-
},
257-
);
252+
const [_nextState, patches, _inversePatches] = store.setState(upds);
258253
ctx.patches = patches;
259254

260-
store.setState(nextState);
261-
262255
yield* next();
263256
},
264257
});

src/store/store.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
createScope,
66
createSignal,
77
} from "effection";
8-
import { produce } from "immer";
8+
import { Draft, produce, produceWithPatches } from "immer";
99
import { ActionContext, emit } from "../action.js";
1010
import { parallel } from "../fx/parallel.js";
1111
import type { AnyAction } from "../types.js";
@@ -17,6 +17,7 @@ import type {
1717
FxStore,
1818
Listener,
1919
SliceFromSchema,
20+
StoreUpdater,
2021
} from "./types.js";
2122
const stubMsg = "This is merely a stub, not implemented";
2223

@@ -148,13 +149,16 @@ export function createStore<O extends FxMap>({
148149
return state;
149150
}
150151

151-
function setState(newState: SliceFromSchema<O>) {
152-
// enables merging multiple states from
153-
// different schemas without overwriting the whole state
154-
// TODO but this means double produce on the default single schema case
155-
state = produce(state, (draft) => {
156-
Object.assign(draft, newState);
157-
});
152+
function setState(upds: StoreUpdater<SliceFromSchema<O>>[]) {
153+
const nextState = produceWithPatches(
154+
state,
155+
(draft: Draft<SliceFromSchema<O>>) => {
156+
upds.forEach((updater) => updater(draft));
157+
},
158+
);
159+
160+
state = nextState[0];
161+
return nextState;
158162
}
159163

160164
function getInitialState() {

src/store/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Operation, Scope } from "effection";
2-
import type { Draft, Immutable, Patch } from "immer";
2+
import type { Draft, Immutable, Patch, produceWithPatches } from "immer";
33
import type { BaseCtx } from "../compose.js";
44
import type { AnyAction, AnyState } from "../types.js";
55
import type { createRun } from "./run.js";
@@ -136,7 +136,9 @@ export interface FxStore<O extends FxMap> {
136136
getScope: () => Scope;
137137
// part of redux store API
138138
getState: () => SliceFromSchema<O>;
139-
setState: (s: SliceFromSchema<O>) => void;
139+
setState: (
140+
upds: StoreUpdater<SliceFromSchema<O>>[],
141+
) => ReturnType<typeof produceWithPatches>;
140142
// part of redux store API
141143
subscribe: (fn: Listener) => () => void;
142144
// the default schema for this store

0 commit comments

Comments
 (0)