@alwatr/signal 2.0.5 → 3.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/CHANGELOG.md +230 -174
- package/README.md +70 -3
- package/dist/logger.d.ts +2 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/main.cjs +3 -0
- package/dist/main.cjs.map +7 -0
- package/dist/main.d.ts +3 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.mjs +3 -0
- package/dist/main.mjs.map +7 -0
- package/dist/signal.d.ts +17 -0
- package/dist/signal.d.ts.map +1 -0
- package/dist/trigger.d.ts +16 -0
- package/dist/trigger.d.ts.map +1 -0
- package/package.json +46 -15
- package/context.d.ts +0 -31
- package/context.d.ts.map +0 -1
- package/context.js +0 -40
- package/context.js.map +0 -1
- package/index.d.ts +0 -7
- package/index.d.ts.map +0 -1
- package/index.js +0 -6
- package/index.js.map +0 -1
- package/multithread-context.d.ts +0 -21
- package/multithread-context.d.ts.map +0 -1
- package/multithread-context.js +0 -50
- package/multithread-context.js.map +0 -1
- package/observable.d.ts +0 -47
- package/observable.d.ts.map +0 -1
- package/observable.js +0 -127
- package/observable.js.map +0 -1
- package/signal.d.ts +0 -19
- package/signal.d.ts.map +0 -1
- package/signal.js +0 -23
- package/signal.js.map +0 -1
- package/simple-signal.d.ts +0 -19
- package/simple-signal.d.ts.map +0 -1
- package/simple-signal.js +0 -23
- package/simple-signal.js.map +0 -1
- package/type.d.ts +0 -35
- package/type.d.ts.map +0 -1
- package/type.js +0 -2
- package/type.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,5 +1,72 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @alwatr/signal
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A simple and efficient TypeScript library for event-driven communication using signals.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
* **Lightweight and focused:** Provides a streamlined implementation of the signal pattern for event handling.
|
|
8
|
+
* **TypeScript Support:** Written in TypeScript with full type definitions for improved code quality and developer experience.
|
|
9
|
+
* **Easy to use:** Intuitive API for emitting and subscribing to events with strongly-typed messages.
|
|
10
|
+
* **Asynchronous Support:** Handle asynchronous events with Promise-based `untilNewNotify` method.
|
|
11
|
+
* **Built-in Logging:** Integrated logging for debugging and monitoring.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @alwatr/signal
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import {AlwatrSignal} from '@alwatr/signal';
|
|
23
|
+
|
|
24
|
+
// Create a new signal
|
|
25
|
+
const mySignal = new AlwatrSignal<{message: string}>({name: 'my-signal'});
|
|
26
|
+
|
|
27
|
+
// Subscribe to the signal
|
|
28
|
+
const subscription = mySignal.subscribe((message) => {
|
|
29
|
+
console.log('Received message:', message);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// Emit an event
|
|
33
|
+
mySignal.notify({message: 'Hello, world!'});
|
|
34
|
+
|
|
35
|
+
// Wait for the next event asynchronously
|
|
36
|
+
const nextMessage = await mySignal.untilNewNotify();
|
|
37
|
+
console.log('Next message:', nextMessage);
|
|
38
|
+
|
|
39
|
+
// Unsubscribe when done
|
|
40
|
+
subscription.unsubscribe();
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## API
|
|
44
|
+
|
|
45
|
+
### `AlwatrSignal`
|
|
46
|
+
|
|
47
|
+
* **`constructor(config: {name: string; loggerPrefix?: string})`:** Creates a new `AlwatrSignal` instance.
|
|
48
|
+
* `config.name`: The name of the signal (used for logging).
|
|
49
|
+
* `config.loggerPrefix`: Optional prefix for log messages.
|
|
50
|
+
|
|
51
|
+
* **`subscribe(listenerCallback: ListenerCallback<this, T>, options: SubscribeOptions = {}): SubscribeResult`:** (Inherited from `AlwatrObservable`) Subscribes to the signal.
|
|
52
|
+
* `listenerCallback`: The function to be called when an event is emitted.
|
|
53
|
+
* `options`:
|
|
54
|
+
* `once`: If `true`, the listener will be automatically unsubscribed after the first event.
|
|
55
|
+
* `priority`: If `true`, the listener will be executed before other listeners.
|
|
56
|
+
* `receivePrevious`: If `true`, the listener will be immediately called with the last emitted event (if available).
|
|
57
|
+
* `disabled`: If `true`, the listener will not be executed.
|
|
58
|
+
|
|
59
|
+
* **`unsubscribe(listenerCallback: ListenerCallback<this, T>)`:** (Inherited from `AlwatrObservable`) Unsubscribes a listener from the signal.
|
|
60
|
+
|
|
61
|
+
* **`notify(message: T)`:** Emits an event to all subscribers.
|
|
62
|
+
* `message`: The data to be sent to the subscribers.
|
|
63
|
+
|
|
64
|
+
* **`untilNewNotify()`:** Returns a Promise that resolves with the next emitted event.
|
|
65
|
+
|
|
66
|
+
## Contributing
|
|
67
|
+
|
|
68
|
+
Contributions are welcome! Please read the contributing guidelines before submitting a pull request.
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|
package/dist/logger.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,MAAM,uCAAuD,CAAC"}
|
package/dist/main.cjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/* @alwatr/signal v3.0.0 */
|
|
2
|
+
"use strict";var l=Object.defineProperty;var u=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var g=(e,r)=>{for(var o in r)l(e,o,{get:r[o],enumerable:!0})},d=(e,r,o,p)=>{if(r&&typeof r=="object"||typeof r=="function")for(let t of b(r))!f.call(e,t)&&t!==o&&l(e,t,{get:()=>r[t],enumerable:!(p=u(r,t))||p.enumerable});return e};var w=e=>d(l({},"__esModule",{value:!0}),e);var v={};g(v,{AlwatrSignal:()=>s,AlwatrTrigger:()=>a});module.exports=w(v);var n=require("@alwatr/logger"),i=(0,n.definePackage)("@alwatr/signal","3.0.0");var m=require("@alwatr/observable");i.logModule?.("trigger");var a=class extends m.AlwatrObservable{constructor(r){r.loggerPrefix??(r.loggerPrefix="signal"),super(r)}notify(){this.notify_({})}async untilTriggered(){await super.untilNewNotify_()}};var y=require("@alwatr/observable");i.logModule?.("signal");var s=class extends y.AlwatrObservable{constructor(r){r.loggerPrefix??(r.loggerPrefix="signal"),super(r)}notify(r){this.notify_(r)}untilNewNotify(){return super.untilNewNotify_()}};0&&(module.exports={AlwatrSignal,AlwatrTrigger});
|
|
3
|
+
//# sourceMappingURL=main.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/main.ts", "../src/logger.ts", "../src/trigger.ts", "../src/signal.ts"],
|
|
4
|
+
"sourcesContent": ["export * from './trigger.js';\nexport * from './signal.js';\n", "import {definePackage} from '@alwatr/logger';\n\nimport type {} from '@alwatr/nano-build';\n\nexport const logger = definePackage('@alwatr/signal', __package_version__);\n", "import {AlwatrObservable, type AlwatrObservableConfig} from '@alwatr/observable';\n\nimport {logger} from './logger.js';\n\nlogger.logModule?.('trigger');\n\n/**\n * Alwatr event signal without any message (no event detail).\n */\nexport class AlwatrTrigger extends AlwatrObservable {\n constructor(config: AlwatrObservableConfig) {\n config.loggerPrefix ??= 'signal';\n super(config);\n }\n\n /**\n * Dispatch an event to all listeners.\n */\n notify(): void {\n this.notify_({});\n }\n\n /**\n * Wait until next event signal.\n */\n async untilTriggered(): Promise<void> {\n await super.untilNewNotify_();\n }\n}\n", "import {AlwatrObservable, type AlwatrObservableConfig} from '@alwatr/observable';\n\nimport {logger} from './logger.js';\n\nimport type {Dictionary} from '@alwatr/type-helper';\n\nlogger.logModule?.('signal');\n\n/**\n * Alwatr event signal with special message (event detail).\n */\nexport class AlwatrSignal<T extends Dictionary = Dictionary> extends AlwatrObservable<T> {\n constructor(config: AlwatrObservableConfig) {\n config.loggerPrefix ??= 'signal';\n super(config);\n }\n\n /**\n * Dispatch an event to all listeners.\n */\n notify(message: T): void {\n this.notify_(message);\n }\n\n /**\n * Wait until next event.\n */\n untilNewNotify(): Promise<T> {\n return super.untilNewNotify_();\n }\n}\n"],
|
|
5
|
+
"mappings": ";yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,kBAAAE,EAAA,kBAAAC,IAAA,eAAAC,EAAAJ,GCAA,IAAAK,EAA4B,0BAIfC,KAAS,iBAAc,iBAAkB,OAAmB,ECJzE,IAAAC,EAA4D,8BAI5DC,EAAO,YAAY,SAAS,EAKrB,IAAMC,EAAN,cAA4B,kBAAiB,CAClD,YAAYC,EAAgC,CAC1CA,EAAO,eAAPA,EAAO,aAAiB,UACxB,MAAMA,CAAM,CACd,CAKA,QAAe,CACb,KAAK,QAAQ,CAAC,CAAC,CACjB,CAKA,MAAM,gBAAgC,CACpC,MAAM,MAAM,gBAAgB,CAC9B,CACF,EC5BA,IAAAC,EAA4D,8BAM5DC,EAAO,YAAY,QAAQ,EAKpB,IAAMC,EAAN,cAA8D,kBAAoB,CACvF,YAAYC,EAAgC,CAC1CA,EAAO,eAAPA,EAAO,aAAiB,UACxB,MAAMA,CAAM,CACd,CAKA,OAAOC,EAAkB,CACvB,KAAK,QAAQA,CAAO,CACtB,CAKA,gBAA6B,CAC3B,OAAO,MAAM,gBAAgB,CAC/B,CACF",
|
|
6
|
+
"names": ["main_exports", "__export", "AlwatrSignal", "AlwatrTrigger", "__toCommonJS", "import_logger", "logger", "import_observable", "logger", "AlwatrTrigger", "config", "import_observable", "logger", "AlwatrSignal", "config", "message"]
|
|
7
|
+
}
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC"}
|
package/dist/main.mjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/* @alwatr/signal v3.0.0 */
|
|
2
|
+
import{definePackage as l}from"@alwatr/logger";var e=l("@alwatr/signal","3.0.0");import{AlwatrObservable as a}from"@alwatr/observable";e.logModule?.("trigger");var t=class extends a{constructor(r){r.loggerPrefix??(r.loggerPrefix="signal"),super(r)}notify(){this.notify_({})}async untilTriggered(){await super.untilNewNotify_()}};import{AlwatrObservable as s}from"@alwatr/observable";e.logModule?.("signal");var o=class extends s{constructor(r){r.loggerPrefix??(r.loggerPrefix="signal"),super(r)}notify(r){this.notify_(r)}untilNewNotify(){return super.untilNewNotify_()}};export{o as AlwatrSignal,t as AlwatrTrigger};
|
|
3
|
+
//# sourceMappingURL=main.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/logger.ts", "../src/trigger.ts", "../src/signal.ts"],
|
|
4
|
+
"sourcesContent": ["import {definePackage} from '@alwatr/logger';\n\nimport type {} from '@alwatr/nano-build';\n\nexport const logger = definePackage('@alwatr/signal', __package_version__);\n", "import {AlwatrObservable, type AlwatrObservableConfig} from '@alwatr/observable';\n\nimport {logger} from './logger.js';\n\nlogger.logModule?.('trigger');\n\n/**\n * Alwatr event signal without any message (no event detail).\n */\nexport class AlwatrTrigger extends AlwatrObservable {\n constructor(config: AlwatrObservableConfig) {\n config.loggerPrefix ??= 'signal';\n super(config);\n }\n\n /**\n * Dispatch an event to all listeners.\n */\n notify(): void {\n this.notify_({});\n }\n\n /**\n * Wait until next event signal.\n */\n async untilTriggered(): Promise<void> {\n await super.untilNewNotify_();\n }\n}\n", "import {AlwatrObservable, type AlwatrObservableConfig} from '@alwatr/observable';\n\nimport {logger} from './logger.js';\n\nimport type {Dictionary} from '@alwatr/type-helper';\n\nlogger.logModule?.('signal');\n\n/**\n * Alwatr event signal with special message (event detail).\n */\nexport class AlwatrSignal<T extends Dictionary = Dictionary> extends AlwatrObservable<T> {\n constructor(config: AlwatrObservableConfig) {\n config.loggerPrefix ??= 'signal';\n super(config);\n }\n\n /**\n * Dispatch an event to all listeners.\n */\n notify(message: T): void {\n this.notify_(message);\n }\n\n /**\n * Wait until next event.\n */\n untilNewNotify(): Promise<T> {\n return super.untilNewNotify_();\n }\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,OAAQ,iBAAAA,MAAoB,iBAIrB,IAAMC,EAASD,EAAc,iBAAkB,OAAmB,ECJzE,OAAQ,oBAAAE,MAAoD,qBAI5DC,EAAO,YAAY,SAAS,EAKrB,IAAMC,EAAN,cAA4BF,CAAiB,CAClD,YAAYG,EAAgC,CAC1CA,EAAO,eAAPA,EAAO,aAAiB,UACxB,MAAMA,CAAM,CACd,CAKA,QAAe,CACb,KAAK,QAAQ,CAAC,CAAC,CACjB,CAKA,MAAM,gBAAgC,CACpC,MAAM,MAAM,gBAAgB,CAC9B,CACF,EC5BA,OAAQ,oBAAAC,MAAoD,qBAM5DC,EAAO,YAAY,QAAQ,EAKpB,IAAMC,EAAN,cAA8DF,CAAoB,CACvF,YAAYG,EAAgC,CAC1CA,EAAO,eAAPA,EAAO,aAAiB,UACxB,MAAMA,CAAM,CACd,CAKA,OAAOC,EAAkB,CACvB,KAAK,QAAQA,CAAO,CACtB,CAKA,gBAA6B,CAC3B,OAAO,MAAM,gBAAgB,CAC/B,CACF",
|
|
6
|
+
"names": ["definePackage", "logger", "AlwatrObservable", "logger", "AlwatrTrigger", "config", "AlwatrObservable", "logger", "AlwatrSignal", "config", "message"]
|
|
7
|
+
}
|
package/dist/signal.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { AlwatrObservable, type AlwatrObservableConfig } from '@alwatr/observable';
|
|
2
|
+
import type { Dictionary } from '@alwatr/type-helper';
|
|
3
|
+
/**
|
|
4
|
+
* Alwatr event signal with special message (event detail).
|
|
5
|
+
*/
|
|
6
|
+
export declare class AlwatrSignal<T extends Dictionary = Dictionary> extends AlwatrObservable<T> {
|
|
7
|
+
constructor(config: AlwatrObservableConfig);
|
|
8
|
+
/**
|
|
9
|
+
* Dispatch an event to all listeners.
|
|
10
|
+
*/
|
|
11
|
+
notify(message: T): void;
|
|
12
|
+
/**
|
|
13
|
+
* Wait until next event.
|
|
14
|
+
*/
|
|
15
|
+
untilNewNotify(): Promise<T>;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=signal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signal.d.ts","sourceRoot":"","sources":["../src/signal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,gBAAgB,EAAE,KAAK,sBAAsB,EAAC,MAAM,oBAAoB,CAAC;AAIjF,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,qBAAqB,CAAC;AAIpD;;GAEG;AACH,qBAAa,YAAY,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU,CAAE,SAAQ,gBAAgB,CAAC,CAAC,CAAC;gBAC1E,MAAM,EAAE,sBAAsB;IAK1C;;OAEG;IACH,MAAM,CAAC,OAAO,EAAE,CAAC,GAAG,IAAI;IAIxB;;OAEG;IACH,cAAc,IAAI,OAAO,CAAC,CAAC,CAAC;CAG7B"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AlwatrObservable, type AlwatrObservableConfig } from '@alwatr/observable';
|
|
2
|
+
/**
|
|
3
|
+
* Alwatr event signal without any message (no event detail).
|
|
4
|
+
*/
|
|
5
|
+
export declare class AlwatrTrigger extends AlwatrObservable {
|
|
6
|
+
constructor(config: AlwatrObservableConfig);
|
|
7
|
+
/**
|
|
8
|
+
* Dispatch an event to all listeners.
|
|
9
|
+
*/
|
|
10
|
+
notify(): void;
|
|
11
|
+
/**
|
|
12
|
+
* Wait until next event signal.
|
|
13
|
+
*/
|
|
14
|
+
untilTriggered(): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=trigger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trigger.d.ts","sourceRoot":"","sources":["../src/trigger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,gBAAgB,EAAE,KAAK,sBAAsB,EAAC,MAAM,oBAAoB,CAAC;AAMjF;;GAEG;AACH,qBAAa,aAAc,SAAQ,gBAAgB;gBACrC,MAAM,EAAE,sBAAsB;IAK1C;;OAEG;IACH,MAAM,IAAI,IAAI;IAId;;OAEG;IACG,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC;CAGtC"}
|
package/package.json
CHANGED
|
@@ -1,41 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alwatr/signal",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"description": "A simple and efficient TypeScript library for event-driven communication using signals.",
|
|
5
|
+
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
|
|
5
6
|
"keywords": [
|
|
6
7
|
"signal",
|
|
8
|
+
"observable",
|
|
7
9
|
"typescript",
|
|
8
10
|
"esm",
|
|
9
11
|
"alwatr"
|
|
10
12
|
],
|
|
11
|
-
"main": "index.js",
|
|
12
13
|
"type": "module",
|
|
13
|
-
"
|
|
14
|
-
"
|
|
14
|
+
"main": "./dist/main.cjs",
|
|
15
|
+
"module": "./dist/main.mjs",
|
|
16
|
+
"types": "./dist/main.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/main.d.ts",
|
|
20
|
+
"import": "./dist/main.mjs",
|
|
21
|
+
"require": "./dist/main.cjs"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
15
24
|
"license": "MIT",
|
|
16
25
|
"files": [
|
|
17
|
-
"**/*.{
|
|
26
|
+
"**/*.{js,mjs,cjs,map,d.ts,html,md}",
|
|
27
|
+
"!demo/**/*"
|
|
18
28
|
],
|
|
19
29
|
"publishConfig": {
|
|
20
30
|
"access": "public"
|
|
21
31
|
},
|
|
22
32
|
"repository": {
|
|
23
33
|
"type": "git",
|
|
24
|
-
"url": "https://github.com/Alwatr/
|
|
34
|
+
"url": "https://github.com/Alwatr/flux",
|
|
25
35
|
"directory": "packages/signal"
|
|
26
36
|
},
|
|
27
|
-
"homepage": "https://github.com/Alwatr/
|
|
37
|
+
"homepage": "https://github.com/Alwatr/flux/tree/next/packages/signal#readme",
|
|
28
38
|
"bugs": {
|
|
29
|
-
"url": "https://github.com/Alwatr/
|
|
39
|
+
"url": "https://github.com/Alwatr/flux/issues"
|
|
40
|
+
},
|
|
41
|
+
"prettier": "@alwatr/prettier-config",
|
|
42
|
+
"scripts": {
|
|
43
|
+
"b": "yarn run build",
|
|
44
|
+
"t": "yarn run test",
|
|
45
|
+
"w": "yarn run watch",
|
|
46
|
+
"c": "yarn run clean",
|
|
47
|
+
"cb": "yarn run clean && yarn run build",
|
|
48
|
+
"d": "yarn run build:es && yarn node --enable-source-maps --trace-warnings",
|
|
49
|
+
"build": "yarn run build:ts & yarn run build:es",
|
|
50
|
+
"build:es": "nano-build --preset=module",
|
|
51
|
+
"build:ts": "tsc --build",
|
|
52
|
+
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --enable-source-maps --experimental-vm-modules\" jest",
|
|
53
|
+
"watch": "yarn run watch:ts & yarn run watch:es",
|
|
54
|
+
"watch:es": "yarn run build:es --watch",
|
|
55
|
+
"watch:ts": "yarn run build:ts --watch --preserveWatchOutput",
|
|
56
|
+
"clean": "rm -rfv dist *.tsbuildinfo"
|
|
30
57
|
},
|
|
31
58
|
"dependencies": {
|
|
32
|
-
"@alwatr/logger": "^2.
|
|
33
|
-
"@alwatr/
|
|
34
|
-
"@alwatr/util": "^1.4.1",
|
|
35
|
-
"tslib": "^2.6.2"
|
|
59
|
+
"@alwatr/logger": "^3.2.13",
|
|
60
|
+
"@alwatr/observable": "^3.0.0"
|
|
36
61
|
},
|
|
37
62
|
"devDependencies": {
|
|
38
|
-
"@
|
|
63
|
+
"@alwatr/nano-build": "^1.3.9",
|
|
64
|
+
"@alwatr/prettier-config": "^1.0.4",
|
|
65
|
+
"@alwatr/tsconfig-base": "^1.2.0",
|
|
66
|
+
"@alwatr/type-helper": "^1.2.6",
|
|
67
|
+
"@types/node": "^22.5.5",
|
|
68
|
+
"jest": "^29.7.0",
|
|
69
|
+
"typescript": "^5.6.2"
|
|
39
70
|
},
|
|
40
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "6c8830345d35da730c386adb047ca2cdcb3c36f0"
|
|
41
72
|
}
|
package/context.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { AlwatrObservable } from './observable.js';
|
|
2
|
-
/**
|
|
3
|
-
* Alwatr context signal.
|
|
4
|
-
*/
|
|
5
|
-
export declare class AlwatrContextSignal<T> extends AlwatrObservable<T> {
|
|
6
|
-
constructor(config: {
|
|
7
|
-
name: string;
|
|
8
|
-
loggerPrefix?: string;
|
|
9
|
-
});
|
|
10
|
-
/**
|
|
11
|
-
* Get context value.
|
|
12
|
-
*
|
|
13
|
-
* Return undefined if context not set before or expired.
|
|
14
|
-
*/
|
|
15
|
-
getValue(): T | undefined;
|
|
16
|
-
/**
|
|
17
|
-
* Set context value and notify all subscribers.
|
|
18
|
-
*/
|
|
19
|
-
setValue(value: T): void;
|
|
20
|
-
/**
|
|
21
|
-
* Clear current context value without notify subscribers.
|
|
22
|
-
*
|
|
23
|
-
* `receivePrevious` in new subscribers not work until new context changes.
|
|
24
|
-
*/
|
|
25
|
-
expire(): void;
|
|
26
|
-
/**
|
|
27
|
-
* Get the value of the next context changes.
|
|
28
|
-
*/
|
|
29
|
-
untilChange(): Promise<T>;
|
|
30
|
-
}
|
|
31
|
-
//# sourceMappingURL=context.d.ts.map
|
package/context.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["src/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,gBAAgB,EAAC,MAAM,iBAAiB,CAAC;AAEjD;;GAEG;AACH,qBAAa,mBAAmB,CAAC,CAAC,CAAE,SAAQ,gBAAgB,CAAC,CAAC,CAAC;gBACjD,MAAM,EAAE;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,YAAY,CAAC,EAAE,MAAM,CAAA;KAAC;IAKzD;;;;OAIG;IACH,QAAQ,IAAI,CAAC,GAAG,SAAS;IAIzB;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI;IAKxB;;;;OAIG;IACH,MAAM,IAAI,IAAI;IAId;;OAEG;IACH,WAAW,IAAI,OAAO,CAAC,CAAC,CAAC;CAG1B"}
|
package/context.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { AlwatrObservable } from './observable.js';
|
|
2
|
-
/**
|
|
3
|
-
* Alwatr context signal.
|
|
4
|
-
*/
|
|
5
|
-
export class AlwatrContextSignal extends AlwatrObservable {
|
|
6
|
-
constructor(config) {
|
|
7
|
-
config.loggerPrefix ?? (config.loggerPrefix = 'context-signal');
|
|
8
|
-
super(config);
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Get context value.
|
|
12
|
-
*
|
|
13
|
-
* Return undefined if context not set before or expired.
|
|
14
|
-
*/
|
|
15
|
-
getValue() {
|
|
16
|
-
return super._getData();
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Set context value and notify all subscribers.
|
|
20
|
-
*/
|
|
21
|
-
setValue(value) {
|
|
22
|
-
this._logger.logMethodArgs?.('setValue', { value });
|
|
23
|
-
super._notify(value);
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Clear current context value without notify subscribers.
|
|
27
|
-
*
|
|
28
|
-
* `receivePrevious` in new subscribers not work until new context changes.
|
|
29
|
-
*/
|
|
30
|
-
expire() {
|
|
31
|
-
super._clear();
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Get the value of the next context changes.
|
|
35
|
-
*/
|
|
36
|
-
untilChange() {
|
|
37
|
-
return super._untilNewNotify();
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
//# sourceMappingURL=context.js.map
|
package/context.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"context.js","sourceRoot":"","sources":["src/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,gBAAgB,EAAC,MAAM,iBAAiB,CAAC;AAEjD;;GAEG;AACH,MAAM,OAAO,mBAAuB,SAAQ,gBAAmB;IAC7D,YAAY,MAA6C;QACvD,MAAM,CAAC,YAAY,KAAnB,MAAM,CAAC,YAAY,GAAK,gBAAgB,EAAC;QACzC,KAAK,CAAC,MAAM,CAAC,CAAC;IAChB,CAAC;IAED;;;;OAIG;IACH,QAAQ;QACN,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;IAC1B,CAAC;IAED;;OAEG;IACH,QAAQ,CAAC,KAAQ;QACf,IAAI,CAAC,OAAO,CAAC,aAAa,EAAE,CAAC,UAAU,EAAE,EAAC,KAAK,EAAC,CAAC,CAAC;QAClD,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;IAED;;;;OAIG;IACH,MAAM;QACJ,KAAK,CAAC,MAAM,EAAE,CAAC;IACjB,CAAC;IAED;;OAEG;IACH,WAAW;QACT,OAAO,KAAK,CAAC,eAAe,EAAE,CAAC;IACjC,CAAC;CACF","sourcesContent":["import {AlwatrObservable} from './observable.js';\n\n/**\n * Alwatr context signal.\n */\nexport class AlwatrContextSignal<T> extends AlwatrObservable<T> {\n constructor(config: {name: string; loggerPrefix?: string}) {\n config.loggerPrefix ??= 'context-signal';\n super(config);\n }\n\n /**\n * Get context value.\n *\n * Return undefined if context not set before or expired.\n */\n getValue(): T | undefined {\n return super._getData();\n }\n\n /**\n * Set context value and notify all subscribers.\n */\n setValue(value: T): void {\n this._logger.logMethodArgs?.('setValue', {value});\n super._notify(value);\n }\n\n /**\n * Clear current context value without notify subscribers.\n *\n * `receivePrevious` in new subscribers not work until new context changes.\n */\n expire(): void {\n super._clear();\n }\n\n /**\n * Get the value of the next context changes.\n */\n untilChange(): Promise<T> {\n return super._untilNewNotify();\n }\n}\n"]}
|
package/index.d.ts
DELETED
package/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,0BAA0B,CAAC;AACzC,mBAAmB,WAAW,CAAC"}
|
package/index.js
DELETED
package/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,0BAA0B,CAAC","sourcesContent":["export * from './observable.js';\nexport * from './simple-signal.js';\nexport * from './signal.js';\nexport * from './context.js';\nexport * from './multithread-context.js';\nexport type * from './type.js';\n"]}
|
package/multithread-context.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { AlwatrContextSignal } from './context.js';
|
|
2
|
-
/**
|
|
3
|
-
* Alwatr multithread context signal.
|
|
4
|
-
*/
|
|
5
|
-
export declare class AlwatrMultithreadContextSignal<TValue> extends AlwatrContextSignal<TValue> {
|
|
6
|
-
protected static _logger: import("@alwatr/logger").AlwatrLogger;
|
|
7
|
-
protected static _worker?: Worker;
|
|
8
|
-
protected static _registry: Record<string, AlwatrMultithreadContextSignal<unknown> | undefined>;
|
|
9
|
-
static setupChannel(worker?: Worker): void;
|
|
10
|
-
static _onMessage(event: MessageEvent): void;
|
|
11
|
-
static _postMessage(name: string, payload: unknown): void;
|
|
12
|
-
constructor(config: {
|
|
13
|
-
name: string;
|
|
14
|
-
loggerPrefix?: string;
|
|
15
|
-
});
|
|
16
|
-
/**
|
|
17
|
-
* Set context value and notify all subscribers.
|
|
18
|
-
*/
|
|
19
|
-
setValue(value: TValue): void;
|
|
20
|
-
}
|
|
21
|
-
//# sourceMappingURL=multithread-context.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"multithread-context.d.ts","sourceRoot":"","sources":["src/multithread-context.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,mBAAmB,EAAC,MAAM,cAAc,CAAC;AAQjD;;GAEG;AACH,qBAAa,8BAA8B,CAAC,MAAM,CAAE,SAAQ,mBAAmB,CAAC,MAAM,CAAC;IACrF,SAAS,CAAC,MAAM,CAAC,OAAO,wCAAqC;IAC7D,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAClC,SAAS,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,8BAA8B,CAAC,OAAO,CAAC,GAAG,SAAS,CAAC,CAAM;IAErG,MAAM,CAAC,YAAY,CAAC,MAAM,GAAE,MAAkC,GAAG,IAAI;IAKrE,MAAM,CAAC,UAAU,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI;IAW5C,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI;gBAY7C,MAAM,EAAE;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,YAAY,CAAC,EAAE,MAAM,CAAA;KAAC;IAUzD;;OAEG;IACM,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;CAIvC"}
|
package/multithread-context.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { createLogger } from '@alwatr/logger';
|
|
2
|
-
import { AlwatrContextSignal } from './context.js';
|
|
3
|
-
/**
|
|
4
|
-
* Alwatr multithread context signal.
|
|
5
|
-
*/
|
|
6
|
-
export class AlwatrMultithreadContextSignal extends AlwatrContextSignal {
|
|
7
|
-
static setupChannel(worker = self) {
|
|
8
|
-
AlwatrMultithreadContextSignal._worker = worker;
|
|
9
|
-
worker.addEventListener('message', AlwatrMultithreadContextSignal._onMessage);
|
|
10
|
-
}
|
|
11
|
-
static _onMessage(event) {
|
|
12
|
-
const message = event.data;
|
|
13
|
-
if (message.type !== 'alwatr_context_changed')
|
|
14
|
-
return;
|
|
15
|
-
AlwatrMultithreadContextSignal._logger.logMethodArgs?.('_onMessage', { message });
|
|
16
|
-
const context = AlwatrMultithreadContextSignal._registry[message.name];
|
|
17
|
-
if (context === undefined) {
|
|
18
|
-
throw new Error('context_not_define', { cause: 'context not define in this thread yet!' });
|
|
19
|
-
}
|
|
20
|
-
context._notify(message.payload);
|
|
21
|
-
}
|
|
22
|
-
static _postMessage(name, payload) {
|
|
23
|
-
AlwatrMultithreadContextSignal._logger.logMethodArgs?.('_postMessage', { name, payload });
|
|
24
|
-
if (AlwatrMultithreadContextSignal._worker === undefined) {
|
|
25
|
-
throw new Error('worker_not_defined', { cause: 'setupChannel must be called before any setValue.' });
|
|
26
|
-
}
|
|
27
|
-
AlwatrMultithreadContextSignal._worker.postMessage({
|
|
28
|
-
type: 'alwatr_context_changed',
|
|
29
|
-
name,
|
|
30
|
-
payload,
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
constructor(config) {
|
|
34
|
-
super(config);
|
|
35
|
-
if (AlwatrMultithreadContextSignal._registry[this._name] !== undefined) {
|
|
36
|
-
throw new Error('context_name_exist');
|
|
37
|
-
}
|
|
38
|
-
AlwatrMultithreadContextSignal._registry[this._name] = this;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Set context value and notify all subscribers.
|
|
42
|
-
*/
|
|
43
|
-
setValue(value) {
|
|
44
|
-
super.setValue(value);
|
|
45
|
-
AlwatrMultithreadContextSignal._postMessage(this._name, value);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
AlwatrMultithreadContextSignal._logger = createLogger(`alwatr/mt-context`);
|
|
49
|
-
AlwatrMultithreadContextSignal._registry = {};
|
|
50
|
-
//# sourceMappingURL=multithread-context.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"multithread-context.js","sourceRoot":"","sources":["src/multithread-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAE5C,OAAO,EAAC,mBAAmB,EAAC,MAAM,cAAc,CAAC;AAQjD;;GAEG;AACH,MAAM,OAAO,8BAAuC,SAAQ,mBAA2B;IAKrF,MAAM,CAAC,YAAY,CAAC,SAAiB,IAAyB;QAC5D,8BAA8B,CAAC,OAAO,GAAG,MAAM,CAAC;QAChD,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,8BAA8B,CAAC,UAAU,CAAC,CAAC;IAChF,CAAC;IAED,MAAM,CAAC,UAAU,CAAC,KAAmB;QACnC,MAAM,OAAO,GAAG,KAAK,CAAC,IAAmC,CAAC;QAC1D,IAAI,OAAO,CAAC,IAAI,KAAK,wBAAwB;YAAE,OAAO;QACtD,8BAA8B,CAAC,OAAO,CAAC,aAAa,EAAE,CAAC,YAAY,EAAE,EAAC,OAAO,EAAC,CAAC,CAAC;QAChF,MAAM,OAAO,GAAG,8BAA8B,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACvE,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YAC1B,MAAM,IAAI,KAAK,CAAC,oBAAoB,EAAE,EAAC,KAAK,EAAE,wCAAwC,EAAC,CAAC,CAAC;QAC3F,CAAC;QACD,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACnC,CAAC;IAED,MAAM,CAAC,YAAY,CAAC,IAAY,EAAE,OAAgB;QAChD,8BAA8B,CAAC,OAAO,CAAC,aAAa,EAAE,CAAC,cAAc,EAAE,EAAC,IAAI,EAAE,OAAO,EAAC,CAAC,CAAC;QACxF,IAAI,8BAA8B,CAAC,OAAO,KAAK,SAAS,EAAE,CAAC;YACzD,MAAM,IAAI,KAAK,CAAC,oBAAoB,EAAE,EAAC,KAAK,EAAE,kDAAkD,EAAC,CAAC,CAAC;QACrG,CAAC;QACD,8BAA8B,CAAC,OAAO,CAAC,WAAW,CAAC;YACjD,IAAI,EAAE,wBAAwB;YAC9B,IAAI;YACJ,OAAO;SACuB,CAAC,CAAC;IACpC,CAAC;IAED,YAAY,MAA6C;QACvD,KAAK,CAAC,MAAM,CAAC,CAAC;QAEd,IAAI,8BAA8B,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,SAAS,EAAE,CAAC;YACvE,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;QACxC,CAAC;QAED,8BAA8B,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,IAA+C,CAAC;IACzG,CAAC;IAED;;OAEG;IACM,QAAQ,CAAC,KAAa;QAC7B,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QACtB,8BAA8B,CAAC,YAAY,CAAC,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IACjE,CAAC;;AAhDgB,sCAAO,GAAG,YAAY,CAAC,mBAAmB,CAAC,CAAC;AAE5C,wCAAS,GAAwE,EAAE,CAAC","sourcesContent":["import {createLogger} from '@alwatr/logger';\n\nimport {AlwatrContextSignal} from './context.js';\n\ninterface AlwatrContextChangedMessage {\n type: 'alwatr_context_changed';\n name: string;\n payload: unknown;\n}\n\n/**\n * Alwatr multithread context signal.\n */\nexport class AlwatrMultithreadContextSignal<TValue> extends AlwatrContextSignal<TValue> {\n protected static _logger = createLogger(`alwatr/mt-context`);\n protected static _worker?: Worker;\n protected static _registry: Record<string, AlwatrMultithreadContextSignal<unknown> | undefined> = {};\n\n static setupChannel(worker: Worker = self as unknown as Worker): void {\n AlwatrMultithreadContextSignal._worker = worker;\n worker.addEventListener('message', AlwatrMultithreadContextSignal._onMessage);\n }\n\n static _onMessage(event: MessageEvent): void {\n const message = event.data as AlwatrContextChangedMessage;\n if (message.type !== 'alwatr_context_changed') return;\n AlwatrMultithreadContextSignal._logger.logMethodArgs?.('_onMessage', {message});\n const context = AlwatrMultithreadContextSignal._registry[message.name];\n if (context === undefined) {\n throw new Error('context_not_define', {cause: 'context not define in this thread yet!'});\n }\n context._notify(message.payload);\n }\n\n static _postMessage(name: string, payload: unknown): void {\n AlwatrMultithreadContextSignal._logger.logMethodArgs?.('_postMessage', {name, payload});\n if (AlwatrMultithreadContextSignal._worker === undefined) {\n throw new Error('worker_not_defined', {cause: 'setupChannel must be called before any setValue.'});\n }\n AlwatrMultithreadContextSignal._worker.postMessage({\n type: 'alwatr_context_changed',\n name,\n payload,\n } as AlwatrContextChangedMessage);\n }\n\n constructor(config: {name: string; loggerPrefix?: string}) {\n super(config);\n\n if (AlwatrMultithreadContextSignal._registry[this._name] !== undefined) {\n throw new Error('context_name_exist');\n }\n\n AlwatrMultithreadContextSignal._registry[this._name] = this as AlwatrMultithreadContextSignal<unknown>;\n }\n\n /**\n * Set context value and notify all subscribers.\n */\n override setValue(value: TValue): void {\n super.setValue(value);\n AlwatrMultithreadContextSignal._postMessage(this._name, value);\n }\n}\n"]}
|
package/observable.d.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import type { SubscribeOptions, ListenerCallback, Observer, SubscribeResult, AlwatrObservableInterface } from './type.js';
|
|
2
|
-
/**
|
|
3
|
-
* Alwatr base signal.
|
|
4
|
-
*/
|
|
5
|
-
export declare abstract class AlwatrObservable<T> implements AlwatrObservableInterface<T> {
|
|
6
|
-
protected _name: string;
|
|
7
|
-
protected _logger: import("@alwatr/logger").AlwatrLogger;
|
|
8
|
-
protected _$data?: T;
|
|
9
|
-
protected _$observers: Observer<this, T>[];
|
|
10
|
-
constructor(config: {
|
|
11
|
-
name: string;
|
|
12
|
-
loggerPrefix?: string;
|
|
13
|
-
});
|
|
14
|
-
/**
|
|
15
|
-
* Get data.
|
|
16
|
-
*
|
|
17
|
-
* Return undefined if signal not notify before or expired.
|
|
18
|
-
*/
|
|
19
|
-
protected _getData(): T | undefined;
|
|
20
|
-
/**
|
|
21
|
-
* Execute all observers and remember data.
|
|
22
|
-
*/
|
|
23
|
-
protected _notify(data: T): void;
|
|
24
|
-
/**
|
|
25
|
-
* Execute all observers callback.
|
|
26
|
-
*/
|
|
27
|
-
protected _$dispatch(data: T): void;
|
|
28
|
-
/**
|
|
29
|
-
* Subscribe to context changes.
|
|
30
|
-
*/
|
|
31
|
-
subscribe(listenerCallback: ListenerCallback<this, T>, options?: SubscribeOptions): SubscribeResult;
|
|
32
|
-
/**
|
|
33
|
-
* Unsubscribe from context.
|
|
34
|
-
*/
|
|
35
|
-
unsubscribe(listenerCallback: ListenerCallback<this, T>): void;
|
|
36
|
-
/**
|
|
37
|
-
* Clear current data without notify subscribers.
|
|
38
|
-
*
|
|
39
|
-
* `receivePrevious` in new subscribers not work until new a notify changes the data.
|
|
40
|
-
*/
|
|
41
|
-
protected _clear(): void;
|
|
42
|
-
/**
|
|
43
|
-
* Get the data of next notify.
|
|
44
|
-
*/
|
|
45
|
-
protected _untilNewNotify(): Promise<T>;
|
|
46
|
-
}
|
|
47
|
-
//# sourceMappingURL=observable.d.ts.map
|
package/observable.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"observable.d.ts","sourceRoot":"","sources":["src/observable.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,gBAAgB,EAAE,gBAAgB,EAAE,QAAQ,EAAE,eAAe,EAAE,yBAAyB,EAAC,MAAM,WAAW,CAAC;AAIxH;;GAEG;AACH,8BAAsB,gBAAgB,CAAC,CAAC,CAAE,YAAW,yBAAyB,CAAC,CAAC,CAAC;IAC/E,SAAS,CAAC,KAAK,SAAC;IAChB,SAAS,CAAC,OAAO,wCAAC;IAClB,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IACrB,SAAS,CAAC,WAAW,EAAE,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,CAAM;gBAEpC,MAAM,EAAE;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,YAAY,CAAC,EAAE,MAAM,CAAA;KAAC;IAOzD;;;;OAIG;IACH,SAAS,CAAC,QAAQ,IAAI,CAAC,GAAG,SAAS;IAKnC;;OAEG;IACH,SAAS,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,GAAG,IAAI;IAMhC;;OAEG;IACH,SAAS,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC,GAAG,IAAI;IAuBnC;;OAEG;IACH,SAAS,CAAC,gBAAgB,EAAE,gBAAgB,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,OAAO,GAAE,gBAAqB,GAAG,eAAe;IAyCvG;;OAEG;IACH,WAAW,CAAC,gBAAgB,EAAE,gBAAgB,CAAC,IAAI,EAAE,CAAC,CAAC,GAAG,IAAI;IAQ9D;;;;OAIG;IACH,SAAS,CAAC,MAAM,IAAI,IAAI;IAKxB;;OAEG;IACH,SAAS,CAAC,eAAe,IAAI,OAAO,CAAC,CAAC,CAAC;CAUxC"}
|
package/observable.js
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { createLogger, definePackage } from '@alwatr/logger';
|
|
2
|
-
definePackage('signal', '2.x');
|
|
3
|
-
/**
|
|
4
|
-
* Alwatr base signal.
|
|
5
|
-
*/
|
|
6
|
-
export class AlwatrObservable {
|
|
7
|
-
constructor(config) {
|
|
8
|
-
this._$observers = [];
|
|
9
|
-
config.loggerPrefix ?? (config.loggerPrefix = 'signal');
|
|
10
|
-
this._name = config.name;
|
|
11
|
-
this._logger = createLogger(`{${config.loggerPrefix}: ${this._name}}`);
|
|
12
|
-
this._logger.logMethod?.('constructor');
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Get data.
|
|
16
|
-
*
|
|
17
|
-
* Return undefined if signal not notify before or expired.
|
|
18
|
-
*/
|
|
19
|
-
_getData() {
|
|
20
|
-
this._logger.logMethodFull?.('_getData', {}, this._$data);
|
|
21
|
-
return this._$data;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Execute all observers and remember data.
|
|
25
|
-
*/
|
|
26
|
-
_notify(data) {
|
|
27
|
-
this._logger.logMethodArgs?.('_notify', data);
|
|
28
|
-
this._$data = data;
|
|
29
|
-
setTimeout(() => this._$dispatch(data), 0);
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Execute all observers callback.
|
|
33
|
-
*/
|
|
34
|
-
_$dispatch(data) {
|
|
35
|
-
const removeList = [];
|
|
36
|
-
for (const listener of this._$observers) {
|
|
37
|
-
if (listener.options.disabled)
|
|
38
|
-
continue;
|
|
39
|
-
if (listener.options.once)
|
|
40
|
-
removeList.push(listener);
|
|
41
|
-
try {
|
|
42
|
-
const ret = listener.callback.call(this, data);
|
|
43
|
-
if (ret instanceof Promise) {
|
|
44
|
-
ret.catch((err) => this._logger.error('_$dispatch', 'call_listener_failed', err));
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
catch (err) {
|
|
48
|
-
this._logger.error('_$dispatch', 'call_listener_failed', err);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
for (const listener of removeList) {
|
|
52
|
-
this.unsubscribe(listener.callback);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Subscribe to context changes.
|
|
57
|
-
*/
|
|
58
|
-
subscribe(listenerCallback, options = {}) {
|
|
59
|
-
this._logger.logMethodArgs?.('subscribe', { options });
|
|
60
|
-
const _listenerObject = {
|
|
61
|
-
callback: listenerCallback,
|
|
62
|
-
options,
|
|
63
|
-
};
|
|
64
|
-
let callbackExecuted = false;
|
|
65
|
-
const data = this._$data;
|
|
66
|
-
if (data !== undefined && options.receivePrevious === true && options.disabled !== true) {
|
|
67
|
-
// Run callback for old dispatch signal
|
|
68
|
-
callbackExecuted = true;
|
|
69
|
-
setTimeout(() => {
|
|
70
|
-
try {
|
|
71
|
-
const ret = listenerCallback.call(this, data);
|
|
72
|
-
if (ret instanceof Promise) {
|
|
73
|
-
ret.catch((err) => this._logger.error('subscribe.receivePrevious', 'call_signal_callback_failed', err));
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
catch (err) {
|
|
77
|
-
this._logger.error('subscribe.receivePrevious', 'call_signal_callback_failed', err);
|
|
78
|
-
}
|
|
79
|
-
}, 0);
|
|
80
|
-
}
|
|
81
|
-
// If once then must remove listener after first callback called! then why push it to listenerList?!
|
|
82
|
-
if (options.once !== true || callbackExecuted === true) {
|
|
83
|
-
if (options.priority === true) {
|
|
84
|
-
this._$observers.unshift(_listenerObject);
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
this._$observers.push(_listenerObject);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
return {
|
|
91
|
-
unsubscribe: this.unsubscribe.bind(this, listenerCallback),
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Unsubscribe from context.
|
|
96
|
-
*/
|
|
97
|
-
unsubscribe(listenerCallback) {
|
|
98
|
-
this._logger.logMethod?.('unsubscribe');
|
|
99
|
-
const listenerIndex = this._$observers.findIndex((listener) => listener.callback === listenerCallback);
|
|
100
|
-
if (listenerIndex !== -1) {
|
|
101
|
-
void this._$observers.splice(listenerIndex, 1);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Clear current data without notify subscribers.
|
|
106
|
-
*
|
|
107
|
-
* `receivePrevious` in new subscribers not work until new a notify changes the data.
|
|
108
|
-
*/
|
|
109
|
-
_clear() {
|
|
110
|
-
this._logger.logMethod?.('_clear');
|
|
111
|
-
this._$data = undefined;
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Get the data of next notify.
|
|
115
|
-
*/
|
|
116
|
-
_untilNewNotify() {
|
|
117
|
-
this._logger.logMethod?.('_untilNewNotify');
|
|
118
|
-
return new Promise((resolve) => {
|
|
119
|
-
this.subscribe(resolve, {
|
|
120
|
-
once: true,
|
|
121
|
-
priority: true,
|
|
122
|
-
receivePrevious: false,
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
//# sourceMappingURL=observable.js.map
|