Skip to content

Commit

Permalink
refactor(store): minor
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Sep 16, 2024
1 parent 4c327bd commit 5eb1648
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/app/store/enhancers/subscribeChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@ type SubscribeChange = Store['subscribe']
export const subscribeChange = injectStoreExtension<{ subscribeChange: SubscribeChange }>(
<State>(store: Store<State>) => {
const NIL = Symbol('NIL')
let stateSnapshot: State | typeof NIL = NIL
let snapshot: State | typeof NIL = NIL

const dispatch: typeof store.dispatch = (action) => {
invariant(stateSnapshot === NIL)
invariant(snapshot === NIL)
snapshot = store.getState()
try {
stateSnapshot = store.getState()
return store.dispatch(action)
} finally {
stateSnapshot = NIL
snapshot = NIL
}
}

const subscribeChange: SubscribeChange = (listener) =>
store.subscribe(() => {
invariant(stateSnapshot !== NIL)
const state = store.getState()
if (state !== stateSnapshot) {
invariant(snapshot !== NIL)
if (snapshot !== store.getState()) {
listener()
}
})
Expand Down

0 comments on commit 5eb1648

Please sign in to comment.