@whatwg-node/events 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @whatwg-node/events
2
+
3
+ ## 0.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [`9502102`](https://github.com/ardatan/whatwg-node/commit/9502102b265945b37ee38b276ec1533fae0f308f) Thanks [@ardatan](https://github.com/ardatan)! - New Event API ponyfill
@@ -0,0 +1,3 @@
1
+ export const Event = globalThis.Event;
2
+ export const EventTarget = globalThis.EventTarget;
3
+ export const CustomEvent = globalThis.CustomEvent;
@@ -0,0 +1,3 @@
1
+ module.exports.Event = globalThis.Event;
2
+ module.exports.EventTarget = globalThis.EventTarget;
3
+ module.exports.CustomEvent = globalThis.CustomEvent;
@@ -0,0 +1,11 @@
1
+ /// <reference lib="dom" />
2
+
3
+ declare const _Event: typeof Event;
4
+ declare const _EventTarget: typeof EventTarget;
5
+ declare const _CustomEvent: typeof CustomEvent;
6
+
7
+ declare module "@whatwg-node/events" {
8
+ export const Event: typeof _Event;
9
+ export const EventTarget: typeof _EventTarget;
10
+ export const CustomEvent: typeof _CustomEvent;
11
+ }
@@ -0,0 +1,101 @@
1
+ module.exports.Event = globalThis.Event;
2
+ if (!module.exports.Event) {
3
+ module.exports.Event = class Event {
4
+ constructor(type, options) {
5
+ this.bubbles = !!options && !!options.bubbles;
6
+ this.cancelable = !!options && !!options.cancelable;
7
+ this.composed = !!options && !!options.composed;
8
+ this.type = type;
9
+ }
10
+ }
11
+ }
12
+
13
+ module.exports.EventTarget = globalThis.EventTarget;
14
+ if (!module.exports.EventTarget) {
15
+ module.exports.EventTarget = class EventTarget {
16
+ constructor() {
17
+ this.__listeners = new Map();
18
+ }
19
+ addEventListener(type, listener, options) { if (arguments.length < 2) {
20
+ throw new TypeError(
21
+ `TypeError: Failed to execute 'addEventListener' on 'EventTarget': 2 arguments required, but only ${arguments.length} present.`
22
+ );
23
+ }
24
+ const __listeners = this.__listeners;
25
+ const actualType = type.toString();
26
+ if (!__listeners.has(actualType)) {
27
+ __listeners.set(actualType, new Map());
28
+ }
29
+ const listenersForType = __listeners.get(actualType);
30
+ if (!listenersForType.has(listener)) {
31
+ // Any given listener is only registered once
32
+ listenersForType.set(listener, options);
33
+ }
34
+ }
35
+ removeEventListener(type, listener, _options) {
36
+ if (arguments.length < 2) {
37
+ throw new TypeError(
38
+ `TypeError: Failed to execute 'addEventListener' on 'EventTarget': 2 arguments required, but only ${arguments.length} present.`
39
+ );
40
+ }
41
+ const __listeners = this.__listeners;
42
+ const actualType = type.toString();
43
+ if (__listeners.has(actualType)) {
44
+ const listenersForType = __listeners.get(actualType);
45
+ if (listenersForType.has(listener)) {
46
+ listenersForType.delete(listener);
47
+ }
48
+ }
49
+ }
50
+ dispatchEvent(event) {
51
+ if (!(event instanceof Event)) {
52
+ throw new TypeError(
53
+ `Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'.`
54
+ );
55
+ }
56
+ const type = event.type;
57
+ const __listeners = this.__listeners;
58
+ const listenersForType = __listeners.get(type);
59
+ if (listenersForType) {
60
+ for (const [listener, options] of listenersForType.entries()) {
61
+ try {
62
+ if (typeof listener === "function") {
63
+ // Listener functions must be executed with the EventTarget as the `this` context.
64
+ listener.call(this, event);
65
+ } else if (listener && typeof listener.handleEvent === "function") {
66
+ // Listener objects have their handleEvent method called, if they have one
67
+ listener.handleEvent(event);
68
+ }
69
+ } catch (err) {
70
+ // We need to report the error to the global error handling event,
71
+ // but we do not want to break the loop that is executing the events.
72
+ // Unfortunately, this is the best we can do, which isn't great, because the
73
+ // native EventTarget will actually do this synchronously before moving to the next
74
+ // event in the loop.
75
+ setTimeout(() => {
76
+ throw err;
77
+ });
78
+ }
79
+ if (options && options.once) {
80
+ // If this was registered with { once: true }, we need
81
+ // to remove it now.
82
+ listenersForType.delete(listener);
83
+ }
84
+ }
85
+ }
86
+ // Since there are no cancellable events on a base EventTarget,
87
+ // this should always return true.
88
+ return true;
89
+ }
90
+ }
91
+ }
92
+
93
+ module.exports.CustomEvent = globalThis.CustomEvent;
94
+ if (!module.exports.CustomEvent) {
95
+ module.exports.CustomEvent = class CustomEvent extends module.exports.Event {
96
+ constructor(type, options) {
97
+ super(type, options);
98
+ this.detail = options && options.detail;
99
+ }
100
+ }
101
+ }
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@whatwg-node/events",
3
+ "version": "0.0.1",
4
+ "description": "Cross Platform Smart Event API Ponyfill",
5
+ "author": "Arda TANRIKULU <ardatanrikulu@gmail.com>",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "ardatan/whatwg-node",
9
+ "directory": "packages/events"
10
+ },
11
+ "license": "MIT",
12
+ "sideEffects": false,
13
+ "main": "dist/node-ponyfill.js",
14
+ "browser": "dist/global-ponyfill.js",
15
+ "react-native": "dist/global-ponyfill.js",
16
+ "types": "dist/index.d.ts",
17
+ "denoify": {
18
+ "index": "dist/deno-ponyfill.ts"
19
+ },
20
+ "publishConfig": {
21
+ "access": "public"
22
+ }
23
+ }