2020-01-21 10:01:55 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-07-06 21:45:39 -04:00
|
|
|
|
2020-03-11 10:49:53 -04:00
|
|
|
import { requiredArguments } from "./util.ts";
|
2020-01-16 19:42:58 -05:00
|
|
|
import { exposeForTest } from "../internals.ts";
|
2018-10-23 07:43:43 -04:00
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2020-04-11 11:42:02 -04:00
|
|
|
export type Constructor<T = {}> = new (...args: any[]) => T;
|
|
|
|
|
|
|
|
export interface DomIterable<K, V> {
|
|
|
|
keys(): IterableIterator<K>;
|
|
|
|
values(): IterableIterator<V>;
|
|
|
|
entries(): IterableIterator<[K, V]>;
|
|
|
|
[Symbol.iterator](): IterableIterator<[K, V]>;
|
|
|
|
forEach(
|
|
|
|
callback: (value: V, key: K, parent: this) => void,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
thisArg?: any
|
|
|
|
): void;
|
|
|
|
}
|
2018-10-23 07:43:43 -04:00
|
|
|
|
|
|
|
export function DomIterableMixin<K, V, TBase extends Constructor>(
|
|
|
|
Base: TBase,
|
|
|
|
dataSymbol: symbol
|
|
|
|
): TBase & Constructor<DomIterable<K, V>> {
|
|
|
|
// we have to cast `this` as `any` because there is no way to describe the
|
|
|
|
// Base class in a way where the Symbol `dataSymbol` is defined. So the
|
|
|
|
// runtime code works, but we do lose a little bit of type safety.
|
|
|
|
|
2018-11-04 13:05:02 -05:00
|
|
|
// Additionally, we have to not use .keys() nor .values() since the internal
|
|
|
|
// slot differs in type - some have a Map, which yields [K, V] in
|
|
|
|
// Symbol.iterator, and some have an Array, which yields V, in this case
|
|
|
|
// [K, V] too as they are arrays of tuples.
|
|
|
|
|
2018-10-23 07:43:43 -04:00
|
|
|
const DomIterable = class extends Base {
|
|
|
|
*entries(): IterableIterator<[K, V]> {
|
2019-03-09 12:30:38 -05:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2018-11-04 13:05:02 -05:00
|
|
|
for (const entry of (this as any)[dataSymbol]) {
|
2018-10-23 07:43:43 -04:00
|
|
|
yield entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*keys(): IterableIterator<K> {
|
2019-03-09 12:30:38 -05:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2018-11-04 13:05:02 -05:00
|
|
|
for (const [key] of (this as any)[dataSymbol]) {
|
2018-10-23 07:43:43 -04:00
|
|
|
yield key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*values(): IterableIterator<V> {
|
2019-03-09 12:30:38 -05:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2018-11-04 13:05:02 -05:00
|
|
|
for (const [, value] of (this as any)[dataSymbol]) {
|
2018-10-23 07:43:43 -04:00
|
|
|
yield value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
forEach(
|
|
|
|
callbackfn: (value: V, key: K, parent: this) => void,
|
2019-03-09 12:30:38 -05:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2018-10-23 07:43:43 -04:00
|
|
|
thisArg?: any
|
|
|
|
): void {
|
2018-12-26 21:12:55 -05:00
|
|
|
requiredArguments(
|
|
|
|
`${this.constructor.name}.forEach`,
|
|
|
|
arguments.length,
|
|
|
|
1
|
|
|
|
);
|
2020-01-20 09:30:30 -05:00
|
|
|
callbackfn = callbackfn.bind(
|
|
|
|
thisArg == null ? globalThis : Object(thisArg)
|
|
|
|
);
|
2019-03-09 12:30:38 -05:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2018-11-04 13:05:02 -05:00
|
|
|
for (const [key, value] of (this as any)[dataSymbol]) {
|
2018-10-23 07:43:43 -04:00
|
|
|
callbackfn(value, key, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*[Symbol.iterator](): IterableIterator<[K, V]> {
|
2019-03-09 12:30:38 -05:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2018-10-23 07:43:43 -04:00
|
|
|
for (const entry of (this as any)[dataSymbol]) {
|
|
|
|
yield entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// we want the Base class name to be the name of the class.
|
|
|
|
Object.defineProperty(DomIterable, "name", {
|
|
|
|
value: Base.name,
|
2020-03-28 13:03:49 -04:00
|
|
|
configurable: true,
|
2018-10-23 07:43:43 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
return DomIterable;
|
|
|
|
}
|
2020-01-16 19:42:58 -05:00
|
|
|
|
|
|
|
exposeForTest("DomIterableMixin", DomIterableMixin);
|