Git Product home page Git Product logo

Comments (6)

drcmda avatar drcmda commented on May 3, 2024

I believe it's the same in Redux, subs will fire for any dispatch?

We probably could just compare the root in order to detect reduced state changes, right?

  const setState = (partialState: PartialState<State>) => {
    const oldState = state
    state = Object.assign(
      {},
      state,
      typeof partialState === 'function' ? partialState(state) : partialState
    )
    if (oldState !== state) listeners.forEach(listener => listener(state))
  }

edit:

On second thought, ... assign creates a new object each time, so that probably wouldn't work. πŸ€”If root change detection can get expensive i would just accept the current behaviour, and the upcoming selective subscribe will also help.

from zustand.

JeremyRH avatar JeremyRH commented on May 3, 2024

I have a fix that's basically the same but does a shallow equal check. I just wanted to open this issue to see if you had any more info on what the behavior should be and to reference this for a potential fix. I'll check if redux is the same.

from zustand.

JeremyRH avatar JeremyRH commented on May 3, 2024

The selective api.subscribe change is how I discovered this. The behavior seemed inconsistent because a selective subscription would not be called unless its state changed but a non-selective one would be called every time even on no change.

from zustand.

drcmda avatar drcmda commented on May 3, 2024

just scared that adding a shallow equal check will add overhead (for instance root objects with hundredes/thousands of objects in it, like a central-state hashmap). At work our state is formed like that, there can be thousands of object in state-root. It would run over each on every setState.

One way: subscribe(fn) could expectedly fire on each setState, with docs declaring it so.

Or, maybe, ... is it possible to run the check only over the partialState?

const partial = typeof partialState === 'function' ? partialState(state) : partialState

// This works if "shallowEqual" could disregard that A has less keys than B somehow
if (shallowEqual(partial, state)) {
   state = Object.assign({}, state, partial)

from zustand.

JeremyRH avatar JeremyRH commented on May 3, 2024

Yeah, we can run the check only on the keys in partialState:

const setState: SetState<TState> = partial => {
  const partialState =
    typeof partial === 'function' ? partial(state) : partial
  if (
    partialState !== state &&
    Object.entries(partialState).some(
      ([key, value]) => !Object.is(state[key], value)
    )
  ) {
    state = Object.assign({}, state, partialState)
    listeners.forEach(listener => listener(state))
  }
}

from zustand.

drcmda avatar drcmda commented on May 3, 2024

I kept thinking about this, sadly this doesn't help when the user does set(state => ({ ...state, something: 123 }))

from zustand.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.