Fastflux Home Reference Source Repository
import {Store} from 'fastflux/core/store'
public class | source

Store

Extends:

ObservableObservableState → Store

Container for application state and logic.

This class is abstract and cannot be instantiated.

Implementing

A store:

  1. Must have instance method getInitialState with signature:

    function(): Any<S>
  2. Must have either one of:

    • static method reducer with signature:

      function(state: Any<S>, message: Message): Any<S>
    • static property reducers with signature:

      {[string]: function(state: Any<S>, message: Message): Any<S>}

Single reducer variant:

let logger = new class extends Store {
  getInitialState() { return [] }
  static reducer(state, {type, value}) {

    if (type === "add") {}
      return state.concat([value]);
    }

  }
}

Object mapping Message#type to reducers:

let logger = new class extends Store {
  getInitialState() { return [] }
  static reducers = {

     add(state, {value}) {
       return state.concat([value])
     }

  }
}

Note: you can also create a store with createStore.


Example:

let counter = new class extends Store {
  getInitialState() { return 0 }
  static reducers = {
    inc(state, {amount}) { return state + amount; },
    dec(state, {amount}) { return state - amount; }
  }
}

function increment(amount=1) {
  counter.send({type: "inc", amount})
}

function decrement(amount=1) {
  counter.send({type: "dec", amount})
}

counter.subscribe(n => console.log("Counter value:", n));

increment(); //> Counter value: 1
increment(); //> Counter value: 2
increment(2); //> Counter value: 4

decrement(12); //> Counter value: -8

Constructor Summary

Public Constructor
public

Method Summary

Public Methods
public abstract
public

send(message: Message, context: Any)

Send a message.

Inherited Summary

From class Observable
public

emit(value: Any)

Invoke each registered listener with value.

public

filter(predicate: function(value: Any): boolean): Observable

public

True if listener has subscribed to this observable

public

map(mapper: function(value: Any): Any): Observable

public

reduce(accumulator: Any, reducer: function(accumulator: Any, value: Any): Any): ObservableState

public

Register listener

public

unsubscribe(listener: function)

Unregister listener

From class ObservableState
public

emit(value: Any)

Same as Observable#emit, but sets current state to value before emitting

public

filter(predicate: function(value: Any): boolean): ObservableState

Same as Observable#filter, but returns ObservableState

public

getState(callback: function): Any | undefined

Return current state or asynchronously invoke callback

public

map(mapper: function(value: Any): Any): ObservableState

Same as Observable#map, but returns ObservableState

public

reduce(accumulator: Any, reducer: function(accumulator: Any, value: Any): Any): ObservableState

Same as Observable#reduce, but starts with current state reduced

Public Constructors

public constructor source

Override:

ObservableState#constructor

Throw:

Error

when trying to instantiate the abstract Store directly

Error

when no reducers are defined

Error

when getInitialState is not implemented

Public Methods

public abstract getInitialState(): Any source

Return:

Any

public send(message: Message, context: Any) source

Send a message. Bound method

Params:

NameTypeAttributeDescription
message Message
context Any
  • optional