[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
This commit is contained in:
dan 2024-08-06 01:30:52 +01:00 committed by GitHub
parent 966f6c511f
commit 686d5ebb53
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 95 additions and 42 deletions

View file

@ -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 (

View file

@ -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,
})
}
})