Skip to content

Commit

Permalink
fix: top level await (chrome 129) (#2970)
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin authored Sep 23, 2024
1 parent 7ab2f16 commit 10076be
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 36 deletions.
3 changes: 2 additions & 1 deletion composables/idb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export async function useAsyncIDBKeyval<T>(
key: IDBValidKey,
initialValue: MaybeRefOrGetter<T>,
options: UseIDBOptions = {},
source?: Ref<T>,
): Promise<RemovableRef<T>> {
const {
flush = 'pre',
Expand All @@ -19,7 +20,7 @@ export async function useAsyncIDBKeyval<T>(
},
} = options

const data = (shallow ? shallowRef : ref)(initialValue) as Ref<T>
const data = source ?? (shallow ? shallowRef : ref)(initialValue) as Ref<T>

const rawInit: T = toValue<T>(initialValue)

Expand Down
27 changes: 1 addition & 26 deletions composables/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,12 @@ import {
STORAGE_KEY_NOTIFICATION,
STORAGE_KEY_NOTIFICATION_POLICY,
STORAGE_KEY_SERVERS,
STORAGE_KEY_USERS,
} from '~/constants'
import type { PushNotificationPolicy, PushNotificationRequest } from '~/composables/push-notifications/types'
import { useAsyncIDBKeyval } from '~/composables/idb'

const mock = process.mock

function initializeUsers(): Promise<Ref<UserLogin[]> | RemovableRef<UserLogin[]>> | Ref<UserLogin[]> | RemovableRef<UserLogin[]> {
let defaultUsers = mock ? [mock.user] : []

// Backward compatibility with localStorage
let removeUsersOnLocalStorage = false
if (globalThis?.localStorage) {
const usersOnLocalStorageString = globalThis.localStorage.getItem(STORAGE_KEY_USERS)
if (usersOnLocalStorageString) {
defaultUsers = JSON.parse(usersOnLocalStorageString)
removeUsersOnLocalStorage = true
}
}

const users = import.meta.server
? ref<UserLogin[]>(defaultUsers)
: useAsyncIDBKeyval<UserLogin[]>(STORAGE_KEY_USERS, defaultUsers, { deep: true })

if (removeUsersOnLocalStorage)
globalThis.localStorage.removeItem(STORAGE_KEY_USERS)

return users
}

const users = import.meta.server ? initializeUsers() as Ref<UserLogin[]> | RemovableRef<UserLogin[]> : await initializeUsers()
const users: Ref<UserLogin[]> | RemovableRef<UserLogin[]> = import.meta.server ? ref<UserLogin[]>([]) : ref<UserLogin[]>([]) as RemovableRef<UserLogin[]>
const nodes = useLocalStorage<Record<string, any>>(STORAGE_KEY_NODES, {}, { deep: true })
const currentUserHandle = useLocalStorage<string>(STORAGE_KEY_CURRENT_USER_HANDLE, mock ? mock.user.account.id : '')
export const instanceStorage = useLocalStorage<Record<string, mastodon.v1.Instance>>(STORAGE_KEY_SERVERS, mock ? mock.server : {}, { deep: true })
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@iconify-emoji/twemoji": "^1.0.2",
"@iconify/json": "^2.2.170",
"@iconify/utils": "^2.1.22",
"@nuxt/devtools": "^1.4.2",
"@nuxt/devtools": "^1.5.0",
"@nuxt/test-utils": "^3.14.2",
"@nuxtjs/color-mode": "^3.4.4",
"@nuxtjs/i18n": "^8.5.3",
Expand Down
34 changes: 34 additions & 0 deletions plugins/0.setup-users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useAsyncIDBKeyval } from '~/composables/idb'
import type { UserLogin } from '~/types'
import { STORAGE_KEY_USERS } from '~/constants'

const mock = process.mock

export default defineNuxtPlugin({
parallel: import.meta.server,
async setup() {
const users = useUsers()

let defaultUsers = mock ? [mock.user] : []

// Backward compatibility with localStorage
let removeUsersOnLocalStorage = false
if (globalThis?.localStorage) {
const usersOnLocalStorageString = globalThis.localStorage.getItem(STORAGE_KEY_USERS)
if (usersOnLocalStorageString) {
defaultUsers = JSON.parse(usersOnLocalStorageString)
removeUsersOnLocalStorage = true
}
}

if (import.meta.server) {
users.value = defaultUsers
}
if (import.meta.client) {
await useAsyncIDBKeyval<UserLogin[]>(STORAGE_KEY_USERS, defaultUsers, { deep: true }, users)
}

if (removeUsersOnLocalStorage)
globalThis.localStorage.removeItem(STORAGE_KEY_USERS)
},
})
130 changes: 122 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 10076be

Please sign in to comment.