File tree Expand file tree Collapse file tree 3 files changed +17
-18
lines changed
Expand file tree Collapse file tree 3 files changed +17
-18
lines changed Original file line number Diff line number Diff 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 } ) ;
Original file line number Diff line number Diff line change 55 createScope ,
66 createSignal ,
77} from "effection" ;
8- import { produce } from "immer" ;
8+ import { Draft , produce , produceWithPatches } from "immer" ;
99import { ActionContext , emit } from "../action.js" ;
1010import { parallel } from "../fx/parallel.js" ;
1111import type { AnyAction } from "../types.js" ;
@@ -17,6 +17,7 @@ import type {
1717 FxStore ,
1818 Listener ,
1919 SliceFromSchema ,
20+ StoreUpdater ,
2021} from "./types.js" ;
2122const 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 ( ) {
Original file line number Diff line number Diff line change 11import type { Operation , Scope } from "effection" ;
2- import type { Draft , Immutable , Patch } from "immer" ;
2+ import type { Draft , Immutable , Patch , produceWithPatches } from "immer" ;
33import type { BaseCtx } from "../compose.js" ;
44import type { AnyAction , AnyState } from "../types.js" ;
55import 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
You can’t perform that action at this time.
0 commit comments