CLASSES
TagsA - Z

Observable

Description

class Observable

The Observable class is a simple implementation of the Observable pattern.

There's one slight particularity though: a given Observable can notify its observer using a particular mask value, only the Observers registered with this mask value will be notified.

This enable a more fine grained execution without having to rely on multiple different Observable objects.

For instance you may have a given Observable that have four different types of notifications: Move (mask = 0x01), Stop (mask = 0x02), Turn Right (mask = 0X04), Turn Left (mask = 0X08).

A given observer can register itself with only Move and Stop (mask = 0x03), then it will only be notified when one of these two occurs and will never be for Turn Left/Right.

Constructor

new Observable()

Methods

add(callback, mask, insertFirst) → Observer<T>

Create a new Observer with the specified callback

Parameters

Name Type Description
callback (eventData: T, eventState: EventState) => void the callback that will be executed for that Observer the callback to remove. If it doesn't belong to this Observable, false will be returned.
optional mask number the mask used to filter observers
optional insertFirst boolean if true the callback will be inserted at the first position, hence executed before the others ones. If false (default behavior) the callback will be inserted at the last position, executed after all the others already present. if true the callback will be inserted at the first position, hence executed before the others ones. If false (default behavior) the callback will be inserted at the last position, executed after all the others already present.

remove(observer) → boolean

Remove an Observer from the Observable object

Parameters

Name Type Description
observer Observer<T> the instance of the Observer to remove. If it doesn't belong to this Observable, false will be returned. the instance of the Observer to remove. If it doesn't belong to this Observable, false will be returned.

removeCallback(callback) → boolean

Remove a callback from the Observable object

Parameters

Name Type Description
callback (eventData: T, eventState: EventState) => void the callback to remove. If it doesn't belong to this Observable, false will be returned. the callback to remove. If it doesn't belong to this Observable, false will be returned.

notifyObservers(eventData, mask) → boolean

Notify all Observers by calling their respective callback with the given data

Will return true if all observers were executed, false if an observer set skipNextObservers to true, then prevent the subsequent ones to execute

Parameters

Name Type Description
eventData T
optional mask number

hasObservers() → boolean

return true is the Observable has at least one Observer registered

hasSpecificMask(mask) → boolean

return true if the Observable has at least one Observer registered with this specific mask

clear() → void

Clear the list of observers

clone() → Observable<T>

Clone the current observable