This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(nuxt): deep watch useCookie
ref value by default
#9664
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8b32b29
feat(nuxt): useCookie add deep watch option
xtoolkit b6b6bfc
docs(api): useCookie add deepWatch option
xtoolkit 8955560
feat(nuxt): useCookie change deepWatch to watch option
xtoolkit 748dd0b
boolean|shallow
pi0 5b81275
enable watch by default
pi0 98e4cbf
docs: fix example
danielroe ab85b91
docs(api): update useCookie example
xtoolkit 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 | ||||
---|---|---|---|---|---|---|
|
@@ -15,6 +15,7 @@ export interface CookieOptions<T = any> extends _CookieOptions { | |||||
decode?(value: string): T | ||||||
encode?(value: T): string | ||||||
default?: () => T | Ref<T> | ||||||
watch?: boolean | 'deep' | 'shallow' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think better keep export function useCookieTest(watch: boolean) {
const test = useCookie('test', {}, {watch})
const change = () => {test.value = 100}
return {change}
} |
||||||
} | ||||||
|
||||||
export interface CookieRef<T> extends Ref<T> {} | ||||||
|
@@ -32,7 +33,12 @@ export function useCookie <T = string | null> (name: string, _opts?: CookieOptio | |||||
const cookie = ref<T | undefined>(cookies[name] as any ?? opts.default?.()) | ||||||
|
||||||
if (process.client) { | ||||||
watch(cookie, () => { writeClientCookie(name, cookie.value, opts as CookieSerializeOptions) }) | ||||||
const callback = () => { writeClientCookie(name, cookie.value, opts as CookieSerializeOptions) } | ||||||
if (opts.watch) { | ||||||
pi0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
watch(cookie, callback, { deep: opts.watch === 'deep' }) | ||||||
} else { | ||||||
callback() | ||||||
} | ||||||
} else if (process.server) { | ||||||
const nuxtApp = useNuxtApp() | ||||||
const writeFinalCookieValue = () => { | ||||||
|
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.