vue-ref-without-value
A ref() made without a value (ref<User>()), set after an await, and shown in the template.
Why: that is data being loaded, kept as "undefined until it's there". The template has to guard every read of it (vue-maybe-undefined), "loading" looks the same as "not started", and a failure has nowhere to go. An AsyncState gives each its own variant, and <Match> gives each its own slot. Guide: Vue.
✕ Rejectedvue-ref-without-value
vue
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { isErr } from "@evobug_com/safe-ts-std/result";
import { fetchProfile, type Profile } from "./api";
const profile = ref<Profile>();
onMounted(async () => {
const result = await fetchProfile();
if (!isErr(result)) profile.value = result;
});
</script>
<template>
<h1 v-if="profile">{{ profile.name }}</h1>
</template>If the request fails, the page stays empty, the same as while it loads.
✓ 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 { fetchProfile } from "./api";
const { state, run } = useTask((signal) => fetchProfile(signal));
onMounted(() => {
run().catch((reason: unknown) => console.error(reason));
});
</script>
<template>
<Match :value="state">
<template #Ready="{ value }"><h1>{{ value.name }}</h1></template>
<template #Failed>Couldn't load your profile.</template>
<template #_>Loading…</template>
</Match>
</template>A ref that starts empty and is never shown is fine: a timer id, a socket, an element. So is one set without an await (a selection, a draft).
A ref given a starting value, undefined or null included (ref<User | undefined>(undefined), ref<string | null>(null)), is not reported: writing the value out says that "none" is one of its states, as for which row is open or which action is running. For loaded data, useTask is still the better shape. (null itself is no-null's business.)
A write in a helper counts where the helper is called: show(await load()), with show setting the ref, is the same as setting it after the await directly. A ref already reported by vue-split-state or vue-async-race isn't reported again: the fix is the same useTask.