Modules._BaseModule Class
A basic module to build other modules upon
Constructor
Modules._BaseModule
()
Item Index
Methods
addObserver
-
x -
[m = 'update']
Adds a observer object to the observable
Parameters:
-
xObjectThe object to register as observer
-
[m = 'update']Function | String optionalFunction or method to use as handler
Returns:
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
Allows to add a new state (see example for more info)
Parameters:
-
stateObjectThe new state
Returns:
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
Allows to add an array of new states all at once
Parameters:
-
statesArrayThe new states
Returns:
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
Transitions from the current state to the state specified by the given name of event to consume.
Parameters:
-
eventNameStringName of the event to consume
Returns:
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:
Nothing
destroyObservers
()
Undefined
Destroys the observable, unregisters all observers
Returns:
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:
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:
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']
Allows to test if a object is registered as a observer
Parameters:
-
xObjectThe object to check against
-
[m = 'update']Function | String optionalThe function or method name to check
Returns:
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']
Notifies all registered observers with data
Parameters:
-
xObjectThe object to register as observer
-
[m = 'update']Function | String optionalFunction or method to use as handler
Returns:
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
Allows to publish information through a channel of the mediator
Parameters:
-
channelStringThe channel to publish information through
-
dataThe data to send through
Returns:
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']
Removes an observer object from the observable
Parameters:
-
xObjectThe object to remove
-
[m = 'update']Function | String optionalFunction or method used as handler
Returns:
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
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:
-
stateObjectThe state to remove
Returns:
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
Allows to subscribe to a channel
Parameters:
-
channelStringThe channel to subscribe to
-
handlerFunctionA handler function for incoming data
Returns:
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
Allows to subscribe to a channel
Parameters:
-
identifierStringA subscription ID returned from subscribe()
Returns:
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
