fix: top level await (chrome 129) (#2970)

This commit is contained in:
Joaquín Sánchez 2024-09-23 12:53:10 +02:00 committed by GitHub
parent 7ab2f16f35
commit 10076be909
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 160 additions and 36 deletions

View file

@ -9,6 +9,7 @@ export async function useAsyncIDBKeyval<T>(
key: IDBValidKey, key: IDBValidKey,
initialValue: MaybeRefOrGetter<T>, initialValue: MaybeRefOrGetter<T>,
options: UseIDBOptions = {}, options: UseIDBOptions = {},
source?: Ref<T>,
): Promise<RemovableRef<T>> { ): Promise<RemovableRef<T>> {
const { const {
flush = 'pre', flush = 'pre',
@ -19,7 +20,7 @@ export async function useAsyncIDBKeyval<T>(
}, },
} = options } = 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) const rawInit: T = toValue<T>(initialValue)

View file

@ -12,37 +12,12 @@ import {
STORAGE_KEY_NOTIFICATION, STORAGE_KEY_NOTIFICATION,
STORAGE_KEY_NOTIFICATION_POLICY, STORAGE_KEY_NOTIFICATION_POLICY,
STORAGE_KEY_SERVERS, STORAGE_KEY_SERVERS,
STORAGE_KEY_USERS,
} from '~/constants' } from '~/constants'
import type { PushNotificationPolicy, PushNotificationRequest } from '~/composables/push-notifications/types' import type { PushNotificationPolicy, PushNotificationRequest } from '~/composables/push-notifications/types'
import { useAsyncIDBKeyval } from '~/composables/idb'
const mock = process.mock const mock = process.mock
function initializeUsers(): Promise<Ref<UserLogin[]> | RemovableRef<UserLogin[]>> | Ref<UserLogin[]> | RemovableRef<UserLogin[]> { const users: Ref<UserLogin[]> | RemovableRef<UserLogin[]> = import.meta.server ? ref<UserLogin[]>([]) : ref<UserLogin[]>([]) as 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 nodes = useLocalStorage<Record<string, any>>(STORAGE_KEY_NODES, {}, { deep: true }) 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 : '') 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 }) export const instanceStorage = useLocalStorage<Record<string, mastodon.v1.Instance>>(STORAGE_KEY_SERVERS, mock ? mock.server : {}, { deep: true })

View file

@ -37,7 +37,7 @@
"@iconify-emoji/twemoji": "^1.0.2", "@iconify-emoji/twemoji": "^1.0.2",
"@iconify/json": "^2.2.170", "@iconify/json": "^2.2.170",
"@iconify/utils": "^2.1.22", "@iconify/utils": "^2.1.22",
"@nuxt/devtools": "^1.4.2", "@nuxt/devtools": "^1.5.0",
"@nuxt/test-utils": "^3.14.2", "@nuxt/test-utils": "^3.14.2",
"@nuxtjs/color-mode": "^3.4.4", "@nuxtjs/color-mode": "^3.4.4",
"@nuxtjs/i18n": "^8.5.3", "@nuxtjs/i18n": "^8.5.3",

34
plugins/0.setup-users.ts Normal file
View file

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

View file

@ -47,8 +47,8 @@ importers:
specifier: ^2.1.22 specifier: ^2.1.22
version: 2.1.32 version: 2.1.32
'@nuxt/devtools': '@nuxt/devtools':
specifier: ^1.4.2 specifier: ^1.5.0
version: 1.4.2(rollup@2.79.1)(vue@3.5.4(typescript@5.6.2))(webpack-sources@3.2.3) version: 1.5.0(rollup@2.79.1)(vue@3.5.4(typescript@5.6.2))(webpack-sources@3.2.3)
'@nuxt/test-utils': '@nuxt/test-utils':
specifier: ^3.14.2 specifier: ^3.14.2
version: 3.14.2(@vue/test-utils@2.4.6)(h3@1.12.0)(happy-dom@15.0.0)(magicast@0.3.5)(nitropack@2.9.7(@upstash/redis@1.34.0)(@vercel/kv@2.0.0)(encoding@0.1.13)(idb-keyval@6.2.1)(magicast@0.3.5)(webpack-sources@3.2.3))(rollup@2.79.1)(vitest@2.0.5(@types/node@20.8.6)(happy-dom@15.0.0)(terser@5.22.0))(vue-router@4.4.5(vue@3.5.4(typescript@5.6.2)))(vue@3.5.4(typescript@5.6.2))(webpack-sources@3.2.3) version: 3.14.2(@vue/test-utils@2.4.6)(h3@1.12.0)(happy-dom@15.0.0)(magicast@0.3.5)(nitropack@2.9.7(@upstash/redis@1.34.0)(@vercel/kv@2.0.0)(encoding@0.1.13)(idb-keyval@6.2.1)(magicast@0.3.5)(webpack-sources@3.2.3))(rollup@2.79.1)(vitest@2.0.5(@types/node@20.8.6)(happy-dom@15.0.0)(terser@5.22.0))(vue-router@4.4.5(vue@3.5.4(typescript@5.6.2)))(vue@3.5.4(typescript@5.6.2))(webpack-sources@3.2.3)
@ -1985,16 +1985,31 @@ packages:
peerDependencies: peerDependencies:
vite: '*' vite: '*'
'@nuxt/devtools-kit@1.5.0':
resolution: {integrity: sha512-Q8sOquz9CoUMTABo6Bq+nkbNMZi+WVN4xpz1USZPZazcJhSj9imSmQRSycY2fBYqkfB1AKBRhm2UV2ujCQfw0Q==}
peerDependencies:
vite: '*'
'@nuxt/devtools-wizard@1.4.2': '@nuxt/devtools-wizard@1.4.2':
resolution: {integrity: sha512-TyhmPBg/xJKPOdnwR3DAh8KMUt6/0dUNABCxGVeY7PYbIiXt4msIGVJkBc4y+WwIJHOYPrSRClmZVsXQfRlB4A==} resolution: {integrity: sha512-TyhmPBg/xJKPOdnwR3DAh8KMUt6/0dUNABCxGVeY7PYbIiXt4msIGVJkBc4y+WwIJHOYPrSRClmZVsXQfRlB4A==}
hasBin: true hasBin: true
'@nuxt/devtools-wizard@1.5.0':
resolution: {integrity: sha512-Yvc3MHzNZAN3hMoUr4FpVoBQ6etkp1STy56LntHgdEc9ngzcKzGuJJp5kxvytuY3dLUVFKQ6Ptvtv+yjLeoPZQ==}
hasBin: true
'@nuxt/devtools@1.4.2': '@nuxt/devtools@1.4.2':
resolution: {integrity: sha512-Ok3g2P7iwKyK8LiwozbYVAZTo8t91iXSmlJj2ozeo1okKQ2Qi1AtwB6nYgIlkUHZmo155ZjG/LCHYI5uhQ/sGw==} resolution: {integrity: sha512-Ok3g2P7iwKyK8LiwozbYVAZTo8t91iXSmlJj2ozeo1okKQ2Qi1AtwB6nYgIlkUHZmo155ZjG/LCHYI5uhQ/sGw==}
hasBin: true hasBin: true
peerDependencies: peerDependencies:
vite: '*' vite: '*'
'@nuxt/devtools@1.5.0':
resolution: {integrity: sha512-82LEPZUVU0osPRypSTq/bPXfl1Oo/+R2UaXx/pq9WkE8Vj1V/n0v7a40EVHJsusZ+e/JGjed8+8oYDwF8nNIQw==}
hasBin: true
peerDependencies:
vite: '*'
'@nuxt/kit@3.13.1': '@nuxt/kit@3.13.1':
resolution: {integrity: sha512-FkUL349lp/3nVfTIyws4UDJ3d2jyv5Pk1DC1HQUCOkSloYYMdbRcQAUcb4fe2TCLNWvHM+FhU8jnzGTzjALZYA==} resolution: {integrity: sha512-FkUL349lp/3nVfTIyws4UDJ3d2jyv5Pk1DC1HQUCOkSloYYMdbRcQAUcb4fe2TCLNWvHM+FhU8jnzGTzjALZYA==}
engines: {node: ^14.18.0 || >=16.10.0} engines: {node: ^14.18.0 || >=16.10.0}
@ -6985,6 +7000,9 @@ packages:
simple-git@3.26.0: simple-git@3.26.0:
resolution: {integrity: sha512-5tbkCSzuskR6uA7uA23yjasmA0RzugVo8QM2bpsnxkrgP13eisFT7TMS4a+xKEJvbmr4qf+l0WT3eKa9IxxUyw==} resolution: {integrity: sha512-5tbkCSzuskR6uA7uA23yjasmA0RzugVo8QM2bpsnxkrgP13eisFT7TMS4a+xKEJvbmr4qf+l0WT3eKa9IxxUyw==}
simple-git@3.27.0:
resolution: {integrity: sha512-ivHoFS9Yi9GY49ogc6/YAi3Fl9ROnF4VyubNylgCkA+RVqLaKWnDSzXOVzya8csELIaWaYNutsEuAhZrtOjozA==}
simple-swizzle@0.2.2: simple-swizzle@0.2.2:
resolution: {integrity: sha512-JA//kQgZtbuY83m+xT+tXJkmJncGMTFT+C+g2h2R9uxkYIrE2yy9sgmcLhCnw57/WSD+Eh3J97FPEDFnbXnDUg==} resolution: {integrity: sha512-JA//kQgZtbuY83m+xT+tXJkmJncGMTFT+C+g2h2R9uxkYIrE2yy9sgmcLhCnw57/WSD+Eh3J97FPEDFnbXnDUg==}
@ -8519,7 +8537,7 @@ snapshots:
'@babel/helper-validator-identifier': 7.24.7 '@babel/helper-validator-identifier': 7.24.7
chalk: 2.4.2 chalk: 2.4.2
js-tokens: 4.0.0 js-tokens: 4.0.0
picocolors: 1.0.1 picocolors: 1.1.0
'@babel/parser@7.25.6': '@babel/parser@7.25.6':
dependencies: dependencies:
@ -9948,6 +9966,17 @@ snapshots:
- supports-color - supports-color
- webpack-sources - webpack-sources
'@nuxt/devtools-kit@1.5.0(magicast@0.3.5)(rollup@2.79.1)(webpack-sources@3.2.3)':
dependencies:
'@nuxt/kit': 3.13.2(magicast@0.3.5)(rollup@2.79.1)(webpack-sources@3.2.3)
'@nuxt/schema': 3.13.2(rollup@2.79.1)(webpack-sources@3.2.3)
execa: 7.2.0
transitivePeerDependencies:
- magicast
- rollup
- supports-color
- webpack-sources
'@nuxt/devtools-wizard@1.4.2': '@nuxt/devtools-wizard@1.4.2':
dependencies: dependencies:
consola: 3.2.3 consola: 3.2.3
@ -9961,6 +9990,19 @@ snapshots:
rc9: 2.1.2 rc9: 2.1.2
semver: 7.6.3 semver: 7.6.3
'@nuxt/devtools-wizard@1.5.0':
dependencies:
consola: 3.2.3
diff: 7.0.0
execa: 7.2.0
global-directory: 4.0.1
magicast: 0.3.5
pathe: 1.1.2
pkg-types: 1.2.0
prompts: 2.4.2
rc9: 2.1.2
semver: 7.6.3
'@nuxt/devtools@1.4.2(rollup@2.79.1)(vue@3.5.4(typescript@5.6.2))(webpack-sources@3.2.3)': '@nuxt/devtools@1.4.2(rollup@2.79.1)(vue@3.5.4(typescript@5.6.2))(webpack-sources@3.2.3)':
dependencies: dependencies:
'@antfu/utils': 0.7.10 '@antfu/utils': 0.7.10
@ -10056,6 +10098,53 @@ snapshots:
- vue - vue
- webpack-sources - webpack-sources
'@nuxt/devtools@1.5.0(rollup@2.79.1)(vue@3.5.4(typescript@5.6.2))(webpack-sources@3.2.3)':
dependencies:
'@antfu/utils': 0.7.10
'@nuxt/devtools-kit': 1.5.0(magicast@0.3.5)(rollup@2.79.1)(webpack-sources@3.2.3)
'@nuxt/devtools-wizard': 1.5.0
'@nuxt/kit': 3.13.2(magicast@0.3.5)(rollup@2.79.1)(webpack-sources@3.2.3)
'@vue/devtools-core': 7.4.4(vite@5.4.5(@types/node@20.8.6)(terser@5.22.0))(vue@3.5.4(typescript@5.6.2))
'@vue/devtools-kit': 7.4.4
birpc: 0.2.17
consola: 3.2.3
cronstrue: 2.50.0
destr: 2.0.3
error-stack-parser-es: 0.1.5
execa: 7.2.0
fast-npm-meta: 0.2.2
flatted: 3.3.1
get-port-please: 3.1.2
hookable: 5.5.3
image-meta: 0.2.1
is-installed-globally: 1.0.0
launch-editor: 2.9.1
local-pkg: 0.5.0
magicast: 0.3.5
nypm: 0.3.11
ohash: 1.1.4
pathe: 1.1.2
perfect-debounce: 1.0.0
pkg-types: 1.2.0
rc9: 2.1.2
scule: 1.3.0
semver: 7.6.3
simple-git: 3.27.0
sirv: 2.0.4
tinyglobby: 0.2.6
unimport: 3.12.0(rollup@2.79.1)(webpack-sources@3.2.3)
vite-plugin-inspect: 0.8.7(@nuxt/kit@3.13.2(magicast@0.3.5)(rollup@2.79.1)(webpack-sources@3.2.3))(rollup@2.79.1)
vite-plugin-vue-inspector: 5.2.0(vite@5.4.5(@types/node@20.8.6)(terser@5.22.0))
which: 3.0.1
ws: 8.18.0
transitivePeerDependencies:
- bufferutil
- rollup
- supports-color
- utf-8-validate
- vue
- webpack-sources
'@nuxt/kit@3.13.1(magicast@0.3.5)(rollup@2.79.1)(webpack-sources@3.2.3)': '@nuxt/kit@3.13.1(magicast@0.3.5)(rollup@2.79.1)(webpack-sources@3.2.3)':
dependencies: dependencies:
'@nuxt/schema': 3.13.1(rollup@2.79.1)(webpack-sources@3.2.3) '@nuxt/schema': 3.13.1(rollup@2.79.1)(webpack-sources@3.2.3)
@ -13105,7 +13194,7 @@ snapshots:
css-tree@2.2.1: css-tree@2.2.1:
dependencies: dependencies:
mdn-data: 2.0.28 mdn-data: 2.0.28
source-map-js: 1.2.0 source-map-js: 1.2.1
css-tree@2.3.1: css-tree@2.3.1:
dependencies: dependencies:
@ -14753,7 +14842,7 @@ snapshots:
launch-editor@2.9.1: launch-editor@2.9.1:
dependencies: dependencies:
picocolors: 1.0.1 picocolors: 1.1.0
shell-quote: 1.8.1 shell-quote: 1.8.1
lazystream@1.0.1: lazystream@1.0.1:
@ -14916,7 +15005,7 @@ snapshots:
dependencies: dependencies:
'@babel/parser': 7.25.6 '@babel/parser': 7.25.6
'@babel/types': 7.25.6 '@babel/types': 7.25.6
source-map-js: 1.2.0 source-map-js: 1.2.1
make-dir@3.1.0: make-dir@3.1.0:
dependencies: dependencies:
@ -17051,6 +17140,14 @@ snapshots:
transitivePeerDependencies: transitivePeerDependencies:
- supports-color - supports-color
simple-git@3.27.0:
dependencies:
'@kwsites/file-exists': 1.1.1
'@kwsites/promise-deferred': 1.1.1
debug: 4.3.6
transitivePeerDependencies:
- supports-color
simple-swizzle@0.2.2: simple-swizzle@0.2.2:
dependencies: dependencies:
is-arrayish: 0.3.2 is-arrayish: 0.3.2
@ -17341,7 +17438,7 @@ snapshots:
css-tree: 2.3.1 css-tree: 2.3.1
css-what: 6.1.0 css-what: 6.1.0
csso: 5.0.5 csso: 5.0.5
picocolors: 1.0.1 picocolors: 1.1.0
synckit@0.6.2: synckit@0.6.2:
dependencies: dependencies:
@ -18004,7 +18101,7 @@ snapshots:
dependencies: dependencies:
browserslist: 4.23.3 browserslist: 4.23.3
escalade: 3.1.2 escalade: 3.1.2
picocolors: 1.0.1 picocolors: 1.1.0
upper-case-first@2.0.2: upper-case-first@2.0.2:
dependencies: dependencies:
@ -18150,6 +18247,23 @@ snapshots:
- rollup - rollup
- supports-color - supports-color
vite-plugin-inspect@0.8.7(@nuxt/kit@3.13.2(magicast@0.3.5)(rollup@2.79.1)(webpack-sources@3.2.3))(rollup@2.79.1):
dependencies:
'@antfu/utils': 0.7.10
'@rollup/pluginutils': 5.1.0(rollup@2.79.1)
debug: 4.3.6
error-stack-parser-es: 0.1.5
fs-extra: 11.2.0
open: 10.1.0
perfect-debounce: 1.0.0
picocolors: 1.0.1
sirv: 2.0.4
optionalDependencies:
'@nuxt/kit': 3.13.2(magicast@0.3.5)(rollup@2.79.1)(webpack-sources@3.2.3)
transitivePeerDependencies:
- rollup
- supports-color
vite-plugin-pwa@0.20.5(workbox-build@7.1.1)(workbox-window@7.1.0): vite-plugin-pwa@0.20.5(workbox-build@7.1.1)(workbox-window@7.1.0):
dependencies: dependencies:
debug: 4.3.6 debug: 4.3.6