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(onObserverAdded)

Parameters

Name Type Description
optional onObserverAdded observer Observer<T>

|

Methods

add(callback, mask, insertFirst, scope) Nullable<Observer<T>>

Create a new Observer with the specified callback

Parameters

Name Type Description
callback eventData T
eventState EventState

| the callback that will be executed for that Observer 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.

remove(observer) boolean

Remove an Observer from the Observable object

Parameters

Name Type Description
observer Nullable<Observer<T>> 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

notifyObservers(eventData, mask, target, currentTarget) 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
optional target any

notifyObserver(observer, eventData, mask) void

Notify a specific observer

Parameters

Name Type Description
observer Observer<T>
eventData T
optional mask number

hasObservers() boolean

return true is the Observable has at least one Observer registered

clear() void

Clear the list of observers

clone() Observable<T>

Clone the current observable

hasSpecificMask(mask) boolean

Does this observable handles observer registered with a given mask

@return {boolean} whether or not one observer registered with the given mask is handeled

Parameters

Name Type Description
optional mask number