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
fix(nuxt): use shared state for asyncData #7055
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const useSleep = () => useAsyncData('sleep', async () => { | ||
await new Promise(resolve => setTimeout(resolve, 50)) | ||
|
||
return 'Slept!' | ||
}) | ||
|
||
export const useCounter = () => useFetch('/api/useAsyncData/count') |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<template> | ||
<div> | ||
Single | ||
<div> | ||
data1: {{ data1 }} | ||
data2: {{ data2 }} | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
const { data: data1 } = await useSleep() | ||
const { data: data2 } = await useSleep() | ||
|
||
if (data1.value === null || data1.value === undefined || data1.value.length <= 0) { | ||
throw new Error('Data should never be null or empty.') | ||
} | ||
|
||
if (data2.value === null || data2.value === undefined || data2.value.length <= 0) { | ||
throw new Error('Data should never be null or empty.') | ||
} | ||
|
||
if (data1.value !== data2.value) { | ||
throw new Error('AsyncData not synchronised') | ||
} | ||
</script> |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<template> | ||
<div> | ||
Single | ||
<div> | ||
data1: {{ result1.data.value }} | ||
data2: {{ result2.data.value }} | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
const [result1, result2] = await Promise.all([useSleep(), useSleep()]) | ||
|
||
if (result1.data.value === null || result1.data.value === undefined || result1.data.value.length <= 0) { | ||
throw new Error('Data should never be null or empty.') | ||
} | ||
|
||
if (result2.data.value === null || result2.data.value === undefined || result2.data.value.length <= 0) { | ||
throw new Error('Data should never be null or empty.') | ||
} | ||
|
||
if (result1.data.value !== result2.data.value) { | ||
throw new Error('AsyncData not synchronised') | ||
} | ||
|
||
await result1.refresh() | ||
|
||
if (result1.data.value !== result2.data.value) { | ||
throw new Error('AsyncData not synchronised') | ||
} | ||
</script> |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<template> | ||
<div> | ||
Single | ||
<div> | ||
{{ data }} - {{ data2 }} | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
const { data, refresh } = await useCounter() | ||
const { data: data2, refresh: refresh2 } = await useCounter() | ||
|
||
let inital = data.value.count | ||
|
||
// Refresh on client and server side | ||
await refresh() | ||
|
||
if (data.value.count !== inital + 1) { | ||
throw new Error('Data not refreshed?' + data.value.count + ' : ' + data2.value.count) | ||
} | ||
|
||
if (data.value.count !== data2.value.count) { | ||
throw new Error('AsyncData not synchronised') | ||
} | ||
|
||
inital = data.value.count | ||
|
||
await refresh2() | ||
|
||
if (data.value.count !== inital + 1) { | ||
throw new Error('data2 refresh not syncronised?') | ||
} | ||
|
||
if (data.value.count !== data2.value.count) { | ||
throw new Error('AsyncData not synchronised') | ||
} | ||
|
||
</script> |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<template> | ||
<div> | ||
Single | ||
<div> | ||
{{ data }} | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
const { data } = await useSleep() | ||
|
||
if (data.value === null || data.value === undefined || data.value.length <= 0) { | ||
throw new Error('Data should never be null or empty.') | ||
} | ||
</script> |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
let counter = 0 | ||
|
||
export default () => ({ count: counter++ }) |
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.
@pi0 Have you checked whether this retains reactivity if this is created outside a component setup (eg in plugin), or when it is used after the original component that created it has unmounted?
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.
You can try it (as mentioned in the description we need to iterate over to fix the related issue).
BTW really the use case of useAsyncData is to be used within the component lifecycle, not the plugins even if partially possible.
Good point about reactivity after unmounting. Will test this.