Home Manual Reference Source Repository
import Stream from 'protonic/src/stream.js'
public class | source

Stream

Direct Subclass:

Funnel, View

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 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.

Method Summary

Public Methods
public

forceState(state: Immutable.Map)

forceState - called when hydrating state from another source.

public

logStack(name: string, stack: Stack)

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 sendState (and, all the observers, by extension).

public

subscribe(observer: function): object

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:

NameTypeAttributeDescription
initialState Immutable

this is the state that the stream will start with.

Return:

Stream

Instance of a Stream.

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:

NameTypeAttributeDescription
state Immutable.Map

new state.

public logStack(name: string, stack: Stack) source

logStack - Adds a stack to automatically add state changes.

Params:

NameTypeAttributeDescription
name string

The name of this stream to be viewed in stack logs.

stack Stack

The stack to add state to.

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:

NameTypeAttributeDescription
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:

NameTypeAttributeDescription
observer function

the callback function to be executed whenever state updates

Return:

object

a subscriber object with an unsubscribe method