Skip to content

9.9. Vue 3 Integration

Wire a ComponentVMOf<M> to a Vue 3 component through the Composition API shallowRef and triggerRef primitives.

9.9.1. Reactivity Primitive

Vue 3 tracks reads of a shallowRef value and re-renders the template when that value changes. triggerRef forces the same re-render when the value keeps its reference. The adapter updates a shallowRef from VMx hub messages. Each message notifies watchers exactly once.

9.9.2. Mapping

Vue 3 VMx
shallowRef value + triggerRef PropertyChangedMessage<T> handler
@click="fn" command.execute() in fn
:disabled on a button canExecuteChanged handler + canExecute()
onBeforeMount(start) subscribe to the hub
onScopeDispose(() => cleanup) dispose the subscription

A republishModel() announcement keeps the same reference. When a matching message leaves the reference unchanged, the composable calls triggerRef. Otherwise it assigns the new value. A message for another VM or another property is filtered out.

The composable has three rules:

  • Call it inside setup() or an effectScope. Elsewhere it throws a TypeError, because no scope would release the subscription. Stopping the scope releases it.
  • Called in setup(), it subscribes just before the component's first render. A server render never runs that hook, so it leaves no subscription. Called after mount, for example in onMounted, it subscribes at once.
  • Hold view-models in shallowRef or markRaw. ref(), reactive() and data() wrap a VM in a proxy. The composable unwraps it with toRaw, but other code that reads a VM member through the proxy throws on VMx's #private fields.

9.9.3. Adapter Skeleton

The composable and component below are the exact files run by examples/typescript/integration-recipes against vue 3, the major version of the exact pin in the fixture's package.json. The tests cover an in-place mutation announced with republishModel(), replacement and scalar updates, one synchronous watcher run per message, proxied view-models, a change announced before the first mount, unrelated messages, unmount and effect-scope cleanup, a server render, and Save disabled while its command cannot run.

// composables/useVm.ts
import {
  getCurrentInstance,
  getCurrentScope,
  onBeforeMount,
  onScopeDispose,
  shallowRef,
  toRaw,
  triggerRef,
  type ShallowRef,
} from "vue";
import type { Subscription } from "rxjs";
import { type ComponentVMOf, type IMessageHub, whenPropertyChanged } from "@thekaveh/vmx";

/**
 * Binds one VM property for the lifetime of the calling component or effect
 * scope. `vm` and `hub` are fixed inputs: give the component a `:key` to rebind
 * it to another VM.
 */
export function useVm<M, K extends keyof ComponentVMOf<M>>(
  vm: ComponentVMOf<M>,
  hub: IMessageHub,
  property: K,
): Readonly<ShallowRef<ComponentVMOf<M>[K]>> {
  const scope = getCurrentScope();
  if (!scope) {
    throw new TypeError("useVm must be called inside setup() or an effect scope");
  }
  // A VM held in ref(), reactive() or data() arrives as a proxy. VMx reads
  // #private fields and hub messages carry the raw object, so bind the raw one.
  const raw = toRaw(vm);
  let current = raw[property];
  const value = shallowRef(current) as ShallowRef<ComponentVMOf<M>[K]>;
  let sub: Subscription | undefined;
  const start = (): void => {
    if (!scope.active) return;
    current = raw[property];
    value.value = current;
    sub = whenPropertyChanged(toRaw(hub), raw, property).subscribe(() => {
      const next = raw[property];
      if (Object.is(next, current)) {
        // A republished model keeps its reference, so trigger explicitly.
        triggerRef(value);
      } else {
        current = next;
        value.value = next;
      }
    });
  };
  // Before the first render, subscribe just before it: a server render never
  // runs mount hooks, so SSR subscribes nothing.
  const instance = getCurrentInstance();
  if (instance && !instance.isMounted) onBeforeMount(start);
  else start();
  onScopeDispose(() => sub?.unsubscribe());
  return value;
}
<script setup lang="ts">
import { onScopeDispose, shallowRef } from "vue";
import type { ComponentVMOf, ICommand, IMessageHub } from "@thekaveh/vmx";
import { useVm } from "./composables/useVm";
import type { Note } from "./note";

const props = defineProps<{
  vm: ComponentVMOf<Note>;
  hub: IMessageHub;
  saveCommand: ICommand;
}>();
const model = useVm(props.vm, props.hub, "model");
// Save is disabled while its command cannot run.
const canSave = shallowRef(props.saveCommand.canExecute());
const canSaveSub = props.saveCommand.canExecuteChanged.subscribe(() => {
  canSave.value = props.saveCommand.canExecute();
});
onScopeDispose(() => canSaveSub.unsubscribe());
</script>

<template>
  <h1>{{ model.title }}</h1>
  <button :disabled="!canSave" @click="props.saveCommand.execute()">Save</button>
</template>

vm and hub are fixed for the binding's lifetime. To show a different VM, re-create the component with a :key that identifies the VM, such as :key="vm.name" when sibling names are unique.

9.9.4. Fuller Example

There is no Vue Notes-Showcase. The runnable reference is examples/typescript/integration-recipes. For a packaged adapter, see the React recipe (react.md).