@vnejs/observer 0.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.
Files changed (2) hide show
  1. package/index.js +27 -0
  2. package/package.json +11 -0
package/index.js ADDED
@@ -0,0 +1,27 @@
1
+ const getFirstArrayElement = ([oneResult]) => oneResult;
2
+
3
+ export class Observer {
4
+ subscribers = {};
5
+
6
+ subscribe = (event, func) => {
7
+ if (!this.subscribers[event]) this.subscribers[event] = [];
8
+
9
+ if (typeof func !== "function") {
10
+ console.error("Function is undefined:", event, "| func:", func);
11
+ return;
12
+ }
13
+
14
+ this.subscribers[event].push(func);
15
+ };
16
+
17
+ notify = (e, ...args) => {
18
+ if (!e || !this.subscribers[e]) {
19
+ console.error("Event is undefined or no subscribers:", e, "| args:", args);
20
+ return [];
21
+ }
22
+
23
+ return Promise.all(this.subscribers[e].map((func) => func(...args)));
24
+ };
25
+
26
+ notifyOne = (e, ...args) => this.notify(e, ...args).then(getFirstArrayElement);
27
+ }
package/package.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "@vnejs/observer",
3
+ "version": "0.0.1",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "author": "",
10
+ "license": "ISC"
11
+ }