DEPRECATED: Please use Observux instead. It has the same functionality with a simpler API.
Spitfire.js
A tiny reactive data model library.
Requirements
Installation
npm install rxjs
npm install spitfirejs
API
Model.constructor([source])
Convenience class for extension and lazy model transformation. A Model
instance gets transformed into a reactive data model when its prototype property state$
is called for the first time. If a source
object is available in the constructor, its own enumerable properties are copied onto the new Model
instance.
const model = foo: 'bar' // => Model { foo: 'bar' } model // => false; the model is not transformed yetmodelstate$ // => Observable<object>; the property `state$` gets called for the first timemodel // => true; the model is now transformed
Model.transform(source)
Transforms the source
object into a reactive model by redefining its own enumerable non-function properties into proxies which handle values via getters and setters using RxJS BehaviorSubject
instances. The combined latest properties are accessible via the state$
property, an RxJS Observable
stream of previous and next states.
Returns the frozen source
object.
const source = foo: 'bar' const model = Model // => Object { foo: 'bar' } model === source // => true; the original source object is transformed into a modelmodel // => true; the model is immediately transformed
Usage
The reactive data model can be used to store any data and immediately notify its state$
observers of any state changes. A simple example:
{ super thiscount = 0 } { thiscount += 1 } { thiscount -= 1 } const counter = // => Counter { count: 0 } const subscriber1 = counterstate$ // `0` is logged on subscriptioncounter // `1` is logged on changecounter // `0` is logged on change subscriber1counter // the count is incremented, but nothing is logged because there are no subscribersconst subscriber2 = counterstate$
Since each state$
property is a regular RxJS Observable
instance, they can be used with any RxJS operators, e.g. for filtering, transforming or combining multiple states. An example:
{ super thisname = name thislastWatched = null } { thislastWatched = } const movie1 = "Monty Python and the Holy Grail"const movie2 = "Monty Python's Life of Brian"const movie3 = "Monty Python's The Meaning of Life" // Observe the order of watched movies when all movies have been watched at least onceObservable // Discard the previous state // Proceed only if all movies have been watched // Sort movies by the time they were last watched // Take only movie names movie1 // Nothing happens, because not all movies have been watched yetmovie2 // Nothing happens, because not all movies have been watched yetmovie3 // ["Monty Python and the Holy Grail", "Monty Python's Life of Brian", "Monty Python's The Meaning of Life"]movie2 // ["Monty Python and the Holy Grail", "Monty Python's The Meaning of Life", "Monty Python's Life of Brian"]movie1 // ["Monty Python's The Meaning of Life", "Monty Python's Life of Brian", "Monty Python and the Holy Grail"]