1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 07:44:48 -05:00
denoland-deno/js/custom_event.ts

65 lines
1.9 KiB
TypeScript
Raw Normal View History

2019-01-23 07:20:53 -05:00
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import * as domTypes from "./dom_types";
import * as event from "./event";
import { getPrivateValue, requiredArguments } from "./util";
2019-01-23 07:20:53 -05:00
// WeakMaps are recommended for private attributes (see MDN link below)
// https://developer.mozilla.org/en-US/docs/Archive/Add-ons/Add-on_SDK/Guides/Contributor_s_Guide/Private_Properties#Using_WeakMaps
export const customEventAttributes = new WeakMap();
export class CustomEventInit extends event.EventInit
implements domTypes.CustomEventInit {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2019-01-23 07:20:53 -05:00
detail: any;
constructor({
bubbles = false,
cancelable = false,
composed = false,
detail = null
}: domTypes.CustomEventInit) {
super({ bubbles, cancelable, composed });
this.detail = detail;
}
}
export class CustomEvent extends event.Event implements domTypes.CustomEvent {
constructor(
type: string,
customEventInitDict: domTypes.CustomEventInit = {}
) {
requiredArguments("CustomEvent", arguments.length, 1);
2019-01-23 07:20:53 -05:00
super(type, customEventInitDict);
const { detail = null } = customEventInitDict;
customEventAttributes.set(this, { detail });
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2019-01-23 07:20:53 -05:00
get detail(): any {
return getPrivateValue(this, customEventAttributes, "detail");
}
initCustomEvent(
type: string,
bubbles?: boolean,
cancelable?: boolean,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2019-01-23 07:20:53 -05:00
detail?: any
): void {
2019-01-23 07:20:53 -05:00
if (this.dispatched) {
return;
}
customEventAttributes.set(this, { detail });
}
get [Symbol.toStringTag](): string {
return "CustomEvent";
}
2019-01-23 07:20:53 -05:00
}
/** Built-in objects providing `get` methods for our
* interceptable JavaScript operations.
*/
Reflect.defineProperty(CustomEvent.prototype, "detail", { enumerable: true });