hookified 1.2.1 → 1.3.0
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 +68 -15
- package/dist/browser/index.global.js +2 -0
- package/dist/browser/index.global.js.map +1 -0
- package/dist/browser/index.js +2 -0
- package/dist/browser/index.js.map +1 -0
- package/package.json +13 -12
- /package/dist/{index.cjs → node/index.cjs} +0 -0
- /package/dist/{index.d.cts → node/index.d.cts} +0 -0
- /package/dist/{index.d.ts → node/index.d.ts} +0 -0
- /package/dist/{index.js → node/index.js} +0 -0
package/README.md
CHANGED
|
@@ -8,18 +8,18 @@
|
|
|
8
8
|
[](https://npmjs.com/package/hookified)
|
|
9
9
|
[](https://npmjs.com/package/hookified)
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
# Features
|
|
12
12
|
- Simple replacement for EventEmitter
|
|
13
13
|
- Async Middleware Hooks for Your Methods
|
|
14
14
|
- ESM / CJS with Types and Nodejs 20+
|
|
15
15
|
- Maintained on a regular basis!
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
# Installation
|
|
18
18
|
```bash
|
|
19
19
|
npm install hookified --save
|
|
20
20
|
```
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
# Usage
|
|
23
23
|
This was built because we constantly wanted hooks and events extended on libraires we are building such as [Keyv](https://keyv.org) and [Cacheable](https://cacheable.org). This is a simple way to add hooks and events to your classes.
|
|
24
24
|
|
|
25
25
|
```javascript
|
|
@@ -66,39 +66,92 @@ class MyClass extends Hookified {
|
|
|
66
66
|
}
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
-
|
|
69
|
+
# Using it in the Browser
|
|
70
70
|
|
|
71
|
-
|
|
71
|
+
```html
|
|
72
|
+
<script type="module">
|
|
73
|
+
import { Hookified } from 'https://cdn.jsdelivr.net/npm/hookified/dist/browser/index.js';
|
|
74
|
+
|
|
75
|
+
class MyClass extends Hookified {
|
|
76
|
+
constructor() {
|
|
77
|
+
super();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async myMethodEmittingEvent() {
|
|
81
|
+
this.emit('message', 'Hello World'); //using Emittery
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
//with hooks you can pass data in and if they are subscribed via onHook they can modify the data
|
|
85
|
+
async myMethodWithHooks() Promise<any> {
|
|
86
|
+
let data = { some: 'data' };
|
|
87
|
+
// do something
|
|
88
|
+
await this.hook('before:myMethod2', data);
|
|
89
|
+
|
|
90
|
+
return data;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
</script>
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
if you are not using ESM modules, you can use the following:
|
|
97
|
+
|
|
98
|
+
```html
|
|
99
|
+
<script src="https://cdn.jsdelivr.net/npm/hookified/dist/browser/index.global.js"></script>
|
|
100
|
+
<script>
|
|
101
|
+
class MyClass extends Hookified {
|
|
102
|
+
constructor() {
|
|
103
|
+
super();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async myMethodEmittingEvent() {
|
|
107
|
+
this.emit('message', 'Hello World'); //using Emittery
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
//with hooks you can pass data in and if they are subscribed via onHook they can modify the data
|
|
111
|
+
async myMethodWithHooks() Promise<any> {
|
|
112
|
+
let data = { some: 'data' };
|
|
113
|
+
// do something
|
|
114
|
+
await this.hook('before:myMethod2', data);
|
|
115
|
+
|
|
116
|
+
return data;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
</script>
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
# API
|
|
123
|
+
|
|
124
|
+
## .onHook(eventName, handler)
|
|
72
125
|
|
|
73
126
|
Subscribe to a hook event.
|
|
74
127
|
|
|
75
|
-
|
|
128
|
+
## .removeHook(eventName)
|
|
76
129
|
|
|
77
130
|
Unsubscribe from a hook event.
|
|
78
131
|
|
|
79
|
-
|
|
132
|
+
## .hook(eventName, ...args)
|
|
80
133
|
|
|
81
134
|
Run a hook event.
|
|
82
135
|
|
|
83
|
-
|
|
136
|
+
## .hooks
|
|
84
137
|
|
|
85
138
|
Get all hooks.
|
|
86
139
|
|
|
87
|
-
|
|
140
|
+
## .getHooks(eventName)
|
|
88
141
|
|
|
89
142
|
Get all hooks for an event.
|
|
90
143
|
|
|
91
|
-
|
|
144
|
+
## .clearHooks(eventName)
|
|
92
145
|
|
|
93
|
-
|
|
146
|
+
## .on(eventName, handler)
|
|
94
147
|
|
|
95
148
|
Subscribe to an event.
|
|
96
149
|
|
|
97
|
-
|
|
150
|
+
## .off(eventName, handler)
|
|
98
151
|
|
|
99
152
|
Unsubscribe from an event.
|
|
100
153
|
|
|
101
|
-
|
|
154
|
+
## .emit(eventName, ...args)
|
|
102
155
|
|
|
103
156
|
Emit an event.
|
|
104
157
|
|
|
@@ -114,7 +167,7 @@ Remove all listeners for an event.
|
|
|
114
167
|
|
|
115
168
|
Set the maximum number of listeners and will truncate if there are already too many.
|
|
116
169
|
|
|
117
|
-
|
|
170
|
+
# Development and Testing
|
|
118
171
|
|
|
119
172
|
Hookified is written in TypeScript and tests are written in `vitest`. To run the tests, use the following command:
|
|
120
173
|
|
|
@@ -126,7 +179,7 @@ npm i && npm test
|
|
|
126
179
|
|
|
127
180
|
To contribute follow the [Contributing Guidelines](CONTRIBUTING.md) and [Code of Conduct](CODE_OF_CONDUCT.md).
|
|
128
181
|
|
|
129
|
-
|
|
182
|
+
# License
|
|
130
183
|
|
|
131
184
|
[MIT & © Jared Wray](LICENSE)
|
|
132
185
|
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";(()=>{var c=Object.defineProperty;var v=(n,e,s)=>e in n?c(n,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):n[e]=s;var r=(n,e,s)=>v(n,typeof e!="symbol"?e+"":e,s);var o=class{constructor(){r(this,"_eventListeners");r(this,"_maxListeners");this._eventListeners=new Map,this._maxListeners=100}maxListeners(){return this._maxListeners}addListener(e,s){this.on(e,s)}on(e,s){this._eventListeners.has(e)||this._eventListeners.set(e,[]);let t=this._eventListeners.get(e);t&&(t.length>=this._maxListeners&&console.warn(`MaxListenersExceededWarning: Possible event memory leak detected. ${t.length+1} ${e} listeners added. Use setMaxListeners() to increase limit.`),t.push(s))}removeListener(e,s){this.off(e,s)}off(e,s){let t=this._eventListeners.get(e)??[],i=t.indexOf(s);i>-1&&t.splice(i,1),t.length===0&&this._eventListeners.delete(e)}emit(e,...s){let t=this._eventListeners.get(e);if(t&&t.length>0)for(let i of t)i(...s)}listeners(e){return this._eventListeners.get(e)??[]}removeAllListeners(e){e?this._eventListeners.delete(e):this._eventListeners.clear()}setMaxListeners(e){this._maxListeners=e;for(let s of this._eventListeners.values())s.length>e&&s.splice(e)}};var l=class extends o{constructor(){super();r(this,"_hooks");this._hooks=new Map}onHook(s,t){let i=this._hooks.get(s);i?i.push(t):this._hooks.set(s,[t])}removeHook(s,t){let i=this._hooks.get(s);if(i){let h=i.indexOf(t);h!==-1&&i.splice(h,1)}}async hook(s,...t){let i=this._hooks.get(s);if(i)for(let h of i)try{await h(...t)}catch(a){this.emit("error",new Error(`Error in hook handler for event "${s}": ${a.message}`))}}get hooks(){return this._hooks}getHooks(s){return this._hooks.get(s)}clearHooks(){this._hooks.clear()}};})();
|
|
2
|
+
//# sourceMappingURL=index.global.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/eventified.ts","../../src/index.ts"],"sourcesContent":["export type EventListener = (...arguments_: any[]) => void;\n\nexport class Eventified {\n\t_eventListeners: Map<string, EventListener[]>;\n\t_maxListeners: number;\n\n\tconstructor() {\n\t\tthis._eventListeners = new Map();\n\t\tthis._maxListeners = 100; // Default maximum number of listeners\n\t}\n\n\tpublic maxListeners(): number {\n\t\treturn this._maxListeners;\n\t}\n\n\t// Add an event listener\n\tpublic addListener(event: string, listener: EventListener): void {\n\t\tthis.on(event, listener);\n\t}\n\n\tpublic on(event: string, listener: EventListener): void {\n\t\tif (!this._eventListeners.has(event)) {\n\t\t\tthis._eventListeners.set(event, []);\n\t\t}\n\n\t\tconst listeners = this._eventListeners.get(event);\n\n\t\tif (listeners) {\n\t\t\tif (listeners.length >= this._maxListeners) {\n\t\t\t\tconsole.warn(`MaxListenersExceededWarning: Possible event memory leak detected. ${listeners.length + 1} ${event} listeners added. Use setMaxListeners() to increase limit.`);\n\t\t\t}\n\n\t\t\tlisteners.push(listener);\n\t\t}\n\t}\n\n\t// Remove an event listener\n\tpublic removeListener(event: string, listener: EventListener): void {\n\t\tthis.off(event, listener);\n\t}\n\n\tpublic off(event: string, listener: EventListener): void {\n\t\tconst listeners = this._eventListeners.get(event) ?? [];\n\t\tconst index = listeners.indexOf(listener);\n\t\tif (index > -1) {\n\t\t\tlisteners.splice(index, 1);\n\t\t}\n\n\t\tif (listeners.length === 0) {\n\t\t\tthis._eventListeners.delete(event);\n\t\t}\n\t}\n\n\t// Emit an event\n\tpublic emit(event: string, ...arguments_: any[]): void {\n\t\tconst listeners = this._eventListeners.get(event);\n\n\t\tif (listeners && listeners.length > 0) {\n\t\t\tfor (const listener of listeners) {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n\t\t\t\tlistener(...arguments_);\n\t\t\t}\n\t\t}\n\t}\n\n\t// Get all listeners for a specific event\n\tpublic listeners(event: string): EventListener[] {\n\t\treturn this._eventListeners.get(event) ?? [];\n\t}\n\n\t// Remove all listeners for a specific event\n\tpublic removeAllListeners(event?: string): void {\n\t\tif (event) {\n\t\t\tthis._eventListeners.delete(event);\n\t\t} else {\n\t\t\tthis._eventListeners.clear();\n\t\t}\n\t}\n\n\t// Set the maximum number of listeners for a single event\n\tpublic setMaxListeners(n: number): void {\n\t\tthis._maxListeners = n;\n\t\tfor (const listeners of this._eventListeners.values()) {\n\t\t\tif (listeners.length > n) {\n\t\t\t\tlisteners.splice(n);\n\t\t\t}\n\t\t}\n\t}\n}\n","import {Eventified} from './eventified.js';\n\nexport type Hook = (...arguments_: any[]) => Promise<void> | void;\n\nexport class Hookified extends Eventified {\n\t_hooks: Map<string, Hook[]>;\n\n\tconstructor() {\n\t\tsuper();\n\t\tthis._hooks = new Map();\n\t}\n\n\t// Adds a handler function for a specific event\n\tonHook(event: string, handler: Hook) {\n\t\tconst eventHandlers = this._hooks.get(event);\n\t\tif (eventHandlers) {\n\t\t\teventHandlers.push(handler);\n\t\t} else {\n\t\t\tthis._hooks.set(event, [handler]);\n\t\t}\n\t}\n\n\t// Removes a specific handler function for a specific event\n\tremoveHook(event: string, handler: Hook) {\n\t\tconst eventHandlers = this._hooks.get(event);\n\t\tif (eventHandlers) {\n\t\t\tconst index = eventHandlers.indexOf(handler);\n\t\t\tif (index !== -1) {\n\t\t\t\teventHandlers.splice(index, 1);\n\t\t\t}\n\t\t}\n\t}\n\n\t// Triggers all handlers for a specific event with provided data\n\tasync hook<T>(event: string, ...arguments_: T[]) {\n\t\tconst eventHandlers = this._hooks.get(event);\n\t\tif (eventHandlers) {\n\t\t\tfor (const handler of eventHandlers) {\n\t\t\t\ttry {\n\t\t\t\t\t// eslint-disable-next-line no-await-in-loop\n\t\t\t\t\tawait handler(...arguments_);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tthis.emit('error', new Error(`Error in hook handler for event \"${event}\": ${(error as Error).message}`));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t// Provides read-only access to the current handlers\n\tget hooks() {\n\t\t// Creating a new map to prevent external modifications to the original map\n\t\treturn this._hooks;\n\t}\n\n\tgetHooks(event: string) {\n\t\treturn this._hooks.get(event);\n\t}\n\n\tclearHooks() {\n\t\tthis._hooks.clear();\n\t}\n}\n\nexport {Eventified, type EventListener} from './eventified.js';\n"],"mappings":"uLAEO,IAAMA,EAAN,KAAiB,CAIvB,aAAc,CAHdC,EAAA,wBACAA,EAAA,sBAGC,KAAK,gBAAkB,IAAI,IAC3B,KAAK,cAAgB,GACtB,CAEO,cAAuB,CAC7B,OAAO,KAAK,aACb,CAGO,YAAYC,EAAe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OC,EAAeC,EAAe,CACpC,IAAMC,EAAgB,KAAK,OAAO,IAAIF,CAAK,EACvCE,EACHA,EAAc,KAAKD,CAAO,EAE1B,KAAK,OAAO,IAAID,EAAO,CAACC,CAAO,CAAC,CAElC,CAGA,WAAWD,EAAeC,EAAe,CACxC,IAAMC,EAAgB,KAAK,OAAO,IAAIF,CAAK,EAC3C,GAAIE,EAAe,CAClB,IAAMC,EAAQD,EAAc,QAAQD,CAAO,EACvCE,IAAU,IACbD,EAAc,OAAOC,EAAO,CAAC,CAE/B,CACD,CAGA,MAAM,KAAQH,KAAkBI,EAAiB,CAChD,IAAMF,EAAgB,KAAK,OAAO,IAAIF,CAAK,EAC3C,GAAIE,EACH,QAAWD,KAAWC,EACrB,GAAI,CAEH,MAAMD,EAAQ,GAAGG,CAAU,CAC5B,OAASC,EAAO,CACf,KAAK,KAAK,QAAS,IAAI,MAAM,oCAAoCL,CAAK,MAAOK,EAAgB,OAAO,EAAE,CAAC,CACxG,CAGH,CAGA,IAAI,OAAQ,CAEX,OAAO,KAAK,MACb,CAEA,SAASL,EAAe,CACvB,OAAO,KAAK,OAAO,IAAIA,CAAK,CAC7B,CAEA,YAAa,CACZ,KAAK,OAAO,MAAM,CACnB,CACD","names":["Eventified","__publicField","event","listener","listeners","index","arguments_","n","Hookified","Eventified","__publicField","event","handler","eventHandlers","index","arguments_","error"]}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var c=Object.defineProperty;var v=(n,e,s)=>e in n?c(n,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):n[e]=s;var r=(n,e,s)=>v(n,typeof e!="symbol"?e+"":e,s);var o=class{constructor(){r(this,"_eventListeners");r(this,"_maxListeners");this._eventListeners=new Map,this._maxListeners=100}maxListeners(){return this._maxListeners}addListener(e,s){this.on(e,s)}on(e,s){this._eventListeners.has(e)||this._eventListeners.set(e,[]);let t=this._eventListeners.get(e);t&&(t.length>=this._maxListeners&&console.warn(`MaxListenersExceededWarning: Possible event memory leak detected. ${t.length+1} ${e} listeners added. Use setMaxListeners() to increase limit.`),t.push(s))}removeListener(e,s){this.off(e,s)}off(e,s){let t=this._eventListeners.get(e)??[],i=t.indexOf(s);i>-1&&t.splice(i,1),t.length===0&&this._eventListeners.delete(e)}emit(e,...s){let t=this._eventListeners.get(e);if(t&&t.length>0)for(let i of t)i(...s)}listeners(e){return this._eventListeners.get(e)??[]}removeAllListeners(e){e?this._eventListeners.delete(e):this._eventListeners.clear()}setMaxListeners(e){this._maxListeners=e;for(let s of this._eventListeners.values())s.length>e&&s.splice(e)}};var l=class extends o{constructor(){super();r(this,"_hooks");this._hooks=new Map}onHook(s,t){let i=this._hooks.get(s);i?i.push(t):this._hooks.set(s,[t])}removeHook(s,t){let i=this._hooks.get(s);if(i){let h=i.indexOf(t);h!==-1&&i.splice(h,1)}}async hook(s,...t){let i=this._hooks.get(s);if(i)for(let h of i)try{await h(...t)}catch(a){this.emit("error",new Error(`Error in hook handler for event "${s}": ${a.message}`))}}get hooks(){return this._hooks}getHooks(s){return this._hooks.get(s)}clearHooks(){this._hooks.clear()}};export{o as Eventified,l as Hookified};
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/eventified.ts","../../src/index.ts"],"sourcesContent":["export type EventListener = (...arguments_: any[]) => void;\n\nexport class Eventified {\n\t_eventListeners: Map<string, EventListener[]>;\n\t_maxListeners: number;\n\n\tconstructor() {\n\t\tthis._eventListeners = new Map();\n\t\tthis._maxListeners = 100; // Default maximum number of listeners\n\t}\n\n\tpublic maxListeners(): number {\n\t\treturn this._maxListeners;\n\t}\n\n\t// Add an event listener\n\tpublic addListener(event: string, listener: EventListener): void {\n\t\tthis.on(event, listener);\n\t}\n\n\tpublic on(event: string, listener: EventListener): void {\n\t\tif (!this._eventListeners.has(event)) {\n\t\t\tthis._eventListeners.set(event, []);\n\t\t}\n\n\t\tconst listeners = this._eventListeners.get(event);\n\n\t\tif (listeners) {\n\t\t\tif (listeners.length >= this._maxListeners) {\n\t\t\t\tconsole.warn(`MaxListenersExceededWarning: Possible event memory leak detected. ${listeners.length + 1} ${event} listeners added. Use setMaxListeners() to increase limit.`);\n\t\t\t}\n\n\t\t\tlisteners.push(listener);\n\t\t}\n\t}\n\n\t// Remove an event listener\n\tpublic removeListener(event: string, listener: EventListener): void {\n\t\tthis.off(event, listener);\n\t}\n\n\tpublic off(event: string, listener: EventListener): void {\n\t\tconst listeners = this._eventListeners.get(event) ?? [];\n\t\tconst index = listeners.indexOf(listener);\n\t\tif (index > -1) {\n\t\t\tlisteners.splice(index, 1);\n\t\t}\n\n\t\tif (listeners.length === 0) {\n\t\t\tthis._eventListeners.delete(event);\n\t\t}\n\t}\n\n\t// Emit an event\n\tpublic emit(event: string, ...arguments_: any[]): void {\n\t\tconst listeners = this._eventListeners.get(event);\n\n\t\tif (listeners && listeners.length > 0) {\n\t\t\tfor (const listener of listeners) {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n\t\t\t\tlistener(...arguments_);\n\t\t\t}\n\t\t}\n\t}\n\n\t// Get all listeners for a specific event\n\tpublic listeners(event: string): EventListener[] {\n\t\treturn this._eventListeners.get(event) ?? [];\n\t}\n\n\t// Remove all listeners for a specific event\n\tpublic removeAllListeners(event?: string): void {\n\t\tif (event) {\n\t\t\tthis._eventListeners.delete(event);\n\t\t} else {\n\t\t\tthis._eventListeners.clear();\n\t\t}\n\t}\n\n\t// Set the maximum number of listeners for a single event\n\tpublic setMaxListeners(n: number): void {\n\t\tthis._maxListeners = n;\n\t\tfor (const listeners of this._eventListeners.values()) {\n\t\t\tif (listeners.length > n) {\n\t\t\t\tlisteners.splice(n);\n\t\t\t}\n\t\t}\n\t}\n}\n","import {Eventified} from './eventified.js';\n\nexport type Hook = (...arguments_: any[]) => Promise<void> | void;\n\nexport class Hookified extends Eventified {\n\t_hooks: Map<string, Hook[]>;\n\n\tconstructor() {\n\t\tsuper();\n\t\tthis._hooks = new Map();\n\t}\n\n\t// Adds a handler function for a specific event\n\tonHook(event: string, handler: Hook) {\n\t\tconst eventHandlers = this._hooks.get(event);\n\t\tif (eventHandlers) {\n\t\t\teventHandlers.push(handler);\n\t\t} else {\n\t\t\tthis._hooks.set(event, [handler]);\n\t\t}\n\t}\n\n\t// Removes a specific handler function for a specific event\n\tremoveHook(event: string, handler: Hook) {\n\t\tconst eventHandlers = this._hooks.get(event);\n\t\tif (eventHandlers) {\n\t\t\tconst index = eventHandlers.indexOf(handler);\n\t\t\tif (index !== -1) {\n\t\t\t\teventHandlers.splice(index, 1);\n\t\t\t}\n\t\t}\n\t}\n\n\t// Triggers all handlers for a specific event with provided data\n\tasync hook<T>(event: string, ...arguments_: T[]) {\n\t\tconst eventHandlers = this._hooks.get(event);\n\t\tif (eventHandlers) {\n\t\t\tfor (const handler of eventHandlers) {\n\t\t\t\ttry {\n\t\t\t\t\t// eslint-disable-next-line no-await-in-loop\n\t\t\t\t\tawait handler(...arguments_);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tthis.emit('error', new Error(`Error in hook handler for event \"${event}\": ${(error as Error).message}`));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t// Provides read-only access to the current handlers\n\tget hooks() {\n\t\t// Creating a new map to prevent external modifications to the original map\n\t\treturn this._hooks;\n\t}\n\n\tgetHooks(event: string) {\n\t\treturn this._hooks.get(event);\n\t}\n\n\tclearHooks() {\n\t\tthis._hooks.clear();\n\t}\n}\n\nexport {Eventified, type EventListener} from './eventified.js';\n"],"mappings":"oKAEO,IAAMA,EAAN,KAAiB,CAIvB,aAAc,CAHdC,EAAA,wBACAA,EAAA,sBAGC,KAAK,gBAAkB,IAAI,IAC3B,KAAK,cAAgB,GACtB,CAEO,cAAuB,CAC7B,OAAO,KAAK,aACb,CAGO,YAAYC,EAAeC,EAA+B,CAChE,KAAK,GAAGD,EAAOC,CAAQ,CACxB,CAEO,GAAGD,EAAeC,EAA+B,CAClD,KAAK,gB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AO,EAE1B,KAAK,OAAO,IAAID,EAAO,CAACC,CAAO,CAAC,CAElC,CAGA,WAAWD,EAAeC,EAAe,CACxC,IAAMC,EAAgB,KAAK,OAAO,IAAIF,CAAK,EAC3C,GAAIE,EAAe,CAClB,IAAMC,EAAQD,EAAc,QAAQD,CAAO,EACvCE,IAAU,IACbD,EAAc,OAAOC,EAAO,CAAC,CAE/B,CACD,CAGA,MAAM,KAAQH,KAAkBI,EAAiB,CAChD,IAAMF,EAAgB,KAAK,OAAO,IAAIF,CAAK,EAC3C,GAAIE,EACH,QAAWD,KAAWC,EACrB,GAAI,CAEH,MAAMD,EAAQ,GAAGG,CAAU,CAC5B,OAASC,EAAO,CACf,KAAK,KAAK,QAAS,IAAI,MAAM,oCAAoCL,CAAK,MAAOK,EAAgB,OAAO,EAAE,CAAC,CACxG,CAGH,CAGA,IAAI,OAAQ,CAEX,OAAO,KAAK,MACb,CAEA,SAASL,EAAe,CACvB,OAAO,KAAK,OAAO,IAAIA,CAAK,CAC7B,CAEA,YAAa,CACZ,KAAK,OAAO,MAAM,CACnB,CACD","names":["Eventified","__publicField","event","listener","listeners","index","arguments_","n","Hookified","Eventified","__publicField","event","handler","eventHandlers","index","arguments_","error"]}
|
package/package.json
CHANGED
|
@@ -1,25 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hookified",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Event and Middleware Hooks",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "
|
|
7
|
-
"module": "
|
|
8
|
-
"types": "./dist/index.d.ts",
|
|
6
|
+
"main": "dist/node/index.cjs",
|
|
7
|
+
"module": "dist/node/index.js",
|
|
9
8
|
"exports": {
|
|
10
9
|
".": {
|
|
11
|
-
"
|
|
12
|
-
"
|
|
10
|
+
"import": "./dist/node/index.js",
|
|
11
|
+
"require": "./dist/node/index.cjs"
|
|
12
|
+
},
|
|
13
|
+
"./browser": {
|
|
14
|
+
"import": "./dist/browser/index.js",
|
|
15
|
+
"default": "./dist/browser/index.global.js"
|
|
13
16
|
}
|
|
14
17
|
},
|
|
15
|
-
"
|
|
16
|
-
"node": ">=20"
|
|
17
|
-
},
|
|
18
|
+
"types": "dist/node/index.d.ts",
|
|
18
19
|
"scripts": {
|
|
19
20
|
"test": "xo --fix && vitest run --coverage",
|
|
20
21
|
"test:ci": "xo && vitest run --coverage",
|
|
21
22
|
"clean": "rimraf ./dist ./coverage ./site/dist",
|
|
22
|
-
"build": "rimraf ./dist && tsup
|
|
23
|
+
"build": "rimraf ./dist && tsup",
|
|
23
24
|
"website:build": "docula build",
|
|
24
25
|
"website:serve": "docula serve",
|
|
25
26
|
"prepare": "npm run build"
|
|
@@ -56,12 +57,12 @@
|
|
|
56
57
|
},
|
|
57
58
|
"homepage": "https://github.com/jaredwray/hookified#readme",
|
|
58
59
|
"devDependencies": {
|
|
59
|
-
"@vitest/coverage-v8": "^2.1.
|
|
60
|
+
"@vitest/coverage-v8": "^2.1.3",
|
|
60
61
|
"docula": "^0.9.3",
|
|
61
62
|
"rimraf": "^6.0.1",
|
|
62
63
|
"tsup": "^8.3.0",
|
|
63
64
|
"typescript": "^5.6.3",
|
|
64
|
-
"vitest": "^2.1.
|
|
65
|
+
"vitest": "^2.1.3",
|
|
65
66
|
"xo": "^0.59.3"
|
|
66
67
|
},
|
|
67
68
|
"files": [
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|