@atomic-reactor/event-handle 1.0.0 → 1.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/README.md +17 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +21 -2
- package/src/EventHandler.ts +13 -13
- package/src/index.ts +2 -2
package/README.md
CHANGED
|
@@ -17,17 +17,17 @@ npm install --save @atomic-reactor/EventHandle
|
|
|
17
17
|
## Usage
|
|
18
18
|
|
|
19
19
|
```ts
|
|
20
|
-
import { EventTarget, Event } from
|
|
20
|
+
import { EventTarget, Event } from '@atomic-reactor/event-handle';
|
|
21
21
|
|
|
22
22
|
class DatePicker extends EventTarget {
|
|
23
23
|
setDate(value: Date) {
|
|
24
|
-
this.dispatchEvent(new Event(
|
|
24
|
+
this.dispatchEvent(new Event('date-changed', { value }));
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
const picker = new DatePicker();
|
|
29
29
|
|
|
30
|
-
picker.addEventListener(
|
|
30
|
+
picker.addEventListener('date-changed', (event) => {
|
|
31
31
|
console.log(event.type, event.value); // "date-changed", Date
|
|
32
32
|
});
|
|
33
33
|
|
|
@@ -43,22 +43,22 @@ Calling `event.stopPropagation()` in a listener stops the remaining listeners
|
|
|
43
43
|
for that dispatch from being called.
|
|
44
44
|
|
|
45
45
|
```ts
|
|
46
|
-
picker.addEventListener(
|
|
46
|
+
picker.addEventListener('date-changed', (event) => {
|
|
47
47
|
if (!event.value) event.stopPropagation();
|
|
48
48
|
});
|
|
49
49
|
```
|
|
50
50
|
|
|
51
51
|
### What a listener receives
|
|
52
52
|
|
|
53
|
-
| Property
|
|
54
|
-
|
|
|
55
|
-
| `type`
|
|
56
|
-
| `target`
|
|
57
|
-
| `timeStamp`
|
|
58
|
-
| `currentTarget`
|
|
59
|
-
| `stopPropagation()`
|
|
60
|
-
| `isPropagationStopped` | Whether propagation has been stopped
|
|
61
|
-
| ...detail
|
|
53
|
+
| Property | Description |
|
|
54
|
+
| ---------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
|
|
55
|
+
| `type` | The event type |
|
|
56
|
+
| `target` | The `EventTarget` that dispatched it |
|
|
57
|
+
| `timeStamp` | `Date.now()` when the `Event` was created |
|
|
58
|
+
| `currentTarget` | The name of the function that called `dispatchEvent`, for debugging (mangled in minified builds; `"anonymous"` when unavailable) |
|
|
59
|
+
| `stopPropagation()` | Stops later listeners in this dispatch |
|
|
60
|
+
| `isPropagationStopped` | Whether propagation has been stopped |
|
|
61
|
+
| ...detail | Every key of the detail object |
|
|
62
62
|
|
|
63
63
|
Detail keys that clash with the fields above are ignored.
|
|
64
64
|
|
|
@@ -69,7 +69,10 @@ globals. Importing them shadows the globals in that module only; if you need
|
|
|
69
69
|
both, rename on import:
|
|
70
70
|
|
|
71
71
|
```ts
|
|
72
|
-
import {
|
|
72
|
+
import {
|
|
73
|
+
EventTarget as Emitter,
|
|
74
|
+
Event as AppEvent,
|
|
75
|
+
} from '@atomic-reactor/event-handle';
|
|
73
76
|
```
|
|
74
77
|
|
|
75
78
|
## Compatibility
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/EventHandler.ts"],"sourcesContent":["export { EventTarget, Event } from
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/EventHandler.ts"],"sourcesContent":["export { EventTarget, Event } from './EventHandler';\nexport type { AppEvent, Listener } from './EventHandler';\n","// A small event system for plain objects. Unlike the DOM's\n// EventTarget, listeners get a copy of the event with its detail spread onto it.\n//\n// Uses only ES2019 built-ins (no DOM or Node APIs), so it runs the same in\n// browsers, React Native (Hermes/JSC) and Node.\n\n// What a listener receives: the event's type and its detail's keys\n// (e.g. { type: 'date-changed', value })\nexport interface AppEvent {\n type: string;\n target?: unknown;\n timeStamp?: number;\n currentTarget?: string | null;\n // Stops the remaining listeners for this dispatch from being called\n stopPropagation?: () => void;\n readonly isPropagationStopped?: boolean;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [key: string]: any;\n}\n\nexport type Listener = (event: AppEvent) => void;\n\n// V8/Hermes frames look like \" at name (location)\"; anonymous ones are\n// \" at location\". JSC (Safari, older React Native) and SpiderMonkey frames\n// look like \"name@location\", anonymous ones \"@location\".\nconst V8_FRAME = /^\\s*at\\s+(?:async\\s+)?(?:new\\s+)?([^\\s(]+)\\s+\\(/;\nconst V8_ANY_FRAME = /^\\s*at\\s/;\nconst JSC_FRAME = /^([^@]*)@/;\n\n// The name of the function that called dispatchEvent, for debugging: it's\n// stored on each event as currentTarget. Names will be mangled in minified\n// builds, and it's \"anonymous\" where no stack is available.\nfunction getCallerName(): string {\n const stack = new Error().stack;\n if (!stack) return 'anonymous';\n\n // Drop V8's \"Error\" header line so frames line up across engines:\n // frames[0] = getCallerName, frames[1] = dispatchEvent, frames[2] = caller\n const frames = stack\n .split('\\n')\n .filter((line) => V8_ANY_FRAME.test(line) || line.includes('@'));\n const frame = frames[2];\n if (!frame) return 'anonymous';\n\n const name = V8_ANY_FRAME.test(frame)\n ? frame.match(V8_FRAME)?.[1]\n : frame.match(JSC_FRAME)?.[1];\n return name || 'anonymous';\n}\n\n// Listeners by event type. dispatchEvent calls them in the order they were\n// added, each with its own copy of the event.\nexport class EventTarget {\n listeners = new Map<string, Set<Listener>>();\n\n addEventListener(type: string, listener: Listener) {\n let subs = this.listeners.get(type);\n if (!subs) {\n subs = new Set();\n this.listeners.set(type, subs);\n }\n subs.add(listener);\n return this;\n }\n\n removeEventListener(type: string, listener: Listener) {\n const subs = this.listeners.get(type);\n if (subs) {\n subs.delete(listener);\n if (!subs.size) this.listeners.delete(type);\n }\n return this;\n }\n\n dispatchEvent(event: Event) {\n event.target = this;\n event.currentTarget = getCallerName();\n const subs = this.listeners.get(event.type);\n\n if (!subs?.size) return this;\n\n // Snapshot so listeners added during this dispatch wait for the next one\n for (const listener of Array.from(subs)) {\n if (event.isPropagationStopped) break;\n // Listeners removed by an earlier listener in this dispatch are skipped\n if (!subs.has(listener)) continue;\n listener(copyForListener(event, this));\n }\n return this;\n }\n}\n\n// A listener's own copy of the event. stopPropagation and\n// isPropagationStopped live on Event's prototype, so a plain spread would\n// drop them; these versions act on the original event.\nfunction copyForListener(event: Event, target: EventTarget): AppEvent {\n const copy: AppEvent = {\n ...event,\n target,\n stopPropagation: () => event.stopPropagation(),\n };\n delete copy.stopped;\n Object.defineProperty(copy, 'isPropagationStopped', {\n get: () => event.isPropagationStopped,\n enumerable: true,\n });\n return copy;\n}\n\n// Keys a detail can't set: they're managed by Event and EventTarget\nconst PROTECTED_KEYS = new Set([\n 'type',\n 'timeStamp',\n 'target',\n 'currentTarget',\n 'stopped',\n 'stopPropagation',\n 'isPropagationStopped',\n]);\n\n// An event of `type` with `detail`'s keys copied onto it (the keys in\n// PROTECTED_KEYS can't be overridden)\nexport class Event implements AppEvent {\n type: string;\n target: unknown = null;\n stopped = false;\n currentTarget: string | null = null;\n timeStamp = Date.now();\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [key: string]: any;\n\n constructor(type: string, detail: Record<string, unknown> = {}) {\n this.type = type;\n\n Object.keys(detail).forEach((key) => {\n if (PROTECTED_KEYS.has(key)) return;\n this[key] = detail[key];\n });\n }\n\n stopPropagation() {\n this.stopped = true;\n }\n\n get isPropagationStopped() {\n return this.stopped;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACyBA,IAAM,WAAW;AACjB,IAAM,eAAe;AACrB,IAAM,YAAY;AAKlB,SAAS,gBAAwB;AAhCjC;AAiCE,QAAM,QAAQ,IAAI,MAAM,EAAE;AAC1B,MAAI,CAAC,MAAO,QAAO;AAInB,QAAM,SAAS,MACZ,MAAM,IAAI,EACV,OAAO,CAAC,SAAS,aAAa,KAAK,IAAI,KAAK,KAAK,SAAS,GAAG,CAAC;AACjE,QAAM,QAAQ,OAAO,CAAC;A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AA,IACjB,YAAY;AAAA,EACd,CAAC;AACD,SAAO;AACT;AAGA,IAAM,iBAAiB,oBAAI,IAAI;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAIM,IAAM,QAAN,MAAgC;AAAA,EASrC,YAAY,MAAc,SAAkC,CAAC,GAAG;AAPhE,kBAAkB;AAClB,mBAAU;AACV,yBAA+B;AAC/B,qBAAY,KAAK,IAAI;AAKnB,SAAK,OAAO;AAEZ,WAAO,KAAK,MAAM,EAAE,QAAQ,CAAC,QAAQ;AACnC,UAAI,eAAe,IAAI,GAAG,EAAG;AAC7B,WAAK,GAAG,IAAI,OAAO,GAAG;AAAA,IACxB,CAAC;AAAA,EACH;AAAA,EAEA,kBAAkB;AAChB,SAAK,UAAU;AAAA,EACjB;AAAA,EAEA,IAAI,uBAAuB;AACzB,WAAO,KAAK;AAAA,EACd;AACF;","names":[]}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/EventHandler.ts"],"sourcesContent":["// A small event system for plain objects. Unlike the DOM's\n// EventTarget, listeners get a copy of the event with its detail spread onto it.\n//\n// Uses only ES2019 built-ins (no DOM or Node APIs), so it runs the same in\n// browsers, React Native (Hermes/JSC) and Node.\n\n// What a listener receives: the event's type and its detail's keys\n// (e.g. { type: 'date-changed', value })\nexport interface AppEvent {\n type: string;\n target?: unknown;\n timeStamp?: number;\n currentTarget?: string | null;\n // Stops the remaining listeners for this dispatch from being called\n stopPropagation?: () => void;\n readonly isPropagationStopped?: boolean;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [key: string]: any;\n}\n\nexport type Listener = (event: AppEvent) => void;\n\n// V8/Hermes frames look like \" at name (location)\"; anonymous ones are\n// \" at location\". JSC (Safari, older React Native) and SpiderMonkey frames\n// look like \"name@location\", anonymous ones \"@location\".\nconst V8_FRAME = /^\\s*at\\s+(?:async\\s+)?(?:new\\s+)?([^\\s(]+)\\s+\\(/;\nconst V8_ANY_FRAME = /^\\s*at\\s/;\nconst JSC_FRAME = /^([^@]*)@/;\n\n// The name of the function that called dispatchEvent, for debugging: it's\n// stored on each event as currentTarget. Names will be mangled in minified\n// builds, and it's \"anonymous\" where no stack is available.\nfunction getCallerName(): string {\n const stack = new Error().stack;\n if (!stack) return
|
|
1
|
+
{"version":3,"sources":["../src/EventHandler.ts"],"sourcesContent":["// A small event system for plain objects. Unlike the DOM's\n// EventTarget, listeners get a copy of the event with its detail spread onto it.\n//\n// Uses only ES2019 built-ins (no DOM or Node APIs), so it runs the same in\n// browsers, React Native (Hermes/JSC) and Node.\n\n// What a listener receives: the event's type and its detail's keys\n// (e.g. { type: 'date-changed', value })\nexport interface AppEvent {\n type: string;\n target?: unknown;\n timeStamp?: number;\n currentTarget?: string | null;\n // Stops the remaining listeners for this dispatch from being called\n stopPropagation?: () => void;\n readonly isPropagationStopped?: boolean;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [key: string]: any;\n}\n\nexport type Listener = (event: AppEvent) => void;\n\n// V8/Hermes frames look like \" at name (location)\"; anonymous ones are\n// \" at location\". JSC (Safari, older React Native) and SpiderMonkey frames\n// look like \"name@location\", anonymous ones \"@location\".\nconst V8_FRAME = /^\\s*at\\s+(?:async\\s+)?(?:new\\s+)?([^\\s(]+)\\s+\\(/;\nconst V8_ANY_FRAME = /^\\s*at\\s/;\nconst JSC_FRAME = /^([^@]*)@/;\n\n// The name of the function that called dispatchEvent, for debugging: it's\n// stored on each event as currentTarget. Names will be mangled in minified\n// builds, and it's \"anonymous\" where no stack is available.\nfunction getCallerName(): string {\n const stack = new Error().stack;\n if (!stack) return 'anonymous';\n\n // Drop V8's \"Error\" header line so frames line up across engines:\n // frames[0] = getCallerName, frames[1] = dispatchEvent, frames[2] = caller\n const frames = stack\n .split('\\n')\n .filter((line) => V8_ANY_FRAME.test(line) || line.includes('@'));\n const frame = frames[2];\n if (!frame) return 'anonymous';\n\n const name = V8_ANY_FRAME.test(frame)\n ? frame.match(V8_FRAME)?.[1]\n : frame.match(JSC_FRAME)?.[1];\n return name || 'anonymous';\n}\n\n// Listeners by event type. dispatchEvent calls them in the order they were\n// added, each with its own copy of the event.\nexport class EventTarget {\n listeners = new Map<string, Set<Listener>>();\n\n addEventListener(type: string, listener: Listener) {\n let subs = this.listeners.get(type);\n if (!subs) {\n subs = new Set();\n this.listeners.set(type, subs);\n }\n subs.add(listener);\n return this;\n }\n\n removeEventListener(type: string, listener: Listener) {\n const subs = this.listeners.get(type);\n if (subs) {\n subs.delete(listener);\n if (!subs.size) this.listeners.delete(type);\n }\n return this;\n }\n\n dispatchEvent(event: Event) {\n event.target = this;\n event.currentTarget = getCallerName();\n const subs = this.listeners.get(event.type);\n\n if (!subs?.size) return this;\n\n // Snapshot so listeners added during this dispatch wait for the next one\n for (const listener of Array.from(subs)) {\n if (event.isPropagationStopped) break;\n // Listeners removed by an earlier listener in this dispatch are skipped\n if (!subs.has(listener)) continue;\n listener(copyForListener(event, this));\n }\n return this;\n }\n}\n\n// A listener's own copy of the event. stopPropagation and\n// isPropagationStopped live on Event's prototype, so a plain spread would\n// drop them; these versions act on the original event.\nfunction copyForListener(event: Event, target: EventTarget): AppEvent {\n const copy: AppEvent = {\n ...event,\n target,\n stopPropagation: () => event.stopPropagation(),\n };\n delete copy.stopped;\n Object.defineProperty(copy, 'isPropagationStopped', {\n get: () => event.isPropagationStopped,\n enumerable: true,\n });\n return copy;\n}\n\n// Keys a detail can't set: they're managed by Event and EventTarget\nconst PROTECTED_KEYS = new Set([\n 'type',\n 'timeStamp',\n 'target',\n 'currentTarget',\n 'stopped',\n 'stopPropagation',\n 'isPropagationStopped',\n]);\n\n// An event of `type` with `detail`'s keys copied onto it (the keys in\n// PROTECTED_KEYS can't be overridden)\nexport class Event implements AppEvent {\n type: string;\n target: unknown = null;\n stopped = false;\n currentTarget: string | null = null;\n timeStamp = Date.now();\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [key: string]: any;\n\n constructor(type: string, detail: Record<string, unknown> = {}) {\n this.type = type;\n\n Object.keys(detail).forEach((key) => {\n if (PROTECTED_KEYS.has(key)) return;\n this[key] = detail[key];\n });\n }\n\n stopPropagation() {\n this.stopped = true;\n }\n\n get isPropagationStopped() {\n return this.stopped;\n }\n}\n"],"mappings":";AAyBA,IAAM,WAAW;AACjB,IAAM,eAAe;AACrB,IAAM,YAAY;AAKlB,SAAS,gBAAwB;AAhCjC;AAiCE,QAAM,QAAQ,IAAI,MAAM,EAAE;AAC1B,MAAI,CAAC,MAAO,QAAO;AAInB,QAAM,SAAS,MACZ,MAAM,IAAI,EACV,OAAO,CAAC,SAAS,aAAa,KAAK,IAAI,KAAK,KAAK,SAAS,GAAG,CAAC;AACjE,QAAM,QAAQ,OAAO,CAAC;AACtB,MAAI,CAAC,MAAO,QAAO;AAEnB,QAAM,OAAO,aAAa,KAAK,KAAK,KAChC,WAAM,MAAM,QAAQ,MAApB,mBAAwB,MACxB,WAAM,MAAM,SAAS,MAArB,mBAAyB;AAC7B,SAAO,QAAQ;AACjB;AAIO,IAAM,cAAN,MAAkB;AAAA,EAAlB;AACL,qBAAY,oBAAI,IAA2B;AAAA;AAAA,EAE3C,iBAAiB,MAAc,UAAoB;AACjD,QAAI,OAAO,KAAK,UAAU,IAAI,IAAI;AAClC,QAAI,CAAC,MAAM;AACT,aAAO,oBAAI,IAAI;AACf,WAAK,UAAU,IAAI,MAAM,IAAI;AAAA,IAC/B;AACA,SAAK,IAAI,QAAQ;AACjB,WAAO;AAAA,EACT;AAAA,EAEA,oBAAoB,MAAc,UAAoB;AACpD,UAAM,OAAO,KAAK,UAAU,IAAI,IAAI;AACpC,QAAI,MAAM;AACR,WAAK,OAAO,QAAQ;AACpB,UAAI,CAAC,KAAK,KAAM,MAAK,UAAU,OAAO,IAAI;AAAA,IAC5C;AACA,WAAO;AAAA,EACT;AAAA,EAEA,cAAc,OAAc;AAC1B,UAAM,SAAS;AACf,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
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "A tiny, dependency-free event system for plain objects. Works in browsers, React Native and Node.",
|
|
5
5
|
"author": "camdagr8",
|
|
6
6
|
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/ReactiumCore/EventHandle.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/ReactiumCore/EventHandle/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/ReactiumCore/EventHandle#readme",
|
|
7
15
|
"type": "module",
|
|
8
16
|
"sideEffects": false,
|
|
9
17
|
"publishConfig": {
|
|
@@ -34,7 +42,11 @@
|
|
|
34
42
|
"build": "tsup",
|
|
35
43
|
"test": "vitest run",
|
|
36
44
|
"typecheck": "tsc --noEmit",
|
|
37
|
-
"
|
|
45
|
+
"lint": "eslint .",
|
|
46
|
+
"lint:fix": "eslint . --fix",
|
|
47
|
+
"format": "prettier --write .",
|
|
48
|
+
"format:check": "prettier --check .",
|
|
49
|
+
"prepublishOnly": "npm run typecheck && npm run lint && npm test && npm run build"
|
|
38
50
|
},
|
|
39
51
|
"keywords": [
|
|
40
52
|
"event",
|
|
@@ -46,8 +58,15 @@
|
|
|
46
58
|
"browser"
|
|
47
59
|
],
|
|
48
60
|
"devDependencies": {
|
|
61
|
+
"@eslint/js": "^10.0.1",
|
|
62
|
+
"@stylistic/eslint-plugin": "^5.10.0",
|
|
63
|
+
"eslint": "^10.15.0",
|
|
64
|
+
"eslint-config-prettier": "^10.1.8",
|
|
65
|
+
"globals": "^17.14.0",
|
|
66
|
+
"prettier": "^3.10.1",
|
|
49
67
|
"tsup": "^8.0.0",
|
|
50
68
|
"typescript": "^5.6.0",
|
|
69
|
+
"typescript-eslint": "^8.73.0",
|
|
51
70
|
"vitest": "^3.0.0"
|
|
52
71
|
}
|
|
53
72
|
}
|
package/src/EventHandler.ts
CHANGED
|
@@ -32,20 +32,20 @@ const JSC_FRAME = /^([^@]*)@/;
|
|
|
32
32
|
// builds, and it's "anonymous" where no stack is available.
|
|
33
33
|
function getCallerName(): string {
|
|
34
34
|
const stack = new Error().stack;
|
|
35
|
-
if (!stack) return
|
|
35
|
+
if (!stack) return 'anonymous';
|
|
36
36
|
|
|
37
37
|
// Drop V8's "Error" header line so frames line up across engines:
|
|
38
38
|
// frames[0] = getCallerName, frames[1] = dispatchEvent, frames[2] = caller
|
|
39
39
|
const frames = stack
|
|
40
|
-
.split(
|
|
41
|
-
.filter((line) => V8_ANY_FRAME.test(line) || line.includes(
|
|
40
|
+
.split('\n')
|
|
41
|
+
.filter((line) => V8_ANY_FRAME.test(line) || line.includes('@'));
|
|
42
42
|
const frame = frames[2];
|
|
43
|
-
if (!frame) return
|
|
43
|
+
if (!frame) return 'anonymous';
|
|
44
44
|
|
|
45
45
|
const name = V8_ANY_FRAME.test(frame)
|
|
46
46
|
? frame.match(V8_FRAME)?.[1]
|
|
47
47
|
: frame.match(JSC_FRAME)?.[1];
|
|
48
|
-
return name ||
|
|
48
|
+
return name || 'anonymous';
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
// Listeners by event type. dispatchEvent calls them in the order they were
|
|
@@ -100,7 +100,7 @@ function copyForListener(event: Event, target: EventTarget): AppEvent {
|
|
|
100
100
|
stopPropagation: () => event.stopPropagation(),
|
|
101
101
|
};
|
|
102
102
|
delete copy.stopped;
|
|
103
|
-
Object.defineProperty(copy,
|
|
103
|
+
Object.defineProperty(copy, 'isPropagationStopped', {
|
|
104
104
|
get: () => event.isPropagationStopped,
|
|
105
105
|
enumerable: true,
|
|
106
106
|
});
|
|
@@ -109,13 +109,13 @@ function copyForListener(event: Event, target: EventTarget): AppEvent {
|
|
|
109
109
|
|
|
110
110
|
// Keys a detail can't set: they're managed by Event and EventTarget
|
|
111
111
|
const PROTECTED_KEYS = new Set([
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
112
|
+
'type',
|
|
113
|
+
'timeStamp',
|
|
114
|
+
'target',
|
|
115
|
+
'currentTarget',
|
|
116
|
+
'stopped',
|
|
117
|
+
'stopPropagation',
|
|
118
|
+
'isPropagationStopped',
|
|
119
119
|
]);
|
|
120
120
|
|
|
121
121
|
// An event of `type` with `detail`'s keys copied onto it (the keys in
|
package/src/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { EventTarget, Event } from
|
|
2
|
-
export type { AppEvent, Listener } from
|
|
1
|
+
export { EventTarget, Event } from './EventHandler';
|
|
2
|
+
export type { AppEvent, Listener } from './EventHandler';
|