feat!: implement injectHydration
; remove manualHydration
#195
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
@affects atoms, stores
Description
A missing piece of signals is manual hydration.
injectSignal
doesn't have any sort ofhydrate
config option currently, even though I mentioned it in the v2 spec. I hadn't decided whether we needed to tackle this before v2 or if Zedux's automatic hydration was good enough that we could add it in a minor version later.Several recent requests for better SSR support and documentation made me revisit this now.
After long debate around how hydrating signals will work, considering current pain points with hydration and new challenges signals themselves introduce, I've decided we finally need a full, dedicated hydration injector.
Implement
injectHydration
. This offers full flexibility over when/how you use the hydration. It can be used in combination with third-party hydrators to easily determine priority, especially in local-first setups where url state, localStorage, and similar client storage mechanisms are involved.Example:
As always, these injectors can be consolidated into a single custom injector:
injectHydration
takes an optional config object with two options:intercept
- the first timeinjectHydration
is called in an atom, it disables automatic hydration. Zedux assumes you're using the injected hydration to hydrate manually. This should pretty much always be what you want, but if not, you can prevent this behavior withintercept: false
.transform
- if the atom defines ahydrate
config function, it's called to transform the hydration by default. Passtransform: false
to prevent this.Breaking Change
Remove the
manualHydration
atom config option, as per the v2 spec. Automatic hydration is now disabled ifinjectHydration
is called in the atom state factory without{ intercept: false }
.Also kill the concept of "consuming hydrations" (deleting them after first use). Hydrations will now stay on
ecosystem.hydration
by default, allowing them to be reused if the atom is destroyed and recreated. This can be reimplemented manually with an ecosystem event listener on thecycle
event: