Repository navigation
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.
|
Hey @KAMRONBEK, thank you for this PR! What you're suggesting is introducing a new unsafe API, without marking it as such. My feeling is we could still do this, because apparently there is a valid use-case, but I'd have to think more how the actual API for this should look for this, considering the fact we might want to handle more use-cases than the injected reducer one. |
…rate() Addresses review feedback that this introduced an unsafe API without marking it as such. Renames store.rehydrate() to store.unsafeRehydrate(), and fixes four timing defects that made the old implementation genuinely unsafe rather than merely unconventional: - Read the state after the storage read, in the same tick as the dispatch. It was captured before the await, so REMEMBER_REHYDRATED re-broadcast a stale snapshot and silently reverted anything dispatched while storage was being read. This also made concurrent calls revert each other. - Only add keys to the remembered set once their stored value has been loaded. Adding them up front let the persist subscriber write the slice's initial state over good stored data mid-read - and the in-flight read then returned that clobbered value, losing it for good. - Do not add keys, or dispatch, when the read fails. A transient storage error otherwise started persisting the slice's initial state. - Do not re-apply migrate(). It is a whole-state, run-once function; re-running it over already-migrated state is unsound. Also stops mutating the rememberedKeys array the caller passed in: the store keeps its own copy, shared with init() so newly remembered keys are still persisted. Adds an end-to-end suite covering these guarantees against a real store and a real async driver - they are all timing properties that a mocked driver cannot express.
|
Thanks for looking, and fair call — you were right that it was unsafe, more specifically than I'd realised. Digging in, the old implementation had four real timing defects, not just an unlabelled API:
It also mutated the I've pushed a commit that closes all of it and renames the method to 7829 tests pass, 100% coverage, lint/typecheck clean, docs build. The new One caveat I couldn't close: with On wanting to cover more than the injected-reducer case — agreed, and I think a keyed rehydrate is the primitive underneath most of them: cross-tab sync ( a. what's there now — I'd lean b: it keeps this PR narrow, makes "unsafe" a deliberate setup-time choice rather than something every store is handed, and leaves c open later without a breaking change. The branch currently implements a. Happy to build c up front if you'd rather design it once — and if you'd rather not add the API at all, I'll cut this back to just the docs page for the non-persisted case, which stands on its own. |
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.