@vnejs/observer 0.0.1 → 0.0.3

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 +11 -10
  2. package/package.json +8 -2
package/index.js CHANGED
@@ -1,27 +1,28 @@
1
- const getFirstArrayElement = ([oneResult]) => oneResult;
1
+ const getFirstArrayElement = (arr) => arr[0];
2
+ const getFirstRealArrayElement = (arr) => arr.filter(Boolean)[0];
3
+ const getRealArrayElements = (arr) => arr.filter(Boolean);
2
4
 
3
5
  export class Observer {
4
6
  subscribers = {};
5
7
 
6
- subscribe = (event, func) => {
8
+ subscribe = (event, func, options = {}) => {
7
9
  if (!this.subscribers[event]) this.subscribers[event] = [];
8
10
 
9
- if (typeof func !== "function") {
10
- console.error("Function is undefined:", event, "| func:", func);
11
- return;
12
- }
11
+ if (typeof func !== "function") return void console.error("Function is undefined:", event, "| func:", func);
13
12
 
14
- this.subscribers[event].push(func);
13
+ this.subscribers[event].push({ func, options });
15
14
  };
16
15
 
17
- notify = (e, ...args) => {
16
+ emit = async (e, ...args) => {
18
17
  if (!e || !this.subscribers[e]) {
19
18
  console.error("Event is undefined or no subscribers:", e, "| args:", args);
20
19
  return [];
21
20
  }
22
21
 
23
- return Promise.all(this.subscribers[e].map((func) => func(...args)));
22
+ return (await Promise.all(this.subscribers[e].map(({ func }) => func(...args)))).filter((r, i) => !this.subscribers[e][i].options.filterFromResult);
24
23
  };
25
24
 
26
- notifyOne = (e, ...args) => this.notify(e, ...args).then(getFirstArrayElement);
25
+ emitOne = (e, ...args) => this.emit(e, ...args).then(getFirstArrayElement);
26
+ emitReal = (e, ...args) => this.emit(e, ...args).then(getFirstRealArrayElement);
27
+ emitReals = (e, ...args) => this.emit(e, ...args).then(getRealArrayElements);
27
28
  }
package/package.json CHANGED
@@ -1,10 +1,16 @@
1
1
  {
2
2
  "name": "@vnejs/observer",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
7
+ "test": "echo \"Error: no test specified\" && exit 1",
8
+ "publish:major:plugin": "npm run publish:major",
9
+ "publish:minor:plugin": "npm run publish:minor",
10
+ "publish:patch:plugin": "npm run publish:patch",
11
+ "publish:major": "npm version major && npm publish --access public",
12
+ "publish:minor": "npm version minor && npm publish --access public",
13
+ "publish:patch": "npm version patch && npm publish --access public"
8
14
  },
9
15
  "author": "",
10
16
  "license": "ISC"