wxt-zustand
Version:
High-performance Zustand state management for WXT web extensions with seamless cross-tab synchronization and sub-10ms React re-renders
24 lines • 1.02 kB
JavaScript
import { connectAndFetchInitialState } from './connect';
import { getStoreReadiness, setStoreReadiness } from './ready';
/**
* Fetch initial state from the background service and apply it to the local store.
* - Uses readiness caching to avoid duplicate initialization work per store+name.
* - Follows best practices for initial state loading in WXT extensions.
* - Optimized to avoid double-fetch by using a connect+fetch helper.
*/
export async function syncInitialStateFromBackground(storeName, store) {
// If an initialization is in-flight or done, await it.
const existing = getStoreReadiness(storeName, store);
if (existing) {
await existing;
return;
}
const readiness = (async () => {
const { initialState } = await connectAndFetchInitialState(storeName);
// Apply initial state to local store
store.setState(initialState, true);
})();
setStoreReadiness(storeName, store, readiness);
await readiness;
}
//# sourceMappingURL=initial.js.map