diff --git a/.changeset/eleven-spiders-chew.md b/.changeset/eleven-spiders-chew.md new file mode 100644 index 00000000..76e59b4d --- /dev/null +++ b/.changeset/eleven-spiders-chew.md @@ -0,0 +1,5 @@ +--- +"@zoom-image/core": minor +--- + +optimize normal store setState mechanism 🎢 diff --git a/.changeset/good-dogs-think.md b/.changeset/good-dogs-think.md new file mode 100644 index 00000000..6a00c95b --- /dev/null +++ b/.changeset/good-dogs-think.md @@ -0,0 +1,5 @@ +--- +"@zoom-image/core": minor +--- + +Optimize rendering mechanism 🚀 diff --git a/packages/core/src/store.ts b/packages/core/src/store.ts index d0b0afc0..cfbf33c0 100644 --- a/packages/core/src/store.ts +++ b/packages/core/src/store.ts @@ -7,7 +7,7 @@ export function createStore(initialState: TState) { let prevState: TState | undefined const setState = (updatedState: Partial) => { - if (batching && !prevState) { + if (!prevState) { prevState = { ...state } } @@ -20,16 +20,14 @@ export function createStore(initialState: TState) { const flush = () => { if (batching) return - if (!prevState) { - listeners.forEach((listener) => listener(state)) - return - } - let hasChanged = false - for (const key in state) { - if (state[key] !== prevState[key]) { - hasChanged = true - break + + if (prevState) { + for (const key in state) { + if (state[key] !== prevState[key]) { + hasChanged = true + break + } } } diff --git a/packages/core/test/store.test.ts b/packages/core/test/store.test.ts index d586f0ae..3d198eb3 100644 --- a/packages/core/test/store.test.ts +++ b/packages/core/test/store.test.ts @@ -15,6 +15,12 @@ describe("simple store", () => { expect(listener1).toHaveBeenCalledTimes(1) expect(listener2).toHaveBeenCalledTimes(1) + store.setState({ count: 1 }) + store.setState({ count: 1 }) + store.setState({ count: 1 }) + expect(listener1).toHaveBeenCalledTimes(1) + expect(listener2).toHaveBeenCalledTimes(1) + unsubscribe() store.setState({ count: 2 }) expect(store.getState()).toEqual({ count: 2 })