2021-11-05 02:29:02 +01:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
export interface Destroyable {
|
|
|
|
destroy(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function clearableArray<T>(): (T & Destroyable)[] {
|
|
|
|
const list: (T & Destroyable)[] = [];
|
|
|
|
|
|
|
|
return new Proxy(list, {
|
2022-11-28 00:33:04 +01:00
|
|
|
get: function(target: (T & Destroyable)[], prop: string | symbol) {
|
2021-11-24 01:25:24 +01:00
|
|
|
if (!(typeof prop === "symbol") && !isNaN(Number(prop)) && !target[prop]) {
|
2021-11-05 02:29:02 +01:00
|
|
|
const item = {} as T & Destroyable;
|
|
|
|
|
|
|
|
const destroy = (): void => {
|
|
|
|
const index = list.indexOf(item);
|
|
|
|
list.splice(index, 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
target[prop] = new Proxy(item, {
|
2022-11-28 00:33:04 +01:00
|
|
|
get: function(target: T & Destroyable, prop: string | symbol) {
|
2021-11-05 02:29:02 +01:00
|
|
|
if (prop === "destroy") {
|
|
|
|
return destroy;
|
|
|
|
}
|
|
|
|
|
|
|
|
return target[prop];
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return target[prop];
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|