@ankhzet/eventual 1.1.0 → 1.2.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/CHANGELOG.md +6 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @public
|
|
3
|
+
* Supported event name types.
|
|
4
|
+
*/
|
|
5
|
+
export declare type EventName = string | number | symbol;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @public
|
|
9
|
+
* Event source/consumer implementation.s
|
|
10
|
+
*/
|
|
11
|
+
export declare class Eventual implements IEventSource {
|
|
12
|
+
#private;
|
|
13
|
+
dispatchEvent<D = never, N extends EventName = EventName>(event: N, data?: D): void;
|
|
14
|
+
handleEvent<D, N extends EventName>(type: N, handler: IEventHandler<D>, once?: boolean): () => void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @public
|
|
19
|
+
* Event handler callback.
|
|
20
|
+
*/
|
|
21
|
+
export declare type IEventHandler<Data = never> = (event: Data) => void;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @public
|
|
25
|
+
* Event source/consumer interface.
|
|
26
|
+
*/
|
|
27
|
+
export declare interface IEventSource {
|
|
28
|
+
dispatchEvent<N extends EventName>(event: N): void;
|
|
29
|
+
dispatchEvent<D, N extends EventName = EventName>(event: N, data: D): void;
|
|
30
|
+
handleEvent<D, N extends EventName>(type: N, handler: IEventHandler<D>, once?: boolean): () => void;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export { }
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//#region src/Eventual.ts
|
|
2
|
+
var e = class {
|
|
3
|
+
#e;
|
|
4
|
+
dispatchEvent(e, t) {
|
|
5
|
+
let n = this.#e?.[e];
|
|
6
|
+
if (n) for (let e of n) e(t);
|
|
7
|
+
}
|
|
8
|
+
handleEvent(e, t, n) {
|
|
9
|
+
let r, i = n ? (e) => {
|
|
10
|
+
r && (r(), t(e));
|
|
11
|
+
} : t, a = this.#e?.[e];
|
|
12
|
+
return a ? (a = a.slice(), a.push(i), this.#e[e] = a) : (a = [i], (this.#e ||= Object.create(null))[e] = a), r = () => {
|
|
13
|
+
r = void 0;
|
|
14
|
+
let t = this.#e?.[e], n = t?.indexOf(i);
|
|
15
|
+
if (t && n >= 0) if (t.length > 1) {
|
|
16
|
+
let r = t.slice(n + 1);
|
|
17
|
+
this.#e[e] = n > 0 ? t.slice(0, n).concat(r) : r;
|
|
18
|
+
} else delete this.#e[e];
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
//#endregion
|
|
23
|
+
export { e as Eventual };
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["#handlers"],"sources":["../src/Eventual.ts"],"sourcesContent":["import type { EventName, IEventHandler, IEventSource } from './types';\n\n/**\n * @public\n * Event source/consumer implementation.s\n */\nexport class Eventual implements IEventSource {\n #handlers?: Record<EventName, IEventHandler<any>[]>;\n\n public dispatchEvent<D = never, N extends EventName = EventName>(event: N, data?: D): void {\n const bucket = this.#handlers?.[event];\n\n if (bucket) {\n for (const handler of bucket) {\n handler(data);\n }\n }\n }\n\n public handleEvent<D, N extends EventName>(type: N, handler: IEventHandler<D>, once?: boolean): () => void {\n let off: (() => void) | undefined;\n const actual = once\n ? (data: D) => {\n if (!off) {\n return;\n }\n\n off();\n handler(data);\n }\n : handler;\n\n let bucket: IEventHandler<D>[] | undefined = this.#handlers?.[type];\n\n if (bucket) {\n bucket = bucket.slice();\n bucket.push(actual);\n this.#handlers![type] = bucket;\n } else {\n bucket = [actual];\n (this.#handlers || (this.#handlers = Object.create(null)))[type] = bucket;\n }\n\n return (off = () => {\n off = void 0;\n const bucket: IEventHandler<D>[] | undefined = this.#handlers?.[type];\n const idx = bucket?.indexOf(actual)!;\n\n if (!(bucket && idx >= 0)) {\n return;\n }\n\n if (bucket.length > 1) {\n const rest = bucket.slice(idx + 1);\n\n this.#handlers![type] = (idx > 0)\n ? bucket.slice(0, idx).concat(rest)\n : rest\n ;\n } else {\n delete this.#handlers![type];\n }\n });\n }\n}\n"],"mappings":";AAMA,IAAa,IAAb,MAA8C;CAC1C;CAEA,cAAiE,GAAU,GAAgB;EACvF,IAAM,IAAS,KAAKA,KAAY;EAEhC,IAAI,GACA,KAAK,IAAM,KAAW,GAClB,EAAQ,CAAI;CAGxB;CAEA,YAA2C,GAAS,GAA2B,GAA4B;EACvG,IAAI,GACE,IAAS,KACR,MAAY;GACJ,MAIL,EAAI,GACJ,EAAQ,CAAI;EAChB,IACA,GAEF,IAAyC,KAAKA,KAAY;EAW9D,OATI,KACA,IAAS,EAAO,MAAM,GACtB,EAAO,KAAK,CAAM,GAClB,KAAKA,GAAW,KAAQ,MAExB,IAAS,CAAC,CAAM,GAChB,CAAC,AAAmB,KAAKA,OAAY,OAAO,OAAO,IAAI,GAAI,KAAQ,IAG/D,UAAY;GAChB,IAAM,KAAK;GACX,IAAM,IAAyC,KAAKA,KAAY,IAC1D,IAAM,GAAQ,QAAQ,CAAM;GAE5B,SAAU,KAAO,GAIvB,IAAI,EAAO,SAAS,GAAG;IACnB,IAAM,IAAO,EAAO,MAAM,IAAM,CAAC;IAEjC,KAAKA,GAAW,KAAS,IAAM,IACzB,EAAO,MAAM,GAAG,CAAG,EAAE,OAAO,CAAI,IAChC;GAEV,OACI,OAAO,KAAKA,GAAW;EAE/B;CACJ;AACJ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ankhzet/eventual",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"author": "AnkhZet ankhzet@gmail.com",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"files": [
|
|
9
9
|
"package.json",
|
|
10
10
|
"src/**/*.ts",
|
|
11
|
-
"
|
|
11
|
+
"dist",
|
|
12
12
|
"*.md"
|
|
13
13
|
],
|
|
14
14
|
"exports": {
|