Observux
Reactive state management for JavaScript apps with a minimal API and full power of RxJS.
Requirements
Installation
npm install observux
Usage
Observux manages states using reactive state containers called stores: JavaScript objects with observable properties. Assigning values to the properties emits an updated state to its subscribers:
const counter = count: 0 const subscription = counterstate // logs {count: 0} countercount++ // logs {count: 1}countercount-- // logs {count: 0} subscription
Stores can be extended, and their observable state
values can be adjusted using RxJS operators:
{ // Observable properties are passed to the superclass super users: lastFetched: null // Non-observable properties are assigned to the subclass thisname = 'Github user service' } { const request = Promiseallusernames thislastFetched = // Monitor future timestamp changes to generate an abort signal const abortSignal = thisstate // Wrap the request promise in an Observable to make it cancellable Observable } const githubUserService = githubUserServicestate githubUserService // logs 'The Octocat', 'Linus Torvalds'
API
Store(props)
Base class for Observux stores.
A Store
instance's observable properties are defined in the props
object. It also has a special state
property, an observable sequence of its state values. Assigning values to its observable properties emits a new state value to subscribers.
Under the hood, props
are converted to getters/setters to allow tracking of state changes. Due to ES5 limitations (and poor ES6 Proxy support), it is currently not possible to detect new observable properties once a store has been created.