Skip to content
Discussion options

You must be logged in to vote

This has now landed on main as createSourceAtom in PR #5782. It connects the external subscription to the atom dependency graph, including derived consumers, rather than wrapping the source atom's public subscribe().

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);
  }
});

…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@tmkx
Comment options

Answer selected by tmkx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants