@kuindji/reactive 1.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/README.md +985 -0
- package/dist/action.d.ts +49 -0
- package/dist/action.js +77 -0
- package/dist/actionBus.d.ts +43 -0
- package/dist/actionBus.js +81 -0
- package/dist/actionMap.d.ts +23 -0
- package/dist/actionMap.js +26 -0
- package/dist/event.d.ts +143 -0
- package/dist/event.js +538 -0
- package/dist/eventBus.d.ts +115 -0
- package/dist/eventBus.js +508 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +21 -0
- package/dist/lib/asyncCall.d.ts +1 -0
- package/dist/lib/asyncCall.js +17 -0
- package/dist/lib/listenerSorter.d.ts +5 -0
- package/dist/lib/listenerSorter.js +13 -0
- package/dist/lib/tagsIntersect.d.ts +1 -0
- package/dist/lib/tagsIntersect.js +11 -0
- package/dist/lib/types.d.ts +49 -0
- package/dist/lib/types.js +37 -0
- package/dist/react/ErrorBoundary.d.ts +10 -0
- package/dist/react/ErrorBoundary.js +25 -0
- package/dist/react/useAction.d.ts +21 -0
- package/dist/react/useAction.js +66 -0
- package/dist/react/useActionBus.d.ts +38 -0
- package/dist/react/useActionBus.js +44 -0
- package/dist/react/useActionMap.d.ts +23 -0
- package/dist/react/useActionMap.js +25 -0
- package/dist/react/useEvent.d.ts +47 -0
- package/dist/react/useEvent.js +66 -0
- package/dist/react/useEventBus.d.ts +117 -0
- package/dist/react/useEventBus.js +66 -0
- package/dist/react/useListenToAction.d.ts +3 -0
- package/dist/react/useListenToAction.js +38 -0
- package/dist/react/useListenToEvent.d.ts +4 -0
- package/dist/react/useListenToEvent.js +34 -0
- package/dist/react/useListenToEventBus.d.ts +5 -0
- package/dist/react/useListenToEventBus.js +37 -0
- package/dist/react/useStore.d.ts +51 -0
- package/dist/react/useStore.js +30 -0
- package/dist/react/useStoreState.d.ts +3 -0
- package/dist/react/useStoreState.js +32 -0
- package/dist/react.d.ts +10 -0
- package/dist/react.js +26 -0
- package/dist/store.d.ts +54 -0
- package/dist/store.js +193 -0
- package/package.json +70 -0
- package/src/index.ts +5 -0
package/dist/action.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { ApiType, BaseHandler, ErrorListenerSignature, ErrorResponse } from "./lib/types";
|
|
2
|
+
export type ActionResponse<Response extends any = any, Args extends any[] = any[]> = {
|
|
3
|
+
response: Response;
|
|
4
|
+
error: null;
|
|
5
|
+
args: Args;
|
|
6
|
+
} | {
|
|
7
|
+
response: null;
|
|
8
|
+
error: string;
|
|
9
|
+
args: Args;
|
|
10
|
+
};
|
|
11
|
+
export type ListenerSignature<ActionSignature extends BaseHandler> = (arg: ActionResponse<Awaited<ReturnType<ActionSignature>>, Parameters<ActionSignature>>) => void;
|
|
12
|
+
export type ActionDefinitionHelper<A extends BaseHandler> = {
|
|
13
|
+
actionSignature: A;
|
|
14
|
+
actionArguments: Parameters<A>;
|
|
15
|
+
actionReturnType: Awaited<ReturnType<A>>;
|
|
16
|
+
responseType: ActionResponse<Awaited<ReturnType<A>>, Parameters<A>>;
|
|
17
|
+
errorResponseType: ErrorResponse<Parameters<A>>;
|
|
18
|
+
listenerArgument: ActionResponse<Awaited<ReturnType<A>>, Parameters<A>>;
|
|
19
|
+
listenerSignature: ListenerSignature<A>;
|
|
20
|
+
errorListenerArgument: ErrorResponse<Parameters<A>>;
|
|
21
|
+
errorListenerSignature: ErrorListenerSignature<Parameters<A>>;
|
|
22
|
+
};
|
|
23
|
+
export declare function createAction<A extends BaseHandler>(action: A): ApiType<ActionDefinitionHelper<A>, {
|
|
24
|
+
readonly invoke: (...args: Parameters<A>) => Promise<ActionResponse<Awaited<ReturnType<A>>, Parameters<A>>>;
|
|
25
|
+
readonly addListener: (handler: ListenerSignature<A>, listenerOptions?: import("./event").ListenerOptions) => void;
|
|
26
|
+
/** @alias addListener */
|
|
27
|
+
readonly on: (handler: ListenerSignature<A>, listenerOptions?: import("./event").ListenerOptions) => void;
|
|
28
|
+
/** @alias addListener */
|
|
29
|
+
readonly subscribe: (handler: ListenerSignature<A>, listenerOptions?: import("./event").ListenerOptions) => void;
|
|
30
|
+
/** @alias addListener */
|
|
31
|
+
readonly listen: (handler: ListenerSignature<A>, listenerOptions?: import("./event").ListenerOptions) => void;
|
|
32
|
+
readonly removeAllListeners: (tag?: string) => void;
|
|
33
|
+
readonly removeListener: (handler: ListenerSignature<A>, context?: object | null, tag?: string | null) => boolean;
|
|
34
|
+
/** @alias removeListener */
|
|
35
|
+
readonly un: (handler: ListenerSignature<A>, context?: object | null, tag?: string | null) => boolean;
|
|
36
|
+
/** @alias removeListener */
|
|
37
|
+
readonly off: (handler: ListenerSignature<A>, context?: object | null, tag?: string | null) => boolean;
|
|
38
|
+
/** @alias removeListener */
|
|
39
|
+
readonly remove: (handler: ListenerSignature<A>, context?: object | null, tag?: string | null) => boolean;
|
|
40
|
+
/** @alias removeListener */
|
|
41
|
+
readonly unsubscribe: (handler: ListenerSignature<A>, context?: object | null, tag?: string | null) => boolean;
|
|
42
|
+
readonly promise: (options?: import("./event").ListenerOptions) => Promise<[arg: ActionResponse<Awaited<ReturnType<A>>, Parameters<A>>]>;
|
|
43
|
+
readonly addErrorListener: (handler: ErrorListenerSignature<Parameters<A>>, listenerOptions?: import("./event").ListenerOptions) => void;
|
|
44
|
+
readonly removeAllErrorListeners: (tag?: string) => void;
|
|
45
|
+
readonly removeErrorListener: (handler: ErrorListenerSignature<Parameters<A>>, context?: object | null, tag?: string | null) => boolean;
|
|
46
|
+
readonly errorPromise: (options?: import("./event").ListenerOptions) => Promise<[errorResponse: ErrorResponse<Parameters<A>>]>;
|
|
47
|
+
}>;
|
|
48
|
+
export type BaseActionDefinition = ActionDefinitionHelper<(...args: [any]) => any>;
|
|
49
|
+
export type BaseAction = ReturnType<typeof createAction<(...args: [any]) => any>>;
|
package/dist/action.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.createAction = createAction;
|
|
13
|
+
const event_1 = require("./event");
|
|
14
|
+
function createAction(action) {
|
|
15
|
+
const { trigger, addListener, removeAllListeners, removeListener, promise, } = (0, event_1.createEvent)();
|
|
16
|
+
const { trigger: triggerError, addListener: addErrorListener, removeAllListeners: removeAllErrorListeners, removeListener: removeErrorListener, promise: errorPromise, hasListener: hasErrorListeners, } = (0, event_1.createEvent)();
|
|
17
|
+
const invoke = (...args) => __awaiter(this, void 0, void 0, function* () {
|
|
18
|
+
try {
|
|
19
|
+
let result = action(...args);
|
|
20
|
+
if (result instanceof Promise) {
|
|
21
|
+
result = yield result;
|
|
22
|
+
}
|
|
23
|
+
const response = {
|
|
24
|
+
response: result,
|
|
25
|
+
error: null,
|
|
26
|
+
args: args,
|
|
27
|
+
};
|
|
28
|
+
trigger(response);
|
|
29
|
+
return response;
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
if (!hasErrorListeners()) {
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
const response = {
|
|
36
|
+
response: null,
|
|
37
|
+
error: error instanceof Error ? error.message : error,
|
|
38
|
+
args: args,
|
|
39
|
+
};
|
|
40
|
+
trigger(response);
|
|
41
|
+
triggerError({
|
|
42
|
+
error: error instanceof Error
|
|
43
|
+
? error
|
|
44
|
+
: new Error(error),
|
|
45
|
+
args: args,
|
|
46
|
+
type: "action",
|
|
47
|
+
});
|
|
48
|
+
return response;
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
const api = {
|
|
52
|
+
invoke,
|
|
53
|
+
addListener,
|
|
54
|
+
/** @alias addListener */
|
|
55
|
+
on: addListener,
|
|
56
|
+
/** @alias addListener */
|
|
57
|
+
subscribe: addListener,
|
|
58
|
+
/** @alias addListener */
|
|
59
|
+
listen: addListener,
|
|
60
|
+
removeAllListeners,
|
|
61
|
+
removeListener,
|
|
62
|
+
/** @alias removeListener */
|
|
63
|
+
un: removeListener,
|
|
64
|
+
/** @alias removeListener */
|
|
65
|
+
off: removeListener,
|
|
66
|
+
/** @alias removeListener */
|
|
67
|
+
remove: removeListener,
|
|
68
|
+
/** @alias removeListener */
|
|
69
|
+
unsubscribe: removeListener,
|
|
70
|
+
promise,
|
|
71
|
+
addErrorListener,
|
|
72
|
+
removeAllErrorListeners,
|
|
73
|
+
removeErrorListener,
|
|
74
|
+
errorPromise,
|
|
75
|
+
};
|
|
76
|
+
return api;
|
|
77
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ActionDefinitionHelper, createAction } from "./action";
|
|
2
|
+
import { ListenerOptions } from "./event";
|
|
3
|
+
import type { ApiType, BaseHandler, ErrorListenerSignature, KeyOf, MapKey } from "./lib/types";
|
|
4
|
+
export interface BaseActionsMap {
|
|
5
|
+
[key: MapKey]: BaseHandler;
|
|
6
|
+
}
|
|
7
|
+
type GetActionTypesMap<ActionsMap extends BaseActionsMap> = {
|
|
8
|
+
[key in KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : ReturnType<typeof createAction<ActionsMap[key]>>;
|
|
9
|
+
};
|
|
10
|
+
type GetActionDefinitionsMap<ActionsMap extends BaseActionsMap> = {
|
|
11
|
+
[key in KeyOf<ActionsMap>]: [ActionsMap[key]] extends [never] ? never : ActionDefinitionHelper<ActionsMap[key]>;
|
|
12
|
+
};
|
|
13
|
+
export type ActionBusDefinitionHelper<ActionsMap extends BaseActionsMap> = {
|
|
14
|
+
actions: GetActionDefinitionsMap<ActionsMap>;
|
|
15
|
+
actionTypes: GetActionTypesMap<ActionsMap>;
|
|
16
|
+
};
|
|
17
|
+
export declare function createActionBus<ActionsMap extends BaseActionsMap>(initialActions?: ActionsMap, errorListener?: ErrorListenerSignature<any[]>): ApiType<ActionBusDefinitionHelper<ActionsMap>, {
|
|
18
|
+
readonly add: (name: MapKey, action: BaseHandler) => void;
|
|
19
|
+
readonly get: <K extends KeyOf<GetActionDefinitionsMap<ActionsMap>>>(name: K) => GetActionTypesMap<ActionsMap>[K];
|
|
20
|
+
readonly invoke: <K extends KeyOf<GetActionDefinitionsMap<ActionsMap>>>(name: K, ...args: GetActionDefinitionsMap<ActionsMap>[K]["actionArguments"]) => Promise<import("./action").ActionResponse<Awaited<ReturnType<ActionsMap[K]>>, Parameters<ActionsMap[K]>>>;
|
|
21
|
+
readonly addListener: <K extends KeyOf<GetActionDefinitionsMap<ActionsMap>>>(name: K, handler: GetActionDefinitionsMap<ActionsMap>[K]["listenerSignature"], options?: ListenerOptions) => void;
|
|
22
|
+
/** @alias addListener */
|
|
23
|
+
readonly on: <K extends KeyOf<GetActionDefinitionsMap<ActionsMap>>>(name: K, handler: GetActionDefinitionsMap<ActionsMap>[K]["listenerSignature"], options?: ListenerOptions) => void;
|
|
24
|
+
/** @alias addListener */
|
|
25
|
+
readonly subscribe: <K extends KeyOf<GetActionDefinitionsMap<ActionsMap>>>(name: K, handler: GetActionDefinitionsMap<ActionsMap>[K]["listenerSignature"], options?: ListenerOptions) => void;
|
|
26
|
+
/** @alias addListener */
|
|
27
|
+
readonly listen: <K extends KeyOf<GetActionDefinitionsMap<ActionsMap>>>(name: K, handler: GetActionDefinitionsMap<ActionsMap>[K]["listenerSignature"], options?: ListenerOptions) => void;
|
|
28
|
+
readonly once: <K extends KeyOf<GetActionDefinitionsMap<ActionsMap>>>(name: K, handler: GetActionDefinitionsMap<ActionsMap>[K]["listenerSignature"], options?: ListenerOptions) => void;
|
|
29
|
+
readonly removeListener: <K extends KeyOf<GetActionDefinitionsMap<ActionsMap>>>(name: K, handler: GetActionDefinitionsMap<ActionsMap>[K]["listenerSignature"], context?: object | null, tag?: string | null) => boolean;
|
|
30
|
+
/** @alias removeListener */
|
|
31
|
+
readonly off: <K extends KeyOf<GetActionDefinitionsMap<ActionsMap>>>(name: K, handler: GetActionDefinitionsMap<ActionsMap>[K]["listenerSignature"], context?: object | null, tag?: string | null) => boolean;
|
|
32
|
+
/** @alias removeListener */
|
|
33
|
+
readonly remove: <K extends KeyOf<GetActionDefinitionsMap<ActionsMap>>>(name: K, handler: GetActionDefinitionsMap<ActionsMap>[K]["listenerSignature"], context?: object | null, tag?: string | null) => boolean;
|
|
34
|
+
/** @alias removeListener */
|
|
35
|
+
readonly un: <K extends KeyOf<GetActionDefinitionsMap<ActionsMap>>>(name: K, handler: GetActionDefinitionsMap<ActionsMap>[K]["listenerSignature"], context?: object | null, tag?: string | null) => boolean;
|
|
36
|
+
/** @alias removeListener */
|
|
37
|
+
readonly unsubscribe: <K extends KeyOf<GetActionDefinitionsMap<ActionsMap>>>(name: K, handler: GetActionDefinitionsMap<ActionsMap>[K]["listenerSignature"], context?: object | null, tag?: string | null) => boolean;
|
|
38
|
+
readonly addErrorListener: (handler: ErrorListenerSignature<any[]>, listenerOptions?: ListenerOptions) => void;
|
|
39
|
+
readonly removeErrorListener: (handler: ErrorListenerSignature<any[]>, context?: object | null, tag?: string | null) => boolean;
|
|
40
|
+
}>;
|
|
41
|
+
export type BaseActionBusDefinition = ActionBusDefinitionHelper<BaseActionsMap>;
|
|
42
|
+
export type BaseActionBus = ReturnType<typeof createActionBus<BaseActionsMap>>;
|
|
43
|
+
export {};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createActionBus = createActionBus;
|
|
4
|
+
const action_1 = require("./action");
|
|
5
|
+
const event_1 = require("./event");
|
|
6
|
+
function createActionBus(initialActions = {}, errorListener) {
|
|
7
|
+
const actions = new Map();
|
|
8
|
+
const errorEvent = (0, event_1.createEvent)();
|
|
9
|
+
if (errorListener) {
|
|
10
|
+
errorEvent.addListener(({ error, args }) => {
|
|
11
|
+
errorEvent.emit({ error, args, type: "action" });
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
const add = (name, action) => {
|
|
15
|
+
if (!actions.has(name)) {
|
|
16
|
+
const a = (0, action_1.createAction)(action);
|
|
17
|
+
a.addErrorListener(({ error, args }) => {
|
|
18
|
+
errorEvent.emit({ name, error, args, type: "action" });
|
|
19
|
+
});
|
|
20
|
+
actions.set(name, a);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
Object.entries(initialActions).forEach(([name, action]) => {
|
|
24
|
+
add(name, action);
|
|
25
|
+
});
|
|
26
|
+
const get = (name) => {
|
|
27
|
+
return actions.get(name);
|
|
28
|
+
};
|
|
29
|
+
const invoke = (name, ...args) => {
|
|
30
|
+
const action = get(name);
|
|
31
|
+
return action.invoke(...args);
|
|
32
|
+
};
|
|
33
|
+
const on = (name, handler, options) => {
|
|
34
|
+
const action = get(name);
|
|
35
|
+
if (!action) {
|
|
36
|
+
throw new Error(`Action ${name} not found`);
|
|
37
|
+
}
|
|
38
|
+
return action.addListener(handler, options);
|
|
39
|
+
};
|
|
40
|
+
const once = (name, handler, options) => {
|
|
41
|
+
options = options || {};
|
|
42
|
+
options.limit = 1;
|
|
43
|
+
const action = get(name);
|
|
44
|
+
if (!action) {
|
|
45
|
+
throw new Error(`Action ${name} not found`);
|
|
46
|
+
}
|
|
47
|
+
return action.addListener(handler, options);
|
|
48
|
+
};
|
|
49
|
+
const un = (name, handler, context, tag) => {
|
|
50
|
+
const action = get(name);
|
|
51
|
+
if (!action) {
|
|
52
|
+
throw new Error(`Action ${name} not found`);
|
|
53
|
+
}
|
|
54
|
+
return action.removeListener(handler, context, tag);
|
|
55
|
+
};
|
|
56
|
+
const api = {
|
|
57
|
+
add,
|
|
58
|
+
get,
|
|
59
|
+
invoke,
|
|
60
|
+
addListener: on,
|
|
61
|
+
/** @alias addListener */
|
|
62
|
+
on,
|
|
63
|
+
/** @alias addListener */
|
|
64
|
+
subscribe: on,
|
|
65
|
+
/** @alias addListener */
|
|
66
|
+
listen: on,
|
|
67
|
+
once,
|
|
68
|
+
removeListener: un,
|
|
69
|
+
/** @alias removeListener */
|
|
70
|
+
off: un,
|
|
71
|
+
/** @alias removeListener */
|
|
72
|
+
remove: un,
|
|
73
|
+
/** @alias removeListener */
|
|
74
|
+
un: un,
|
|
75
|
+
/** @alias removeListener */
|
|
76
|
+
unsubscribe: un,
|
|
77
|
+
addErrorListener: errorEvent.addListener,
|
|
78
|
+
removeErrorListener: errorEvent.removeListener,
|
|
79
|
+
};
|
|
80
|
+
return api;
|
|
81
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ActionResponse } from "./action";
|
|
2
|
+
import type { BaseActionsMap } from "./actionBus";
|
|
3
|
+
import type { ErrorListenerSignature, ErrorResponse, KeyOf } from "./lib/types";
|
|
4
|
+
export type { ActionResponse, BaseActionsMap, ErrorListenerSignature, ErrorResponse, };
|
|
5
|
+
export declare function createActionMap<M extends BaseActionsMap>(actions: M, onAnyError?: ErrorListenerSignature<any[]> | ErrorListenerSignature<any[]>[]): { [key in KeyOf<M>]: {
|
|
6
|
+
readonly invoke: (...args: Parameters<M[key]>) => Promise<ActionResponse<Awaited<ReturnType<M[key]>>, Parameters<M[key]>>>;
|
|
7
|
+
readonly addListener: (handler: import("./action").ListenerSignature<M[key]>, listenerOptions?: import("./event").ListenerOptions) => void;
|
|
8
|
+
readonly on: (handler: import("./action").ListenerSignature<M[key]>, listenerOptions?: import("./event").ListenerOptions) => void;
|
|
9
|
+
readonly subscribe: (handler: import("./action").ListenerSignature<M[key]>, listenerOptions?: import("./event").ListenerOptions) => void;
|
|
10
|
+
readonly listen: (handler: import("./action").ListenerSignature<M[key]>, listenerOptions?: import("./event").ListenerOptions) => void;
|
|
11
|
+
readonly removeAllListeners: (tag?: string) => void;
|
|
12
|
+
readonly removeListener: (handler: import("./action").ListenerSignature<M[key]>, context?: object | null, tag?: string | null) => boolean;
|
|
13
|
+
readonly un: (handler: import("./action").ListenerSignature<M[key]>, context?: object | null, tag?: string | null) => boolean;
|
|
14
|
+
readonly off: (handler: import("./action").ListenerSignature<M[key]>, context?: object | null, tag?: string | null) => boolean;
|
|
15
|
+
readonly remove: (handler: import("./action").ListenerSignature<M[key]>, context?: object | null, tag?: string | null) => boolean;
|
|
16
|
+
readonly unsubscribe: (handler: import("./action").ListenerSignature<M[key]>, context?: object | null, tag?: string | null) => boolean;
|
|
17
|
+
readonly promise: (options?: import("./event").ListenerOptions) => Promise<[arg: ActionResponse<Awaited<ReturnType<M[key]>>, Parameters<M[key]>>]>;
|
|
18
|
+
readonly addErrorListener: (handler: ErrorListenerSignature<Parameters<M[key]>>, listenerOptions?: import("./event").ListenerOptions) => void;
|
|
19
|
+
readonly removeAllErrorListeners: (tag?: string) => void;
|
|
20
|
+
readonly removeErrorListener: (handler: ErrorListenerSignature<Parameters<M[key]>>, context?: object | null, tag?: string | null) => boolean;
|
|
21
|
+
readonly errorPromise: (options?: import("./event").ListenerOptions) => Promise<[errorResponse: ErrorResponse<Parameters<M[key]>>]>;
|
|
22
|
+
__type: import("./action").ActionDefinitionHelper<M[key]>;
|
|
23
|
+
}; };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createActionMap = createActionMap;
|
|
4
|
+
const action_1 = require("./action");
|
|
5
|
+
function createActionMap(actions, onAnyError) {
|
|
6
|
+
const errorListenersMap = {};
|
|
7
|
+
for (const key in actions) {
|
|
8
|
+
errorListenersMap[key] = (errorResponse) => {
|
|
9
|
+
if (Array.isArray(onAnyError)) {
|
|
10
|
+
for (const listener of onAnyError) {
|
|
11
|
+
listener === null || listener === void 0 ? void 0 : listener(Object.assign({ name: key }, errorResponse));
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
onAnyError === null || onAnyError === void 0 ? void 0 : onAnyError(Object.assign({ name: key }, errorResponse));
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
const map = {};
|
|
20
|
+
for (const key in actions) {
|
|
21
|
+
const action = (0, action_1.createAction)(actions[key]);
|
|
22
|
+
action.addErrorListener(errorListenersMap[key]);
|
|
23
|
+
map[key] = action;
|
|
24
|
+
}
|
|
25
|
+
return map;
|
|
26
|
+
}
|
package/dist/event.d.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import type { ApiType, BaseHandler, ErrorListenerSignature } from "./lib/types";
|
|
2
|
+
type Unarray<T> = T extends (infer U)[] ? U : T;
|
|
3
|
+
interface BaseOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Call this listener asynchronously.
|
|
6
|
+
*/
|
|
7
|
+
async?: boolean | number | null;
|
|
8
|
+
}
|
|
9
|
+
export interface ListenerOptions extends BaseOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Call handler this number of times; 0 for unlimited
|
|
12
|
+
* @default 0
|
|
13
|
+
*/
|
|
14
|
+
limit?: number;
|
|
15
|
+
/**
|
|
16
|
+
* True to prepend to the list of listeners
|
|
17
|
+
* @default false
|
|
18
|
+
*/
|
|
19
|
+
first?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* True to always run this listener before others
|
|
22
|
+
* @default false
|
|
23
|
+
*/
|
|
24
|
+
alwaysFirst?: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* True to always run this listener after others
|
|
27
|
+
* @default false
|
|
28
|
+
*/
|
|
29
|
+
alwaysLast?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Start calling listener after this number of calls. Starts from 1
|
|
32
|
+
* @default 1
|
|
33
|
+
*/
|
|
34
|
+
start?: number;
|
|
35
|
+
/**
|
|
36
|
+
* Listener's context (this) object
|
|
37
|
+
*/
|
|
38
|
+
context?: object | null;
|
|
39
|
+
/**
|
|
40
|
+
* Listener tags
|
|
41
|
+
*/
|
|
42
|
+
tags?: string[];
|
|
43
|
+
/**
|
|
44
|
+
* You can pass any additional fields here. They will be passed back to TriggerFilter
|
|
45
|
+
*/
|
|
46
|
+
extraData?: any;
|
|
47
|
+
}
|
|
48
|
+
interface ListenerPrototype<Handler extends BaseHandler> extends Required<ListenerOptions> {
|
|
49
|
+
handler: Handler;
|
|
50
|
+
called: number;
|
|
51
|
+
count: number;
|
|
52
|
+
index: number;
|
|
53
|
+
start: number;
|
|
54
|
+
}
|
|
55
|
+
export interface EventOptions<ListenerSignature extends BaseHandler> extends BaseOptions {
|
|
56
|
+
/**
|
|
57
|
+
* Call this event this number of times; 0 for unlimited
|
|
58
|
+
* @default 0
|
|
59
|
+
*/
|
|
60
|
+
limit?: number | null;
|
|
61
|
+
/**
|
|
62
|
+
* Trigger newly added listeners automatically this last trigger arguments
|
|
63
|
+
* @default false
|
|
64
|
+
*/
|
|
65
|
+
autoTrigger?: boolean | null;
|
|
66
|
+
/**
|
|
67
|
+
* A function that decides whether event should trigger a listener this time
|
|
68
|
+
*/
|
|
69
|
+
filter?: ((args: any[], listener: ListenerPrototype<ListenerSignature>) => boolean) | null;
|
|
70
|
+
/**
|
|
71
|
+
* TriggerFilter's this object, if needed
|
|
72
|
+
*/
|
|
73
|
+
filterContext?: object | null;
|
|
74
|
+
/**
|
|
75
|
+
* Maximum number of listeners to add
|
|
76
|
+
* @default 1000
|
|
77
|
+
*/
|
|
78
|
+
maxListeners?: number;
|
|
79
|
+
}
|
|
80
|
+
export type EventDefinitionHelper<ListenerSignature extends BaseHandler = BaseHandler> = {
|
|
81
|
+
signature: ListenerSignature;
|
|
82
|
+
arguments: Parameters<ListenerSignature>;
|
|
83
|
+
returnType: ReturnType<ListenerSignature>;
|
|
84
|
+
options: EventOptions<ListenerSignature>;
|
|
85
|
+
errorListenerSignature: ErrorListenerSignature<Parameters<ListenerSignature>>;
|
|
86
|
+
};
|
|
87
|
+
export declare function createEvent<ListenerSignature extends BaseHandler>(eventOptions?: EventOptions<ListenerSignature>): ApiType<EventDefinitionHelper<ListenerSignature>, {
|
|
88
|
+
readonly addListener: (handler: ListenerSignature, listenerOptions?: ListenerOptions) => void;
|
|
89
|
+
/** @alias addListener */
|
|
90
|
+
readonly on: (handler: ListenerSignature, listenerOptions?: ListenerOptions) => void;
|
|
91
|
+
/** @alias addListener */
|
|
92
|
+
readonly listen: (handler: ListenerSignature, listenerOptions?: ListenerOptions) => void;
|
|
93
|
+
/** @alias addListener */
|
|
94
|
+
readonly subscribe: (handler: ListenerSignature, listenerOptions?: ListenerOptions) => void;
|
|
95
|
+
readonly removeListener: (handler: ListenerSignature, context?: object | null, tag?: string | null) => boolean;
|
|
96
|
+
/** @alias removeListener */
|
|
97
|
+
readonly un: (handler: ListenerSignature, context?: object | null, tag?: string | null) => boolean;
|
|
98
|
+
/** @alias removeListener */
|
|
99
|
+
readonly off: (handler: ListenerSignature, context?: object | null, tag?: string | null) => boolean;
|
|
100
|
+
/** @alias removeListener */
|
|
101
|
+
readonly remove: (handler: ListenerSignature, context?: object | null, tag?: string | null) => boolean;
|
|
102
|
+
/** @alias removeListener */
|
|
103
|
+
readonly unsubscribe: (handler: ListenerSignature, context?: object | null, tag?: string | null) => boolean;
|
|
104
|
+
readonly trigger: (...args: Parameters<ListenerSignature>) => void;
|
|
105
|
+
/** @alias trigger */
|
|
106
|
+
readonly emit: (...args: Parameters<ListenerSignature>) => void;
|
|
107
|
+
/** @alias trigger */
|
|
108
|
+
readonly dispatch: (...args: Parameters<ListenerSignature>) => void;
|
|
109
|
+
readonly hasListener: (handler?: ListenerSignature | null, context?: object | null, tag?: string | null) => boolean;
|
|
110
|
+
/** @alias hasListener */
|
|
111
|
+
readonly has: (handler?: ListenerSignature | null, context?: object | null, tag?: string | null) => boolean;
|
|
112
|
+
readonly removeAllListeners: (tag?: string) => void;
|
|
113
|
+
readonly addErrorListener: (handler: ErrorListenerSignature<Parameters<ListenerSignature>>, context?: object | null) => void;
|
|
114
|
+
readonly removeErrorListener: (handler: ErrorListenerSignature<Parameters<ListenerSignature>>, context?: object | null) => boolean;
|
|
115
|
+
readonly suspend: (withQueue?: boolean) => void;
|
|
116
|
+
readonly resume: () => void;
|
|
117
|
+
readonly setOptions: (eventOptions: Pick<EventOptions<ListenerSignature>, "async" | "limit" | "autoTrigger">) => void;
|
|
118
|
+
readonly reset: () => void;
|
|
119
|
+
readonly isSuspended: () => boolean;
|
|
120
|
+
readonly isQueued: () => boolean;
|
|
121
|
+
readonly withTags: <T extends (...args: any[]) => any>(tags: string[], callback: T) => ReturnType<T>;
|
|
122
|
+
readonly promise: (options?: ListenerOptions) => Promise<Parameters<ListenerSignature>>;
|
|
123
|
+
readonly first: (...args: Parameters<ListenerSignature>) => ReturnType<ListenerSignature> | undefined;
|
|
124
|
+
readonly resolveFirst: (...args: Parameters<ListenerSignature>) => Promise<Awaited<ReturnType<ListenerSignature>> | undefined>;
|
|
125
|
+
readonly all: (...args: Parameters<ListenerSignature>) => ReturnType<ListenerSignature>[];
|
|
126
|
+
readonly resolveAll: (...args: Parameters<ListenerSignature>) => Promise<Awaited<ReturnType<ListenerSignature>>[]>;
|
|
127
|
+
readonly last: (...args: Parameters<ListenerSignature>) => ReturnType<ListenerSignature> | undefined;
|
|
128
|
+
readonly resolveLast: (...args: Parameters<ListenerSignature>) => Promise<Awaited<ReturnType<ListenerSignature>> | undefined>;
|
|
129
|
+
readonly merge: (...args: Parameters<ListenerSignature>) => ReturnType<ListenerSignature> | undefined;
|
|
130
|
+
readonly resolveMerge: (...args: Parameters<ListenerSignature>) => Promise<Awaited<ReturnType<ListenerSignature>> | undefined>;
|
|
131
|
+
readonly concat: (...args: Parameters<ListenerSignature>) => Unarray<ReturnType<ListenerSignature>>[];
|
|
132
|
+
readonly resolveConcat: (...args: Parameters<ListenerSignature>) => Promise<Unarray<Awaited<ReturnType<ListenerSignature>>>[]>;
|
|
133
|
+
readonly firstNonEmpty: (...args: Parameters<ListenerSignature>) => ReturnType<ListenerSignature> | undefined;
|
|
134
|
+
readonly resolveFirstNonEmpty: (...args: Parameters<ListenerSignature>) => Promise<Awaited<ReturnType<ListenerSignature>> | undefined>;
|
|
135
|
+
readonly untilTrue: (...args: Parameters<ListenerSignature>) => void;
|
|
136
|
+
readonly untilFalse: (...args: Parameters<ListenerSignature>) => void;
|
|
137
|
+
readonly pipe: (...args: Parameters<ListenerSignature>) => ReturnType<ListenerSignature> | undefined;
|
|
138
|
+
readonly resolvePipe: (...args: Parameters<ListenerSignature>) => Promise<Awaited<ReturnType<ListenerSignature>> | undefined>;
|
|
139
|
+
readonly raw: (...args: Parameters<ListenerSignature>) => Unarray<ReturnType<ListenerSignature>>[];
|
|
140
|
+
}>;
|
|
141
|
+
export type BaseEventDefinition = EventDefinitionHelper<BaseHandler>;
|
|
142
|
+
export type BaseEvent = ReturnType<typeof createEvent<BaseHandler>>;
|
|
143
|
+
export {};
|