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

feat!: improve injectPromise - add dataSignal, initialData, ZeduxPromise #222

Merged
merged 4 commits into from
Mar 30, 2025

Conversation

bowheart
Copy link
Collaborator

@bowheart bowheart commented Mar 18, 2025

@affects atoms, react, stores

Description

Implement all injectPromise improvements discussed in #220 for Zedux v2, with one change: Instead of passing the full dataSignal to the callback function, pass the dataSignal's current value as prevData.

After digging in, I saw this change was necessary for two reasons:

  • TS infers the callback's return value as unknown, even when using NoInfer on the dataSignal's type. With prevData and NoInfer, TS also infers unknown, but it's much easier to manually type prevData than dataSignal to fix it. If you don't actually use prevData, inference works as normal. Since it should be rare that you need prevData, I'm fine requiring manual typing when you do.
  • It becomes confusing that the value returned from the promise factory overwrites the dataSignal's value, even if you set it manually right before the promise factory returned.

Full change list:

  • Rename initialState to initialData
  • Replace the dataOnly with a new dataSignal that's returned from injectPromise. This signal's value is also passed to the injectPromise promise factory:
  • Rework the promise factory's arguments. Instead of receiving a single AbortController, it now receives an object with { controller, prevData } properties.
  • Create a new ZeduxPromise type that is a drop-in replacement for any atom's Promise generic and holds the type of injectPromise's resolved data value.
    • Improve overloads of useAtomState and useAtomValue to know that the value is resolved when using suspense. Use the new ZeduxPromise type to infer that value.
    • Create a new ResolvedStateOf type helper

Also improve injectStorePromise:

  • Rename initialState to initialData
  • Add ZeduxPromise types for better hook suspense inference.

I didn't update injectStorePromise to pass prevData to the promise factory. I figure that's an advanced usage and if people want it, we can just say they need to use the signals-based injectPromise instead.

@bowheart bowheart merged commit 8fc4723 into master Mar 30, 2025
2 checks passed
@bowheart bowheart deleted the josh/220 branch March 30, 2025 00:16
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

Successfully merging this pull request may close these issues.

1 participant