Skip to content
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

Merged
merged 11 commits into from
Nov 7, 2023
Merged

Add persistent state provider #1830

merged 11 commits into from
Nov 7, 2023

Conversation

estrattonbailey
Copy link
Member

@estrattonbailey estrattonbailey commented Nov 7, 2023

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.

Comment on lines 29 to 36
invitedUsers: {
seenDids: string[]
copiedInvites: string[]
}

onboarding: {
step: string
}
Copy link
Member Author

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.

Comment on lines 44 to 49
primaryLanguage: deviceLocales[0] || 'en',
contentLanguages: deviceLocales || [],
postLanguage: deviceLocales[0] || 'en',
postLanguageHistory: (deviceLocales || [])
.concat(['en', 'ja', 'pt', 'de'])
.slice(0, 6),
Copy link
Member Author

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

Comment on lines 7 to 14
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
}
Copy link
Member Author

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.

Copy link
Collaborator

@pfrazee pfrazee left a 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).

@estrattonbailey
Copy link
Member Author

I'm keen on the reducer pattern @gaearon suggested too, looking at that now

Copy link
Collaborator

@gaearon gaearon left a 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

Copy link
Member Author

@estrattonbailey estrattonbailey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@pfrazee pfrazee merged commit 96d8faf into main Nov 7, 2023
4 checks passed
@pfrazee pfrazee deleted the eric/persistent-state branch November 7, 2023 22:06
estrattonbailey added a commit that referenced this pull request Nov 8, 2023
…-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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants