-
-
Notifications
You must be signed in to change notification settings - Fork 586
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
fix: change page reload and account switch logic #2975
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
db57722
fix: change page reload and account switch logic
userquin b041293
chore: cleanup users.ts module
userquin 8793678
chore: initialize users
userquin f2b4149
chore: update `useAsyncIDBKeyval` composable
userquin 192e1e6
chore: remove `onBeforeUnmount` from plugin
userquin 5da9cb6
fix: don't call masto on server side
userquin 6a389cc
chore: update logic
userquin 20b9793
chore: revert masto plugin changes
userquin 72a323e
fix: masto plugin
userquin 1d56f55
fix: lists and hashtags pages
userquin 2dc34e7
fix: move masto plugin logic to setup users
userquin eb486ef
chore: use `finally` to enable watcher
userquin 45b2c10
fix: `NaviSide` hydration warnings
userquin f4430b7
chore: update `NaviSide` explore and notifications links
userquin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,75 @@ | ||
import type { MaybeRefOrGetter, RemovableRef } from '@vueuse/core' | ||
import type { Ref } from 'vue' | ||
import type { UseIDBOptions } from '@vueuse/integrations/useIDBKeyval' | ||
import { del, get, set, update } from '~/utils/elk-idb' | ||
|
||
const isIDBSupported = !process.test && typeof indexedDB !== 'undefined' | ||
export interface UseAsyncIDBKeyvalReturn<T> { | ||
set: (value: T) => Promise<void> | ||
readIDB: () => Promise<T | undefined> | ||
} | ||
|
||
export async function useAsyncIDBKeyval<T>( | ||
key: IDBValidKey, | ||
initialValue: MaybeRefOrGetter<T>, | ||
options: UseIDBOptions = {}, | ||
source?: Ref<T>, | ||
): Promise<RemovableRef<T>> { | ||
source: RemovableRef<T>, | ||
options: Omit<UseIDBOptions, 'shallow'> = {}, | ||
): Promise<UseAsyncIDBKeyvalReturn<T>> { | ||
const { | ||
flush = 'pre', | ||
deep = true, | ||
shallow, | ||
writeDefaults = true, | ||
onError = (e: unknown) => { | ||
console.error(e) | ||
}, | ||
} = options | ||
|
||
const data = source ?? (shallow ? shallowRef : ref)(initialValue) as Ref<T> | ||
|
||
const rawInit: T = toValue<T>(initialValue) | ||
|
||
async function read() { | ||
if (!isIDBSupported) | ||
return | ||
try { | ||
const rawValue = await get<T>(key) | ||
if (rawValue === undefined) { | ||
if (rawInit !== undefined && rawInit !== null) | ||
await set(key, rawInit) | ||
} | ||
else { | ||
data.value = rawValue | ||
try { | ||
const rawValue = await get<T>(key) | ||
if (rawValue === undefined) { | ||
if (rawInit !== undefined && rawInit !== null && writeDefaults) { | ||
await set(key, rawInit) | ||
source.value = rawInit | ||
} | ||
} | ||
catch (e) { | ||
onError(e) | ||
else { | ||
source.value = rawValue | ||
} | ||
} | ||
catch (e) { | ||
onError(e) | ||
} | ||
|
||
await read() | ||
|
||
async function write() { | ||
if (!isIDBSupported) | ||
return | ||
async function write(data: T) { | ||
try { | ||
if (data.value == null) { | ||
if (data == null) { | ||
await del(key) | ||
} | ||
else { | ||
// IndexedDB does not support saving proxies, convert from proxy before saving | ||
if (Array.isArray(data.value)) | ||
await update(key, () => (JSON.parse(JSON.stringify(data.value)))) | ||
else if (typeof data.value === 'object') | ||
await update(key, () => ({ ...data.value })) | ||
else | ||
await update(key, () => (data.value)) | ||
await update(key, () => toRaw(data)) | ||
} | ||
} | ||
catch (e) { | ||
onError(e) | ||
} | ||
} | ||
|
||
watch(data, () => write(), { flush, deep }) | ||
const { | ||
pause: pauseWatch, | ||
resume: resumeWatch, | ||
} = watchPausable(source, data => write(data), { flush, deep }) | ||
|
||
async function setData(value: T): Promise<void> { | ||
pauseWatch() | ||
try { | ||
await write(value) | ||
source.value = value | ||
} | ||
finally { | ||
resumeWatch() | ||
} | ||
} | ||
|
||
return data as RemovableRef<T> | ||
return { set: setData, readIDB: () => get<T>(key) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
shouldn't eslint fail here (computed without
.value
)?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.
RemovableRef
is a type from VueUse using type instead interface (type RemovableRef<T> = Omit<Ref, 'value'> & {...}
), maybe some check not being resolved: https://github.com/vuejs/eslint-plugin-vue/blob/2dc606ca589ff5cdcd197c3c64ace7e575a6347d/lib/utils/index.js#L1130