1 | dataflo.ws task is a flow building block.
|
2 |
|
3 | Tasks are described using configuration, which includes code reference to run,
|
4 | input parameters, output parameter and meta.
|
5 |
|
6 | ### Example
|
7 |
|
8 | ```json
|
9 | "tasks": [{
|
10 | "$promise": "fetchDBRecordById",
|
11 | "$args": {
|
12 | "id": "123"
|
13 | },
|
14 | "$set": "record",
|
15 | "$setOnEmpty": "noRecord"
|
16 | }, {
|
17 | "task": "urlData",
|
18 | "if": "{$noRecord}",
|
19 | "url": "http://apiserver/db-records/123",
|
20 | "$set": "recordDataResponse"
|
21 | }, {
|
22 | "$function": "JSON.parse",
|
23 | "$args": "{$recordDataResponse.data}",
|
24 | "$set": "recordData"
|
25 | }, {
|
26 | "$promise": "insertDBRecord",
|
27 | "$args": {
|
28 | "id": "{$recordData.id}",
|
29 | "name": "{$recordData.name}"
|
30 | },
|
31 | "$set": "record"
|
32 | }, {
|
33 | "$function": "console.log",
|
34 | "$args": "{$record}"
|
35 | }]
|
36 | ```
|
37 |
|
38 | ### Variables
|
39 |
|
40 | As you may noticed, there is some magic values within strings.
|
41 | Main purpose of those magic values is task requirements declaration
|
42 | and dataflow description. You can read it like:
|
43 |
|
44 | ```
|
45 | recordDataResponse.data is a parameter of JSON.parse
|
46 | urlData provides recordDataResponse
|
47 | we need to launch JSON.parse after urlData is completed and recordDataResponse.data is true value
|
48 | ```
|
49 |
|
50 | In the current stage we have only two expansions: true value (`{$key}`) and any value (`{*key}`);
|
51 |
|
52 | ### Task code
|
53 |
|
54 | Task code reference can be:
|
55 |
|
56 | 1. function with immediate return value — synchronous, keys: `fn`, `$function`;
|
57 | 2. function with node-styled callback (errback) — asynchronous, keys: `errback`, `$errback`;
|
58 | 3. promise — asynchronous, keys: `promise`, `$promise`;
|
59 | 4. task class — asynchronous, `task`, `$class`, TODO: take a look into [TASKCLASS](TASKCLASS.md).
|
60 | 5. TODO: special case — every task;
|
61 | 5. TODO: special case — flow task;
|
62 |
|
63 | Functions and promises is looked up in `require.main.exports`, tasks is a regular node modules
|
64 | and usually lies within `node_modules/task` directory.
|
65 |
|
66 | ### Task requirements
|
67 |
|
68 | Task requirements need to be fulfilled before task launch. Those requirements can be
|
69 | input task parameters or just values in task configuration. Task will not receive second ones,
|
70 | they're only evaluated at task launch stage. `urlData` task won't launch unless
|
71 | key `{$noRecord}` is not set.
|
72 |
|
73 | ### Input parameters
|
74 |
|
75 | Input parameters for tasks is defined by $args key. If value for that key
|
76 | is object, string, boolean or number then object will be the first parameter of function arguments.
|
77 | If $args is array, array items became function arguments
|
78 |
|
79 | ### Output parameters
|
80 |
|
81 | When task return value, this value can be omited or used in flow data. You can:
|
82 |
|
83 | 1. Write value to the key in flow data when task succeeds with `$set`;
|
84 | 1. Merge returned object with key in flow data when task succeeds with `$mergeWith`;
|
85 | 1. Write value to the key in flow data when task succeeds, but don't provided a true value with `$setOnEmpty` (bad key name, I know);
|
86 | 1. Write value to the key in flow data when task fails, with `$setOnFail`;
|
87 |
|
88 | Key name must be a plain string, not the expansion.
|
89 |
|