@alwatr/signal 0.29.0 → 0.30.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 +15 -0
- package/command-handler.d.ts +1 -1
- package/context-provider.d.ts +23 -3
- package/context-provider.d.ts.map +1 -1
- package/context-provider.js +21 -1
- package/context-provider.js.map +1 -1
- package/core.d.ts +2 -2
- package/core.d.ts.map +1 -1
- package/core.js +20 -18
- package/core.js.map +1 -1
- package/index.d.ts +1 -1
- package/index.d.ts.map +1 -1
- package/index.js.map +1 -1
- package/package.json +5 -3
- package/requestable-context-consumer.d.ts.map +1 -1
- package/requestable-context-consumer.js +1 -1
- package/requestable-context-consumer.js.map +1 -1
- package/requestable-context-provider.d.ts +2 -0
- package/requestable-context-provider.d.ts.map +1 -1
- package/type.d.ts +15 -11
- package/type.d.ts.map +1 -1
- package/type.js.map +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,21 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [0.30.0](https://github.com/AliMD/alwatr/compare/v0.29.0...v0.30.0) (2023-03-06)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- **signal:** NextCycle with own detail ([01f3c79](https://github.com/AliMD/alwatr/commit/01f3c79500927f6384a33abcc9b0cb2355794b3e))
|
|
11
|
+
- **signal:** nodejs compatibility ([69d8a60](https://github.com/AliMD/alwatr/commit/69d8a60ad64d44ee7c3ced002e702f13408a5a50))
|
|
12
|
+
- **signal:** requestableContextConsumer bind issue ([66467f6](https://github.com/AliMD/alwatr/commit/66467f6e5681d84d7f2e0b353206d4bb579b26f2))
|
|
13
|
+
- **signal:** requestContext dispatch issue ([e937ebd](https://github.com/AliMD/alwatr/commit/e937ebd3a90fc6a9946f5c35ef4f6f40b6ab4b00))
|
|
14
|
+
|
|
15
|
+
### Features
|
|
16
|
+
|
|
17
|
+
- **signal:** add untilChange for contextProvider ([cb44916](https://github.com/AliMD/alwatr/commit/cb4491698fd5ddfbe055032fc2cb50691de31194))
|
|
18
|
+
- **signal:** defineCommand return ListenerSpec ([21fecac](https://github.com/AliMD/alwatr/commit/21fecacb6aa9423da9e3c177a4bbc59952d94e35))
|
|
19
|
+
- **signal:** dispatch NextCycle option ([b30eb31](https://github.com/AliMD/alwatr/commit/b30eb316d92f594034fc40d195c4033e38e4d2e2))
|
|
20
|
+
|
|
6
21
|
# [0.29.0](https://github.com/AliMD/alwatr/compare/v0.28.0...v0.29.0) (2023-02-10)
|
|
7
22
|
|
|
8
23
|
### Bug Fixes
|
package/command-handler.d.ts
CHANGED
|
@@ -20,6 +20,6 @@ export declare const commandHandler: {
|
|
|
20
20
|
*/
|
|
21
21
|
readonly define: <TArgument extends import("@alwatr/type").StringifyableRecord, TReturn extends import("@alwatr/type").Stringifyable>(signalId: string, signalProvider: import("./type.js").ProviderFunction<TArgument & {
|
|
22
22
|
_callbackSignalId?: string | undefined;
|
|
23
|
-
}, TReturn>, options?: Partial<Pick<import("./type.js").ProviderOptions, "debounce">>) =>
|
|
23
|
+
}, TReturn>, options?: Partial<Pick<import("./type.js").ProviderOptions, "debounce">>) => import("./type.js").ListenerSpec;
|
|
24
24
|
};
|
|
25
25
|
//# sourceMappingURL=command-handler.d.ts.map
|
package/context-provider.d.ts
CHANGED
|
@@ -42,6 +42,16 @@ export declare const contextProvider: {
|
|
|
42
42
|
* ```
|
|
43
43
|
*/
|
|
44
44
|
readonly expire: (signalId: string) => void;
|
|
45
|
+
/**
|
|
46
|
+
* Waits until the context value changes.
|
|
47
|
+
*
|
|
48
|
+
* Example:
|
|
49
|
+
*
|
|
50
|
+
* ```ts
|
|
51
|
+
* const newProductList = await contextProvider.untilChange<ProductListType>('product-list');
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
readonly untilChange: <T_2 extends Stringifyable>(signalId: string) => Promise<T_2>;
|
|
45
55
|
/**
|
|
46
56
|
* Bind this interface to special context.
|
|
47
57
|
*
|
|
@@ -51,7 +61,7 @@ export declare const contextProvider: {
|
|
|
51
61
|
* const productListProvider = contextProvider.bind<ProductListType>('product-list');
|
|
52
62
|
* ```
|
|
53
63
|
*/
|
|
54
|
-
readonly bind: <
|
|
64
|
+
readonly bind: <T_3 extends Stringifyable>(contextId: string) => {
|
|
55
65
|
/**
|
|
56
66
|
* Context signal Id.
|
|
57
67
|
*/
|
|
@@ -70,7 +80,7 @@ export declare const contextProvider: {
|
|
|
70
80
|
* }
|
|
71
81
|
* ```
|
|
72
82
|
*/
|
|
73
|
-
readonly getValue: () =>
|
|
83
|
+
readonly getValue: () => T_3 | undefined;
|
|
74
84
|
/**
|
|
75
85
|
* Set context value and send signal to all consumers.
|
|
76
86
|
*
|
|
@@ -82,7 +92,7 @@ export declare const contextProvider: {
|
|
|
82
92
|
* productListProvider.setValue(newProductList);
|
|
83
93
|
* ```
|
|
84
94
|
*/
|
|
85
|
-
readonly setValue: (detail:
|
|
95
|
+
readonly setValue: (detail: T_3, options?: Partial<import("./type.js").DispatchOptions> | undefined) => void;
|
|
86
96
|
/**
|
|
87
97
|
* Clear current context value without send signal to all consumers.
|
|
88
98
|
*
|
|
@@ -95,6 +105,16 @@ export declare const contextProvider: {
|
|
|
95
105
|
* ```
|
|
96
106
|
*/
|
|
97
107
|
readonly expire: () => void;
|
|
108
|
+
/**
|
|
109
|
+
* Waits until the context value changes.
|
|
110
|
+
*
|
|
111
|
+
* Example:
|
|
112
|
+
*
|
|
113
|
+
* ```ts
|
|
114
|
+
* const newProductList = await productListProvider.untilChange();
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
readonly untilChange: () => Promise<T_3>;
|
|
98
118
|
};
|
|
99
119
|
};
|
|
100
120
|
//# sourceMappingURL=context-provider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context-provider.d.ts","sourceRoot":"","sources":["src/context-provider.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,aAAa,EAAiB,MAAM,cAAc,CAAC;AAEhE;;GAEG;AACH,eAAO,MAAM,eAAe;IAC1B;;;;;;;;;;;;;OAaG;;IAGH;;;;;;;;;;OAUG;;IAGH;;;;;;;;;;OAUG;;IAGH;;;;;;;;OAQG;0DACwC,MAAM;QAC/C;;WAEG;;QAGH;;;;;;;;;;;;;WAaG;;QAGH;;;;;;;;;;WAUG;;QAGH;;;;;;;;;;WAUG;;;CAGG,CAAC"}
|
|
1
|
+
{"version":3,"file":"context-provider.d.ts","sourceRoot":"","sources":["src/context-provider.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,aAAa,EAAiB,MAAM,cAAc,CAAC;AAEhE;;GAEG;AACH,eAAO,MAAM,eAAe;IAC1B;;;;;;;;;;;;;OAaG;;IAGH;;;;;;;;;;OAUG;;IAGH;;;;;;;;;;OAUG;;IAGH;;;;;;;;OAQG;;IAGH;;;;;;;;OAQG;0DACwC,MAAM;QAC/C;;WAEG;;QAGH;;;;;;;;;;;;;WAaG;;QAGH;;;;;;;;;;WAUG;;QAGH;;;;;;;;;;WAUG;;QAGH;;;;;;;;WAQG;;;CAGG,CAAC"}
|
package/context-provider.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { clearDetail, dispatch, getDetail } from './core.js';
|
|
1
|
+
import { clearDetail, dispatch, getDetail, untilNext } from './core.js';
|
|
2
2
|
/**
|
|
3
3
|
* Context provider interface.
|
|
4
4
|
*/
|
|
@@ -42,6 +42,16 @@ export const contextProvider = {
|
|
|
42
42
|
* ```
|
|
43
43
|
*/
|
|
44
44
|
expire: clearDetail,
|
|
45
|
+
/**
|
|
46
|
+
* Waits until the context value changes.
|
|
47
|
+
*
|
|
48
|
+
* Example:
|
|
49
|
+
*
|
|
50
|
+
* ```ts
|
|
51
|
+
* const newProductList = await contextProvider.untilChange<ProductListType>('product-list');
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
untilChange: untilNext,
|
|
45
55
|
/**
|
|
46
56
|
* Bind this interface to special context.
|
|
47
57
|
*
|
|
@@ -95,6 +105,16 @@ export const contextProvider = {
|
|
|
95
105
|
* ```
|
|
96
106
|
*/
|
|
97
107
|
expire: clearDetail.bind(null, contextId),
|
|
108
|
+
/**
|
|
109
|
+
* Waits until the context value changes.
|
|
110
|
+
*
|
|
111
|
+
* Example:
|
|
112
|
+
*
|
|
113
|
+
* ```ts
|
|
114
|
+
* const newProductList = await productListProvider.untilChange();
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
untilChange: untilNext.bind(null, contextId),
|
|
98
118
|
}),
|
|
99
119
|
};
|
|
100
120
|
//# sourceMappingURL=context-provider.js.map
|
package/context-provider.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context-provider.js","sourceRoot":"","sources":["src/context-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,WAAW,EAAE,QAAQ,EAAE,SAAS,EAAC,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"context-provider.js","sourceRoot":"","sources":["src/context-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,WAAW,EAAE,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAC,MAAM,WAAW,CAAC;AAItE;;GAEG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG;IAC7B;;;;;;;;;;;;;OAaG;IACH,QAAQ,EAAE,SAAS;IAEnB;;;;;;;;;;OAUG;IACH,QAAQ,EAAE,QAAQ;IAElB;;;;;;;;;;OAUG;IACH,MAAM,EAAE,WAAW;IAEnB;;;;;;;;OAQG;IACH,WAAW,EAAE,SAAS;IAEtB;;;;;;;;OAQG;IACH,IAAI,EAAE,CAA0B,SAAiB,EAAE,EAAE,CAAA,CAAC;QACpD;;WAEG;QACH,EAAE,EAAE,SAAS;QAEb;;;;;;;;;;;;;WAaG;QACH,QAAQ,EAAE,SAAS,CAAC,IAAI,CAAC,IAAI,EAAE,SAAS,CAAwC;QAEhF;;;;;;;;;;WAUG;QACH,QAAQ,EAAE,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,SAAS,CAAuC;QAE9E;;;;;;;;;;WAUG;QACH,MAAM,EAAE,WAAW,CAAC,IAAI,CAAC,IAAI,EAAE,SAAS,CAAC;QAEzC;;;;;;;;WAQG;QACH,WAAW,EAAE,SAAS,CAAC,IAAI,CAAC,IAAI,EAAE,SAAS,CAAwC;KAC1E,CAAA;CACH,CAAC","sourcesContent":["import {clearDetail, dispatch, getDetail, untilNext} from './core.js';\n\nimport type {Stringifyable, OmitFirstParam} from '@alwatr/type';\n\n/**\n * Context provider interface.\n */\nexport const contextProvider = {\n /**\n * Get context value.\n *\n * Return undefined if context not set before or expired.\n *\n * Example:\n *\n * ```ts\n * const currentProductList = contextProvider.getValue<ProductListType>('product-list');\n * if (currentProductList === undefined) {\n * // productList not set before or expired.\n * }\n * ```\n */\n getValue: getDetail,\n\n /**\n * Set context value and send signal to all consumers.\n *\n * Signal detail changed immediately without any debounce.\n *\n * Example:\n *\n * ```ts\n * contextProvider.setValue<ProductListType>('product-list', newProductList);\n * ```\n */\n setValue: dispatch,\n\n /**\n * Clear current context value without send signal to all consumers.\n *\n * new subscriber options.receivePrevious not work until new signal\n *\n * Example:\n *\n * ```ts\n * contextProvider.expire('product-list');\n * ```\n */\n expire: clearDetail,\n\n /**\n * Waits until the context value changes.\n *\n * Example:\n *\n * ```ts\n * const newProductList = await contextProvider.untilChange<ProductListType>('product-list');\n * ```\n */\n untilChange: untilNext,\n\n /**\n * Bind this interface to special context.\n *\n * Example:\n *\n * ```ts\n * const productListProvider = contextProvider.bind<ProductListType>('product-list');\n * ```\n */\n bind: <T extends Stringifyable>(contextId: string) =>({\n /**\n * Context signal Id.\n */\n id: contextId,\n\n /**\n * Get context value.\n *\n * Return undefined if context not set before or expired.\n *\n * Example:\n *\n * ```ts\n * const currentProductList = productListProvider.getValue();\n * if (currentProductList === undefined) {\n * // productList not set before or expired.\n * }\n * ```\n */\n getValue: getDetail.bind(null, contextId) as OmitFirstParam<typeof getDetail<T>>,\n\n /**\n * Set context value and send signal to all consumers.\n *\n * Signal detail changed immediately without any debounce.\n *\n * Example:\n *\n * ```ts\n * productListProvider.setValue(newProductList);\n * ```\n */\n setValue: dispatch.bind(null, contextId) as OmitFirstParam<typeof dispatch<T>>,\n\n /**\n * Clear current context value without send signal to all consumers.\n *\n * new subscriber options.receivePrevious not work until new signal\n *\n * Example:\n *\n * ```ts\n * productListProvider.expire();\n * ```\n */\n expire: clearDetail.bind(null, contextId),\n\n /**\n * Waits until the context value changes.\n *\n * Example:\n *\n * ```ts\n * const newProductList = await productListProvider.untilChange();\n * ```\n */\n untilChange: untilNext.bind(null, contextId) as OmitFirstParam<typeof untilNext<T>>,\n } as const),\n} as const;\n"]}
|
package/core.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ export declare const getSignalObject: <T extends Stringifyable>(id: string) => S
|
|
|
19
19
|
*
|
|
20
20
|
* Used inside dispatch, Don't use it directly.
|
|
21
21
|
*/
|
|
22
|
-
export declare const _callListeners: <T extends Stringifyable>(signal: SignalObject<T
|
|
22
|
+
export declare const _callListeners: <T extends Stringifyable>(signal: SignalObject<T>, detail: T) => void;
|
|
23
23
|
/**
|
|
24
24
|
* Subscribe new signal listener to a signal, work like addEventListener.
|
|
25
25
|
*
|
|
@@ -120,7 +120,7 @@ export declare const setContextProvider: <TContext extends Stringifyable, TRques
|
|
|
120
120
|
*/
|
|
121
121
|
export declare const defineCommand: <TArgument extends StringifyableRecord, TReturn extends Stringifyable>(signalId: string, signalProvider: ProviderFunction<TArgument & {
|
|
122
122
|
_callbackSignalId?: string | undefined;
|
|
123
|
-
}, TReturn>, options?: Partial<Pick<ProviderOptions, 'debounce'>>) =>
|
|
123
|
+
}, TReturn>, options?: Partial<Pick<ProviderOptions, 'debounce'>>) => ListenerSpec;
|
|
124
124
|
/**
|
|
125
125
|
* Dispatch request context signal with requestParam as detail.
|
|
126
126
|
*
|
package/core.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["src/core.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["src/core.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,eAAe,EACf,gBAAgB,EAEhB,gBAAgB,EAChB,YAAY,EACZ,gBAAgB,EAChB,eAAe,EAEf,YAAY,EACb,MAAM,WAAW,CAAC;AACnB,OAAO,KAAK,EAAC,aAAa,EAAE,mBAAmB,EAAC,MAAM,cAAc,CAAC;AAmBrE,eAAO,MAAM,MAAM,uCAAgC,CAAC;AAEpD;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,eAAe,gCAAiC,MAAM,oBAWlE,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,cAAc,mEAAkE,IA0B5F,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,SAAS,sCACV,MAAM,mDAEP,QAAQ,gBAAgB,CAAC,KACjC,YAuDF,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,WAAW,aAAc,YAAY,KAAG,IAOpD,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,kBAAkB,aAAc,MAAM,KAAG,IAMrD,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,QAAQ,sCACT,MAAM,uBAEP,QAAQ,eAAe,CAAC,KAChC,IAoCF,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,SAAS,sCAAuC,MAAM,kBAElE,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,SAAS,sCAAuC,MAAM,eASlE,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,kBAAkB,4EACnB,MAAM,wEAEP,QAAQ,eAAe,CAAC,KAChC,IAkBF,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,aAAa,mFACd,MAAM;;uBAEP,QAAQ,KAAK,eAAe,EAAE,UAAU,CAAC,CAAC,KAClD,YAuBF,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,cAAc,8CACd,MAAM,oCAER,QAAQ,eAAe,CAAC,KAChC,IAGF,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,qDACd,MAAM,iCAEhB,IAGF,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,0BAA0B,oFAI1B,MAAM,iDAqBlB,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,WAAW,aAAc,MAAM,KAAG,IAI9C,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,aAAa,aAAc,MAAM,KAAG,IAMhD,CAAC"}
|
package/core.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createLogger, globalAlwatr } from '@alwatr/logger';
|
|
2
|
+
import { requestAnimationFrame } from '@alwatr/util';
|
|
2
3
|
globalAlwatr.registeredList.push({
|
|
3
4
|
name: '@alwatr/signal',
|
|
4
5
|
version: _ALWATR_VERSION_,
|
|
@@ -42,14 +43,8 @@ export const getSignalObject = (id) => {
|
|
|
42
43
|
*
|
|
43
44
|
* Used inside dispatch, Don't use it directly.
|
|
44
45
|
*/
|
|
45
|
-
export const _callListeners = (signal) => {
|
|
46
|
-
logger.logMethodArgs('_callListeners', { signalId: signal.id, signalDetail:
|
|
47
|
-
if (signal.detail === undefined) {
|
|
48
|
-
logger.accident('_callListeners', 'no_signal_detail', 'signal must have a detail', {
|
|
49
|
-
signalId: signal.id,
|
|
50
|
-
});
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
46
|
+
export const _callListeners = (signal, detail) => {
|
|
47
|
+
logger.logMethodArgs('_callListeners', { signalId: signal.id, signalDetail: detail });
|
|
53
48
|
const removeList = [];
|
|
54
49
|
for (const listener of signal.listenerList) {
|
|
55
50
|
if (listener.disabled)
|
|
@@ -57,7 +52,7 @@ export const _callListeners = (signal) => {
|
|
|
57
52
|
if (listener.once)
|
|
58
53
|
removeList.push(listener);
|
|
59
54
|
try {
|
|
60
|
-
const ret = listener.callback(
|
|
55
|
+
const ret = listener.callback(detail);
|
|
61
56
|
if (ret instanceof Promise) {
|
|
62
57
|
ret.catch((err) => logger.error('_callListeners', 'call_listener_failed', err, {
|
|
63
58
|
signalId: signal.id,
|
|
@@ -87,7 +82,7 @@ export const subscribe = (signalId, listenerCallback, options = {}) => {
|
|
|
87
82
|
var _a, _b, _c, _d;
|
|
88
83
|
(_a = options.once) !== null && _a !== void 0 ? _a : (options.once = false);
|
|
89
84
|
(_b = options.disabled) !== null && _b !== void 0 ? _b : (options.disabled = false);
|
|
90
|
-
(_c = options.receivePrevious) !== null && _c !== void 0 ? _c : (options.receivePrevious = '
|
|
85
|
+
(_c = options.receivePrevious) !== null && _c !== void 0 ? _c : (options.receivePrevious = 'NextCycle');
|
|
91
86
|
(_d = options.priority) !== null && _d !== void 0 ? _d : (options.priority = false);
|
|
92
87
|
logger.logMethodArgs('subscribe', { signalId, options });
|
|
93
88
|
const signal = getSignalObject(signalId);
|
|
@@ -189,16 +184,23 @@ export const dispatch = (signalId, detail, options = {}) => {
|
|
|
189
184
|
signal.detail = detail;
|
|
190
185
|
if (signal.disabled)
|
|
191
186
|
return; // signal is disabled.
|
|
192
|
-
// Simple debounce noise filtering
|
|
193
|
-
if (options.debounce !== 'No' && signal.debounced === true)
|
|
194
|
-
return; // last dispatch in progress.
|
|
195
187
|
if (options.debounce === 'No') {
|
|
196
|
-
return _callListeners(signal);
|
|
188
|
+
return _callListeners(signal, detail);
|
|
189
|
+
}
|
|
190
|
+
// else
|
|
191
|
+
if (options.debounce === 'NextCycle') {
|
|
192
|
+
setTimeout(_callListeners, 0, signal, detail);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
// else
|
|
196
|
+
if (signal.debounced === true) {
|
|
197
|
+
return; // last dispatch in progress.
|
|
197
198
|
}
|
|
198
199
|
// else
|
|
199
200
|
signal.debounced = true;
|
|
200
201
|
const callListeners = () => {
|
|
201
|
-
|
|
202
|
+
var _a;
|
|
203
|
+
_callListeners(signal, (_a = signal.detail) !== null && _a !== void 0 ? _a : detail);
|
|
202
204
|
signal.debounced = false;
|
|
203
205
|
};
|
|
204
206
|
options.debounce === 'AnimationFrame'
|
|
@@ -289,7 +291,7 @@ export const defineCommand = (signalId, signalProvider, options = {}) => {
|
|
|
289
291
|
logger.logMethodArgs('defineCommand', { commandId: signalId, options });
|
|
290
292
|
const requestSignalId = 'request-' + signalId;
|
|
291
293
|
removeAllListeners(requestSignalId);
|
|
292
|
-
subscribe(requestSignalId, async (argumentObject) => {
|
|
294
|
+
return subscribe(requestSignalId, async (argumentObject) => {
|
|
293
295
|
clearDetail(requestSignalId); // clean argumentObject from memory
|
|
294
296
|
if (argumentObject._callbackSignalId == null) {
|
|
295
297
|
signalProvider(argumentObject);
|
|
@@ -312,7 +314,7 @@ export const defineCommand = (signalId, signalProvider, options = {}) => {
|
|
|
312
314
|
*/
|
|
313
315
|
export const requestContext = (contextId, requestParam, options = {}) => {
|
|
314
316
|
logger.logMethodArgs('requestContext', { contextId, requestParam });
|
|
315
|
-
return dispatch(contextId
|
|
317
|
+
return dispatch(`request-${contextId}`, requestParam, options);
|
|
316
318
|
};
|
|
317
319
|
/**
|
|
318
320
|
* Dispatch request command signal with commandArgument as detail.
|
|
@@ -346,7 +348,7 @@ export const requestCommandWithResponse = async (commandId, commandArgument) =>
|
|
|
346
348
|
dispatch(_requestSignalId, {
|
|
347
349
|
...commandArgument,
|
|
348
350
|
_callbackSignalId,
|
|
349
|
-
}, { debounce: '
|
|
351
|
+
}, { debounce: 'NextCycle' });
|
|
350
352
|
const response = await untilCallback;
|
|
351
353
|
destroySignal(_callbackSignalId);
|
|
352
354
|
return response;
|
package/core.js.map
CHANGED
|
@@ -1 +1 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["import {createLogger, globalAlwatr} from '@alwatr/logger';\n\nimport type {\n DispatchOptions,\n ListenerFunction,\n ListenerObject,\n SubscribeOptions,\n SignalObject,\n ProviderFunction,\n ProviderOptions,\n SignalStorage,\n ListenerSpec,\n} from './type.js';\nimport type {Stringifyable, StringifyableRecord} from '@alwatr/type';\n\nglobalAlwatr.registeredList.push({\n name: '@alwatr/signal',\n version: _ALWATR_VERSION_,\n});\n\n/**\n * Listener `id`\n */\nlet _lastListenerAutoId = 0;\n\nconst debounceTimeout = 5;\n\n/**\n * Signal stack database.\n */\nconst _signalStorage: SignalStorage = {};\n\nexport const logger = createLogger('alwatr/signal');\n\n/**\n * Get signal object by id, If not available, it will create a new signal with default options.\n *\n * Don't use it directly.\n *\n * Example:\n *\n * ```ts\n * const signal = getSignalObject<ContentType>('content-change');\n * signal.disabled = true;\n * ```\n */\nexport const getSignalObject = <T extends Stringifyable>(id: string): SignalObject<T> => {\n let signal = _signalStorage[id] as SignalObject<T> | undefined;\n if (signal == null) {\n signal = _signalStorage[id] = {\n id,\n disabled: false,\n debounced: false,\n listenerList: [],\n };\n }\n return signal;\n};\n\n/**\n * Call all listeners callback of special signal.\n *\n * Used inside dispatch, Don't use it directly.\n */\nexport const _callListeners = <T extends Stringifyable>(signal: SignalObject<T>): void => {\n logger.logMethodArgs('_callListeners', {signalId: signal.id, signalDetail: signal.detail});\n\n if (signal.detail === undefined) {\n logger.accident('_callListeners', 'no_signal_detail', 'signal must have a detail', {\n signalId: signal.id,\n });\n return;\n }\n\n const removeList: Array<ListenerObject<T>> = [];\n\n for (const listener of signal.listenerList) {\n if (listener.disabled) continue;\n if (listener.once) removeList.push(listener);\n try {\n const ret = listener.callback(signal.detail);\n if (ret instanceof Promise) {\n ret.catch((err) =>\n logger.error('_callListeners', 'call_listener_failed', err, {\n signalId: signal.id,\n }),\n );\n }\n }\n catch (err) {\n logger.error('_callListeners', 'call_listener_failed', err, {\n signalId: signal.id,\n });\n }\n }\n\n removeList.forEach((listener) => unsubscribe(listener));\n};\n\n/**\n * Subscribe new signal listener to a signal, work like addEventListener.\n *\n * Example:\n *\n * ```ts\n * const listener = subscribe<ContentType>('content-change', (content) => console.log(content));\n * // ...\n * unsubscribe(listener);\n * ```\n */\nexport const subscribe = <T extends Stringifyable>(\n signalId: string,\n listenerCallback: ListenerFunction<T>,\n options: Partial<SubscribeOptions> = {},\n): ListenerSpec => {\n options.once ??= false;\n options.disabled ??= false;\n options.receivePrevious ??= 'AnimationFrame';\n options.priority ??= false;\n\n logger.logMethodArgs('subscribe', {signalId, options});\n\n const signal = getSignalObject<T>(signalId);\n\n const listener: ListenerObject<T> = {\n id: ++_lastListenerAutoId,\n signalId: signal.id,\n once: options.once,\n disabled: options.disabled,\n callback: listenerCallback,\n };\n\n const callbackCall = signal.detail !== undefined && options.receivePrevious !== 'No';\n if (callbackCall) {\n // Run callback for old dispatch signal\n\n const callback = (): void => {\n try {\n if (signal.detail !== undefined) listenerCallback(signal.detail);\n }\n catch (err) {\n logger.error('subscribe', 'call_signal_callback_failed', err, {\n signalId: signal.id,\n });\n }\n };\n\n if (options.receivePrevious === 'AnimationFrame') {\n requestAnimationFrame(callback);\n }\n else {\n setTimeout(callback, options.receivePrevious === 'NextCycle' ? 0 : debounceTimeout);\n }\n }\n\n // if once then must remove listener after fist callback called! then why push it to listenerList?!\n if (!(callbackCall && options.once)) {\n if (options.priority === true) {\n signal.listenerList.unshift(listener);\n }\n else {\n signal.listenerList.push(listener);\n }\n }\n\n return {\n id: listener.id,\n signalId: listener.signalId,\n };\n};\n\n/**\n * Unsubscribe listener from signal, work like removeEventListener.\n *\n * Example:\n *\n * ```ts\n * const listener = subscribe<ContentType>('content-change', (content) => console.log(content));\n * // ...\n * unsubscribe(listener);\n * ```\n */\nexport const unsubscribe = (listener: ListenerSpec): void => {\n logger.logMethodArgs('unsubscribe', listener);\n const signal = getSignalObject(listener.signalId);\n const listenerIndex = signal.listenerList.findIndex((_listener) => _listener.id === listener.id);\n if (listenerIndex !== -1) {\n void signal.listenerList.splice(listenerIndex, 1);\n }\n};\n\n/**\n * Unsubscribe all listener from a signal, clear all listeners.\n *\n * Example:\n *\n * ```ts\n * removeAllListeners('content-change');\n * ```\n */\nexport const removeAllListeners = (signalId: string): void => {\n logger.logMethodArgs('removeAllListeners', signalId);\n const signal = getSignalObject(signalId);\n if (signal.listenerList.length === 0) return;\n signal.listenerList.length = 0;\n signal.listenerList = [];\n};\n\n/**\n * Dispatch (send) signal to all listeners.\n *\n * Signal detail changed immediately without any debounce.\n *\n * Example:\n *\n * ```ts\n * dispatch<ContentType>('content-change', newContent);\n * ```\n */\nexport const dispatch = <T extends Stringifyable>(\n signalId: string,\n detail: T,\n options: Partial<DispatchOptions> = {},\n): void => {\n options.debounce ??= 'AnimationFrame';\n\n logger.logMethodArgs('dispatch', {signalId, detail, options});\n\n const signal = getSignalObject<T>(signalId);\n\n // set detail before check signal.debounced for act like throttle (call listeners with last dispatch detail).\n signal.detail = detail;\n\n if (signal.disabled) return; // signal is disabled.\n\n // Simple debounce noise filtering\n if (options.debounce !== 'No' && signal.debounced === true) return; // last dispatch in progress.\n\n if (options.debounce === 'No') {\n return _callListeners(signal);\n }\n // else\n signal.debounced = true;\n const callListeners = (): void => {\n _callListeners(signal);\n signal.debounced = false;\n };\n options.debounce === 'AnimationFrame'\n ? requestAnimationFrame(callListeners)\n : setTimeout(callListeners, debounceTimeout);\n};\n\n/**\n * Get current signal detail/value.\n *\n * Return undefined if signal not dispatched before or expired.\n *\n * Example:\n *\n * ```ts\n * const currentContent = getDetail<ContentType>('content-change');\n * if (currentContent === undefined) {\n * // signal not dispatched yet\n * }\n * ```\n */\nexport const getDetail = <T extends Stringifyable>(signalId: string): T | undefined => {\n return getSignalObject<T>(signalId).detail;\n};\n\n/**\n * Get the detail/value of the next received signal.\n *\n * Example:\n *\n * ```ts\n * const newContent = await untilNext<ContentType>('content-change');\n * ```\n */\nexport const untilNext = <T extends Stringifyable>(signalId: string): Promise<T> => {\n return new Promise((resolve) => {\n logger.logMethodArgs('untilNext', signalId);\n subscribe<T>(signalId, resolve, {\n once: true,\n priority: true,\n receivePrevious: 'No',\n });\n });\n};\n\n/**\n * Defines the provider of the context signal that will be called when the context requested.\n * Subscribe to `request-signalId`.\n *\n * Example:\n *\n * ```ts\n * setContextProvider('content-change', async (requestParam) => await fetchNewContent(requestParam));\n * ```\n */\nexport const setContextProvider = <TContext extends Stringifyable, TRquest extends Stringifyable>(\n signalId: string,\n signalProvider: ProviderFunction<TRquest, TContext | void>,\n options: Partial<ProviderOptions> = {},\n): void => {\n options.debounce ??= 'AnimationFrame';\n options.receivePrevious ??= 'AnimationFrame';\n logger.logMethodArgs('setContextProvider', {signalId, options});\n const requestSignalId = 'request-' + signalId;\n removeAllListeners(requestSignalId);\n subscribe<TRquest>(\n requestSignalId,\n async (argumentObject): Promise<void> => {\n const signalDetail = await signalProvider(argumentObject);\n if (signalDetail !== undefined) {\n dispatch<TContext>(signalId, signalDetail, {debounce: options.debounce});\n }\n },\n {\n receivePrevious: options.receivePrevious,\n },\n );\n};\n\n/**\n * Defines the command and dispatch returned value.\n *\n * Subscribe commandFunction to request-command-signal and dispatch callback-signal with commandFunction return value.\n *\n * Example:\n *\n * ```ts\n * defineCommand<TArgument, TReturn>(\n * 'show-prompt',\n * async (argumentObject) => {\n * return await showPrompt(argumentObject);\n * },\n * );\n * ```\n */\nexport const defineCommand = <TArgument extends StringifyableRecord, TReturn extends Stringifyable>(\n signalId: string,\n signalProvider: ProviderFunction<TArgument & {_callbackSignalId?: string}, TReturn>,\n options: Partial<Pick<ProviderOptions, 'debounce'>> = {},\n): void => {\n options.debounce ??= 'AnimationFrame';\n logger.logMethodArgs('defineCommand', {commandId: signalId, options});\n const requestSignalId = 'request-' + signalId;\n removeAllListeners(requestSignalId);\n subscribe<TArgument & {_callbackSignalId?: string}>(\n requestSignalId,\n async (argumentObject) => {\n clearDetail(requestSignalId); // clean argumentObject from memory\n if (argumentObject._callbackSignalId == null) {\n signalProvider(argumentObject);\n }\n else {\n // prettier-ignore\n dispatch<TReturn>(\n argumentObject._callbackSignalId,\n await signalProvider(argumentObject),\n {debounce: options.debounce},\n );\n }\n },\n {receivePrevious: 'NextCycle'},\n );\n};\n\n/**\n * Dispatch request context signal with requestParam as detail.\n *\n * Example:\n *\n * ```ts\n * requestContext<RequestParamType>('content-change', {foo: 'bar'});\n * const newContent = await untilNext<ContentType>('content-change');\n * ```\n */\nexport const requestContext = <TRequest extends Stringifyable>(\n contextId: string,\n requestParam: TRequest,\n options: Partial<DispatchOptions> = {},\n): void => {\n logger.logMethodArgs('requestContext', {contextId, requestParam});\n return dispatch<TRequest>(contextId, requestParam, options);\n};\n\n/**\n * Dispatch request command signal with commandArgument as detail.\n *\n * Example:\n *\n * ```ts\n * requestCommand<ArgumentType>('show-dialog', {foo: 'bar'});\n * ```\n */\nexport const requestCommand = <TArgument extends StringifyableRecord>(\n commandId: string,\n commandArgument: TArgument,\n): void => {\n logger.logMethodArgs('requestCommand', {commandId, commandArgument});\n dispatch<TArgument>(`request-${commandId}`, commandArgument, {debounce: 'No'});\n};\n\n/**\n * Dispatch request command signal with commandArgument as detail and return untilNext of callback signal.\n *\n * Request command and wait for answer.\n *\n * Example:\n *\n * ```ts\n * const response = await requestCommandWithResponse<ArgumentType, ReturnType>('show-dialog', {foo: 'bar'});\n * ```\n */\nexport const requestCommandWithResponse = async <\n TArgument extends StringifyableRecord,\n TReturn extends Stringifyable\n>(\n commandId: string,\n commandArgument: TArgument,\n): Promise<TReturn> => {\n logger.logMethodArgs('requestCommand', {commandId, commandArgument});\n\n const _requestSignalId = `request-${commandId}`;\n const _callbackSignalId = `callback-${commandId}-${++_lastListenerAutoId}`;\n const untilCallback = untilNext<TReturn>(_callbackSignalId);\n\n dispatch<TArgument & {_callbackSignalId: string}>(\n _requestSignalId,\n {\n ...commandArgument,\n _callbackSignalId,\n },\n {debounce: 'No'},\n );\n\n const response = await untilCallback;\n destroySignal(_callbackSignalId);\n return response;\n};\n\n/**\n * Clear current signal detail without dispatch new signal.\n *\n * new subscriber options.receivePrevious not work until new signal\n *\n * Example:\n *\n * ```ts\n * clearDetail('product-list');\n * ```\n */\nexport const clearDetail = (signalId: string): void => {\n logger.logMethodArgs('expire', signalId);\n const signal = getSignalObject(signalId);\n delete signal.detail;\n};\n\n/**\n * Delete signal object with detail and listeners and options.\n *\n * new subscriber options.receivePrevious not work until new signal\n *\n * Example:\n *\n * ```ts\n * destroySignal('product-list');\n * ```\n */\nexport const destroySignal = (signalId: string): void => {\n logger.logMethodArgs('destroySignal', signalId);\n const signal = _signalStorage[signalId];\n if (signal == null) return;\n signal.listenerList.length = 0;\n delete _signalStorage[signalId];\n};\n"]}
|
|
1
|
+
{"version":3,"file":"core.js","sourceRoot":"","sources":["src/core.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAE,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAC1D,OAAO,EAAC,qBAAqB,EAAC,MAAM,cAAc,CAAC;AAenD,YAAY,CAAC,cAAc,CAAC,IAAI,CAAC;IAC/B,IAAI,EAAE,gBAAgB;IACtB,OAAO,EAAE,gBAAgB;CAC1B,CAAC,CAAC;AAEH;;GAEG;AACH,IAAI,mBAAmB,GAAG,CAAC,CAAC;AAE5B,MAAM,eAAe,GAAG,CAAC,CAAC;AAE1B;;GAEG;AACH,MAAM,cAAc,GAAkB,EAAE,CAAC;AAEzC,MAAM,CAAC,MAAM,MAAM,GAAG,YAAY,CAAC,eAAe,CAAC,CAAC;AAEpD;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAA0B,EAAU,EAAmB,EAAE;IACtF,IAAI,MAAM,GAAG,cAAc,CAAC,EAAE,CAAgC,CAAC;IAC/D,IAAI,MAAM,IAAI,IAAI,EAAE;QAClB,MAAM,GAAG,cAAc,CAAC,EAAE,CAAC,GAAG;YAC5B,EAAE;YACF,QAAQ,EAAE,KAAK;YACf,SAAS,EAAE,KAAK;YAChB,YAAY,E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AAG,eAAe,CAAC,QAAQ,CAAC,CAAC;IACzC,OAAO,MAAM,CAAC,MAAM,CAAC;AACvB,CAAC,CAAC;AAEF;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,QAAgB,EAAQ,EAAE;IACtD,MAAM,CAAC,aAAa,CAAC,eAAe,EAAE,QAAQ,CAAC,CAAC;IAChD,MAAM,MAAM,GAAG,cAAc,CAAC,QAAQ,CAAC,CAAC;IACxC,IAAI,MAAM,IAAI,IAAI;QAAE,OAAO;IAC3B,MAAM,CAAC,YAAY,CAAC,MAAM,GAAG,CAAC,CAAC;IAC/B,OAAO,cAAc,CAAC,QAAQ,CAAC,CAAC;AAClC,CAAC,CAAC","sourcesContent":["import {createLogger, globalAlwatr} from '@alwatr/logger';\nimport {requestAnimationFrame} from '@alwatr/util';\n\nimport type {\n DispatchOptions,\n ListenerFunction,\n ListenerObject,\n SubscribeOptions,\n SignalObject,\n ProviderFunction,\n ProviderOptions,\n SignalStorage,\n ListenerSpec,\n} from './type.js';\nimport type {Stringifyable, StringifyableRecord} from '@alwatr/type';\n\nglobalAlwatr.registeredList.push({\n name: '@alwatr/signal',\n version: _ALWATR_VERSION_,\n});\n\n/**\n * Listener `id`\n */\nlet _lastListenerAutoId = 0;\n\nconst debounceTimeout = 5;\n\n/**\n * Signal stack database.\n */\nconst _signalStorage: SignalStorage = {};\n\nexport const logger = createLogger('alwatr/signal');\n\n/**\n * Get signal object by id, If not available, it will create a new signal with default options.\n *\n * Don't use it directly.\n *\n * Example:\n *\n * ```ts\n * const signal = getSignalObject<ContentType>('content-change');\n * signal.disabled = true;\n * ```\n */\nexport const getSignalObject = <T extends Stringifyable>(id: string): SignalObject<T> => {\n let signal = _signalStorage[id] as SignalObject<T> | undefined;\n if (signal == null) {\n signal = _signalStorage[id] = {\n id,\n disabled: false,\n debounced: false,\n listenerList: [],\n };\n }\n return signal;\n};\n\n/**\n * Call all listeners callback of special signal.\n *\n * Used inside dispatch, Don't use it directly.\n */\nexport const _callListeners = <T extends Stringifyable>(signal: SignalObject<T>, detail: T): void => {\n logger.logMethodArgs('_callListeners', {signalId: signal.id, signalDetail: detail});\n\n const removeList: Array<ListenerObject<T>> = [];\n\n for (const listener of signal.listenerList) {\n if (listener.disabled) continue;\n if (listener.once) removeList.push(listener);\n try {\n const ret = listener.callback(detail);\n if (ret instanceof Promise) {\n ret.catch((err) =>\n logger.error('_callListeners', 'call_listener_failed', err, {\n signalId: signal.id,\n }),\n );\n }\n }\n catch (err) {\n logger.error('_callListeners', 'call_listener_failed', err, {\n signalId: signal.id,\n });\n }\n }\n\n removeList.forEach((listener) => unsubscribe(listener));\n};\n\n/**\n * Subscribe new signal listener to a signal, work like addEventListener.\n *\n * Example:\n *\n * ```ts\n * const listener = subscribe<ContentType>('content-change', (content) => console.log(content));\n * // ...\n * unsubscribe(listener);\n * ```\n */\nexport const subscribe = <T extends Stringifyable>(\n signalId: string,\n listenerCallback: ListenerFunction<T>,\n options: Partial<SubscribeOptions> = {},\n): ListenerSpec => {\n options.once ??= false;\n options.disabled ??= false;\n options.receivePrevious ??= 'NextCycle';\n options.priority ??= false;\n\n logger.logMethodArgs('subscribe', {signalId, options});\n\n const signal = getSignalObject<T>(signalId);\n\n const listener: ListenerObject<T> = {\n id: ++_lastListenerAutoId,\n signalId: signal.id,\n once: options.once,\n disabled: options.disabled,\n callback: listenerCallback,\n };\n\n const callbackCall = signal.detail !== undefined && options.receivePrevious !== 'No';\n if (callbackCall) {\n // Run callback for old dispatch signal\n\n const callback = (): void => {\n try {\n if (signal.detail !== undefined) listenerCallback(signal.detail);\n }\n catch (err) {\n logger.error('subscribe', 'call_signal_callback_failed', err, {\n signalId: signal.id,\n });\n }\n };\n\n if (options.receivePrevious === 'AnimationFrame') {\n requestAnimationFrame(callback);\n }\n else {\n setTimeout(callback, options.receivePrevious === 'NextCycle' ? 0 : debounceTimeout);\n }\n }\n\n // if once then must remove listener after fist callback called! then why push it to listenerList?!\n if (!(callbackCall && options.once)) {\n if (options.priority === true) {\n signal.listenerList.unshift(listener);\n }\n else {\n signal.listenerList.push(listener);\n }\n }\n\n return {\n id: listener.id,\n signalId: listener.signalId,\n };\n};\n\n/**\n * Unsubscribe listener from signal, work like removeEventListener.\n *\n * Example:\n *\n * ```ts\n * const listener = subscribe<ContentType>('content-change', (content) => console.log(content));\n * // ...\n * unsubscribe(listener);\n * ```\n */\nexport const unsubscribe = (listener: ListenerSpec): void => {\n logger.logMethodArgs('unsubscribe', listener);\n const signal = getSignalObject(listener.signalId);\n const listenerIndex = signal.listenerList.findIndex((_listener) => _listener.id === listener.id);\n if (listenerIndex !== -1) {\n void signal.listenerList.splice(listenerIndex, 1);\n }\n};\n\n/**\n * Unsubscribe all listener from a signal, clear all listeners.\n *\n * Example:\n *\n * ```ts\n * removeAllListeners('content-change');\n * ```\n */\nexport const removeAllListeners = (signalId: string): void => {\n logger.logMethodArgs('removeAllListeners', signalId);\n const signal = getSignalObject(signalId);\n if (signal.listenerList.length === 0) return;\n signal.listenerList.length = 0;\n signal.listenerList = [];\n};\n\n/**\n * Dispatch (send) signal to all listeners.\n *\n * Signal detail changed immediately without any debounce.\n *\n * Example:\n *\n * ```ts\n * dispatch<ContentType>('content-change', newContent);\n * ```\n */\nexport const dispatch = <T extends Stringifyable>(\n signalId: string,\n detail: T,\n options: Partial<DispatchOptions> = {},\n): void => {\n options.debounce ??= 'AnimationFrame';\n\n logger.logMethodArgs('dispatch', {signalId, detail, options});\n\n const signal = getSignalObject<T>(signalId);\n\n // set detail before check signal.debounced for act like throttle (call listeners with last dispatch detail).\n signal.detail = detail;\n\n if (signal.disabled) return; // signal is disabled.\n\n if (options.debounce === 'No') {\n return _callListeners(signal, detail);\n }\n\n // else\n if (options.debounce === 'NextCycle') {\n setTimeout(_callListeners, 0, signal, detail);\n return;\n }\n\n // else\n if (signal.debounced === true) {\n return; // last dispatch in progress.\n }\n\n // else\n signal.debounced = true;\n const callListeners = (): void => {\n _callListeners(signal, signal.detail ?? detail);\n signal.debounced = false;\n };\n options.debounce === 'AnimationFrame'\n ? requestAnimationFrame(callListeners)\n : setTimeout(callListeners, debounceTimeout);\n};\n\n/**\n * Get current signal detail/value.\n *\n * Return undefined if signal not dispatched before or expired.\n *\n * Example:\n *\n * ```ts\n * const currentContent = getDetail<ContentType>('content-change');\n * if (currentContent === undefined) {\n * // signal not dispatched yet\n * }\n * ```\n */\nexport const getDetail = <T extends Stringifyable>(signalId: string): T | undefined => {\n return getSignalObject<T>(signalId).detail;\n};\n\n/**\n * Get the detail/value of the next received signal.\n *\n * Example:\n *\n * ```ts\n * const newContent = await untilNext<ContentType>('content-change');\n * ```\n */\nexport const untilNext = <T extends Stringifyable>(signalId: string): Promise<T> => {\n return new Promise((resolve) => {\n logger.logMethodArgs('untilNext', signalId);\n subscribe<T>(signalId, resolve, {\n once: true,\n priority: true,\n receivePrevious: 'No',\n });\n });\n};\n\n/**\n * Defines the provider of the context signal that will be called when the context requested.\n * Subscribe to `request-signalId`.\n *\n * Example:\n *\n * ```ts\n * setContextProvider('content-change', async (requestParam) => await fetchNewContent(requestParam));\n * ```\n */\nexport const setContextProvider = <TContext extends Stringifyable, TRquest extends Stringifyable>(\n signalId: string,\n signalProvider: ProviderFunction<TRquest, TContext | void>,\n options: Partial<ProviderOptions> = {},\n): void => {\n options.debounce ??= 'AnimationFrame';\n options.receivePrevious ??= 'AnimationFrame';\n logger.logMethodArgs('setContextProvider', {signalId, options});\n const requestSignalId = 'request-' + signalId;\n removeAllListeners(requestSignalId);\n subscribe<TRquest>(\n requestSignalId,\n async (argumentObject): Promise<void> => {\n const signalDetail = await signalProvider(argumentObject);\n if (signalDetail !== undefined) {\n dispatch<TContext>(signalId, signalDetail, {debounce: options.debounce});\n }\n },\n {\n receivePrevious: options.receivePrevious,\n },\n );\n};\n\n/**\n * Defines the command and dispatch returned value.\n *\n * Subscribe commandFunction to request-command-signal and dispatch callback-signal with commandFunction return value.\n *\n * Example:\n *\n * ```ts\n * defineCommand<TArgument, TReturn>(\n * 'show-prompt',\n * async (argumentObject) => {\n * return await showPrompt(argumentObject);\n * },\n * );\n * ```\n */\nexport const defineCommand = <TArgument extends StringifyableRecord, TReturn extends Stringifyable>(\n signalId: string,\n signalProvider: ProviderFunction<TArgument & {_callbackSignalId?: string}, TReturn>,\n options: Partial<Pick<ProviderOptions, 'debounce'>> = {},\n): ListenerSpec => {\n options.debounce ??= 'AnimationFrame';\n logger.logMethodArgs('defineCommand', {commandId: signalId, options});\n const requestSignalId = 'request-' + signalId;\n removeAllListeners(requestSignalId);\n return subscribe<TArgument & {_callbackSignalId?: string}>(\n requestSignalId,\n async (argumentObject) => {\n clearDetail(requestSignalId); // clean argumentObject from memory\n if (argumentObject._callbackSignalId == null) {\n signalProvider(argumentObject);\n }\n else {\n // prettier-ignore\n dispatch<TReturn>(\n argumentObject._callbackSignalId,\n await signalProvider(argumentObject),\n {debounce: options.debounce},\n );\n }\n },\n {receivePrevious: 'NextCycle'},\n );\n};\n\n/**\n * Dispatch request context signal with requestParam as detail.\n *\n * Example:\n *\n * ```ts\n * requestContext<RequestParamType>('content-change', {foo: 'bar'});\n * const newContent = await untilNext<ContentType>('content-change');\n * ```\n */\nexport const requestContext = <TRequest extends Stringifyable>(\n contextId: string,\n requestParam: TRequest,\n options: Partial<DispatchOptions> = {},\n): void => {\n logger.logMethodArgs('requestContext', {contextId, requestParam});\n return dispatch<TRequest>(`request-${contextId}`, requestParam, options);\n};\n\n/**\n * Dispatch request command signal with commandArgument as detail.\n *\n * Example:\n *\n * ```ts\n * requestCommand<ArgumentType>('show-dialog', {foo: 'bar'});\n * ```\n */\nexport const requestCommand = <TArgument extends StringifyableRecord>(\n commandId: string,\n commandArgument: TArgument,\n): void => {\n logger.logMethodArgs('requestCommand', {commandId, commandArgument});\n dispatch<TArgument>(`request-${commandId}`, commandArgument, {debounce: 'No'});\n};\n\n/**\n * Dispatch request command signal with commandArgument as detail and return untilNext of callback signal.\n *\n * Request command and wait for answer.\n *\n * Example:\n *\n * ```ts\n * const response = await requestCommandWithResponse<ArgumentType, ReturnType>('show-dialog', {foo: 'bar'});\n * ```\n */\nexport const requestCommandWithResponse = async <\n TArgument extends StringifyableRecord,\n TReturn extends Stringifyable\n>(\n commandId: string,\n commandArgument: TArgument,\n): Promise<TReturn> => {\n logger.logMethodArgs('requestCommand', {commandId, commandArgument});\n\n const _requestSignalId = `request-${commandId}`;\n const _callbackSignalId = `callback-${commandId}-${++_lastListenerAutoId}`;\n const untilCallback = untilNext<TReturn>(_callbackSignalId);\n\n dispatch<TArgument & {_callbackSignalId: string}>(\n _requestSignalId,\n {\n ...commandArgument,\n _callbackSignalId,\n },\n {debounce: 'NextCycle'},\n );\n\n const response = await untilCallback;\n destroySignal(_callbackSignalId);\n return response;\n};\n\n/**\n * Clear current signal detail without dispatch new signal.\n *\n * new subscriber options.receivePrevious not work until new signal\n *\n * Example:\n *\n * ```ts\n * clearDetail('product-list');\n * ```\n */\nexport const clearDetail = (signalId: string): void => {\n logger.logMethodArgs('expire', signalId);\n const signal = getSignalObject(signalId);\n delete signal.detail;\n};\n\n/**\n * Delete signal object with detail and listeners and options.\n *\n * new subscriber options.receivePrevious not work until new signal\n *\n * Example:\n *\n * ```ts\n * destroySignal('product-list');\n * ```\n */\nexport const destroySignal = (signalId: string): void => {\n logger.logMethodArgs('destroySignal', signalId);\n const signal = _signalStorage[signalId];\n if (signal == null) return;\n signal.listenerList.length = 0;\n delete _signalStorage[signalId];\n};\n"]}
|
package/index.d.ts
CHANGED
|
@@ -6,5 +6,5 @@ export * from './event-listener.js';
|
|
|
6
6
|
export * from './event-trigger.js';
|
|
7
7
|
export * from './requestable-context-consumer.js';
|
|
8
8
|
export * from './requestable-context-provider.js';
|
|
9
|
-
export type { ListenerSpec } from './type.js';
|
|
9
|
+
export type { ListenerSpec, DebounceType, DispatchOptions } from './type.js';
|
|
10
10
|
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mCAAmC,CAAC;AAClD,cAAc,mCAAmC,CAAC;AAClD,YAAY,EAAC,YAAY,EAAC,MAAM,WAAW,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mCAAmC,CAAC;AAClD,cAAc,mCAAmC,CAAC;AAClD,YAAY,EAAC,YAAY,EAAE,YAAY,EAAE,eAAe,EAAC,MAAM,WAAW,CAAC"}
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mCAAmC,CAAC;AAClD,cAAc,mCAAmC,CAAC;AAGlD;;;;;;;EAOE","sourcesContent":["export * from './command-handler.js';\nexport * from './command-trigger.js';\nexport * from './context-consumer.js';\nexport * from './context-provider.js';\nexport * from './event-listener.js';\nexport * from './event-trigger.js';\nexport * from './requestable-context-consumer.js';\nexport * from './requestable-context-provider.js';\nexport type {ListenerSpec} from './type.js';\n\n/*\nTODO:\n 1. change signal option like disable\n 2. Get signal value without undefined\n (Get signal value from last dispatched signal (if any) or wait for new signal received.)\n 3. dispatched bool\n 4. optional commandProvider debounce\n*/\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mCAAmC,CAAC;AAClD,cAAc,mCAAmC,CAAC;AAGlD;;;;;;;EAOE","sourcesContent":["export * from './command-handler.js';\nexport * from './command-trigger.js';\nexport * from './context-consumer.js';\nexport * from './context-provider.js';\nexport * from './event-listener.js';\nexport * from './event-trigger.js';\nexport * from './requestable-context-consumer.js';\nexport * from './requestable-context-provider.js';\nexport type {ListenerSpec, DebounceType, DispatchOptions} from './type.js';\n\n/*\nTODO:\n 1. change signal option like disable\n 2. Get signal value without undefined\n (Get signal value from last dispatched signal (if any) or wait for new signal received.)\n 3. dispatched bool\n 4. optional commandProvider debounce\n*/\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alwatr/signal",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.30.0",
|
|
4
4
|
"description": "Elegant powerful event system for handle global signals and states written in tiny TypeScript module.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"signal",
|
|
@@ -29,8 +29,10 @@
|
|
|
29
29
|
"url": "https://github.com/AliMD/alwatr/issues"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@alwatr/logger": "^0.
|
|
32
|
+
"@alwatr/logger": "^0.30.0",
|
|
33
|
+
"@alwatr/type": "^0.30.0",
|
|
34
|
+
"@alwatr/util": "^0.30.0",
|
|
33
35
|
"tslib": "^2.5.0"
|
|
34
36
|
},
|
|
35
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "36f55780ccdcb1acc07400b0cdb3fe7b0df56cca"
|
|
36
38
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"requestable-context-consumer.d.ts","sourceRoot":"","sources":["src/requestable-context-consumer.ts"],"names":[],"mappings":"AAGA,OAAO,
|
|
1
|
+
{"version":3,"file":"requestable-context-consumer.d.ts","sourceRoot":"","sources":["src/requestable-context-consumer.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAC,aAAa,EAAiB,MAAM,cAAc,CAAC;AAEhE;;GAEG;AACH,eAAO,MAAM,0BAA0B;IAGrC;;;;;;;;;OASG;;IAGH;;;;;;;;OAQG;8FAC8E,MAAM;QAGrF;;;;;;;;;WASG;;;;;;;;;;;;CAIG,CAAC"}
|
|
@@ -37,7 +37,7 @@ export const requestableContextConsumer = {
|
|
|
37
37
|
* const newProductList = await productListConsumer.untilChange();
|
|
38
38
|
* ```
|
|
39
39
|
*/
|
|
40
|
-
request: requestContext,
|
|
40
|
+
request: requestContext.bind(null, contextId),
|
|
41
41
|
}),
|
|
42
42
|
};
|
|
43
43
|
//# sourceMappingURL=requestable-context-consumer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"requestable-context-consumer.js","sourceRoot":"","sources":["src/requestable-context-consumer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAAC,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAC,cAAc,EAAC,MAAM,WAAW,CAAC;AAIzC;;GAEG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG;IACxC,GAAG,eAAe;IAElB;;;;;;;;;OASG;IACH,OAAO,EAAE,cAAc;IAEvB;;;;;;;;OAQG;IACH,IAAI,EAAE,CAAgE,SAAiB,EAAE,EAAE,CAAA,CAAC;QAC1F,GAAG,eAAe,CAAC,IAAI,CAAW,SAAS,CAAC;QAE5C;;;;;;;;;WASG;QACH,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"requestable-context-consumer.js","sourceRoot":"","sources":["src/requestable-context-consumer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAAC,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAC,cAAc,EAAC,MAAM,WAAW,CAAC;AAIzC;;GAEG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG;IACxC,GAAG,eAAe;IAElB;;;;;;;;;OASG;IACH,OAAO,EAAE,cAAc;IAEvB;;;;;;;;OAQG;IACH,IAAI,EAAE,CAAgE,SAAiB,EAAE,EAAE,CAAA,CAAC;QAC1F,GAAG,eAAe,CAAC,IAAI,CAAW,SAAS,CAAC;QAE5C;;;;;;;;;WASG;QACH,OAAO,EAAE,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,SAAS,CACI;KACvC,CAAA;CACH,CAAC","sourcesContent":["import {contextConsumer} from './context-consumer.js';\nimport {requestContext} from './core.js';\n\nimport type {Stringifyable, OmitFirstParam} from '@alwatr/type';\n\n/**\n * Requestable context consumer interface.\n */\nexport const requestableContextConsumer = {\n ...contextConsumer,\n\n /**\n * Send new context request to the provider.\n *\n * Example:\n *\n * ```ts\n * requestableContextConsumer.request<RequestParamType>('product-list', {foo: 'bar'});\n * const newProductList = await requestableContextConsumer.untilChange<ProductListType>('product-list');\n * ```\n */\n request: requestContext,\n\n /**\n * Bind this interface to special context.\n *\n * Example:\n *\n * ```ts\n * const productListConsumer = requestableContextConsumer.bind<ProductListType>('product-list');\n * ```\n */\n bind: <TContext extends Stringifyable, TRquest extends Stringifyable>(contextId: string) =>({\n ...contextConsumer.bind<TContext>(contextId),\n\n /**\n * Send new context request to the provider.\n *\n * Example:\n *\n * ```ts\n * productListConsumer.request({foo: 'bar'});\n * const newProductList = await productListConsumer.untilChange();\n * ```\n */\n request: requestContext.bind(null, contextId) as\n OmitFirstParam<typeof requestContext<TRquest>>,\n } as const),\n} as const;\n"]}
|
|
@@ -48,9 +48,11 @@ export declare const requestableContextProvider: {
|
|
|
48
48
|
readonly getValue: () => TContext_1 | undefined;
|
|
49
49
|
readonly setValue: (detail: TContext_1, options?: Partial<import("./type.js").DispatchOptions> | undefined) => void;
|
|
50
50
|
readonly expire: () => void;
|
|
51
|
+
readonly untilChange: () => Promise<TContext_1>;
|
|
51
52
|
};
|
|
52
53
|
readonly getValue: <T extends Stringifyable>(signalId: string) => T | undefined;
|
|
53
54
|
readonly setValue: <T_1 extends Stringifyable>(signalId: string, detail: T_1, options?: Partial<import("./type.js").DispatchOptions>) => void;
|
|
54
55
|
readonly expire: (signalId: string) => void;
|
|
56
|
+
readonly untilChange: <T_2 extends Stringifyable>(signalId: string) => Promise<T_2>;
|
|
55
57
|
};
|
|
56
58
|
//# sourceMappingURL=requestable-context-provider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"requestable-context-provider.d.ts","sourceRoot":"","sources":["src/requestable-context-provider.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAC,aAAa,EAAiB,MAAM,cAAc,CAAC;AAEhE;;GAEG;AACH,eAAO,MAAM,0BAA0B;IAGrC;;;;;;;;;;;;;;;OAeG;;IAGH;;;;;;;;OAQG;kGAC8E,MAAM;QAGrF;;;;;;;;;;;;WAYG
|
|
1
|
+
{"version":3,"file":"requestable-context-provider.d.ts","sourceRoot":"","sources":["src/requestable-context-provider.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAC,aAAa,EAAiB,MAAM,cAAc,CAAC;AAEhE;;GAEG;AACH,eAAO,MAAM,0BAA0B;IAGrC;;;;;;;;;;;;;;;OAeG;;IAGH;;;;;;;;OAQG;kGAC8E,MAAM;QAGrF;;;;;;;;;;;;WAYG;;;;;;;;;;;;CAIG,CAAC"}
|
package/type.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { MaybePromise, Stringifyable } from '@alwatr/type';
|
|
2
|
-
export type DebounceType = 'No' | 'AnimationFrame' | 'Timeout';
|
|
1
|
+
import type { MaybePromise, Stringifyable } from '@alwatr/type';
|
|
2
|
+
export type DebounceType = 'No' | 'NextCycle' | 'AnimationFrame' | 'Timeout';
|
|
3
3
|
/**
|
|
4
4
|
* Subscribe options type.
|
|
5
5
|
*/
|
|
@@ -24,18 +24,20 @@ export interface SubscribeOptions {
|
|
|
24
24
|
* Calling this listener (callback) with preview signal value (if dispatched before).
|
|
25
25
|
* If Immediate, the listener will be called immediately without any debounce for preview signal.
|
|
26
26
|
*
|
|
27
|
-
* @default `
|
|
27
|
+
* @default `NextCycle`
|
|
28
28
|
*/
|
|
29
|
-
receivePrevious: DebounceType
|
|
29
|
+
receivePrevious: DebounceType;
|
|
30
30
|
}
|
|
31
31
|
/**
|
|
32
32
|
* dispatchSignal options type
|
|
33
33
|
*/
|
|
34
34
|
export interface DispatchOptions {
|
|
35
35
|
/**
|
|
36
|
-
* If
|
|
37
|
-
*
|
|
38
|
-
*
|
|
36
|
+
* If 'AnimationFrame' or 'Timeout', the dispatch will be debounced (single dispatch for all changes).
|
|
37
|
+
*
|
|
38
|
+
* If 'No' or 'NextCycle', every signal is matter and count without debounced (every changes dispatched).
|
|
39
|
+
*
|
|
40
|
+
* tips: debounce work like throttle this means listeners call with latest dispatch value.
|
|
39
41
|
*
|
|
40
42
|
* @default `AnimationFrame`
|
|
41
43
|
*/
|
|
@@ -51,11 +53,13 @@ export interface ProviderOptions {
|
|
|
51
53
|
*
|
|
52
54
|
* @default `NextCycle`
|
|
53
55
|
*/
|
|
54
|
-
receivePrevious: DebounceType
|
|
56
|
+
receivePrevious: DebounceType;
|
|
55
57
|
/**
|
|
56
|
-
* If
|
|
57
|
-
*
|
|
58
|
-
*
|
|
58
|
+
* If 'AnimationFrame' or 'Timeout', the dispatch will be debounced (single dispatch for all changes).
|
|
59
|
+
*
|
|
60
|
+
* If 'No' or 'NextCycle', every signal is matter and count without debounced (every changes dispatched).
|
|
61
|
+
*
|
|
62
|
+
* tips: debounce work like throttle this means listeners call with latest dispatch value.
|
|
59
63
|
*
|
|
60
64
|
* @default `AnimationFrame`
|
|
61
65
|
*/
|
package/type.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["src/type.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAE,aAAa,EAAC,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["src/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,YAAY,EAAE,aAAa,EAAC,MAAM,cAAc,CAAC;AAE9D,MAAM,MAAM,YAAY,GAAG,IAAI,GAAG,WAAW,GAAG,gBAAgB,GAAG,SAAS,CAAC;AAE7E;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,IAAI,EAAE,OAAO,CAAC;IAEd;;;OAGG;IACH,QAAQ,EAAE,OAAO,CAAC;IAElB;;;;OAIG;IACH,QAAQ,EAAE,OAAO,CAAC;IAElB;;;;;OAKG;IACH,eAAe,EAAE,YAAY,CAAC;CAC/B;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B;;;;;;;;OAQG;IACH,QAAQ,EAAE,YAAY,CAAC;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B;;;;;OAKG;IACH,eAAe,EAAE,YAAY,CAAC;IAE9B;;;;;;;;OAQG;IACH,QAAQ,EAAE,YAAY,CAAC;CACxB;AAED;;GAEG;AACH,MAAM,MAAM,gBAAgB,CAAC,CAAC,SAAS,aAAa,IAAI,CAAC,MAAM,EAAE,CAAC,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAE5F;;GAEG;AACH,MAAM,MAAM,gBAAgB,CAAC,SAAS,EAAE,OAAO,IAAI,CACjD,cAAc,EAAE,SAAS,KACtB,YAAY,CAAC,OAAO,CAAC,CAAC;AAE3B;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB;;OAEG;IACH,EAAE,EAAE,MAAM,CAAC;IAEX;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAA;AAED;;GAEG;AACH,MAAM,MAAM,cAAc,CAAC,CAAC,SAAS,aAAa,IAAI,YAAY,GAAG;IACnE;;OAEG;IACH,IAAI,EAAE,OAAO,CAAC;IAEd;;OAEG;IACH,QAAQ,EAAE,OAAO,CAAC;IAElB,QAAQ,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAC;CAC/B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,aAAa,IAAI;IAClD;;OAEG;IACH,EAAE,EAAE,MAAM,CAAC;IAEX;;OAEG;IACH,MAAM,CAAC,EAAE,CAAC,CAAC;IAEX;;OAEG;IACH,QAAQ,EAAE,OAAO,CAAC;IAElB;;;OAGG;IACH,SAAS,EAAE,OAAO,CAAC;IAEnB;;OAEG;IACH,YAAY,EAAE,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC;CACxC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,aAAa,CAAC,GAAG,SAAS,CAAC,CAAC"}
|
package/type.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type.js","sourceRoot":"","sources":["src/type.ts"],"names":[],"mappings":"","sourcesContent":["import {MaybePromise, Stringifyable} from '@alwatr/type';\n\nexport type DebounceType = 'No' | 'AnimationFrame' | 'Timeout';\n\n/**\n * Subscribe options type.\n */\nexport interface SubscribeOptions {\n /**\n * If true, the listener will be called only once.\n * @default false\n */\n once: boolean;\n\n /**\n * If true, the listener will be called before other.\n * @default false\n */\n priority: boolean;\n\n /**\n * If true, the listener will be defined disabled by default.\n *\n * @default false\n */\n disabled: boolean;\n\n /**\n * Calling this listener (callback) with preview signal value (if dispatched before).\n * If Immediate, the listener will be called immediately without any debounce for preview signal.\n *\n * @default `
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["src/type.ts"],"names":[],"mappings":"","sourcesContent":["import type {MaybePromise, Stringifyable} from '@alwatr/type';\n\nexport type DebounceType = 'No' | 'NextCycle' | 'AnimationFrame' | 'Timeout';\n\n/**\n * Subscribe options type.\n */\nexport interface SubscribeOptions {\n /**\n * If true, the listener will be called only once.\n * @default false\n */\n once: boolean;\n\n /**\n * If true, the listener will be called before other.\n * @default false\n */\n priority: boolean;\n\n /**\n * If true, the listener will be defined disabled by default.\n *\n * @default false\n */\n disabled: boolean;\n\n /**\n * Calling this listener (callback) with preview signal value (if dispatched before).\n * If Immediate, the listener will be called immediately without any debounce for preview signal.\n *\n * @default `NextCycle`\n */\n receivePrevious: DebounceType;\n}\n\n/**\n * dispatchSignal options type\n */\nexport interface DispatchOptions {\n /**\n * If 'AnimationFrame' or 'Timeout', the dispatch will be debounced (single dispatch for all changes).\n *\n * If 'No' or 'NextCycle', every signal is matter and count without debounced (every changes dispatched).\n *\n * tips: debounce work like throttle this means listeners call with latest dispatch value.\n *\n * @default `AnimationFrame`\n */\n debounce: DebounceType;\n}\n\n/**\n * setSignalProvider options type.\n */\nexport interface ProviderOptions {\n /**\n * Calling signal provider (request signal callback) with preview signal value (if dispatched before).\n * If Immediate, the listener will be called immediately without any debounce for preview signal.\n *\n * @default `NextCycle`\n */\n receivePrevious: DebounceType;\n\n /**\n * If 'AnimationFrame' or 'Timeout', the dispatch will be debounced (single dispatch for all changes).\n *\n * If 'No' or 'NextCycle', every signal is matter and count without debounced (every changes dispatched).\n *\n * tips: debounce work like throttle this means listeners call with latest dispatch value.\n *\n * @default `AnimationFrame`\n */\n debounce: DebounceType;\n}\n\n/**\n * Subscribe callback function.\n */\nexport type ListenerFunction<T extends Stringifyable> = (detail: T) => void | Promise<void>;\n\n/**\n * Command/Context provider/handler function.\n */\nexport type ProviderFunction<TArgument, TReturn> = (\n argumentObject: TArgument\n) => MaybePromise<TReturn>;\n\n/**\n * Listener spec.\n */\nexport type ListenerSpec = {\n /**\n * Unique listener id\n */\n id: number;\n\n /**\n * Signal id\n */\n signalId: string;\n}\n\n/**\n * Signal listeners object in storage.\n */\nexport type ListenerObject<T extends Stringifyable> = ListenerSpec & {\n /**\n * If true, the listener will be called only once and removed automatically after first call\n */\n once: boolean;\n\n /**\n * If true, the listener will be disabled.\n */\n disabled: boolean;\n\n callback: ListenerFunction<T>;\n};\n\n/**\n * Signal object in storage.\n */\nexport type SignalObject<T extends Stringifyable> = {\n /**\n * Signal id for direct access.\n */\n id: string;\n\n /**\n * Last dispatched detail.\n */\n detail?: T;\n\n /**\n * If true, the signal is disabled.\n */\n disabled: boolean;\n\n /**\n * Dispatches debounced (delayed).\n * Internal use case for debounce dispatch signal.\n */\n debounced: boolean;\n\n /**\n * Signal listeners list.\n */\n listenerList: Array<ListenerObject<T>>;\n};\n\n/**\n * Signal stack storage.\n */\nexport type SignalStorage = Record<string, SignalObject<Stringifyable> | undefined>;\n"]}
|