@whatwg-node/events 0.0.2 → 0.0.3-rc-20230419123748-0bceaec
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 +13 -2
- package/README.md +49 -0
- package/dist/index.d.ts +3 -7
- package/dist/node-ponyfill.js +23 -22
- package/package.json +8 -7
- package/tests/events.spec.ts +25 -25
package/CHANGELOG.md
CHANGED
|
@@ -1,13 +1,24 @@
|
|
|
1
1
|
# @whatwg-node/events
|
|
2
2
|
|
|
3
|
+
## 0.0.3-rc-20230419123748-0bceaec
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#427](https://github.com/ardatan/whatwg-node/pull/427)
|
|
8
|
+
[`e8bda7c`](https://github.com/ardatan/whatwg-node/commit/e8bda7cdf440a7f4bb617ee1b5df8ee1becb4ad6)
|
|
9
|
+
Thanks [@Rugvip](https://github.com/Rugvip)! - Restructure type declarations to avoid polluting
|
|
10
|
+
global namespace.
|
|
11
|
+
|
|
3
12
|
## 0.0.2
|
|
4
13
|
|
|
5
14
|
### Patch Changes
|
|
6
15
|
|
|
7
|
-
- [`c0d5c43`](https://github.com/ardatan/whatwg-node/commit/c0d5c43a1c4d3d9fcdf542472fabdebd5118fe23)
|
|
16
|
+
- [`c0d5c43`](https://github.com/ardatan/whatwg-node/commit/c0d5c43a1c4d3d9fcdf542472fabdebd5118fe23)
|
|
17
|
+
Thanks [@ardatan](https://github.com/ardatan)! - Fix dispatchEvent on Node 14
|
|
8
18
|
|
|
9
19
|
## 0.0.1
|
|
10
20
|
|
|
11
21
|
### Patch Changes
|
|
12
22
|
|
|
13
|
-
- [`9502102`](https://github.com/ardatan/whatwg-node/commit/9502102b265945b37ee38b276ec1533fae0f308f)
|
|
23
|
+
- [`9502102`](https://github.com/ardatan/whatwg-node/commit/9502102b265945b37ee38b276ec1533fae0f308f)
|
|
24
|
+
Thanks [@ardatan](https://github.com/ardatan)! - New Event API ponyfill
|
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# `@whatwg-node/events`
|
|
2
|
+
|
|
3
|
+
A ponyfill package for JavaScript [DOM Events Standard](https://dom.spec.whatwg.org/#events). If
|
|
4
|
+
your JavaScript environment doesn't implement this standard natively, this package automatically
|
|
5
|
+
ponyfills the missing parts, and export them as a module.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
yarn add @whatwg-node/events
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { Event, EventTarget } from '@whatwg-node/events'
|
|
17
|
+
|
|
18
|
+
const target = new EventTarget()
|
|
19
|
+
target.addEventListener('foo', (event: Event) => {
|
|
20
|
+
console.log(event.type) // foo
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
target.dispatchEvent(new Event('foo'))
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
> If your environment already implements these natively, this package will export the native ones
|
|
27
|
+
> automatically.
|
|
28
|
+
|
|
29
|
+
## Custom Events
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { CustomEvent, EventTarget } from '@whatwg-node/events'
|
|
33
|
+
|
|
34
|
+
const target = new EventTarget()
|
|
35
|
+
target.addEventListener('foo', (event: CustomEvent) => {
|
|
36
|
+
console.assert(event.detail.foo, 'bar')
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
// `detail` can take any value
|
|
40
|
+
target.dispatchEvent(new CustomEvent('foo', { detail: { foo: 'bar' } }))
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## API
|
|
44
|
+
|
|
45
|
+
The following classes are exported by this package:
|
|
46
|
+
|
|
47
|
+
- [Event](https://developer.mozilla.org/en-US/docs/Web/API/Event)
|
|
48
|
+
- [CustomEvent](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent)
|
|
49
|
+
- [EventTarget](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
/// <reference lib="dom" />
|
|
2
2
|
|
|
3
|
-
declare const _Event: typeof Event;
|
|
4
|
-
declare const _EventTarget: typeof EventTarget;
|
|
5
|
-
declare const _CustomEvent: typeof CustomEvent;
|
|
6
|
-
|
|
7
3
|
declare module "@whatwg-node/events" {
|
|
8
|
-
export const Event: typeof
|
|
9
|
-
export const EventTarget: typeof
|
|
10
|
-
export const CustomEvent: typeof
|
|
4
|
+
export const Event: typeof globalThis.Event;
|
|
5
|
+
export const EventTarget: typeof globalThis.EventTarget;
|
|
6
|
+
export const CustomEvent: typeof globalThis.CustomEvent;
|
|
11
7
|
}
|
package/dist/node-ponyfill.js
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
module.exports.Event = globalThis.Event;
|
|
2
2
|
if (!module.exports.Event) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
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;
|
|
10
9
|
}
|
|
10
|
+
}
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
module.exports.EventTarget = globalThis.EventTarget;
|
|
14
14
|
if (!module.exports.EventTarget) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
module.exports.EventTarget = class EventTarget {
|
|
16
|
+
constructor() {
|
|
17
|
+
this.__listeners = new Map();
|
|
18
|
+
}
|
|
19
|
+
addEventListener(type, listener, options) {
|
|
20
|
+
if (arguments.length < 2) {
|
|
20
21
|
throw new TypeError(
|
|
21
22
|
`TypeError: Failed to execute 'addEventListener' on 'EventTarget': 2 arguments required, but only ${arguments.length} present.`
|
|
22
23
|
);
|
|
@@ -31,8 +32,8 @@ if (!module.exports.EventTarget) {
|
|
|
31
32
|
// Any given listener is only registered once
|
|
32
33
|
listenersForType.set(listener, options);
|
|
33
34
|
}
|
|
34
|
-
|
|
35
|
-
|
|
35
|
+
}
|
|
36
|
+
removeEventListener(type, listener, _options) {
|
|
36
37
|
if (arguments.length < 2) {
|
|
37
38
|
throw new TypeError(
|
|
38
39
|
`TypeError: Failed to execute 'addEventListener' on 'EventTarget': 2 arguments required, but only ${arguments.length} present.`
|
|
@@ -46,8 +47,8 @@ if (!module.exports.EventTarget) {
|
|
|
46
47
|
listenersForType.delete(listener);
|
|
47
48
|
}
|
|
48
49
|
}
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
}
|
|
51
|
+
dispatchEvent(event) {
|
|
51
52
|
if (!(event instanceof module.exports.Event)) {
|
|
52
53
|
throw new TypeError(
|
|
53
54
|
`Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'.`
|
|
@@ -86,16 +87,16 @@ if (!module.exports.EventTarget) {
|
|
|
86
87
|
// Since there are no cancellable events on a base EventTarget,
|
|
87
88
|
// this should always return true.
|
|
88
89
|
return true;
|
|
89
|
-
}
|
|
90
90
|
}
|
|
91
|
+
}
|
|
91
92
|
}
|
|
92
93
|
|
|
93
94
|
module.exports.CustomEvent = globalThis.CustomEvent;
|
|
94
95
|
if (!module.exports.CustomEvent) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
96
|
+
module.exports.CustomEvent = class CustomEvent extends module.exports.Event {
|
|
97
|
+
constructor(type, options) {
|
|
98
|
+
super(type, options);
|
|
99
|
+
this.detail = options && options.detail;
|
|
100
100
|
}
|
|
101
|
+
}
|
|
101
102
|
}
|
package/package.json
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@whatwg-node/events",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3-rc-20230419123748-0bceaec",
|
|
4
4
|
"description": "Cross Platform Smart Event API Ponyfill",
|
|
5
|
-
"author": "Arda TANRIKULU <ardatanrikulu@gmail.com>",
|
|
6
5
|
"repository": {
|
|
7
6
|
"type": "git",
|
|
8
7
|
"url": "ardatan/whatwg-node",
|
|
9
8
|
"directory": "packages/events"
|
|
10
9
|
},
|
|
10
|
+
"author": "Arda TANRIKULU <ardatanrikulu@gmail.com>",
|
|
11
11
|
"license": "MIT",
|
|
12
|
-
"sideEffects": false,
|
|
13
12
|
"main": "dist/node-ponyfill.js",
|
|
14
13
|
"browser": "dist/global-ponyfill.js",
|
|
15
|
-
"react-native": "dist/global-ponyfill.js",
|
|
16
14
|
"types": "dist/index.d.ts",
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"bob": false,
|
|
17
20
|
"denoify": {
|
|
18
21
|
"index": "dist/deno-ponyfill.ts"
|
|
19
22
|
},
|
|
20
|
-
"
|
|
21
|
-
"access": "public"
|
|
22
|
-
}
|
|
23
|
+
"react-native": "dist/global-ponyfill.js"
|
|
23
24
|
}
|
package/tests/events.spec.ts
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CustomEvent, Event, EventTarget } from '@whatwg-node/events';
|
|
2
2
|
|
|
3
|
-
describe(
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
3
|
+
describe('EventTarget', () => {
|
|
4
|
+
it('addEventListener + dispatchEvent', () => {
|
|
5
|
+
const target = new EventTarget();
|
|
6
|
+
const listener = jest.fn();
|
|
7
|
+
target.addEventListener('test', listener);
|
|
8
|
+
target.dispatchEvent(new Event('test'));
|
|
9
|
+
expect(listener).toHaveBeenCalled();
|
|
10
|
+
});
|
|
11
|
+
it('removeEventListener', () => {
|
|
12
|
+
const target = new EventTarget();
|
|
13
|
+
const listener = jest.fn();
|
|
14
|
+
target.addEventListener('test', listener);
|
|
15
|
+
target.removeEventListener('test', listener);
|
|
16
|
+
target.dispatchEvent(new Event('test'));
|
|
17
|
+
expect(listener).not.toHaveBeenCalled();
|
|
18
|
+
});
|
|
19
19
|
});
|
|
20
20
|
|
|
21
21
|
describe('CustomEvent', () => {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
})
|
|
22
|
+
it('detail', () => {
|
|
23
|
+
const target = new EventTarget();
|
|
24
|
+
const listener = jest.fn();
|
|
25
|
+
target.addEventListener('test', listener);
|
|
26
|
+
target.dispatchEvent(new CustomEvent('test', { detail: 123 }));
|
|
27
|
+
expect(listener).toHaveBeenCalledWith(expect.objectContaining({ detail: 123 }));
|
|
28
|
+
});
|
|
29
|
+
});
|