Vize

create-shared-composable

Share one instance of a composable between all simultaneously mounted callers, reference-counted by their effect scopes.

Package @vizejs/composable/create-shared-composable
Own the source vize lib pull composable:create-shared-composable
Runtime exports createSharedComposable
Gzip budget 1536 B

Usage

import { createSharedComposable } from "@vizejs/composable/create-shared-composable";

Runtime contract

Utility Category Stability SSR Hydration Cleanup Targets Host globals Uses
createSharedComposable state experimental safe stable reactive-scope web, server, worker, native, desktop, terminal window tryOnScopeDispose

API

createSharedComposable

Share one instance of a composable between all simultaneously mounted callers, reference-counted by their effect scopes. The first caller creates the instance inside a detached scope using its arguments (later callers' arguments are ignored while it lives). When the last subscribing scope stops, the instance's scope is stopped and the next call starts fresh. Calls outside any effect scope subscribe permanently. On the server every call gets its own instance unless shareOnServer is set, so request state never leaks.

function createSharedComposable<Arguments extends readonly unknown[], Result>( composable: (...args: Arguments) => Result, options: CreateSharedComposableOptions = {}, ): (...args: Arguments) => Result
const useSharedMouse = createSharedComposable(useMouse);

Types

CreateSharedComposableOptions

Options for createSharedComposable.

Member Type Description
shareOnServer? boolean Share one instance across callers on the server too. Disabled by default because module-level sharing leaks state between requests; without a browser window every call then gets its own instance.