@vnejs/observer 0.0.3 → 0.0.5

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.
@@ -0,0 +1,9 @@
1
+ import type { Subscriber, SubscriberOptions } from "./types.js";
2
+ export declare class Observer {
3
+ subscribers: Record<string, Subscriber[]>;
4
+ subscribe: (event: string, func: (...args: unknown[]) => unknown, options?: SubscriberOptions) => undefined;
5
+ emit: (e: string, ...args: unknown[]) => Promise<unknown[]>;
6
+ emitOne: (e: string, ...args: unknown[]) => Promise<unknown>;
7
+ emitReal: (e: string, ...args: unknown[]) => Promise<unknown>;
8
+ emitReals: (e: string, ...args: unknown[]) => Promise<unknown[]>;
9
+ }
package/dist/index.js ADDED
@@ -0,0 +1,21 @@
1
+ import { getFirstArrayElement, getFirstRealArrayElement, getRealArrayElements } from "./utils.js";
2
+ export class Observer {
3
+ subscribers = {};
4
+ subscribe = (event, func, options = {}) => {
5
+ if (!this.subscribers[event])
6
+ this.subscribers[event] = [];
7
+ if (typeof func !== "function")
8
+ return void console.error("Function is undefined:", event, "| func:", func);
9
+ this.subscribers[event].push({ func, options });
10
+ };
11
+ emit = async (e, ...args) => {
12
+ if (!e || !this.subscribers[e]) {
13
+ console.error("Event is undefined or no subscribers:", e, "| args:", args);
14
+ return [];
15
+ }
16
+ return (await Promise.all(this.subscribers[e].map(({ func }) => func(...args)))).filter((r, i) => !this.subscribers[e][i].options.filterFromResult);
17
+ };
18
+ emitOne = (e, ...args) => this.emit(e, ...args).then(getFirstArrayElement);
19
+ emitReal = (e, ...args) => this.emit(e, ...args).then(getFirstRealArrayElement);
20
+ emitReals = (e, ...args) => this.emit(e, ...args).then(getRealArrayElements);
21
+ }
@@ -0,0 +1,7 @@
1
+ export type SubscriberOptions = {
2
+ filterFromResult?: boolean;
3
+ };
4
+ export type Subscriber = {
5
+ func: (...args: unknown[]) => unknown;
6
+ options: SubscriberOptions;
7
+ };
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ export declare const getFirstArrayElement: <T>(arr: T[]) => T;
2
+ export declare const getFirstRealArrayElement: <T>(arr: T[]) => T;
3
+ export declare const getRealArrayElements: <T>(arr: T[]) => T[];
package/dist/utils.js ADDED
@@ -0,0 +1,3 @@
1
+ export const getFirstArrayElement = (arr) => arr[0];
2
+ export const getFirstRealArrayElement = (arr) => arr.filter(Boolean)[0];
3
+ export const getRealArrayElements = (arr) => arr.filter(Boolean);
package/package.json CHANGED
@@ -1,17 +1,25 @@
1
1
  {
2
2
  "name": "@vnejs/observer",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "",
5
- "main": "index.js",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
6
10
  "scripts": {
7
11
  "test": "echo \"Error: no test specified\" && exit 1",
12
+ "build": "rm -rf dist && tsc -p tsconfig.json",
8
13
  "publish:major:plugin": "npm run publish:major",
9
14
  "publish:minor:plugin": "npm run publish:minor",
10
15
  "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"
16
+ "publish:major": "npm run build && npm version major && npm publish --access public",
17
+ "publish:minor": "npm run build && npm version minor && npm publish --access public",
18
+ "publish:patch": "npm run build && npm version patch && npm publish --access public"
14
19
  },
15
20
  "author": "",
16
- "license": "ISC"
21
+ "license": "ISC",
22
+ "devDependencies": {
23
+ "typescript": "^6.0.3"
24
+ }
17
25
  }
package/index.js DELETED
@@ -1,28 +0,0 @@
1
- const getFirstArrayElement = (arr) => arr[0];
2
- const getFirstRealArrayElement = (arr) => arr.filter(Boolean)[0];
3
- const getRealArrayElements = (arr) => arr.filter(Boolean);
4
-
5
- export class Observer {
6
- subscribers = {};
7
-
8
- subscribe = (event, func, options = {}) => {
9
- if (!this.subscribers[event]) this.subscribers[event] = [];
10
-
11
- if (typeof func !== "function") return void console.error("Function is undefined:", event, "| func:", func);
12
-
13
- this.subscribers[event].push({ func, options });
14
- };
15
-
16
- emit = async (e, ...args) => {
17
- if (!e || !this.subscribers[e]) {
18
- console.error("Event is undefined or no subscribers:", e, "| args:", args);
19
- return [];
20
- }
21
-
22
- return (await Promise.all(this.subscribers[e].map(({ func }) => func(...args)))).filter((r, i) => !this.subscribers[e][i].options.filterFromResult);
23
- };
24
-
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);
28
- }