Skip to content

Commit d3c66ef

Browse files
committed
feat: introduce AbstractEvent and upgrade to node 20.x
1 parent 1c8f31e commit d3c66ef

File tree

7 files changed

+62
-9
lines changed

7 files changed

+62
-9
lines changed

.eslintrc

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"message": "Use `globalThis` instead"
4040
}
4141
],
42+
"prefer-rest-params": 0,
4243
"require-yield": 0,
4344
"eqeqeq": ["error", "smart"],
4445
"spaced-comment": [

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,8 @@
4747
"tsconfig-paths": "^3.9.0",
4848
"typedoc": "^0.23.21",
4949
"typescript": "^4.9.3"
50+
},
51+
"engines": {
52+
"node": ">=19.0.0"
5053
}
5154
}

pkgs.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import (
2-
let rev = "f294325aed382b66c7a188482101b0f336d1d7db"; in
2+
let rev = "ea5234e7073d5f44728c499192544a84244bf35a"; in
33
builtins.fetchTarball "https://github.com/NixOS/nixpkgs/archive/${rev}.tar.gz"
44
)

shell.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
with pkgs;
44
mkShell {
55
nativeBuildInputs = [
6-
nodejs
6+
nodejs_20
77
shellcheck
88
gitAndTools.gh
99
];

src/AbstractEvent.ts

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* By default the `detail` property of `CustomEvent` is `null`.
3+
* So the default type has to be `null` too.
4+
*/
5+
class AbstractEvent<T = null> extends CustomEvent<T> {
6+
protected constructorParams: IArguments;
7+
8+
public constructor(
9+
type?: string,
10+
options?: CustomEventInit<T>,
11+
constructorParams?: IArguments,
12+
);
13+
public constructor(
14+
options: CustomEventInit<T>,
15+
constructorParams?: IArguments,
16+
);
17+
public constructor(
18+
type: string | CustomEventInit<T> = new.target.name,
19+
options?: CustomEventInit<T> | IArguments,
20+
constructorParams?: IArguments,
21+
) {
22+
if (typeof type === 'string') {
23+
super(type, options as CustomEventInit<T> | undefined);
24+
} else {
25+
super(new.target.name, type);
26+
constructorParams = options as IArguments | undefined;
27+
}
28+
this.constructorParams = constructorParams ?? arguments;
29+
}
30+
31+
/**
32+
* Events cannot be re-dispatched. This was probably to prevent infinite
33+
* loops. So instead of re-dispatching the same instance, we re-dispatch
34+
* a clone of the instance.
35+
*/
36+
public clone(): this {
37+
try {
38+
return new (this.constructor as any)(...this.constructorParams);
39+
} catch (e) {
40+
if (e instanceof TypeError) {
41+
throw new TypeError(
42+
`Cloning ${this.constructor.name} requires the original constructor arguments to be passed into super`,
43+
);
44+
}
45+
throw e;
46+
}
47+
}
48+
}
49+
50+
export default AbstractEvent;

src/EventAny.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
// @ts-ignore package.json is outside rootDir
1+
import AbstractEvent from './AbstractEvent';
2+
// @ts-ignore package.json is outside root dir
23
import { name } from '../package.json';
34

4-
class EventAny extends Event {
5+
class EventAny extends AbstractEvent<Event> {
56
public static type = `${name}/${this.name}`;
6-
public detail: Event;
7-
constructor(options: EventInit & { detail: Event }) {
8-
super(EventAny.type, options);
9-
this.detail = options.detail;
7+
public constructor(options: EventInit & { detail: Event }) {
8+
super(EventAny.type, options, arguments);
109
}
1110
}
1211

src/Evented.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ interface Evented {
1111
callback: EventListenerOrEventListenerObject | null,
1212
options?: AddEventListenerOptions | boolean,
1313
): void;
14-
dispatchEvent(event: Event): boolean;
1514
removeEventListener(
1615
callback: EventListenerOrEventListenerObject | null,
1716
options?: EventListenerOptions | boolean,
@@ -21,6 +20,7 @@ interface Evented {
2120
callback: EventListenerOrEventListenerObject | null,
2221
options?: EventListenerOptions | boolean,
2322
): void;
23+
dispatchEvent(event: Event): boolean;
2424
}
2525

2626
function Evented() {

0 commit comments

Comments
 (0)