Syncio
Syncio is a utility module for handling potentially asynchronous operations in a uniform way.
Overview
In the framework, many methods (like lifecycle hooks or actions) can return either a plain value (synchronously) or a Promise (asynchronously). Syncio provides a single entry point to ensure these are handled correctly without duplicating if (val instanceof Promise) logic everywhere.
API Reference
ensureSync<T>(val: T | Promise<T>): Promise<Awaited<T>>
Ensures that a value is awaited if it is a Promise.
val: The value or promise to be awaited.- Returns: A
Promisethat resolves to the actual value of typeT.
Why Syncio?
Vue doesn't always await its lifecycle hooks, but sometimes the framework or user needs to ensure certain cleanup or coordination happens.
Syncio is used internally for:
- Action Execution: Awaiting
onActionresults inViewModel.runAction. - Delegates: Awaiting each subscriber during a
SEQUENTIALinvoke or all subscribers during aPARALLELinvoke. - Router Guards: Awaiting
RouteAdapter.guardresults. - Lifecycle Management: Awaiting
unmountedhooks inuseViewModelInstance.
By using ensureSync, the framework can support both synchronous and asynchronous user code with minimal overhead.
typescript
import {syncio} from 'vue-mvvm';
async function process(something: string | Promise<string>) {
// We don't care if it's a promise or a string, we just want the value.
const val = await syncio.ensureSync(something);
console.log(val);
}