Repository navigation
[@xstate/store] Support atom mount/unmount hooks that follow derived subscriptions #5770
|
I’d like to discuss lifecycle hooks for atoms that manage external subscriptions, such as The desired behavior is to start an external subscription when an atom gains its first consumer, including an indirect consumer through a derived atom, and clean it up when the last consumer disappears. For example, using an illustrative const themeAtom = createAtom<'light' | 'dark' | 'system'>('system');
const systemThemeAtom = withAtomMount(createAtom('light'), function (atom) {
const m = window.matchMedia('(prefers-color-scheme: dark)');
atom.set(m.matches ? 'dark' : 'light');
const ac = new AbortController();
m.addEventListener('change', (e) => atom.set(e.matches ? 'dark' : 'light'), {
signal: ac.signal,
});
return () => ac.abort();
});
const resolvedThemeAtom = createAtom(() => {
const theme = themeAtom.get();
if (theme === 'system') {
return systemThemeAtom.get();
}
return theme;
});
useAtom(resolvedThemeAtom);The API shape is open for discussion. The important part is that the lifecycle follows the dependency graph. Expected semantics:
I’m using Derived values still update correctly—the missing piece is propagating resource lifecycle through those dependencies. A wrapper around Jotai’s native Would lifecycle hooks integrated with the dependency graph, or a supported extension point for implementing them, fit the direction of XState Store? Is there an existing recommended approach for this use case? |
Replies: 2 comments 1 reply
|
Are there any plans to support atom effects in My use case is syncing an atom with matchMedia: start listening when it’s consumed, including through derived atoms, and clean up when a derived atom stops Is there a recommended pattern for this today? import { signal, computed, effect } from 'alien-signals';
const theme = signal('system');
const media = window.matchMedia('(prefers-color-scheme: dark)');
const systemThemeSource = computed(() => {
const value = signal(media.matches ? 'dark' : 'light');
effect(() => {
const update = (event: MediaQueryListEvent) => {
value(event.matches ? 'dark' : 'light');
};
media.addEventListener('change', update);
return () => media.removeEventListener('change', update);
});
return value;
});
const resolvedTheme = computed(() => {
const selected = theme();
return selected === 'system' ? systemThemeSource()() : selected;
}); |
|
This has now landed on Your example becomes: import { createAtom, createSourceAtom } from '@xstate/store';
const themeAtom = createAtom<'light' | 'dark' | 'system'>('system');
const media = window.matchMedia('(prefers-color-scheme: dark)');
const systemThemeAtom = createSourceAtom<'light' | 'dark'>({
getSnapshot: () => (media.matches ? 'dark' : 'light'),
subscribe: (notify) => {
media.addEventListener('change', notify);
return () => media.removeEventListener('change', notify);
}
});
const resolvedThemeAtom = createAtom(() => {
const theme = themeAtom.get();
return theme === 'system' ? systemThemeAtom.get() : theme;
});A plain Version caveat: npm's current Verified against the merged implementation: 16 assertions with a mocked media source covered plain reads, shared subscriptions, conditional cleanup, reattachment, and updates. |
This has now landed on
mainascreateSourceAtomin PR #5782. It connects the external subscription to the atom dependency graph, including derived consumers, rather than wrapping the source atom's publicsubscribe().Your example becomes: