Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zobweyt committed Aug 20, 2024
1 parent 66b8ab8 commit f75ccc9
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions frontend/src/lib/preferences/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ export type PreferencesContextValue = {
set: SetStoreFunction<Settings>;
};

export const SYNC_BROADCAST_CHANNEL_NAME = "preferences_sync" as const;

export const PreferencesContext = createContext<PreferencesContextValue>({} as PreferencesContextValue);

export const PreferencesProvider: ParentComponent = (props) => {
const broadcast = new BroadcastChannel("pereferences");
const channel = new BroadcastChannel(SYNC_BROADCAST_CHANNEL_NAME);
const [settings, set] = storage.makePersisted(createStore(getDefaultSettings()), PREFERENCES_COOKIE_OPTIONS);

broadcast.onmessage = (event) => set(event.data);
createEffect(() => broadcast.postMessage({ ...settings }), { defer: true });
channel.onmessage = (event) => set(event.data);
createEffect(() => channel.postMessage({ ...settings }), { defer: true });

return <PreferencesContext.Provider value={{ settings, set }}>{props.children}</PreferencesContext.Provider>;
};
Expand Down

0 comments on commit f75ccc9

Please sign in to comment.