Stream
A Stream holds the canonical state of a part of an application, and pushes changes to that state to any observers that are subscribed to the Stream.
Constructor Summary
Public Constructor | ||
public |
constructor(initialState: Immutable): Stream constructor - When creating a new stream, an initial state is not required, but it is encouraged. |
Member Summary
Public Members | ||
public get |
state: Immutable: * get state - a getter for returning the current internal state of the Stream. |
|
public set |
state(newState: type): type: * set state - a setter that's probably more clever than it needs to be. Instead of setting
the internal state, it calls
Like I said, maybe too clever, but I thinks allow for a more delarative style when writing transformers. |
Method Summary
Public Methods | ||
public |
forceState(state: Immutable.Map) forceState - called when hydrating state from another source. |
|
public |
logStack - Adds a stack to automatically add state changes. |
|
public |
next(newState: Immutable) next - Receives new state (externally, ideally through a transformer) and verifies that the
new state is different from the Stream's current state before passing on to |
|
public |
subscribe - takes a callback function that is executed any time the stream received new state. |
Public Constructors
public constructor(initialState: Immutable): Stream source
constructor - When creating a new stream, an initial state is not required, but it is encouraged. If an initial state is passed in, it should be an Immutable data structure.
Params:
Name | Type | Attribute | Description |
initialState | Immutable | this is the state that the stream will start with. |
Public Members
public get state: Immutable: * source
get state - a getter for returning the current internal state of the Stream.
Return:
Immutable | current state contained by the Stream |
public set state(newState: type): type: * source
set state - a setter that's probably more clever than it needs to be. Instead of setting
the internal state, it calls next
.
stream.next(newState)
// is functionally the same as
stream.state = newState
Like I said, maybe too clever, but I thinks allow for a more delarative style when writing transformers.
Return:
type | description |
Public Methods
public forceState(state: Immutable.Map) source
forceState - called when hydrating state from another source.
Params:
Name | Type | Attribute | Description |
state | Immutable.Map | new state. |
public logStack(name: string, stack: Stack) source
logStack - Adds a stack to automatically add state changes.
public next(newState: Immutable) source
next - Receives new state (externally, ideally through a transformer) and verifies that the
new state is different from the Stream's current state before passing on to sendState
(and,
all the observers, by extension).
Params:
Name | Type | Attribute | Description |
newState | Immutable |
public subscribe(observer: function): object source
subscribe - takes a callback function that is executed any time the stream received new
state. The callback should accept state
as an argument. If the stream currently has state,
it will be passed to the callback immediately upon subscription.
An observer gets added to an observers
array. State changes will propagate to any observers
in this array.
It will return an object (we'll call a subscriber) that has an unsubscribe
method. This
method will remove the observer from the observers
List and should be called anytime the
observer is done listening to the stream. Failing to do so could cause memory leaks.
Params:
Name | Type | Attribute | Description |
observer | function | the callback function to be executed whenever state updates |