@atomic-reactor/event-handle 0.0.0-stage → 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 camdagr8
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,86 @@
1
- # Temporary Holding Version
1
+ # @atomic-reactor/event-handle
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A tiny, dependency-free event system for plain objects. Works in browsers,
4
+ React Native (Hermes and JSC) and Node, and ships ESM, CommonJS and
5
+ TypeScript types.
6
+
7
+ Unlike the DOM's `EventTarget`, each listener gets its own copy of the event,
8
+ with the event's detail spread onto it, so you read `event.value` rather than
9
+ `event.detail.value`.
10
+
11
+ ## Install
12
+
13
+ ```sh
14
+ npm install --save @atomic-reactor/EventHandle
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```ts
20
+ import { EventTarget, Event } from '@atomic-reactor/event-handle';
21
+
22
+ class DatePicker extends EventTarget {
23
+ setDate(value: Date) {
24
+ this.dispatchEvent(new Event('date-changed', { value }));
25
+ }
26
+ }
27
+
28
+ const picker = new DatePicker();
29
+
30
+ picker.addEventListener('date-changed', (event) => {
31
+ console.log(event.type, event.value); // "date-changed", Date
32
+ });
33
+
34
+ picker.setDate(new Date());
35
+ ```
36
+
37
+ `addEventListener` and `removeEventListener` return the target, so calls can
38
+ be chained.
39
+
40
+ ### Stopping propagation
41
+
42
+ Calling `event.stopPropagation()` in a listener stops the remaining listeners
43
+ for that dispatch from being called.
44
+
45
+ ```ts
46
+ picker.addEventListener('date-changed', (event) => {
47
+ if (!event.value) event.stopPropagation();
48
+ });
49
+ ```
50
+
51
+ ### What a listener receives
52
+
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
+
63
+ Detail keys that clash with the fields above are ignored.
64
+
65
+ ### Name clashes with the DOM
66
+
67
+ `EventTarget` and `Event` share names with browser (and newer React Native)
68
+ globals. Importing them shadows the globals in that module only; if you need
69
+ both, rename on import:
70
+
71
+ ```ts
72
+ import {
73
+ EventTarget as Emitter,
74
+ Event as AppEvent,
75
+ } from '@atomic-reactor/event-handle';
76
+ ```
77
+
78
+ ## Compatibility
79
+
80
+ The library uses only ES2019 built-ins (`Map`, `Set`, `Object.keys`,
81
+ `Date.now`, `Error().stack`) and is compiled to ES2019, so it needs no
82
+ polyfills in any current browser, React Native 0.60+ or Node 12+.
83
+
84
+ ## License
85
+
86
+ MIT
package/dist/index.cjs ADDED
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ Event: () => Event,
24
+ EventTarget: () => EventTarget
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+
28
+ // src/EventHandler.ts
29
+ var V8_FRAME = /^\s*at\s+(?:async\s+)?(?:new\s+)?([^\s(]+)\s+\(/;
30
+ var V8_ANY_FRAME = /^\s*at\s/;
31
+ var JSC_FRAME = /^([^@]*)@/;
32
+ function getCallerName() {
33
+ var _a, _b;
34
+ const stack = new Error().stack;
35
+ if (!stack) return "anonymous";
36
+ const frames = stack.split("\n").filter((line) => V8_ANY_FRAME.test(line) || line.includes("@"));
37
+ const frame = frames[2];
38
+ if (!frame) return "anonymous";
39
+ const name = V8_ANY_FRAME.test(frame) ? (_a = frame.match(V8_FRAME)) == null ? void 0 : _a[1] : (_b = frame.match(JSC_FRAME)) == null ? void 0 : _b[1];
40
+ return name || "anonymous";
41
+ }
42
+ var EventTarget = class {
43
+ constructor() {
44
+ this.listeners = /* @__PURE__ */ new Map();
45
+ }
46
+ addEventListener(type, listener) {
47
+ let subs = this.listeners.get(type);
48
+ if (!subs) {
49
+ subs = /* @__PURE__ */ new Set();
50
+ this.listeners.set(type, subs);
51
+ }
52
+ subs.add(listener);
53
+ return this;
54
+ }
55
+ removeEventListener(type, listener) {
56
+ const subs = this.listeners.get(type);
57
+ if (subs) {
58
+ subs.delete(listener);
59
+ if (!subs.size) this.listeners.delete(type);
60
+ }
61
+ return this;
62
+ }
63
+ dispatchEvent(event) {
64
+ event.target = this;
65
+ event.currentTarget = getCallerName();
66
+ const subs = this.listeners.get(event.type);
67
+ if (!(subs == null ? void 0 : subs.size)) return this;
68
+ for (const listener of Array.from(subs)) {
69
+ if (event.isPropagationStopped) break;
70
+ if (!subs.has(listener)) continue;
71
+ listener(copyForListener(event, this));
72
+ }
73
+ return this;
74
+ }
75
+ };
76
+ function copyForListener(event, target) {
77
+ const copy = {
78
+ ...event,
79
+ target,
80
+ stopPropagation: () => event.stopPropagation()
81
+ };
82
+ delete copy.stopped;
83
+ Object.defineProperty(copy, "isPropagationStopped", {
84
+ get: () => event.isPropagationStopped,
85
+ enumerable: true
86
+ });
87
+ return copy;
88
+ }
89
+ var PROTECTED_KEYS = /* @__PURE__ */ new Set([
90
+ "type",
91
+ "timeStamp",
92
+ "target",
93
+ "currentTarget",
94
+ "stopped",
95
+ "stopPropagation",
96
+ "isPropagationStopped"
97
+ ]);
98
+ var Event = class {
99
+ constructor(type, detail = {}) {
100
+ this.target = null;
101
+ this.stopped = false;
102
+ this.currentTarget = null;
103
+ this.timeStamp = Date.now();
104
+ this.type = type;
105
+ Object.keys(detail).forEach((key) => {
106
+ if (PROTECTED_KEYS.has(key)) return;
107
+ this[key] = detail[key];
108
+ });
109
+ }
110
+ stopPropagation() {
111
+ this.stopped = true;
112
+ }
113
+ get isPropagationStopped() {
114
+ return this.stopped;
115
+ }
116
+ };
117
+ // Annotate the CommonJS export names for ESM import in node:
118
+ 0 && (module.exports = {
119
+ Event,
120
+ EventTarget
121
+ });
122
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
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;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,UAAM,gBAAgB,cAAc;AACpC,UAAM,OAAO,KAAK,UAAU,IAAI,MAAM,IAAI;AAE1C,QAAI,EAAC,6BAAM,MAAM,QAAO;AAGxB,eAAW,YAAY,MAAM,KAAK,IAAI,GAAG;AACvC,UAAI,MAAM,qBAAsB;AAEhC,UAAI,CAAC,KAAK,IAAI,QAAQ,EAAG;AACzB,eAAS,gBAAgB,OAAO,IAAI,CAAC;AAAA,IACvC;AACA,WAAO;AAAA,EACT;AACF;AAKA,SAAS,gBAAgB,OAAc,QAA+B;AACpE,QAAM,OAAiB;AAAA,IACrB,GAAG;AAAA,IACH;AAAA,IACA,iBAAiB,MAAM,MAAM,gBAAgB;AAAA,EAC/C;AACA,SAAO,KAAK;AACZ,SAAO,eAAe,MAAM,wBAAwB;AAAA,IAClD,KAAK,MAAM,MAAM;AAAA,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":[]}
@@ -0,0 +1,29 @@
1
+ interface AppEvent {
2
+ type: string;
3
+ target?: unknown;
4
+ timeStamp?: number;
5
+ currentTarget?: string | null;
6
+ stopPropagation?: () => void;
7
+ readonly isPropagationStopped?: boolean;
8
+ [key: string]: any;
9
+ }
10
+ type Listener = (event: AppEvent) => void;
11
+ declare class EventTarget {
12
+ listeners: Map<string, Set<Listener>>;
13
+ addEventListener(type: string, listener: Listener): this;
14
+ removeEventListener(type: string, listener: Listener): this;
15
+ dispatchEvent(event: Event): this;
16
+ }
17
+ declare class Event implements AppEvent {
18
+ type: string;
19
+ target: unknown;
20
+ stopped: boolean;
21
+ currentTarget: string | null;
22
+ timeStamp: number;
23
+ [key: string]: any;
24
+ constructor(type: string, detail?: Record<string, unknown>);
25
+ stopPropagation(): void;
26
+ get isPropagationStopped(): boolean;
27
+ }
28
+
29
+ export { type AppEvent, Event, EventTarget, type Listener };
@@ -0,0 +1,29 @@
1
+ interface AppEvent {
2
+ type: string;
3
+ target?: unknown;
4
+ timeStamp?: number;
5
+ currentTarget?: string | null;
6
+ stopPropagation?: () => void;
7
+ readonly isPropagationStopped?: boolean;
8
+ [key: string]: any;
9
+ }
10
+ type Listener = (event: AppEvent) => void;
11
+ declare class EventTarget {
12
+ listeners: Map<string, Set<Listener>>;
13
+ addEventListener(type: string, listener: Listener): this;
14
+ removeEventListener(type: string, listener: Listener): this;
15
+ dispatchEvent(event: Event): this;
16
+ }
17
+ declare class Event implements AppEvent {
18
+ type: string;
19
+ target: unknown;
20
+ stopped: boolean;
21
+ currentTarget: string | null;
22
+ timeStamp: number;
23
+ [key: string]: any;
24
+ constructor(type: string, detail?: Record<string, unknown>);
25
+ stopPropagation(): void;
26
+ get isPropagationStopped(): boolean;
27
+ }
28
+
29
+ export { type AppEvent, Event, EventTarget, type Listener };
package/dist/index.js ADDED
@@ -0,0 +1,94 @@
1
+ // src/EventHandler.ts
2
+ var V8_FRAME = /^\s*at\s+(?:async\s+)?(?:new\s+)?([^\s(]+)\s+\(/;
3
+ var V8_ANY_FRAME = /^\s*at\s/;
4
+ var JSC_FRAME = /^([^@]*)@/;
5
+ function getCallerName() {
6
+ var _a, _b;
7
+ const stack = new Error().stack;
8
+ if (!stack) return "anonymous";
9
+ const frames = stack.split("\n").filter((line) => V8_ANY_FRAME.test(line) || line.includes("@"));
10
+ const frame = frames[2];
11
+ if (!frame) return "anonymous";
12
+ const name = V8_ANY_FRAME.test(frame) ? (_a = frame.match(V8_FRAME)) == null ? void 0 : _a[1] : (_b = frame.match(JSC_FRAME)) == null ? void 0 : _b[1];
13
+ return name || "anonymous";
14
+ }
15
+ var EventTarget = class {
16
+ constructor() {
17
+ this.listeners = /* @__PURE__ */ new Map();
18
+ }
19
+ addEventListener(type, listener) {
20
+ let subs = this.listeners.get(type);
21
+ if (!subs) {
22
+ subs = /* @__PURE__ */ new Set();
23
+ this.listeners.set(type, subs);
24
+ }
25
+ subs.add(listener);
26
+ return this;
27
+ }
28
+ removeEventListener(type, listener) {
29
+ const subs = this.listeners.get(type);
30
+ if (subs) {
31
+ subs.delete(listener);
32
+ if (!subs.size) this.listeners.delete(type);
33
+ }
34
+ return this;
35
+ }
36
+ dispatchEvent(event) {
37
+ event.target = this;
38
+ event.currentTarget = getCallerName();
39
+ const subs = this.listeners.get(event.type);
40
+ if (!(subs == null ? void 0 : subs.size)) return this;
41
+ for (const listener of Array.from(subs)) {
42
+ if (event.isPropagationStopped) break;
43
+ if (!subs.has(listener)) continue;
44
+ listener(copyForListener(event, this));
45
+ }
46
+ return this;
47
+ }
48
+ };
49
+ function copyForListener(event, target) {
50
+ const copy = {
51
+ ...event,
52
+ target,
53
+ stopPropagation: () => event.stopPropagation()
54
+ };
55
+ delete copy.stopped;
56
+ Object.defineProperty(copy, "isPropagationStopped", {
57
+ get: () => event.isPropagationStopped,
58
+ enumerable: true
59
+ });
60
+ return copy;
61
+ }
62
+ var PROTECTED_KEYS = /* @__PURE__ */ new Set([
63
+ "type",
64
+ "timeStamp",
65
+ "target",
66
+ "currentTarget",
67
+ "stopped",
68
+ "stopPropagation",
69
+ "isPropagationStopped"
70
+ ]);
71
+ var Event = class {
72
+ constructor(type, detail = {}) {
73
+ this.target = null;
74
+ this.stopped = false;
75
+ this.currentTarget = null;
76
+ this.timeStamp = Date.now();
77
+ this.type = type;
78
+ Object.keys(detail).forEach((key) => {
79
+ if (PROTECTED_KEYS.has(key)) return;
80
+ this[key] = detail[key];
81
+ });
82
+ }
83
+ stopPropagation() {
84
+ this.stopped = true;
85
+ }
86
+ get isPropagationStopped() {
87
+ return this.stopped;
88
+ }
89
+ };
90
+ export {
91
+ Event,
92
+ EventTarget
93
+ };
94
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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 '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,UAAM,gBAAgB,cAAc;AACpC,UAAM,OAAO,KAAK,UAAU,IAAI,MAAM,IAAI;AAE1C,QAAI,EAAC,6BAAM,MAAM,QAAO;AAGxB,eAAW,YAAY,MAAM,KAAK,IAAI,GAAG;AACvC,UAAI,MAAM,qBAAsB;AAEhC,UAAI,CAAC,KAAK,IAAI,QAAQ,EAAG;AACzB,eAAS,gBAAgB,OAAO,IAAI,CAAC;AAAA,IACvC;AACA,WAAO;AAAA,EACT;AACF;AAKA,SAAS,gBAAgB,OAAc,QAA+B;AACpE,QAAM,OAAiB;AAAA,IACrB,GAAG;AAAA,IACH;AAAA,IACA,iBAAiB,MAAM,MAAM,gBAAgB;AAAA,EAC/C;AACA,SAAO,KAAK;AACZ,SAAO,eAAe,MAAM,wBAAwB;AAAA,IAClD,KAAK,MAAM,MAAM;AAAA,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/package.json CHANGED
@@ -1,6 +1,72 @@
1
1
  {
2
2
  "name": "@atomic-reactor/event-handle",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.1",
4
+ "description": "A tiny, dependency-free event system for plain objects. Works in browsers, React Native and Node.",
5
+ "author": "camdagr8",
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",
15
+ "type": "module",
16
+ "sideEffects": false,
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
20
+ "main": "./dist/index.cjs",
21
+ "module": "./dist/index.js",
22
+ "types": "./dist/index.d.ts",
23
+ "react-native": "./dist/index.js",
24
+ "exports": {
25
+ ".": {
26
+ "import": {
27
+ "types": "./dist/index.d.ts",
28
+ "default": "./dist/index.js"
29
+ },
30
+ "require": {
31
+ "types": "./dist/index.d.cts",
32
+ "default": "./dist/index.cjs"
33
+ }
34
+ },
35
+ "./package.json": "./package.json"
36
+ },
37
+ "files": [
38
+ "dist",
39
+ "src"
40
+ ],
41
+ "scripts": {
42
+ "build": "tsup",
43
+ "test": "vitest run",
44
+ "typecheck": "tsc --noEmit",
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"
50
+ },
51
+ "keywords": [
52
+ "event",
53
+ "events",
54
+ "eventtarget",
55
+ "event-emitter",
56
+ "pubsub",
57
+ "react-native",
58
+ "browser"
59
+ ],
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",
67
+ "tsup": "^8.0.0",
68
+ "typescript": "^5.6.0",
69
+ "typescript-eslint": "^8.73.0",
70
+ "vitest": "^3.0.0"
71
+ }
72
+ }
@@ -0,0 +1,148 @@
1
+ // A small event system for plain objects. Unlike the DOM's
2
+ // EventTarget, listeners get a copy of the event with its detail spread onto it.
3
+ //
4
+ // Uses only ES2019 built-ins (no DOM or Node APIs), so it runs the same in
5
+ // browsers, React Native (Hermes/JSC) and Node.
6
+
7
+ // What a listener receives: the event's type and its detail's keys
8
+ // (e.g. { type: 'date-changed', value })
9
+ export interface AppEvent {
10
+ type: string;
11
+ target?: unknown;
12
+ timeStamp?: number;
13
+ currentTarget?: string | null;
14
+ // Stops the remaining listeners for this dispatch from being called
15
+ stopPropagation?: () => void;
16
+ readonly isPropagationStopped?: boolean;
17
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
+ [key: string]: any;
19
+ }
20
+
21
+ export type Listener = (event: AppEvent) => void;
22
+
23
+ // V8/Hermes frames look like " at name (location)"; anonymous ones are
24
+ // " at location". JSC (Safari, older React Native) and SpiderMonkey frames
25
+ // look like "name@location", anonymous ones "@location".
26
+ const V8_FRAME = /^\s*at\s+(?:async\s+)?(?:new\s+)?([^\s(]+)\s+\(/;
27
+ const V8_ANY_FRAME = /^\s*at\s/;
28
+ const JSC_FRAME = /^([^@]*)@/;
29
+
30
+ // The name of the function that called dispatchEvent, for debugging: it's
31
+ // stored on each event as currentTarget. Names will be mangled in minified
32
+ // builds, and it's "anonymous" where no stack is available.
33
+ function getCallerName(): string {
34
+ const stack = new Error().stack;
35
+ if (!stack) return 'anonymous';
36
+
37
+ // Drop V8's "Error" header line so frames line up across engines:
38
+ // frames[0] = getCallerName, frames[1] = dispatchEvent, frames[2] = caller
39
+ const frames = stack
40
+ .split('\n')
41
+ .filter((line) => V8_ANY_FRAME.test(line) || line.includes('@'));
42
+ const frame = frames[2];
43
+ if (!frame) return 'anonymous';
44
+
45
+ const name = V8_ANY_FRAME.test(frame)
46
+ ? frame.match(V8_FRAME)?.[1]
47
+ : frame.match(JSC_FRAME)?.[1];
48
+ return name || 'anonymous';
49
+ }
50
+
51
+ // Listeners by event type. dispatchEvent calls them in the order they were
52
+ // added, each with its own copy of the event.
53
+ export class EventTarget {
54
+ listeners = new Map<string, Set<Listener>>();
55
+
56
+ addEventListener(type: string, listener: Listener) {
57
+ let subs = this.listeners.get(type);
58
+ if (!subs) {
59
+ subs = new Set();
60
+ this.listeners.set(type, subs);
61
+ }
62
+ subs.add(listener);
63
+ return this;
64
+ }
65
+
66
+ removeEventListener(type: string, listener: Listener) {
67
+ const subs = this.listeners.get(type);
68
+ if (subs) {
69
+ subs.delete(listener);
70
+ if (!subs.size) this.listeners.delete(type);
71
+ }
72
+ return this;
73
+ }
74
+
75
+ dispatchEvent(event: Event) {
76
+ event.target = this;
77
+ event.currentTarget = getCallerName();
78
+ const subs = this.listeners.get(event.type);
79
+
80
+ if (!subs?.size) return this;
81
+
82
+ // Snapshot so listeners added during this dispatch wait for the next one
83
+ for (const listener of Array.from(subs)) {
84
+ if (event.isPropagationStopped) break;
85
+ // Listeners removed by an earlier listener in this dispatch are skipped
86
+ if (!subs.has(listener)) continue;
87
+ listener(copyForListener(event, this));
88
+ }
89
+ return this;
90
+ }
91
+ }
92
+
93
+ // A listener's own copy of the event. stopPropagation and
94
+ // isPropagationStopped live on Event's prototype, so a plain spread would
95
+ // drop them; these versions act on the original event.
96
+ function copyForListener(event: Event, target: EventTarget): AppEvent {
97
+ const copy: AppEvent = {
98
+ ...event,
99
+ target,
100
+ stopPropagation: () => event.stopPropagation(),
101
+ };
102
+ delete copy.stopped;
103
+ Object.defineProperty(copy, 'isPropagationStopped', {
104
+ get: () => event.isPropagationStopped,
105
+ enumerable: true,
106
+ });
107
+ return copy;
108
+ }
109
+
110
+ // Keys a detail can't set: they're managed by Event and EventTarget
111
+ const PROTECTED_KEYS = new Set([
112
+ 'type',
113
+ 'timeStamp',
114
+ 'target',
115
+ 'currentTarget',
116
+ 'stopped',
117
+ 'stopPropagation',
118
+ 'isPropagationStopped',
119
+ ]);
120
+
121
+ // An event of `type` with `detail`'s keys copied onto it (the keys in
122
+ // PROTECTED_KEYS can't be overridden)
123
+ export class Event implements AppEvent {
124
+ type: string;
125
+ target: unknown = null;
126
+ stopped = false;
127
+ currentTarget: string | null = null;
128
+ timeStamp = Date.now();
129
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
130
+ [key: string]: any;
131
+
132
+ constructor(type: string, detail: Record<string, unknown> = {}) {
133
+ this.type = type;
134
+
135
+ Object.keys(detail).forEach((key) => {
136
+ if (PROTECTED_KEYS.has(key)) return;
137
+ this[key] = detail[key];
138
+ });
139
+ }
140
+
141
+ stopPropagation() {
142
+ this.stopped = true;
143
+ }
144
+
145
+ get isPropagationStopped() {
146
+ return this.stopped;
147
+ }
148
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { EventTarget, Event } from './EventHandler';
2
+ export type { AppEvent, Listener } from './EventHandler';