From 10076be909749fab5189d67fb8fceb080a0c0d44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20S=C3=A1nchez?= Date: Mon, 23 Sep 2024 12:53:10 +0200 Subject: [PATCH] fix: top level await (chrome 129) (#2970) --- composables/idb/index.ts | 3 +- composables/users.ts | 27 +------- package.json | 2 +- plugins/0.setup-users.ts | 34 ++++++++++ pnpm-lock.yaml | 130 ++++++++++++++++++++++++++++++++++++--- 5 files changed, 160 insertions(+), 36 deletions(-) create mode 100644 plugins/0.setup-users.ts diff --git a/composables/idb/index.ts b/composables/idb/index.ts index 2747f765..ff07804b 100644 --- a/composables/idb/index.ts +++ b/composables/idb/index.ts @@ -9,6 +9,7 @@ export async function useAsyncIDBKeyval( key: IDBValidKey, initialValue: MaybeRefOrGetter, options: UseIDBOptions = {}, + source?: Ref, ): Promise> { const { flush = 'pre', @@ -19,7 +20,7 @@ export async function useAsyncIDBKeyval( }, } = options - const data = (shallow ? shallowRef : ref)(initialValue) as Ref + const data = source ?? (shallow ? shallowRef : ref)(initialValue) as Ref const rawInit: T = toValue(initialValue) diff --git a/composables/users.ts b/composables/users.ts index 3ef5d410..1b3fca0f 100644 --- a/composables/users.ts +++ b/composables/users.ts @@ -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 | RemovableRef> | Ref | RemovableRef { - 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(defaultUsers) - : useAsyncIDBKeyval(STORAGE_KEY_USERS, defaultUsers, { deep: true }) - - if (removeUsersOnLocalStorage) - globalThis.localStorage.removeItem(STORAGE_KEY_USERS) - - return users -} - -const users = import.meta.server ? initializeUsers() as Ref | RemovableRef : await initializeUsers() +const users: Ref | RemovableRef = import.meta.server ? ref([]) : ref([]) as RemovableRef const nodes = useLocalStorage>(STORAGE_KEY_NODES, {}, { deep: true }) const currentUserHandle = useLocalStorage(STORAGE_KEY_CURRENT_USER_HANDLE, mock ? mock.user.account.id : '') export const instanceStorage = useLocalStorage>(STORAGE_KEY_SERVERS, mock ? mock.server : {}, { deep: true }) diff --git a/package.json b/package.json index 79d37e0d..d079694d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/plugins/0.setup-users.ts b/plugins/0.setup-users.ts new file mode 100644 index 00000000..92d37f6a --- /dev/null +++ b/plugins/0.setup-users.ts @@ -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(STORAGE_KEY_USERS, defaultUsers, { deep: true }, users) + } + + if (removeUsersOnLocalStorage) + globalThis.localStorage.removeItem(STORAGE_KEY_USERS) + }, +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3c5903f3..b57d9a5a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -47,8 +47,8 @@ importers: specifier: ^2.1.22 version: 2.1.32 '@nuxt/devtools': - specifier: ^1.4.2 - version: 1.4.2(rollup@2.79.1)(vue@3.5.4(typescript@5.6.2))(webpack-sources@3.2.3) + specifier: ^1.5.0 + version: 1.5.0(rollup@2.79.1)(vue@3.5.4(typescript@5.6.2))(webpack-sources@3.2.3) '@nuxt/test-utils': 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) @@ -1985,16 +1985,31 @@ packages: peerDependencies: vite: '*' + '@nuxt/devtools-kit@1.5.0': + resolution: {integrity: sha512-Q8sOquz9CoUMTABo6Bq+nkbNMZi+WVN4xpz1USZPZazcJhSj9imSmQRSycY2fBYqkfB1AKBRhm2UV2ujCQfw0Q==} + peerDependencies: + vite: '*' + '@nuxt/devtools-wizard@1.4.2': resolution: {integrity: sha512-TyhmPBg/xJKPOdnwR3DAh8KMUt6/0dUNABCxGVeY7PYbIiXt4msIGVJkBc4y+WwIJHOYPrSRClmZVsXQfRlB4A==} hasBin: true + '@nuxt/devtools-wizard@1.5.0': + resolution: {integrity: sha512-Yvc3MHzNZAN3hMoUr4FpVoBQ6etkp1STy56LntHgdEc9ngzcKzGuJJp5kxvytuY3dLUVFKQ6Ptvtv+yjLeoPZQ==} + hasBin: true + '@nuxt/devtools@1.4.2': resolution: {integrity: sha512-Ok3g2P7iwKyK8LiwozbYVAZTo8t91iXSmlJj2ozeo1okKQ2Qi1AtwB6nYgIlkUHZmo155ZjG/LCHYI5uhQ/sGw==} hasBin: true peerDependencies: 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': resolution: {integrity: sha512-FkUL349lp/3nVfTIyws4UDJ3d2jyv5Pk1DC1HQUCOkSloYYMdbRcQAUcb4fe2TCLNWvHM+FhU8jnzGTzjALZYA==} engines: {node: ^14.18.0 || >=16.10.0} @@ -6985,6 +7000,9 @@ packages: simple-git@3.26.0: resolution: {integrity: sha512-5tbkCSzuskR6uA7uA23yjasmA0RzugVo8QM2bpsnxkrgP13eisFT7TMS4a+xKEJvbmr4qf+l0WT3eKa9IxxUyw==} + simple-git@3.27.0: + resolution: {integrity: sha512-ivHoFS9Yi9GY49ogc6/YAi3Fl9ROnF4VyubNylgCkA+RVqLaKWnDSzXOVzya8csELIaWaYNutsEuAhZrtOjozA==} + simple-swizzle@0.2.2: resolution: {integrity: sha512-JA//kQgZtbuY83m+xT+tXJkmJncGMTFT+C+g2h2R9uxkYIrE2yy9sgmcLhCnw57/WSD+Eh3J97FPEDFnbXnDUg==} @@ -8519,7 +8537,7 @@ snapshots: '@babel/helper-validator-identifier': 7.24.7 chalk: 2.4.2 js-tokens: 4.0.0 - picocolors: 1.0.1 + picocolors: 1.1.0 '@babel/parser@7.25.6': dependencies: @@ -9948,6 +9966,17 @@ snapshots: - supports-color - 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': dependencies: consola: 3.2.3 @@ -9961,6 +9990,19 @@ snapshots: rc9: 2.1.2 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)': dependencies: '@antfu/utils': 0.7.10 @@ -10056,6 +10098,53 @@ snapshots: - vue - 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)': dependencies: '@nuxt/schema': 3.13.1(rollup@2.79.1)(webpack-sources@3.2.3) @@ -13105,7 +13194,7 @@ snapshots: css-tree@2.2.1: dependencies: mdn-data: 2.0.28 - source-map-js: 1.2.0 + source-map-js: 1.2.1 css-tree@2.3.1: dependencies: @@ -14753,7 +14842,7 @@ snapshots: launch-editor@2.9.1: dependencies: - picocolors: 1.0.1 + picocolors: 1.1.0 shell-quote: 1.8.1 lazystream@1.0.1: @@ -14916,7 +15005,7 @@ snapshots: dependencies: '@babel/parser': 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: dependencies: @@ -17051,6 +17140,14 @@ snapshots: transitivePeerDependencies: - 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: dependencies: is-arrayish: 0.3.2 @@ -17341,7 +17438,7 @@ snapshots: css-tree: 2.3.1 css-what: 6.1.0 csso: 5.0.5 - picocolors: 1.0.1 + picocolors: 1.1.0 synckit@0.6.2: dependencies: @@ -18004,7 +18101,7 @@ snapshots: dependencies: browserslist: 4.23.3 escalade: 3.1.2 - picocolors: 1.0.1 + picocolors: 1.1.0 upper-case-first@2.0.2: dependencies: @@ -18150,6 +18247,23 @@ snapshots: - rollup - 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): dependencies: debug: 4.3.6