Class Observable<T>
Type parameters
-
T
Hierarchy
- Observable
Index
Constructors
constructor
-
Creates a new observable
Parameters
-
Optional onObserverAdded: (observer: Observer<T>) => void
defines a callback to call when a new observer is added
-
-
Parameters
-
observer: Observer<T>
Returns void
-
-
-
Returns Observable
-
Accessors
observers
-
Gets the list of observers
Returns Array<Observer<T>>
Methods
addSearch playground for add
-
Create a new Observer with the specified callback
Parameters
-
callback: (eventData: T, eventState: EventState) => void
the callback that will be executed for that Observer
-
-
Parameters
-
eventData: T
-
eventState: EventState
Returns void
-
-
-
-
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.
-
Optional scope: any
optional scope for the callback to be called from
-
Optional unregisterOnFirstCall: boolean
defines if the observer as to be unregistered after the next notification
Returns Nullable<Observer<T>>
the new observer created for the callback
-
add OnceSearch playground for addOnce
-
Create a new Observer with the specified callback and unregisters after the next notification
Parameters
-
callback: (eventData: T, eventState: EventState) => void
the callback that will be executed for that Observer
-
-
Parameters
-
eventData: T
-
eventState: EventState
Returns void
-
-
-
Returns Nullable<Observer<T>>
the new observer created for the callback
-
clearSearch playground for clear
-
Clear the list of observers
Returns void
cloneSearch playground for clone
-
Clone the current observable
Returns Observable<T>
a new observable
has ObserversSearch playground for hasObservers
-
Gets a boolean indicating if the observable has at least one observer
Returns boolean
true is the Observable has at least one Observer registered
has Specific MaskSearch playground for hasSpecificMask
-
Does this observable handles observer registered with a given mask
Parameters
-
Optional mask: number
defines the mask to be tested
Returns boolean
whether or not one observer registered with the given mask is handeled
-
make Observer Bottom PrioritySearch playground for makeObserverBottomPriority
-
Moves the observable to the bottom of the observer list making it get called last when notified
Parameters
-
observer: Observer<T>
the observer to move
Returns void
-
make Observer Top PrioritySearch playground for makeObserverTopPriority
-
Moves the observable to the top of the observer list making it get called first when notified
Parameters
-
observer: Observer<T>
the observer to move
Returns void
-
notify ObserverSearch playground for notifyObserver
-
Notify a specific observer
Parameters
-
observer: Observer<T>
defines the observer to notify
-
eventData: T
defines the data to be sent to each callback
-
Optional mask: number
is used to filter observers defaults to -1
Returns void
-
notify ObserversSearch playground for notifyObservers
-
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
-
eventData: T
defines the data to send to all observers
-
Optional mask: number
defines the mask of the current notification (observers with incompatible mask (ie mask & observer.mask === 0) will not be notified)
-
Optional target: any
defines the original target of the state
-
Optional currentTarget: any
defines the current target of the state
-
Optional userInfo: any
defines any user info to send to observers
Returns boolean
false if the complete observer chain was not processed (because one observer set the skipNextObservers to true)
-
notify Observers With PromiseSearch playground for notifyObserversWithPromise
-
Calling this will execute each callback, expecting it to be a promise or return a value. If at any point in the chain one function fails, the promise will fail and the execution will not continue. This is useful when a chain of events (sometimes async events) is needed to initialize a certain object and it is crucial that all callbacks will be executed. The order of the callbacks is kept, callbacks are not executed parallel.
Parameters
-
eventData: T
The data to be sent to each callback
-
Optional mask: number
is used to filter observers defaults to -1
-
Optional target: any
defines the callback target (see EventState)
-
Optional currentTarget: any
defines he current object in the bubbling phase
-
Optional userInfo: any
defines any user info to send to observers
Returns Promise<T>
will return a Promise than resolves when all callbacks executed successfully.
-
removeSearch playground for remove
remove CallbackSearch playground for removeCallback
-
Remove a callback from the Observable object
Parameters
-
callback: (eventData: T, eventState: EventState) => void
the callback to remove
-
-
Parameters
-
eventData: T
-
eventState: EventState
Returns void
-
-
-
-
Optional scope: any
optional scope. If used only the callbacks with this scope will be removed
Returns boolean
false if it doesn't belong to this 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.