anki/ts/editor/destroyable.ts
Damien Elmes b2049209ff Re-enable formatting for .ts files
There are some style differences compared to prettier, and not all are
necessarily an improvement, but it's much faster now.
2022-11-28 09:33:04 +10:00

36 lines
1.1 KiB
TypeScript

// 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, {
get: function(target: (T & Destroyable)[], prop: string | symbol) {
if (!(typeof prop === "symbol") && !isNaN(Number(prop)) && !target[prop]) {
const item = {} as T & Destroyable;
const destroy = (): void => {
const index = list.indexOf(item);
list.splice(index, 1);
};
target[prop] = new Proxy(item, {
get: function(target: T & Destroyable, prop: string | symbol) {
if (prop === "destroy") {
return destroy;
}
return target[prop];
},
});
}
return target[prop];
},
});
}