API Docs for:
Show:

Modules._BaseModule Class

A basic module to build other modules upon

Constructor

Modules._BaseModule

()

Methods

addObserver

(
  • x
  • [m = 'update']
)
This

Adds a observer object to the observable

Parameters:

  • x Object

    The object to register as observer

  • [m = 'update'] Function | String optional

    Function or method to use as handler

Returns:

This:

The observable instance

Example:

import Observable from './mixins/observable';

const logger = {
    update(message) {
        console.log(message);
    }
}

const MyClass = Observable(class {
    constructor() {
        this.addObserver(logger);
    }
});
import Observable from './mixins/observable';

const logger = {
    log(message) {
        console.log(message);
    }
}

const MyClass = Observable(class {
    constructor() {
        this.addObserver(logger, 'log');
    }
});

addState

(
  • state
)
This

Allows to add a new state (see example for more info)

Parameters:

  • state Object

    The new state

Returns:

This:

The instance

Example:

import FSM from './mixins/fsm';

const MyClass = FSM(class {
    constructor() {
        // New states MyClass can be in are added
        this.addState({
                name: 'init',
                events: {next: 'after'}
            }).
            addState({
                name: 'after',
                events: {next: 'last', prev: 'init'}
            }).
            addState({
                name: 'last',
                events: {prev: 'after'}
            });
    }
});

addStates

(
  • states
)
This

Allows to add an array of new states all at once

Parameters:

  • states Array

    The new states

Returns:

This:

The instance

Example:

import FSM from './mixins/fsm';

const MyStates = [
    {name: 'init', events: {next: 'after'}},
    {name: 'after', events: {next: 'last', prev: 'init'}},
    {name: 'last', events: {prev: 'after'}}
];

const MyClass = FSM(class {
    constructor() {
        // New states MyClass can be in are added
        this.addStates(MyStates);
    }
});

consume

(
  • eventName
)
This

Transitions from the current state to the state specified by the given name of event to consume.

Parameters:

  • eventName String

    Name of the event to consume

Returns:

This:

The instance

Example:

import FSM from './mixins/fsm';

const MyStates = [
    {name: 'init', events: {next: 'after'}},
    {name: 'after', events: {next: 'last', prev: 'init'}},
    {name: 'last', events: {prev: 'after'}}
];

const MyClass = FSM(class {
    constructor() {
        // New states MyClass can be in are added
        this.addStates(MyStates);
    }
});

let myInstance = new MyClass();
myInstance.getState(); // -> 'init'

myInstance.consume('next').consume('next').getState(); // -> 'last'

destroy

() Undefined

Destroys the instance and removes all observers and states

Returns:

Undefined:

Nothing

destroyObservers

() Undefined

Destroys the observable, unregisters all observers

Returns:

Undefined:

Nothing

Example:

import Observable from './mixins/observable';

const logger = {
    update(message) {
        console.log(message);
    }
}

const MyClass = Observable(class {
    constructor() {
        this.addObserver(logger);
    }
});

let myInstance = new MyClass();
myInstance.notifyObservers('Sending a message');
// logs "Sending a message"

myInstance.destroyObservers();

myInstance.notifyObservers('Sending another message');
// nothing happens

destroyStates

() Undefined

Destroys all states associated with the FSM

Returns:

Undefined:

Nothing

Example:

import FSM from './mixins/fsm';

const MyStates = [
    {name: 'init', events: {next: 'after'}},
    {name: 'after', events: {next: 'last', prev: 'init'}},
    {name: 'last', events: {prev: 'after'}}
];

const MyClass = FSM(class {
    constructor() {
        // New states MyClass can be in are added
        this.addStates(MyStates);
    }
});

let myInstance = new MyClass();
myInstance.destroyStates();

getState

() String | Null

Returns the name of the current state or null if no state is set

Returns:

String | Null:

Name of the current state or null

Example:

import FSM from './mixins/fsm';

const MyStates = [
    {name: 'init', events: {next: 'after'}},
    {name: 'after', events: {next: 'last', prev: 'init'}},
    {name: 'last', events: {prev: 'after'}}
];

const MyClass = FSM(class {
    constructor() {
        // New states MyClass can be in are added
        this.addStates(MyStates);
    }
});

let myInstance = new MyClass();
myInstance.getState(); // -> 'init'

hasObserver

(
  • x
  • [m = 'update']
)
Boolean

Allows to test if a object is registered as a observer

Parameters:

  • x Object

    The object to check against

  • [m = 'update'] Function | String optional

    The function or method name to check

Returns:

Boolean:

True if registered, false if not

Example:

import Observable from './mixins/observable';

const logger = {
    log(message) {
        console.log(message);
    }
}

const MyClass = Observable(class {
    constructor() {
        this.addObserver(logger, 'log');
    }
});

let myInstance = new MyClass();
myInstance.hasObserver(logger, 'log');
// -> true

notifyObservers

(
  • x
  • [m = 'update']
)
This

Notifies all registered observers with data

Parameters:

  • x Object

    The object to register as observer

  • [m = 'update'] Function | String optional

    Function or method to use as handler

Returns:

This:

The observable instance

Example:

import Observable from './mixins/observable';

const logger = {
    update(message) {
        console.log(message);
    }
}

const MyClass = Observable(class {
    constructor() {
        this.addObserver(logger);
    }
});

let myInstance = new MyClass();
myInstance.notifyObservers('Sending a message');
// logs "Sending a message"  

publish

(
  • channel
  • data
)
This

Allows to publish information through a channel of the mediator

Parameters:

  • channel String

    The channel to publish information through

  • data

    The data to send through

Returns:

This:

The instance

Example:

import Mediator from './mixins/mediator';

const MyClass = Mediator(class {
    constructor() {}
});

let myInstance = new MyClass();
myInstance.publish('channelName', 'foo');

removeObserver

(
  • x
  • [m = 'update']
)
This

Removes an observer object from the observable

Parameters:

  • x Object

    The object to remove

  • [m = 'update'] Function | String optional

    Function or method used as handler

Returns:

This:

The observable instance

Example:

import Observable from './mixins/observable';

const logger = {
    update(message) {
        console.log(message);
    }
}

const MyClass = Observable(class {
    constructor() {
        this.addObserver(logger);
    }
});

let myInstance = new MyClass();
myInstance.removeObserver(logger);

removeState

(
  • state
)
This

Removes a state from the FSM, if it is not the active one. Please note that you have to take care to preserve a valid path of events to consume, or otherwise the FSM will be trapped in a single state (which may be fine if that is your intention)

Parameters:

  • state Object

    The state to remove

Returns:

This:

The instance

Example:

import FSM from './mixins/fsm';

const MyStates = [
    {name: 'init', events: {next: 'after'}},
    {name: 'after', events: {next: 'last', prev: 'init'}},
    {name: 'last', events: {prev: 'after'}}
];

const MyClass = FSM(class {
    constructor() {
        // New states MyClass can be in are added
        this.addStates(MyStates);
        this.removeState(MyStates[2]); // remove the 'last' state
    }
});

subscribe

(
  • channel
  • handler
)
String

Allows to subscribe to a channel

Parameters:

  • channel String

    The channel to subscribe to

  • handler Function

    A handler function for incoming data

Returns:

String:

A subscription identifier

Example:

import Mediator from './mixins/mediator';

const Foo = Mediator(class {
    constructor() {
        this.subscribe('BarChannel', this.handleBar, this);
    }
    handleBar(data) {
        console.log('Foo received:', data);
    }
})

const Bar = Mediator(class {
    constructor() {
        this.subscribe('FooChannel', this.handleFoo, this);
    }
    handleFoo(data) {
        console.log('Bar received:', data);
    }
});

let foo = new Foo();
let bar = new Bar();

foo.publish('FooChannel', 'Info from Foo');
// logs "Bar received: Info from Foo"

bar.publish('BarChannel', 'Info from Bar');
// logs "Foo received: Info from Bar"

unsubscribe

(
  • identifier
)
Boolean

Allows to subscribe to a channel

Parameters:

  • identifier String

    A subscription ID returned from subscribe()

Returns:

Boolean:

True on success, false on failure

Example:

import Mediator from './mixins/mediator';

const Foo = Mediator(class {
    constructor() {
        this.listens = this.subscribe('self', this.handler, this);
    }
    handler(data) {
        console.log('Foo received:', data);
    }
    unlisten() {
        return this.unsubscribe(this.listens);
    }
})

let foo = new Foo();
foo.publish('self', 1);
// logs "Foo received: 1"

foo.unlisten()

foo.publish('self', 1);
// nothing happens