1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-29 16:30:56 -05:00
denoland-deno/cli/js/web/dom_iterable.ts
Kitson Kelly fc4819e1e0
refactor: Event and EventTarget implementations (#4707)
Refactors Event and EventTarget so that they better encapsulate their
non-public data as well as are more forward compatible with things like
DOM Nodes.

Moves `dom_types.ts` -> `dom_types.d.ts` which was always the intention,
it was a legacy of when we used to build the types from the code and the
limitations of the compiler.  There was a lot of cruft in `dom_types`
which shouldn't have been there, and mis-alignment to the DOM standards.
This generally has been eliminated, though we still have some minor
differences from the DOM (like the removal of some deprecated
methods/properties).

Adds `DOMException`.  Strictly it shouldn't inherit from `Error`, but
most browsers provide a stack trace when one is thrown, so the behaviour
in Deno actually better matches the browser.

`Event` still doesn't log to console like it does in the browser.  I
 wanted to get this raised and that could be an enhancement later on (it
 currently doesn't either).
2020-04-11 11:42:02 -04:00

91 lines
2.9 KiB
TypeScript

// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { requiredArguments } from "./util.ts";
import { exposeForTest } from "../internals.ts";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
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;
}
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.
// 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.
const DomIterable = class extends Base {
*entries(): IterableIterator<[K, V]> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
for (const entry of (this as any)[dataSymbol]) {
yield entry;
}
}
*keys(): IterableIterator<K> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
for (const [key] of (this as any)[dataSymbol]) {
yield key;
}
}
*values(): IterableIterator<V> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
for (const [, value] of (this as any)[dataSymbol]) {
yield value;
}
}
forEach(
callbackfn: (value: V, key: K, parent: this) => void,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
thisArg?: any
): void {
requiredArguments(
`${this.constructor.name}.forEach`,
arguments.length,
1
);
callbackfn = callbackfn.bind(
thisArg == null ? globalThis : Object(thisArg)
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
for (const [key, value] of (this as any)[dataSymbol]) {
callbackfn(value, key, this);
}
}
*[Symbol.iterator](): IterableIterator<[K, V]> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
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,
configurable: true,
});
return DomIterable;
}
exposeForTest("DomIterableMixin", DomIterableMixin);