Vue 3 Integration
trackVueReactiveEvents watches a Vue 3 reactive() object and sends an RPS StateEvent for every change made through Vue. Feed the events to updateState to keep a plain copy of the state in step, in another process, a worker or on a server.
Signature
ts
function trackVueReactiveEvents<T extends object>(
vueState: T,
emit: (event: StateEvent) => void,
options?: {
emitInitialReplace?: boolean;
onDiffError?: (context: DiffErrorContext) => void;
}
): VueTrackingControl;
type VueTrackingControl = { stop(): void; pause(): void; resume(): void };
type DiffErrorContext = { key: string; path: (string | number | symbol)[]; error: unknown };Parameters
vueState: the object Vue'sreactive()returned (shallowReactive()works too, and so doesreadonly()over areactive()object;readonly()over a plain object is accepted but tracks nothing). It must come from the copy of@vue/reactivitythis library imports, see Requirements.emit: gets each event synchronously, inside the write that caused it. Vue tracking is paused while it runs, so reads it makes do not subscribe the tracker to anything.- The listener may write only to entries of the object whose event it received. The tracker then compares that object once more and sends the write. The second compare runs once: a write made during it changes the state, but the tracker sends it only with the next change to that object, and with a wrong
oldValue. - Any other write must leave the listener, for example through
queueMicrotask. A write into another object while a compare runs can send an event twice, the first one with a wrongoldValue.
- The listener may write only to entries of the object whose event it received. The tracker then compares that object once more and sends the write. The second compare runs once: a write made during it changes the state, but the tracker sends it only with the next change to that object, and with a wrong
options.emitInitialReplace(defaulttrue): first send{ action: 'replace', path: [], newValue }, wherenewValueis a copy of the whole state.options.onDiffError: called whenemitthrows, or when reading the state throws (a getter).pathis the event's path for anemiterror, else the path of the object that failed.keyis the first key ofpath, or''when the failure is in the root object.- After an
emiterror the tracker goes on and sends the rest of that change. - After a read error it rebuilds that object's copy from the current state without events, so that one write is not sent.
- After an
Return value
pause(): stop sending events. Writes made while paused still update the tracker's copy, so they are never sent, and later events compare against the state they left. Use it around writes that came from the other side.resume(): send events again. It costs nothing.stop(): stop for good and free every effect.stop()is final: a stopped tracker cannot start again, andpause()orresume()after it only print a warning. Make a new tracker instead.
Cost
The tracker gives every object, array, Map and Set in the state its own small Vue effect, which reads only that object's own entries. A write runs only the effect of the object it changed, which compares that object's entries with its copy. So a write costs what it changed, not the size of the state, and a paused write costs the same without the events. Storing a new subtree costs one copy of that subtree.
Memory: the tracker holds a plain copy of the state and one effect per object.
What It Emits
- Objects:
setfor a changed or added key anddeletefor a removed key, with the fullpath. A changed value carriesoldValue, an added key does not. - Arrays: compared index by index. A grow sends one
setper new index and nolengthevent, so a replica never has holes. A shrink sends onesetonlength. - A value that changes type (an object to an array, a primitive to an object) sends one
setwith the whole new value. A replaced object with the same content sends nothing. - Maps:
map-setandmap-deletewith whole values at the Map's path. A write anywhere inside a Map value sends amap-setof that whole value. - Sets:
set-addandset-delete.
Notes
- Values in events are copies, and a sent event never changes later. Objects in
setanddeleteevents are plain: no class prototype, no symbol keys. NaNequalsNaN, so aNaNvalue is sent once.- One object at two places sends one event per place.
- Write through Vue's proxy. A write to a raw object (
toRaw(x).a = 1), to the inside of amarkRawor frozen object, or aDatechanged in place (setTime) triggers nothing. The tracker sees it the next time the object that holds it changes. - Set members and Map keys that are objects are compared with copies, so any change to that Set or Map sends them again. Use primitives there.
- A cycle (an object inside itself) is reported through
onDiffErrorand not tracked below that point.
Requirements
- Vue 3.5: the peers are
vue ^3.5.0and@vue/reactivity ^3.5.0. - One copy of
@vue/reactivity. The library imports it and does not bundle it. A proxy from another copy would never trigger the tracker, so creation throws aTypeErrorthat names the cause.- The
@vue/reactivitypeer is optional, so the library uses the copyvuebrings.vuepins its own exact version; a required peer made npm install the newest 3.5 next to it, a second copy. - With Vite, set
resolve.dedupe: ['vue', '@vue/reactivity']. - A package manager with an isolated layout (pnpm,
bun install --linker isolated) needs@vue/reactivityas a direct dependency of the app, at the versionvueuses.
- The
Example
ts
import { reactive as vueReactive } from 'vue';
import { trackVueReactiveEvents, updateState, reactive as rpsReactive, StateEvent } from '@yiin/reactive-proxy-state';
// Vue state (renderer)
const vueState = vueReactive({ todos: [], prefs: new Map([["theme", "light"]]) });
// RPS state (could be in another context)
const remote = rpsReactive({ todos: [], prefs: new Map() });
// Observe Vue and apply changes to RPS
const tracking = trackVueReactiveEvents(vueState, (ev: StateEvent) => updateState(remote, ev));
// Apply a change from the other side without sending it back
tracking.pause();
try {
vueState.todos.push({ title: 'from remote' });
} finally {
tracking.resume();
}
// Later
// tracking.stop();Electron (Renderer) Wiring
ts
// renderer.ts
import { reactive as vueReactive } from 'vue';
import { trackVueReactiveEvents, updateState, StateEvent } from '@yiin/reactive-proxy-state';
const { ipcRenderer } = window.require?.('electron') ?? {};
export const vueState = vueReactive({
todos: [],
user: { name: 'Alice' },
prefs: new Map([["theme", "light"]])
});
// Vue -> main
const tracking = trackVueReactiveEvents(vueState, (ev: StateEvent) => ipcRenderer?.send('state:mutation', ev));
// main -> Vue, not sent back
ipcRenderer?.on('state:mutation', (_evt: any, ev: StateEvent) => {
tracking.pause();
try {
updateState(vueState, ev);
} finally {
tracking.resume();
}
});Related
- Upgrading to 3.0.0 – what changed from 2.x
updateState– apply events to any target object (including Vue proxies)reactive– create RPS proxies that emitStateEvents directly