Skip to main content

lib/observable

Angular-like reactive observable system for FiveM.

This module provides a comprehensive observable system for managing state across client and server boundaries in FiveM.

Observable Types

TypeOwnerSync DirectionUse Case
serverOnlyServerNoneServer-internal state
clientOnlyClientNoneClient-internal state
webOwnedWebClient/Server → WebUI-specific state
serverOwnedServerServer → ClientsAuthoritative server state
clientOwnedClientClient → ServerClient-authoritative state
sharedBothBidirectionalCollaborative state

Player Subscriptions

By default, players must be manually subscribed to observables. Set broadcast: true in options to auto-subscribe all players.

Inter-Observable Subscription

Observables can subscribe to each other for synchronization:

const health = serverOwned({ id: "health", initialValue: 100 });
const display = serverOwned({ id: "healthDisplay", initialValue: "100%" });
 
// display updates when health changes
display.pipe(health, (h) => `${h}%`);

Classes

ClassDescription
ComputedObservableComputed observable that derives its value from other observables. Read-only - value is computed from sources.
ObservableBase Observable class for local-only reactivity.
ReadonlyObservableWrapperRead-only wrapper around an observable. Hides mutation methods while allowing subscription.

Interfaces

InterfaceDescription
ClientOwnedObservableClient-owned observable interface. Client has R/W, server has RO.
ObservableOptionsOptions for creating an observable.
ReadonlyObservableRead-only observable interface. Provides access to current value and subscription capability.
ServerOwnedObservableServer-owned observable interface. Server has R/W, clients have RO.
SharedObservableShared observable interface. Both client and server have R/W with conflict resolution.
SharedObservableOptionsOptions for shared observables with conflict resolution.
SubscriptionSubscription to an observable, can be disposed to unsubscribe.
SyncedObservableOptionsOptions for synchronized observables (serverOwned, clientOwned, shared).
WebOwnedObservableWeb-owned observable interface. Full read/write access on web, accessible via @Web methods from client/server.
WebOwnedObservableOptionsOptions for creating a web-owned observable.
WritableObservableWritable observable interface. Extends read-only with mutation capabilities.

Type Aliases

Type AliasDescription
ConflictResolverCustom conflict resolution function.
ObservableOwnershipObservable ownership type.
PlayerIdPlayer ID type for subscription tracking. This is the FiveM server ID (number).
SubscriptionCallbackCallback function invoked when observable value changes.

Functions

FunctionDescription
addPlayerSubscriptionSubscribe a player to an observable.
clearAllPlayerSubscriptionsClear all player subscriptions. Used during resource teardown.
clearPlayerSubscriptionsClear all player subscriptions for an observable.
clearRegistryClear all registered observables. Used during resource teardown.
clearWebNotifierClear the web notifier callback. Call this during client shutdown.
clientOnlyCreate a client-only observable. Only available on client runtime.
clientOwnedCreate a client-owned observable. Client has R/W, server has RO. Works on both server and client (server gets per-player read-only view).
computedCreate a computed observable that derives its value from other observables.
createClientOwnedClientCreate a client-owned observable. Client has R/W, server has RO. Works on both server and client (server gets per-player read-only view).
createClientOwnedServerCreate a client-owned observable. Client has R/W, server has RO. Works on both server and client (server gets per-player read-only view).
createClientOwnedWebCreate a client-owned observable. Client has R/W, server has RO. Works on both server and client (server gets per-player read-only view).
createObservableCreate a basic observable with local-only reactivity.
createServerOwnedClientCreate a server-owned observable. Server has R/W, clients have RO. Works on both server and client (client gets read-only view).
createServerOwnedServerCreate a server-owned observable. Server has R/W, clients have RO. Works on both server and client (client gets read-only view).
createServerOwnedWebCreate a server-owned observable. Server has R/W, clients have RO. Works on both server and client (client gets read-only view).
createSharedClientCreate a shared observable. Both client and server have R/W with conflict resolution.
createSharedServerCreate a shared observable. Both client and server have R/W with conflict resolution.
createSharedWebCreate a shared observable. Both client and server have R/W with conflict resolution.
createWebOwnedClientCreate a web-owned observable. Web has R/W, client/server can update via @Web methods. Used for UI-specific state.
createWebOwnedServerCreate a web-owned observable. Web has R/W, client/server can update via @Web methods. Used for UI-specific state.
createWebOwnedWebCreate a web-owned observable. Web has R/W, client/server can update via @Web methods. Used for UI-specific state.
getAllObservablesGet all registered observables.
getObservableGet an observable by ID.
getObservableEntryGet a registry entry by ID.
getObservableIdsGet all registered observable IDs.
getSubscribedPlayersGet the set of subscribed players for an observable.
hasObservableCheck if an observable is registered.
initObservableLifecycleInitialize observable lifecycle hooks.
initWebSyncInitialize web sync system on client. Call this during client bootstrap.
isPlayerSubscribedCheck if a player is subscribed to an observable.
notifyWebOfChangeNotify web of an observable value change. No-op if no notifier is registered (non-client runtimes).
registerObservableRegister an observable in the global registry.
removePlayerFromAllSubscriptionsRemove a player from all observable subscriptions. Call this when a player disconnects.
removePlayerSubscriptionRemove a player subscription from an observable.
serverOnlyCreate a server-only observable. Only available on server runtime.
serverOwnedCreate a server-owned observable. Server has R/W, clients have RO. Works on both server and client (client gets read-only view).
setWebNotifierRegister a web notifier callback. Call this from client runtime to enable web notifications.
shallowEqualsShallow equality function for plain objects. Compares object properties at one level deep. Falls back to strict equality for primitives and non-plain objects.
sharedCreate a shared observable. Both client and server have R/W with conflict resolution.
shutdownObservableLifecycleShutdown observable lifecycle management. Call this during server teardown.
shutdownWebSyncShutdown web sync system.
strictEqualsStrict equality function. Use this for primitives or when you want reference equality.
unregisterObservableUnregister an observable from the global registry.
webOwnedCreate a web-owned observable. Web has R/W, client/server can update via @Web methods. Used for UI-specific state.

References

deepEquals

Re-exports deepEquals