From 77a36461beaf9b6e8cf2e71882002070e4883e46 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 15 Jul 2021 15:11:01 +0200 Subject: [PATCH] chore: add counter setup to playground --- playground/src/stores/counterSetup.ts | 62 ++++++++++++++++++++++ playground/src/views/CounterSetupStore.vue | 50 +++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 playground/src/stores/counterSetup.ts create mode 100644 playground/src/views/CounterSetupStore.vue diff --git a/playground/src/stores/counterSetup.ts b/playground/src/stores/counterSetup.ts new file mode 100644 index 00000000..80dbfc4c --- /dev/null +++ b/playground/src/stores/counterSetup.ts @@ -0,0 +1,62 @@ +import { computed, toRefs, reactive } from 'vue' +import { acceptHMRUpdate, defineSetupStore } from '../../../src' + +const delay = (t: number) => new Promise((r) => setTimeout(r, t)) + +export const useCounter = defineSetupStore('counter-setup', () => { + const state = reactive({ + n: 0, + incrementedTimes: 0, + decrementedTimes: 0, + numbers: [] as number[], + }) + + const double = computed(() => state.n * 2) + + function increment(amount = 1) { + if (typeof amount !== 'number') { + amount = 1 + } + state.incrementedTimes++ + state.n += amount + } + + function changeMe() { + console.log('change me to test HMR') + } + + async function fail() { + const n = state.n + await delay(1000) + state.numbers.push(n) + await delay(1000) + if (state.n !== n) { + throw new Error('Someone changed n!') + } + + return n + } + + async function decrementToZero(interval: number = 300) { + if (state.n <= 0) return + + while (state.n > 0) { + state.n -= 1 + state.decrementedTimes += 1 + } + await delay(interval) + } + + return { + ...toRefs(state), + double, + increment, + fail, + changeMe, + decrementToZero, + } +}) + +if (import.meta.hot) { + import.meta.hot.accept(acceptHMRUpdate(useCounter, import.meta.hot)) +} diff --git a/playground/src/views/CounterSetupStore.vue b/playground/src/views/CounterSetupStore.vue new file mode 100644 index 00000000..b68ad5af --- /dev/null +++ b/playground/src/views/CounterSetupStore.vue @@ -0,0 +1,50 @@ + + + -- 2.47.2