Skip to content

Commit

Permalink
refactor: minor
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Apr 18, 2024
1 parent 630b797 commit 4f4e819
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
11 changes: 2 additions & 9 deletions examples/vue-ssr-extra/src/routes/server/_store.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { defineStore } from "pinia";
import { changeCounter } from "./_action";

type CounterState = {
data: number;
isReady: boolean;
data: number | null;
};

export const useServerCounter = defineStore("server-counter", {
state: () => <CounterState>{ data: 0, isReady: false },
actions: {
async change(delta: number) {
this.data = await changeCounter(delta);
},
},
state: () => <CounterState>{ data: null },
});
8 changes: 2 additions & 6 deletions examples/vue-ssr-extra/src/routes/server/page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ const store = useServerCounter();
// TODO: suspend?
onServerPrefetch(async () => {
store.data = await getCounter();
store.isReady = true;
});
onMounted(async () => {
// TODO: refetch on stale?
if (!store.isReady) {
store.data = await getCounter();
store.isReady = true;
}
store.data ??= await getCounter();
});
// TODO: pending state?
Expand All @@ -30,7 +26,7 @@ const formAction = enhanceFormAction(changeCounter, {

<template>
<Form :action="formAction">
<div>Server Counter: {{ store.isReady ? store.data : "..." }}</div>
<div>Server Counter: {{ store.data ?? "..." }}</div>
<button type="submit" name="delta" value="-1">-1</button>
<button type="submit" name="delta" value="+1">+1</button>
</Form>
Expand Down

0 comments on commit 4f4e819

Please sign in to comment.