Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not trigger with an initial value #7

Open
c-dante opened this issue Jun 6, 2017 · 4 comments
Open

Does not trigger with an initial value #7

c-dante opened this issue Jun 6, 2017 · 4 comments

Comments

@c-dante
Copy link

c-dante commented Jun 6, 2017

Because of the var currentValue = getValue(getState(), objectPath) on initial call to the watch constructor, the change handler that you use to subscribe won't get called until the value has a first change.

I don't want to duplicate my side effect logic by having an initial state, then a watch which performs the same logic, so I end up doing this:

const updateFn = (current, prev) => {
  // ...logic
};
const w = watch(() => someSelector(store.getState()));
const unsub = store.subscribe(w(updateFn));
updateFn(someSelector(store.getState()));

It's strange that the current value is captured at the time the watch function is created, but the callback change function isn't run until the next time the state changes.

@xapple
Copy link

xapple commented Nov 6, 2018

It's been over a year, and no answer. Is this library abandoned ?

@RyanZim
Copy link
Contributor

RyanZim commented Oct 26, 2022

If updateFn was called initially, what would be the value of prev?

@c-dante
Copy link
Author

c-dante commented Dec 20, 2022

If updateFn was called initially, what would be the value of prev?

  • Not passed, can use ...args to capture
  • Undefined, ambiguous if you depend on that value
  • A third param to indicate the first call
  • Can add a second arg to w(...) for the initial call, then it's w(updateFn, initialValue) (which can be updateFn if your business logic understands)

@RyanZim
Copy link
Contributor

RyanZim commented Dec 20, 2022

Not passed, can use ...args to capture

Yeah, that isn't great, because you then get objectPath as the second parameter

Undefined, ambiguous if you depend on that value

Right, selectors returning undefined is common enough.

A third param to indicate the first call

I'm not sure I understand this proposal?

Can add a second arg to w(...) for the initial call, then it's w(updateFn, initialValue)

That's one possibility. Is it crazy to make an initial call only if initialValue is provided? Not sure if I like that implicit behavior change.

which can be updateFn if your business logic understands

Sorry, I'm not understanding this bit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants