Class Observable<T>
Type Parameters
- T
Implements
Constructors
constructor
-
new Observable<T>(
onObserverAdded?: (observer: Observer<T>) => void,
notifyIfTriggered?: boolean,
): Observable<T>
Properties
Optional_ coroutine Scheduler
Internal observable-based coroutine scheduler instance.
Optional_ coroutine Scheduler Dispose
Internal disposal method for observable-based coroutine scheduler instance.
notify If Triggered
[false] If set to true the observable will notify when an observer was added if the observable was already triggered. This is helpful to single-state observables like the scene onReady or the dispose observable.
Methods
add
-
add(
callback?: null,
mask?: number,
insertFirst?: boolean,
scope?: any,
unregisterOnFirstCall?: boolean,
): nullCreate a new Observer with the specified callback
Parameters
Optionalcallback: nullthe callback that will be executed for that Observer
Optionalmask: numberthe mask used to filter observers
OptionalinsertFirst: booleanif 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.
Optionalscope: anyoptional scope for the callback to be called from
OptionalunregisterOnFirstCall: booleandefines if the observer as to be unregistered after the next notification
Returns null
the new observer created for the callback
-
add(
callback: (eventData: T, eventState: EventState) => void,
mask?: number,
insertFirst?: boolean,
scope?: any,
unregisterOnFirstCall?: boolean,
): Observer<T>Create a new Observer with the specified callback
Parameters
- callback: (eventData: T, eventState: EventState) => void
the callback that will be executed for that Observer
Optionalmask: numberthe mask used to filter observers
OptionalinsertFirst: booleanif 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.
Optionalscope: anyoptional scope for the callback to be called from
OptionalunregisterOnFirstCall: booleandefines if the observer as to be unregistered after the next notification
Returns Observer<T>
the new observer created for the callback
- callback: (eventData: T, eventState: EventState) => void
-
add(
callback?: ((eventData: T, eventState: EventState) => void) | null,
mask?: number,
insertFirst?: boolean,
scope?: any,
unregisterOnFirstCall?: boolean,
): Nullable<Observer<T>>Create a new Observer with the specified callback
Parameters
Optionalcallback: ((eventData: T, eventState: EventState) => void) | nullthe callback that will be executed for that Observer
Optionalmask: numberthe mask used to filter observers
OptionalinsertFirst: booleanif 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.
Optionalscope: anyoptional scope for the callback to be called from
OptionalunregisterOnFirstCall: booleandefines if the observer as to be unregistered after the next notification
Returns Nullable<Observer<T>>
the new observer created for the callback
add Once
-
Create a new Observer with the specified callback and unregisters after the next notification
Parameters
Optionalcallback: nullthe callback that will be executed for that Observer
Returns null
the new observer created for the callback
-
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
Returns Observer<T>
the new observer created for the callback
- callback: (eventData: T, eventState: EventState) => void
-
addOnce(
callback?: ((eventData: T, eventState: EventState) => void) | null,
): Nullable<Observer<T>>Create a new Observer with the specified callback and unregisters after the next notification
Parameters
Optionalcallback: ((eventData: T, eventState: EventState) => void) | nullthe callback that will be executed for that Observer
Returns Nullable<Observer<T>>
the new observer created for the callback
cancel All Coroutines
-
Cancels all coroutines currently running on this observable
Returns void
clean Last Notified State
-
Clean the last notified state - both the internal last value and the has-notified flag
Returns void
clear
-
Clear the list of observers
Returns void
clone
has Observers
-
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 Mask
-
Does this observable handles observer registered with a given mask
Parameters
- mask: number = -1
defines the mask to be tested
Returns boolean
whether or not one observer registered with the given mask is handled
- mask: number = -1
make Observer Bottom Priority
make Observer Top Priority
notify Observer
notify Observers
-
notifyObservers(
eventData: T,
mask?: number,
target?: any,
currentTarget?: any,
userInfo?: any,
): booleanNotify 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
- mask: number = -1
defines the mask of the current notification (observers with incompatible mask (ie mask & observer.mask === 0) will not be notified)
Optionaltarget: anydefines the original target of the state
OptionalcurrentTarget: anydefines the current target of the state
OptionaluserInfo: anydefines 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)
- eventData: T
notify Observers With Promise
-
notifyObserversWithPromise(
eventData: T,
mask?: number,
target?: any,
currentTarget?: any,
userInfo?: any,
): Promise<T>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
Optionalmask: numberis used to filter observers defaults to -1
Optionaltarget: anydefines the callback target (see EventState)
OptionalcurrentTarget: anydefines he current object in the bubbling phase
OptionaluserInfo: anydefines any user info to send to observers
Returns Promise<T>
will return a Promise than resolves when all callbacks executed successfully.
- eventData: T
remove
remove Callback
-
Remove a callback from the Observable object
Parameters
- callback: (eventData: T, eventState: EventState) => void
the callback to remove
Optionalscope: anyoptional scope. If used only the callbacks with this scope will be removed
Returns boolean
false if it doesn't belong to this Observable
- callback: (eventData: T, eventState: EventState) => void
run Coroutine Async
-
Runs a coroutine asynchronously on this observable
Parameters
- coroutine: AsyncCoroutine<void>
the iterator resulting from having started the coroutine
Returns Promise<void>
a promise which will be resolved when the coroutine finishes or rejected if the coroutine is cancelled
- coroutine: AsyncCoroutine<void>
StaticFrom Promise
-
Create an observable from a Promise.
Type Parameters
- T
- E = Error
Parameters
- promise: Promise<T>
a promise to observe for fulfillment.
OptionalonErrorObservable: Observable<E>an observable to notify if a promise was rejected.
Returns Observable<T>
the new 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.