Skip to content

vue-split-state ​

Async state kept by hand in refs, set around an await:

  • a function sets a ref to true before an await and back after it (a loading flag), with or without other refs set after the await (the data, the error);
  • a ref that starts as "" and is named for an error (error, saveError, failure) is set after an await: a failure as a string, with "" for none.

Why: that is one async state kept in three refs, and the refs can disagree. An error from this request sits next to the data from the previous one. An older, slower request finishes last and turns off the newer one's loading while it is still running. AsyncState is one value that is Idle, Loading, Ready or Failed, so it can't be two at once. Guide: Vue.

✕ Rejectedvue-split-state
vue
<script setup lang="ts">
import { onMounted, ref, shallowRef } from "vue";
import { isErr } from "@evobug_com/safe-ts-std/result";
import { fetchOrders, type Order } from "./api";

const loading = ref(false);
const error = ref("");
const orders = shallowRef<readonly Order[]>([]);

async function load(): Promise<void> {
  loading.value = true;
  const result = await fetchOrders();
  if (isErr(result)) error.value = result.error.message;
  else orders.value = result;
  loading.value = false;
}

onMounted(() => {
  load().catch((reason: unknown) => console.error(reason));
});
</script>

<template>
  <p v-if="loading">Loading…</p>
  <p v-if="error !== ''">{{ error }}</p>
  <p v-for="order in orders" :key="order.id">{{ order.total }}</p>
</template>
✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import { onMounted } from "vue";
import { Match } from "@evobug_com/safe-ts-vue/match";
import { useTask } from "@evobug_com/safe-ts-vue/task";
import { fetchOrders } from "./api";

const { state, run } = useTask((signal) => fetchOrders(signal));

onMounted(() => {
  run().catch((reason: unknown) => console.error(reason));
});
</script>

<template>
  <Match :value="state">
    <template #Loading>Loading…</template>
    <template #Failed="{ error }">{{ error.message }}</template>
    <template #Ready="{ value }">
      <p v-for="order in value" :key="order.id">{{ order.total }}</p>
    </template>
    <template #_></template>
  </Match>
</template>

useTask also aborts the previous run when a new one starts, and the run in progress when the component unmounts.

A lone flag is the same state with the same race: click twice, and the first request to finish turns saving off while the second is still running.

✕ Rejectedvue-split-state
vue
<script setup lang="ts">
import { ref } from "vue";
import { isErr } from "@evobug_com/safe-ts-std/result";
import { saveOrder } from "./api";

const saving = ref(false);

async function save(): Promise<void> {
  saving.value = true;
  const saved = await saveOrder();
  saving.value = false;
  if (isErr(saved)) console.error(saved.error.message);
}
</script>

<template>
  <button :disabled="saving" @click="save().catch((reason: unknown) => console.error(reason))">Save</button>
</template>

With useTask, the button reads the task's state: :disabled="state.tag === 'Loading'".

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra