From d5a880b81875674156bf2355385ed19831b23117 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Tue, 20 Jul 2021 19:06:28 +0200 Subject: [PATCH] chore: more work --- playground/src/stores/jokes-swrv.ts | 27 ++++++++++ playground/src/views/Jokes.vue | 21 ++++---- playground/src/views/swrv.vue | 79 ++++++++++++++++++++++++++++- 3 files changed, 115 insertions(+), 12 deletions(-) create mode 100644 playground/src/stores/jokes-swrv.ts diff --git a/playground/src/stores/jokes-swrv.ts b/playground/src/stores/jokes-swrv.ts new file mode 100644 index 00000000..6f95b531 --- /dev/null +++ b/playground/src/stores/jokes-swrv.ts @@ -0,0 +1,27 @@ +import { ref, unref, watch } from 'vue' +import { acceptHMRUpdate, defineStore } from '../../../src' +import { getRandomJoke, Joke } from '../views/api/jokes' +import useSWRV from 'swrv' + +export const useJokesSetup = defineStore('jokes-swrv-setup', () => { + // const current = ref(null) + const history = ref([]) + + const { data, error, isValidating, mutate, revalidate } = useSWRV( + 'jokes', + getRandomJoke + ) + + watch(data, (joke) => { + if (joke) { + history.value.push(joke) + } + }) + + return { current: data, history, fetchJoke: revalidate } +}) + +if (import.meta.hot) { + // import.meta.hot.accept(acceptHMRUpdate(useJokes, import.meta.hot)) + import.meta.hot.accept(acceptHMRUpdate(useJokesSetup, import.meta.hot)) +} diff --git a/playground/src/views/Jokes.vue b/playground/src/views/Jokes.vue index 2554d5d7..5741f5b5 100644 --- a/playground/src/views/Jokes.vue +++ b/playground/src/views/Jokes.vue @@ -14,15 +14,14 @@
- -
- {{ jokes.current.setup }} -
-
-

- {{ jokes.current.punchline }} -

-
+
+ {{ jokes.current.setup }} +
+
+

+ {{ jokes.current.punchline }} +

+
@@ -60,8 +59,8 @@ onMounted(() => { console.log('mounted') // @ts-expect-error window.jo = jokes - console.log('new pending') - fetchRandomJoke() + console.log('new pending') + fetchRandomJoke() }) diff --git a/playground/src/views/swrv.vue b/playground/src/views/swrv.vue index a63adb89..ca976375 100644 --- a/playground/src/views/swrv.vue +++ b/playground/src/views/swrv.vue @@ -1,3 +1,80 @@ + + + + -- 2.47.3