diff options
author | Adam Conrad <422184+acconrad@users.noreply.github.com> | 2019-01-23 12:20:53 +0000 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-01-23 06:20:53 -0600 |
commit | e470f31d328884303e56f2f15a4121b8ffe097a6 (patch) | |
tree | 84e878d3c53db4f969c29357c7f5985b59c7d9f2 /js/custom_event.ts | |
parent | 77114fbda49382e397095d8214bd76996b0cfb57 (diff) |
Web API: CustomEvent (#1505)
Diffstat (limited to 'js/custom_event.ts')
-rw-r--r-- | js/custom_event.ts | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/js/custom_event.ts b/js/custom_event.ts new file mode 100644 index 000000000..dd1c33d58 --- /dev/null +++ b/js/custom_event.ts @@ -0,0 +1,60 @@ +// Copyright 2018 the Deno authors. All rights reserved. MIT license. +import * as domTypes from "./dom_types"; +import * as event from "./event"; +import { getPrivateValue } from "./util"; + +// WeakMaps are recommended for private attributes (see MDN link below) +// tslint:disable-next-line:max-line-length +// 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 { + // tslint:disable-next-line:no-any + 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 = {} + ) { + super(type, customEventInitDict); + const { detail = null } = customEventInitDict; + customEventAttributes.set(this, { detail }); + } + + // tslint:disable-next-line:no-any + get detail(): any { + return getPrivateValue(this, customEventAttributes, "detail"); + } + + initCustomEvent( + type: string, + bubbles?: boolean, + cancelable?: boolean, + // tslint:disable-next-line:no-any + detail?: any + ) { + if (this.dispatched) { + return; + } + + customEventAttributes.set(this, { detail }); + } +} + +/** Built-in objects providing `get` methods for our + * interceptable JavaScript operations. + */ +Reflect.defineProperty(CustomEvent.prototype, "detail", { enumerable: true }); |