@vanillaspa/event-bus 1.0.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/LICENSE +24 -0
- package/README.md +48 -0
- package/eventbus.png +0 -0
- package/index.js +54 -0
- package/package.json +27 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
|
+
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
+
distribute this software, either in source code form or as a compiled
|
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
+
means.
|
|
7
|
+
|
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
+
of this software dedicate any and all copyright interest in the
|
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
|
11
|
+
of the public at large and to the detriment of our heirs and
|
|
12
|
+
successors. We intend this dedication to be an overt act of
|
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
+
software under copyright law.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
For more information, please refer to <https://unlicense.org>
|
package/README.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
## Getting started
|
|
2
|
+
|
|
3
|
+
To install the event-bus into your project, simply run
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @vanillaspa/event-bus
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## How-To
|
|
10
|
+
|
|
11
|
+
Now integrate the event-bus into your app:
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
<script type="module">
|
|
15
|
+
import * as eventbus from '@vanillaspa/event-bus';
|
|
16
|
+
window.eventbus = eventbus;
|
|
17
|
+
</script>
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
This is only a recommendation. You can use it however you like. With the `eventbus` object attached to the `window` object, you have access to the event-bus in your WebComponents.
|
|
21
|
+
|
|
22
|
+
Then simply use `eventbus.addEventListener(type, listener)` and `eventbus.dispatchEvent(event)` in your WebComponents.
|
|
23
|
+
|
|
24
|
+
You are not bound to event bubbling or capturing, which are the standard event propagation mechanisms, but now you can send events even among any objects!
|
|
25
|
+
|
|
26
|
+
```javascript
|
|
27
|
+
eventbus.addEventListener("click", () => {console.log("click")}, window);
|
|
28
|
+
eventbus.dispatchEvent(new Event("click"), window);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## EventBus API architecture
|
|
32
|
+
|
|
33
|
+

|
|
34
|
+
|
|
35
|
+
```mermaid
|
|
36
|
+
classDiagram
|
|
37
|
+
EventTarget <|-- EventBus
|
|
38
|
+
EventTarget: +addEventListener()
|
|
39
|
+
EventTarget: +removeEventListener()
|
|
40
|
+
EventTarget: +dispatchEvent()
|
|
41
|
+
EventBus: -WeakMap listeners
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
That's it. Check it out! KISS
|
|
45
|
+
|
|
46
|
+
## Contribute
|
|
47
|
+
|
|
48
|
+
Please give your warm feedback.
|
package/eventbus.png
ADDED
|
Binary file
|
package/index.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export const name = "eventbus"; // The module name, can be made globally available via the window object, which should be on top.
|
|
2
|
+
const listeners = new WeakMap(); // WeakMap to store listeners per object. Keep it private unless you know what you do.
|
|
3
|
+
// Should auto-cleanup listeners when object is GC'd
|
|
4
|
+
// This module implements EventTarget.
|
|
5
|
+
export function addEventListener(type, listener, context = undefined) { // (TODO check integration into options)
|
|
6
|
+
if (context && typeof context === 'object') { // context is well defined, should be a WebComponent
|
|
7
|
+
if (!listeners.has(context)) { // context is yet unknown to the listeners
|
|
8
|
+
listeners.set(context, new Map()); // will be stored here
|
|
9
|
+
}
|
|
10
|
+
const contextListeners = listeners.get(context);
|
|
11
|
+
if (!contextListeners.has(type)) { // type is not yet registered
|
|
12
|
+
contextListeners.set(type, []);
|
|
13
|
+
} // next push the handler to the WeakMap for the given context
|
|
14
|
+
contextListeners.get(type).push(listener);
|
|
15
|
+
} else { // default
|
|
16
|
+
if (context) { // illegal context
|
|
17
|
+
throw new Error("Syntax error.");
|
|
18
|
+
}
|
|
19
|
+
window.addEventListener(type, listener);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function removeEventListener(type, listener, context = undefined) {
|
|
24
|
+
if (context && typeof context === 'object') {
|
|
25
|
+
const contextListeners = listeners.get(context);
|
|
26
|
+
if (contextListeners && contextListeners.has(type)) {
|
|
27
|
+
const handlers = contextListeners.get(type);
|
|
28
|
+
const index = handlers.indexOf(listener);
|
|
29
|
+
if (index > -1) {
|
|
30
|
+
handlers.splice(index, 1);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
} else { // default
|
|
34
|
+
window.removeEventListener(type);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function dispatchEvent(event, context = undefined) {
|
|
39
|
+
if (!context) {
|
|
40
|
+
if (event instanceof CustomEvent) {
|
|
41
|
+
context = event.detail.target;
|
|
42
|
+
} else {
|
|
43
|
+
context = event.target;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (context && listeners.has(context)) {
|
|
47
|
+
const contextListeners = listeners.get(context);
|
|
48
|
+
if (contextListeners.has(event.type)) { // Call context-specific listeners
|
|
49
|
+
contextListeners.get(event.type).forEach(handler => handler(event));
|
|
50
|
+
}
|
|
51
|
+
} else { // default
|
|
52
|
+
window.dispatchEvent(event);
|
|
53
|
+
}
|
|
54
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"author": "Robert Meissner <rome@live.com>",
|
|
3
|
+
"bugs": {
|
|
4
|
+
"url": "https://github.com/vanillaspa/event-bus/issues"
|
|
5
|
+
},
|
|
6
|
+
"description": "An Event Bus written in <50 LOC vanilla JS. You can use it with standard WebComponents.",
|
|
7
|
+
"homepage": "https://github.com/vanillaspa/event-bus#readme",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"EventBus",
|
|
10
|
+
"JavaScript",
|
|
11
|
+
"Module",
|
|
12
|
+
"Vanilla",
|
|
13
|
+
"SPA"
|
|
14
|
+
],
|
|
15
|
+
"license": "Unlicense",
|
|
16
|
+
"main": "index.js",
|
|
17
|
+
"name": "@vanillaspa/event-bus",
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/vanillaspa/event-bus.git"
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"version": "1.0.0"
|
|
27
|
+
}
|