@tramvai/core 2.7.1 → 2.20.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 +61 -11
- package/lib/actions/createActions.d.ts +3 -0
- package/lib/actions/declareAction.d.ts +3 -0
- package/lib/actions/declareAction.test-types.d.ts +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.es.js +14 -1
- package/lib/index.js +15 -0
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -83,31 +83,54 @@ class ModulePubSub {
|
|
|
83
83
|
}
|
|
84
84
|
```
|
|
85
85
|
|
|
86
|
-
###
|
|
86
|
+
### declareAction
|
|
87
87
|
|
|
88
|
-
`
|
|
88
|
+
`declareAction` - Method for creating asynchronous actions. It is used both for building chains of sagas and for performing global actions when building a response to a client
|
|
89
89
|
|
|
90
90
|
[More about actions](concepts/action.md)
|
|
91
91
|
|
|
92
|
-
####
|
|
92
|
+
#### declareAction({ name, fn, deps, conditions })
|
|
93
93
|
|
|
94
94
|
- `name` - The name of the action, a unique identifier is expected
|
|
95
|
-
- `fn(
|
|
96
|
-
- `
|
|
97
|
-
- `
|
|
98
|
-
- `deps` - provider instances from `deps`
|
|
95
|
+
- `fn(...params)` - Implementation of the action, this function will be called when the action is used, maybe `async`
|
|
96
|
+
- `this` - action execution context that contains some helper functions and resolved deps
|
|
97
|
+
- `params` - data passed to action
|
|
99
98
|
- `deps` - List of providers that are needed for the action to work
|
|
100
99
|
- `conditions` - List of restrictions for the execution of the action
|
|
100
|
+
- `conditionsFailResult` - [see](#conditionsfailresult)
|
|
101
|
+
|
|
102
|
+
#### Action Execution Context
|
|
103
|
+
|
|
104
|
+
Action execution context that contains some helper functions and resolved deps
|
|
105
|
+
|
|
106
|
+
Context has next fields:
|
|
107
|
+
- `deps` - resolved deps that were specified when declaring action
|
|
108
|
+
- `executeAction` - allows to execute another actions inside current one
|
|
109
|
+
- `getState` - quick access to `STORE_TOKEN.getState`
|
|
110
|
+
- `dispatch` - quick access to `STORE_TOKEN.dispatch`
|
|
111
|
+
- `abortSignal` - instance of signal related to the current execution tree
|
|
112
|
+
- `abortController` - instance of `AbortController` created exclusively for the current action execution
|
|
113
|
+
|
|
114
|
+
#### conditionsFailResult
|
|
115
|
+
|
|
116
|
+
Specifies the output of the action in case its `conditions` was not met during execution.
|
|
117
|
+
|
|
118
|
+
> If `conditions` are not met for action, action's `fn` won't be executed in any way
|
|
119
|
+
|
|
120
|
+
Possible values for the `conditionsFailResult`:
|
|
121
|
+
- `empty` - (default) execution will be resolved with `undefined` as a result
|
|
122
|
+
- `error` - execution will be rejected with [ConditionFailError](references/libs/errors.md#conditionfailerror)
|
|
123
|
+
|
|
101
124
|
|
|
102
125
|
#### Usage example
|
|
103
126
|
|
|
104
127
|
```tsx
|
|
105
|
-
import {
|
|
128
|
+
import { declareAction } from '@tramvai/core';
|
|
106
129
|
|
|
107
|
-
|
|
130
|
+
declareAction({
|
|
108
131
|
name: 'action log error',
|
|
109
|
-
fn
|
|
110
|
-
deps.logger.error('ERROR');
|
|
132
|
+
fn(payload) {
|
|
133
|
+
this.deps.logger.error('ERROR');
|
|
111
134
|
},
|
|
112
135
|
deps: {
|
|
113
136
|
logger: 'logger',
|
|
@@ -118,6 +141,33 @@ createAction({
|
|
|
118
141
|
});
|
|
119
142
|
```
|
|
120
143
|
|
|
144
|
+
abort execution:
|
|
145
|
+
```ts
|
|
146
|
+
const innerAction = declareAction(/** ... */);
|
|
147
|
+
|
|
148
|
+
const action = declareAction({
|
|
149
|
+
name: 'root',
|
|
150
|
+
async fn() {
|
|
151
|
+
setTimeout(() => {
|
|
152
|
+
this.abortController.abort()
|
|
153
|
+
}, 4000);
|
|
154
|
+
|
|
155
|
+
const { payload } = await this.deps.httpClient.request({
|
|
156
|
+
url: 'https://www.domain.com/api/endpoint',
|
|
157
|
+
// pass signal to the request
|
|
158
|
+
signal: this.abortSignal,
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// if innerAction1 will end after abortController.abort was called
|
|
162
|
+
// then calling innerAction2 will throw an instance of ExecutionAbortError
|
|
163
|
+
await this.executeAction(innerAction, payload);
|
|
164
|
+
},
|
|
165
|
+
deps: {
|
|
166
|
+
httpClient: HTTP_CLIENT,
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
```
|
|
170
|
+
|
|
121
171
|
### createBundle
|
|
122
172
|
|
|
123
173
|
`createBundle(options: BundleOptions)` - method to create a bundle.
|
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
import type { ActionParameters, Action } from '@tramvai/tokens-core';
|
|
2
|
+
/**
|
|
3
|
+
* @deprecated: use `declareAction`
|
|
4
|
+
*/
|
|
2
5
|
export declare function createAction<Result = any, Payload = any, Deps = any>(action: ActionParameters<Payload, Result, Deps>): Action<Payload, Result, Deps>;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { TramvaiAction, TramvaiActionDefinition } from '@tramvai/types-actions-state-context';
|
|
2
|
+
export declare function declareAction<Params extends any[], Result, Deps = {}>(action: TramvaiActionDefinition<Params, Result, Deps>): TramvaiAction<Params, Result, Deps>;
|
|
3
|
+
export declare function isTramvaiAction<Params extends any[], Result, Deps>(action: any): action is TramvaiAction<Params, Result, Deps>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { createApp, App } from './createApp';
|
|
2
2
|
export { createBundle } from './bundles/createBundle';
|
|
3
3
|
export { createAction } from './actions/createActions';
|
|
4
|
+
export * from './actions/declareAction';
|
|
4
5
|
export { Module, deprecatedModule as module, MODULE_PARAMETERS } from './modules/module';
|
|
5
6
|
export { getModuleParameters } from './modules/getModuleParameters';
|
|
6
7
|
export { walkOfModules } from './modules/walkOfModules';
|
package/lib/index.es.js
CHANGED
|
@@ -225,6 +225,9 @@ function createBundle(options) {
|
|
|
225
225
|
return mergeOptions(presets, options);
|
|
226
226
|
}
|
|
227
227
|
|
|
228
|
+
/**
|
|
229
|
+
* @deprecated: use `declareAction`
|
|
230
|
+
*/
|
|
228
231
|
function createAction(action) {
|
|
229
232
|
const result = Object.assign(action.fn, {
|
|
230
233
|
[ACTION_PARAMETERS]: action,
|
|
@@ -236,4 +239,14 @@ function createAction(action) {
|
|
|
236
239
|
return result;
|
|
237
240
|
}
|
|
238
241
|
|
|
239
|
-
|
|
242
|
+
function declareAction(action) {
|
|
243
|
+
return {
|
|
244
|
+
...action,
|
|
245
|
+
tramvaiActionVersion: 2,
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
function isTramvaiAction(action) {
|
|
249
|
+
return 'tramvaiActionVersion' in action && action.tramvaiActionVersion === 2;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export { App, MODULE_PARAMETERS, Module, createAction, createApp, createBundle, declareAction, getModuleParameters, isExtendedModule, isTramvaiAction, deprecatedModule as module, walkOfModules };
|
package/lib/index.js
CHANGED
|
@@ -233,6 +233,9 @@ function createBundle(options) {
|
|
|
233
233
|
return mergeOptions(presets, options);
|
|
234
234
|
}
|
|
235
235
|
|
|
236
|
+
/**
|
|
237
|
+
* @deprecated: use `declareAction`
|
|
238
|
+
*/
|
|
236
239
|
function createAction(action) {
|
|
237
240
|
const result = Object.assign(action.fn, {
|
|
238
241
|
[tokensCore.ACTION_PARAMETERS]: action,
|
|
@@ -244,6 +247,16 @@ function createAction(action) {
|
|
|
244
247
|
return result;
|
|
245
248
|
}
|
|
246
249
|
|
|
250
|
+
function declareAction(action) {
|
|
251
|
+
return {
|
|
252
|
+
...action,
|
|
253
|
+
tramvaiActionVersion: 2,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
function isTramvaiAction(action) {
|
|
257
|
+
return 'tramvaiActionVersion' in action && action.tramvaiActionVersion === 2;
|
|
258
|
+
}
|
|
259
|
+
|
|
247
260
|
Object.defineProperty(exports, 'DI_TOKEN', {
|
|
248
261
|
enumerable: true,
|
|
249
262
|
get: function () { return dippy.DI_TOKEN; }
|
|
@@ -274,8 +287,10 @@ exports.Module = Module;
|
|
|
274
287
|
exports.createAction = createAction;
|
|
275
288
|
exports.createApp = createApp;
|
|
276
289
|
exports.createBundle = createBundle;
|
|
290
|
+
exports.declareAction = declareAction;
|
|
277
291
|
exports.getModuleParameters = getModuleParameters;
|
|
278
292
|
exports.isExtendedModule = isExtendedModule;
|
|
293
|
+
exports.isTramvaiAction = isTramvaiAction;
|
|
279
294
|
exports.module = deprecatedModule;
|
|
280
295
|
exports.walkOfModules = walkOfModules;
|
|
281
296
|
Object.keys(tokensCore).forEach(function (k) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tramvai/core",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.20.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
@@ -20,10 +20,10 @@
|
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"@tinkoff/utils": "^2.1.2",
|
|
23
|
-
"@tramvai/tokens-common": "2.
|
|
24
|
-
"@tramvai/tokens-core": "2.
|
|
25
|
-
"@tramvai/types-actions-state-context": "2.
|
|
26
|
-
"@tinkoff/dippy": "0.
|
|
23
|
+
"@tramvai/tokens-common": "2.20.0",
|
|
24
|
+
"@tramvai/tokens-core": "2.20.0",
|
|
25
|
+
"@tramvai/types-actions-state-context": "2.20.0",
|
|
26
|
+
"@tinkoff/dippy": "0.8.2",
|
|
27
27
|
"tslib": "^2.0.3"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|