mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
fix: simplify MutationSensitiveArray listener system
This commit is contained in:
@@ -353,16 +353,17 @@ export class DOMEvent implements Event {
|
|||||||
|
|
||||||
// Set a listener to clone the array just before any mutations.
|
// Set a listener to clone the array just before any mutations.
|
||||||
let listenersLazyCopy: ListenerEntry[] = listenersLive;
|
let listenersLazyCopy: ListenerEntry[] = listenersLive;
|
||||||
const doLazyCopy = () => {
|
listenersLive.onMutation = () => (mutation: string, payload?: unknown) => {
|
||||||
|
console.log(`handleEvent "${data.eventName}": doLazyCopy due to "${mutation}"`, payload);
|
||||||
// Cloning the array via spread syntax is up to 180 nanoseconds
|
// Cloning the array via spread syntax is up to 180 nanoseconds
|
||||||
// faster per run than using Array.prototype.slice().
|
// faster per run than using Array.prototype.slice().
|
||||||
listenersLazyCopy = [...listenersLive];
|
listenersLazyCopy = [...listenersLive];
|
||||||
|
listenersLive.onMutation = null;
|
||||||
};
|
};
|
||||||
listenersLive.once(doLazyCopy);
|
|
||||||
|
|
||||||
// Make sure we remove the listener before we exit the function,
|
// Make sure we clear the callback before we exit the function,
|
||||||
// otherwise we may wastefully clone the array.
|
// otherwise we may wastefully clone the array on future mutations.
|
||||||
const cleanup = () => listenersLive.removeListener(doLazyCopy);
|
const cleanup = () => (listenersLive.onMutation = null);
|
||||||
|
|
||||||
for (let i = listenersLazyCopy.length - 1; i >= 0; i--) {
|
for (let i = listenersLazyCopy.length - 1; i >= 0; i--) {
|
||||||
const listener = listenersLazyCopy[i];
|
const listener = listenersLazyCopy[i];
|
||||||
|
|||||||
@@ -8,32 +8,11 @@
|
|||||||
* its entire purpose is to be used for performance-sensitive tasks.
|
* its entire purpose is to be used for performance-sensitive tasks.
|
||||||
*/
|
*/
|
||||||
export class MutationSensitiveArray<T> extends Array<T> {
|
export class MutationSensitiveArray<T> extends Array<T> {
|
||||||
private readonly listeners: (() => void)[] = [];
|
onMutation: (() => void) | null = null;
|
||||||
|
|
||||||
once(listener: () => void): void {
|
|
||||||
const wrapper = () => {
|
|
||||||
listener();
|
|
||||||
this.removeListener(wrapper);
|
|
||||||
};
|
|
||||||
this.addListener(wrapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
addListener(listener: () => void): void {
|
|
||||||
if (!this.listeners.includes(listener)) {
|
|
||||||
this.listeners.push(listener);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
removeListener(listener: () => void): void {
|
|
||||||
const index = this.listeners.indexOf(listener);
|
|
||||||
if (index > -1) {
|
|
||||||
this.listeners.splice(index, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private invalidate(): void {
|
private invalidate(): void {
|
||||||
for (const listener of this.listeners) {
|
if (this.onMutation) {
|
||||||
listener();
|
this.onMutation();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user