shared()
function shared\<T\>(options: SharedObservableOptions\<T\>): SharedObservable\<T\>;Defined in: src/lib/observable/shared.ts:102
Create a shared observable with bidirectional sync.
On both client and server: Returns a writable observable. Changes propagate bidirectionally with conflict resolution. On web: Returns a local-only stub (shared is for client↔server sync).
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type | Description |
|---|---|---|
options | SharedObservableOptions<T> | Configuration options |
Returns
Shared observable
Example
// In a service or module
const playerPosition = shared({
id: "player:position",
initialValue: { x: 0, y: 0, z: 0 },
conflictResolution: "client-wins", // Trust client for position
});
// Client-side: update position
playerPosition.set({ x: 100, y: 50, z: 0 });
// Server-side: read and override if needed
// Subscribe player first
playerPosition.subscribePlayer(playerId);
// Read player's position
const pos = playerPosition.getPlayerValue(playerId);
// Override if needed (e.g., teleport)
playerPosition.setPlayerValue(playerId, { x: 0, y: 0, z: 0 });