feat: rehydrate lazy-loaded reducers on demand (store.rehydrate) - #28
Open
KAMRONBEK wants to merge 2 commits into
Open
feat: rehydrate lazy-loaded reducers on demand (store.rehydrate)#28KAMRONBEK wants to merge 2 commits into
KAMRONBEK wants to merge 2 commits into
Conversation
Adds store.rehydrate(keys?) so an injected/persisted reducer can be loaded from storage after the store is created. Without it, a reducer injected after init misses its one-time rehydration and its stored value is dropped. - index.ts: attach store.rehydrate; it reads the given keys, dispatches REMEMBER_REHYDRATED, and adds them to the remembered set so they persist. - types.ts: RehydrateFunction + RememberEnhancerStoreExt (default enhancer Ext), so store.rehydrate is typed without extra generics. - tests: 3 new cases; full suite 7812 pass, index.ts 100% covered. - docs: usage/lazy-loaded-reducers documents the API for both cases.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds first-class support for lazy-loaded / injected reducers via a new
store.rehydrate(keys?)method. Addresses #26.In #26 the concern was that injecting a persisted slice races the one-time rehydration:
rehydrate()fires once at init, so a reducer injected later misses its stored value and starts from its initial state. This adds an explicit, opt-in way to load it after injection:store.rehydrate(keys)reads those keys from storage, dispatchesREMEMBER_REHYDRATED, and adds them to the remembered set so they persist from then on. With no arguments it re-reads every currently remembered key.rememberedKeys); that case is documented too.Changes:
index.ts— attachstore.rehydrate(reuses the existing internalrehydrate); the enhancer returnsObject.assign(store, { rehydrate }).types.ts—RehydrateFunction+RememberEnhancerStoreExt, set as the enhancer's defaultExtsostore.rehydrateis typed without extra generics.index.test.ts; full suite 7812 pass,index.ts100% covered; typecheck + lint clean.usage/lazy-loaded-reducersdocuments both cases and is linked in the sidebar.Verified end-to-end against a real store + async driver: inject a slice, call
store.rehydrate([key]), and it loads the persisted value; without the call it stays at its default (the race described in the issue).A couple of things I'd defer to you:
store.rehydratevs something more scoped), and whether you'd rather it be opt-in behind an option instead of always attached.migrate(identity by default). Happy to skipmigrateonstore.rehydrateif you'd prefer it only run at init.I know lazy slices weren't on the roadmap, so if you'd rather not add API surface I'm glad to trim this back to just the docs page.