mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
feat(observable): Implement observable .once (#5309)
Node has a handy one-liner method on its EventEmitter called "once". It is similar to "on", but fires a single time and automatically unsubscribes.
This commit is contained in:
committed by
Alexander Vakrilov
parent
9d7f0e5315
commit
2166d1e415
@@ -95,6 +95,14 @@ export class Observable {
|
||||
*/
|
||||
on(event: "propertyChange", callback: (data: EventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* Adds one-time listener function for the event named `event`.
|
||||
* @param event Name of the event to attach to.
|
||||
* @param callback A function to be called when the specified event is raised.
|
||||
* @param thisArg An optional parameter which when set will be used as "this" in callback method call.
|
||||
*/
|
||||
once(event: string, callback: (data: EventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* Shortcut alias to the removeEventListener method.
|
||||
*/
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
interface ListenerEntry {
|
||||
callback: (data: EventData) => void;
|
||||
thisArg: any;
|
||||
once?: true;
|
||||
}
|
||||
|
||||
let _wrappedIndex = 0;
|
||||
@@ -56,6 +57,11 @@ export class Observable implements ObservableDefinition {
|
||||
this.addEventListener(eventNames, callback, thisArg);
|
||||
}
|
||||
|
||||
public once(event: string, callback: (data: EventData) => void, thisArg?: any) {
|
||||
const list = this._getEventList(event, true);
|
||||
list.push({ callback, thisArg, once: true });
|
||||
}
|
||||
|
||||
public off(eventNames: string, callback?: any, thisArg?: any) {
|
||||
this.removeEventListener(eventNames, callback, thisArg);
|
||||
}
|
||||
@@ -120,6 +126,9 @@ export class Observable implements ObservableDefinition {
|
||||
|
||||
for (let i = observers.length - 1; i >= 0; i--) {
|
||||
let entry = observers[i];
|
||||
if (entry.once) {
|
||||
observers.splice(i, 1);
|
||||
}
|
||||
if (entry.thisArg) {
|
||||
entry.callback.apply(entry.thisArg, [data]);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user