@stacksjs/events 0.57.4 โ 0.58.19
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 +5 -5
- package/dist/index.js +63 -0
- package/package.json +19 -19
- package/LICENSE.md +0 -21
- package/dist/index.d.ts +0 -62
- package/dist/index.mjs +0 -71
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ Functional event emitting.
|
|
|
9
9
|
## ๐ค Usage
|
|
10
10
|
|
|
11
11
|
```bash
|
|
12
|
-
|
|
12
|
+
bun install -d @stacksjs/events
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
Now, you can use it in your project:
|
|
@@ -35,12 +35,12 @@ listen('foo', onFoo) // listen
|
|
|
35
35
|
off('foo', onFoo) // unlisten
|
|
36
36
|
```
|
|
37
37
|
|
|
38
|
-
To view the full documentation, please visit [https://stacksjs.
|
|
38
|
+
To view the full documentation, please visit [https://stacksjs.org/events](https://stacksjs.org/events).
|
|
39
39
|
|
|
40
40
|
## ๐งช Testing
|
|
41
41
|
|
|
42
42
|
```bash
|
|
43
|
-
|
|
43
|
+
bun test
|
|
44
44
|
```
|
|
45
45
|
|
|
46
46
|
## ๐ Changelog
|
|
@@ -59,7 +59,7 @@ For help, discussion about best practices, or any other conversation that would
|
|
|
59
59
|
|
|
60
60
|
For casual chit-chat with others using this package:
|
|
61
61
|
|
|
62
|
-
[Join the Stacks Discord Server](https://discord.
|
|
62
|
+
[Join the Stacks Discord Server](https://discord.gg/stacksjs)
|
|
63
63
|
|
|
64
64
|
## ๐๐ผ Credits
|
|
65
65
|
|
|
@@ -73,4 +73,4 @@ Many thanks to the following core technologies & people who have contributed to
|
|
|
73
73
|
|
|
74
74
|
The MIT License (MIT). Please see [LICENSE](https://github.com/stacksjs/stacks/tree/main/LICENSE.md) for more information.
|
|
75
75
|
|
|
76
|
-
Made with
|
|
76
|
+
Made with ๐
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/index.ts
|
|
3
|
+
function mitt(all) {
|
|
4
|
+
all = all || new Map;
|
|
5
|
+
return {
|
|
6
|
+
all,
|
|
7
|
+
on(type, handler) {
|
|
8
|
+
const handlers = all.get(type);
|
|
9
|
+
if (handlers)
|
|
10
|
+
handlers.push(handler);
|
|
11
|
+
else
|
|
12
|
+
all.set(type, [handler]);
|
|
13
|
+
},
|
|
14
|
+
off(type, handler) {
|
|
15
|
+
const handlers = all.get(type);
|
|
16
|
+
if (handlers) {
|
|
17
|
+
if (handler)
|
|
18
|
+
handlers.splice(handlers.indexOf(handler) >>> 0, 1);
|
|
19
|
+
else
|
|
20
|
+
all.set(type, []);
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
emit(type, evt) {
|
|
24
|
+
let handlers = all.get(type);
|
|
25
|
+
if (handlers) {
|
|
26
|
+
handlers.slice().map((handler) => {
|
|
27
|
+
if (evt)
|
|
28
|
+
return handler(evt);
|
|
29
|
+
console.error("No event provided");
|
|
30
|
+
return "No event provided";
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
handlers = all.get("*");
|
|
34
|
+
if (handlers) {
|
|
35
|
+
handlers.slice().map((handler) => {
|
|
36
|
+
if (evt)
|
|
37
|
+
return handler(type, evt);
|
|
38
|
+
console.error("No event provided");
|
|
39
|
+
return "No event provided";
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
var events = mitt;
|
|
46
|
+
var emitter = events();
|
|
47
|
+
var useEvent = emitter.emit.bind(emitter);
|
|
48
|
+
var useEvents = events();
|
|
49
|
+
var dispatch = emitter.emit.bind(emitter);
|
|
50
|
+
var listen = emitter.on.bind(emitter);
|
|
51
|
+
var off = emitter.off.bind(emitter);
|
|
52
|
+
var all = emitter.all;
|
|
53
|
+
export {
|
|
54
|
+
useEvents,
|
|
55
|
+
useEvent,
|
|
56
|
+
off,
|
|
57
|
+
mitt,
|
|
58
|
+
listen,
|
|
59
|
+
events,
|
|
60
|
+
dispatch,
|
|
61
|
+
mitt as default,
|
|
62
|
+
all
|
|
63
|
+
};
|
package/package.json
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/events",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
5
|
-
"packageManager": "pnpm@8.6.6",
|
|
4
|
+
"version": "0.58.19",
|
|
6
5
|
"description": "Functional event emitting.",
|
|
7
6
|
"author": "Chris Breuer",
|
|
8
7
|
"license": "MIT",
|
|
9
8
|
"funding": "https://github.com/sponsors/chrisbbreuer",
|
|
10
|
-
"homepage": "https://github.com/stacksjs/stacks/tree/main
|
|
9
|
+
"homepage": "https://github.com/stacksjs/stacks/tree/main/storage/framework/core/src/events#readme",
|
|
11
10
|
"repository": {
|
|
12
11
|
"type": "git",
|
|
13
12
|
"url": "git+https://github.com/stacksjs/stacks.git",
|
|
14
|
-
"directory": "
|
|
13
|
+
"directory": "./storage/framework/core/src/events"
|
|
15
14
|
},
|
|
16
15
|
"bugs": {
|
|
17
16
|
"url": "https://github.com/stacksjs/stacks/issues"
|
|
@@ -25,28 +24,29 @@
|
|
|
25
24
|
],
|
|
26
25
|
"exports": {
|
|
27
26
|
".": {
|
|
28
|
-
"
|
|
29
|
-
"import": "./dist/index.
|
|
27
|
+
"bun": "./src/index.ts",
|
|
28
|
+
"import": "./dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./*": {
|
|
31
|
+
"bun": "./src/*",
|
|
32
|
+
"import": "./dist/*"
|
|
30
33
|
}
|
|
31
34
|
},
|
|
32
|
-
"module": "dist/index.
|
|
35
|
+
"module": "dist/index.js",
|
|
33
36
|
"types": "dist/index.d.ts",
|
|
34
37
|
"contributors": [
|
|
35
|
-
"Chris Breuer <chris@
|
|
38
|
+
"Chris Breuer <chris@stacksjs.org>"
|
|
36
39
|
],
|
|
37
40
|
"files": [
|
|
38
|
-
"
|
|
39
|
-
"
|
|
41
|
+
"README.md",
|
|
42
|
+
"dist"
|
|
40
43
|
],
|
|
41
|
-
"
|
|
42
|
-
"
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "bun --bun build.ts",
|
|
46
|
+
"typecheck": "bun --bun tsc --noEmit",
|
|
47
|
+
"prepublishOnly": "bun --bun run build"
|
|
43
48
|
},
|
|
44
49
|
"devDependencies": {
|
|
45
|
-
"@stacksjs/development": "
|
|
46
|
-
},
|
|
47
|
-
"scripts": {
|
|
48
|
-
"build": "unbuild",
|
|
49
|
-
"dev": "unbuild --stub",
|
|
50
|
-
"typecheck": "tsc --noEmit"
|
|
50
|
+
"@stacksjs/development": "workspace:*"
|
|
51
51
|
}
|
|
52
|
-
}
|
|
52
|
+
}
|
package/LICENSE.md
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
# MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022 Open Web Foundation
|
|
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/dist/index.d.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
export type EventType = string | symbol;
|
|
2
|
-
export type Handler<T = unknown> = (event: T) => void;
|
|
3
|
-
export type WildcardHandler<T = Record<string, unknown>> = (type: keyof T, event: T[keyof T]) => void;
|
|
4
|
-
export type EventHandlerList<T = unknown> = Array<Handler<T>>;
|
|
5
|
-
export type WildCardEventHandlerList<T = Record<string, unknown>> = Array<WildcardHandler<T>>;
|
|
6
|
-
export type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<keyof Events | '*', EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>>;
|
|
7
|
-
export interface Emitter<Events extends Record<EventType, unknown>> {
|
|
8
|
-
all: EventHandlerMap<Events>;
|
|
9
|
-
on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void;
|
|
10
|
-
on(type: '*', handler: WildcardHandler<Events>): void;
|
|
11
|
-
off<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>): void;
|
|
12
|
-
off(type: '*', handler: WildcardHandler<Events>): void;
|
|
13
|
-
emit<Key extends keyof Events>(type: Key, event: Events[Key]): void;
|
|
14
|
-
emit<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never): void;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Mitt: Tiny (~200b) functional event emitter / pubsub.
|
|
18
|
-
* @name mitt
|
|
19
|
-
* @returns {Mitt}
|
|
20
|
-
*/
|
|
21
|
-
export default function mitt<Events extends Record<EventType, unknown>>(all?: EventHandlerMap<Events>): Emitter<Events>;
|
|
22
|
-
/**
|
|
23
|
-
* This module provides a simple, yet powerful, event bus for the application.
|
|
24
|
-
*
|
|
25
|
-
* TODO: https://vitejs.dev/guide/api-plugin.html#typescript-for-custom-events
|
|
26
|
-
*
|
|
27
|
-
* @example To fire an event, you may use any of the following approaches:
|
|
28
|
-
* ```ts
|
|
29
|
-
* useEvent('user:registered', { name: 'Chris'})
|
|
30
|
-
* dispatch('user:registered', { name: 'Chris'})
|
|
31
|
-
*
|
|
32
|
-
* // alternatively, you may use the following:
|
|
33
|
-
* events.emit('user:registered', { name: 'Chris'})
|
|
34
|
-
* useEvents.emit('user:registered', { name: 'Chris'})
|
|
35
|
-
* ````
|
|
36
|
-
|
|
37
|
-
* @example To capture an event, you may use any of the following approaches:
|
|
38
|
-
* ```ts
|
|
39
|
-
* useListen('user:registered', (user) => console.log(user))
|
|
40
|
-
* listen('user:registered', (user) => console.log(user))
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
declare const events: typeof mitt;
|
|
44
|
-
declare const useEvent: {
|
|
45
|
-
<Key extends EventType>(type: Key, event: Record<EventType, unknown>[Key]): void;
|
|
46
|
-
<Key_1 extends EventType>(type: undefined extends Record<EventType, unknown>[Key_1] ? Key_1 : never): void;
|
|
47
|
-
};
|
|
48
|
-
declare const useEvents: Emitter<Record<EventType, unknown>>;
|
|
49
|
-
declare const dispatch: {
|
|
50
|
-
<Key extends EventType>(type: Key, event: Record<EventType, unknown>[Key]): void;
|
|
51
|
-
<Key_1 extends EventType>(type: undefined extends Record<EventType, unknown>[Key_1] ? Key_1 : never): void;
|
|
52
|
-
};
|
|
53
|
-
declare const listen: {
|
|
54
|
-
<Key extends EventType>(type: Key, handler: Handler<Record<EventType, unknown>[Key]>): void;
|
|
55
|
-
(type: '*', handler: WildcardHandler<Record<EventType, unknown>>): void;
|
|
56
|
-
};
|
|
57
|
-
declare const off: {
|
|
58
|
-
<Key extends EventType>(type: Key, handler?: Handler<Record<EventType, unknown>[Key]> | undefined): void;
|
|
59
|
-
(type: '*', handler: WildcardHandler<Record<EventType, unknown>>): void;
|
|
60
|
-
};
|
|
61
|
-
declare const all: EventHandlerMap<Record<EventType, unknown>>;
|
|
62
|
-
export { useEvent, useEvents, dispatch, listen, all, off, events, mitt };
|
package/dist/index.mjs
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
export default function mitt(all2) {
|
|
2
|
-
all2 = all2 || /* @__PURE__ */ new Map();
|
|
3
|
-
return {
|
|
4
|
-
/**
|
|
5
|
-
* A Map of event names to registered handler functions.
|
|
6
|
-
*/
|
|
7
|
-
all: all2,
|
|
8
|
-
/**
|
|
9
|
-
* Register an event handler for the given type.
|
|
10
|
-
* @param {string|symbol} type Type of event to listen for, or `'*'` for all events
|
|
11
|
-
* @param {Function} handler Function to call in response to given event
|
|
12
|
-
* @memberOf mitt
|
|
13
|
-
*/
|
|
14
|
-
on(type, handler) {
|
|
15
|
-
const handlers = all2.get(type);
|
|
16
|
-
if (handlers)
|
|
17
|
-
handlers.push(handler);
|
|
18
|
-
else
|
|
19
|
-
all2.set(type, [handler]);
|
|
20
|
-
},
|
|
21
|
-
/**
|
|
22
|
-
* Remove an event handler for the given type.
|
|
23
|
-
* If `handler` is omitted, all handlers of the given type are removed.
|
|
24
|
-
* @param {string|symbol} type Type of event to unregister `handler` from (`'*'` to remove a wildcard handler)
|
|
25
|
-
* @param {Function} [handler] Handler function to remove
|
|
26
|
-
* @memberOf mitt
|
|
27
|
-
*/
|
|
28
|
-
off(type, handler) {
|
|
29
|
-
const handlers = all2.get(type);
|
|
30
|
-
if (handlers) {
|
|
31
|
-
if (handler)
|
|
32
|
-
handlers.splice(handlers.indexOf(handler) >>> 0, 1);
|
|
33
|
-
else
|
|
34
|
-
all2.set(type, []);
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
/**
|
|
38
|
-
* Invoke all handlers for the given type.
|
|
39
|
-
* If present, `'*'` handlers are invoked after type-matched handlers.
|
|
40
|
-
*
|
|
41
|
-
* Note: Manually firing '*' handlers is not supported.
|
|
42
|
-
*
|
|
43
|
-
* @param {string|symbol} type The event type to invoke
|
|
44
|
-
* @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler
|
|
45
|
-
* @memberOf mitt
|
|
46
|
-
*/
|
|
47
|
-
emit(type, evt) {
|
|
48
|
-
let handlers = all2.get(type);
|
|
49
|
-
if (handlers) {
|
|
50
|
-
handlers.slice().map((handler) => {
|
|
51
|
-
return handler(evt);
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
handlers = all2.get("*");
|
|
55
|
-
if (handlers) {
|
|
56
|
-
handlers.slice().map((handler) => {
|
|
57
|
-
return handler(type, evt);
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
const events = mitt;
|
|
64
|
-
const emitter = events();
|
|
65
|
-
const useEvent = emitter.emit;
|
|
66
|
-
const useEvents = events();
|
|
67
|
-
const dispatch = emitter.emit;
|
|
68
|
-
const listen = emitter.on;
|
|
69
|
-
const off = emitter.off;
|
|
70
|
-
const all = emitter.all;
|
|
71
|
-
export { useEvent, useEvents, dispatch, listen, all, off, events, mitt };
|