-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add persistent state provider #1830
Conversation
bb3cda7
to
a2fa089
Compare
a2fa089
to
2a11929
Compare
src/state/persisted/schema.ts
Outdated
invitedUsers: { | ||
seenDids: string[] | ||
copiedInvites: string[] | ||
} | ||
|
||
onboarding: { | ||
step: string | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other properties felt OK to flatten, but these two I decided to leave as objects in case we need to expand their child props.
src/state/persisted/schema.ts
Outdated
primaryLanguage: deviceLocales[0] || 'en', | ||
contentLanguages: deviceLocales || [], | ||
postLanguage: deviceLocales[0] || 'en', | ||
postLanguageHistory: (deviceLocales || []) | ||
.concat(['en', 'ja', 'pt', 'de']) | ||
.slice(0, 6), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This handling is copied from the preferences model
src/state/persisted/store.ts
Outdated
export async function write(data: Schema) { | ||
await AsyncStorage.setItem(BSKY_STORAGE, JSON.stringify(data)) | ||
} | ||
|
||
export async function read(): Promise<Schema | undefined> { | ||
const rawData = await AsyncStorage.getItem(BSKY_STORAGE) | ||
return rawData ? JSON.parse(rawData) : undefined | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to re-implement these so that they DO throw if AsyncStorage
is unavailable, and I wanted to abstract writing to this key.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 LGTM, let's also get @gaearon's sign-off
Dan- the reason we moved to a hook was mainly so that we can react to state changes, and in particular because on Web the state-changes can come from other tabs (thus the broadcastchannel).
I'm keen on the reducer pattern @gaearon suggested too, looking at that now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's try this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
…-state-into-separate-statecontext * origin: Move invite-state to new persistence + context and replace the notifications with just showing uses in the modal (#1840) Move muted threads to new persistence + context (#1838) Move onboarding state to new persistence + reducer context (#1835) Move require alt-text to new persistence + context (#1839) Move reminders to new persisted state layer (#1834) Add persistent state provider (#1830)
Creates a new state provider called "persisted" (name is not final) that manages ALL state that is persisted to to disk in React context, just like any other of our upcoming state providers. The key difference is that after a state update, we write the state object to disk.
Additionally, this implementation uses
BroadcastChannel
to alert other tabs to state updates, and handles refreshing the in memory persisted state representation when a refresh event is received.I migrated
colorMode
from shell as an example.