diff options
author | dan <dan.abramov@gmail.com> | 2024-08-06 01:30:52 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-06 01:30:52 +0100 |
commit | 686d5ebb535710dd8c96aa694b4cd1f7913ff3fa (patch) | |
tree | 35ca7e7646d455468c97520b8cb972e8555fc3f2 /src/state/shell | |
parent | 966f6c511fff510fc011aa5c426c6b7eaf4f21ac (diff) | |
download | voidsky-686d5ebb535710dd8c96aa694b4cd1f7913ff3fa.tar.zst |
[Persisted] Make broadcast subscriptions granular by key (#4874)
* Add fast path for guaranteed noop updates * Change persisted.onUpdate() API to take a key * Implement granular broadcast listeners
Diffstat (limited to 'src/state/shell')
-rw-r--r-- | src/state/shell/color-mode.tsx | 13 | ||||
-rw-r--r-- | src/state/shell/onboarding.tsx | 9 |
2 files changed, 15 insertions, 7 deletions
diff --git a/src/state/shell/color-mode.tsx b/src/state/shell/color-mode.tsx index f3339d240..47b936c0b 100644 --- a/src/state/shell/color-mode.tsx +++ b/src/state/shell/color-mode.tsx @@ -1,4 +1,5 @@ import React from 'react' + import * as persisted from '#/state/persisted' type StateContext = { @@ -43,10 +44,16 @@ export function Provider({children}: React.PropsWithChildren<{}>) { ) React.useEffect(() => { - return persisted.onUpdate(() => { - setColorMode(persisted.get('colorMode')) - setDarkTheme(persisted.get('darkTheme')) + const unsub1 = persisted.onUpdate('darkTheme', nextDarkTheme => { + setDarkTheme(nextDarkTheme) + }) + const unsub2 = persisted.onUpdate('colorMode', nextColorMode => { + setColorMode(nextColorMode) }) + return () => { + unsub1() + unsub2() + } }, []) return ( diff --git a/src/state/shell/onboarding.tsx b/src/state/shell/onboarding.tsx index 6a18b461f..d3a8fec46 100644 --- a/src/state/shell/onboarding.tsx +++ b/src/state/shell/onboarding.tsx @@ -1,6 +1,7 @@ import React from 'react' -import * as persisted from '#/state/persisted' + import {track} from '#/lib/analytics/analytics' +import * as persisted from '#/state/persisted' export const OnboardingScreenSteps = { Welcome: 'Welcome', @@ -81,13 +82,13 @@ export function Provider({children}: React.PropsWithChildren<{}>) { ) React.useEffect(() => { - return persisted.onUpdate(() => { - const next = persisted.get('onboarding').step + return persisted.onUpdate('onboarding', nextOnboarding => { + const next = nextOnboarding.step // TODO we've introduced a footgun if (state.step !== next) { dispatch({ type: 'set', - step: persisted.get('onboarding').step as OnboardingStep, + step: nextOnboarding.step as OnboardingStep, }) } }) |