2023-01-17 16:12:04 +01:00
|
|
|
```js
|
|
|
|
|
import { Task } from '@vates/task'
|
|
|
|
|
|
|
|
|
|
const task = new Task({
|
2023-06-05 15:39:04 +02:00
|
|
|
// this object will be sent in the *start* event
|
|
|
|
|
properties: {
|
2023-04-17 11:29:05 +02:00
|
|
|
name: 'my task',
|
|
|
|
|
},
|
2023-01-17 16:12:04 +01:00
|
|
|
|
|
|
|
|
// if defined, a new detached task is created
|
|
|
|
|
//
|
|
|
|
|
// if not defined and created inside an existing task, the new task is considered a subtask
|
|
|
|
|
onProgress(event) {
|
|
|
|
|
// this function is called each time this task or one of it's subtasks change state
|
|
|
|
|
const { id, timestamp, type } = event
|
|
|
|
|
if (type === 'start') {
|
2023-06-05 15:39:04 +02:00
|
|
|
const { name, parentId, properties } = event
|
2023-01-17 16:12:04 +01:00
|
|
|
} else if (type === 'end') {
|
|
|
|
|
const { result, status } = event
|
|
|
|
|
} else if (type === 'info' || type === 'warning') {
|
|
|
|
|
const { data, message } = event
|
|
|
|
|
} else if (type === 'property') {
|
|
|
|
|
const { name, value } = event
|
2023-06-05 16:43:38 +02:00
|
|
|
} else if (type === 'abortionRequested') {
|
|
|
|
|
const { reason } = event
|
2023-01-17 16:12:04 +01:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// this field is settable once before being observed
|
|
|
|
|
task.id
|
|
|
|
|
|
2023-04-17 11:34:24 +02:00
|
|
|
// contains the current status of the task
|
|
|
|
|
//
|
|
|
|
|
// possible statuses are:
|
|
|
|
|
// - pending
|
|
|
|
|
// - success
|
|
|
|
|
// - failure
|
2023-01-17 16:12:04 +01:00
|
|
|
task.status
|
2023-04-17 11:34:24 +02:00
|
|
|
|
|
|
|
|
// Triggers the abort signal associated to the task.
|
|
|
|
|
//
|
|
|
|
|
// This simply requests the task to abort, it will be up to the task to handle or not this signal.
|
|
|
|
|
task.abort(reason)
|
2023-01-17 16:12:04 +01:00
|
|
|
|
|
|
|
|
// if fn rejects, the task will be marked as failed
|
|
|
|
|
const result = await task.runInside(fn)
|
|
|
|
|
|
|
|
|
|
// if fn rejects, the task will be marked as failed
|
|
|
|
|
// if fn resolves, the task will be marked as succeeded
|
|
|
|
|
const result = await task.run(fn)
|
2023-04-17 11:34:24 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Inside a task:
|
2023-01-17 16:12:04 +01:00
|
|
|
|
2023-04-17 11:34:24 +02:00
|
|
|
```js
|
2023-01-17 16:12:04 +01:00
|
|
|
// the abort signal of the current task if any, otherwise is `undefined`
|
|
|
|
|
Task.abortSignal
|
|
|
|
|
|
|
|
|
|
// sends an info on the current task if any, otherwise does nothing
|
|
|
|
|
Task.info(message, data)
|
|
|
|
|
|
|
|
|
|
// sends an info on the current task if any, otherwise does nothing
|
|
|
|
|
Task.warning(message, data)
|
|
|
|
|
|
|
|
|
|
// attaches a property to the current task if any, otherwise does nothing
|
|
|
|
|
//
|
|
|
|
|
// the latest value takes precedence
|
|
|
|
|
//
|
|
|
|
|
// examples:
|
|
|
|
|
// - progress
|
|
|
|
|
Task.set(property, value)
|
|
|
|
|
```
|
2023-04-17 11:32:02 +02:00
|
|
|
|
|
|
|
|
### `combineEvents`
|
|
|
|
|
|
|
|
|
|
Create a consolidated log from individual events.
|
|
|
|
|
|
|
|
|
|
It can be used directly as an `onProgress` callback:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
import { makeOnProgress } from '@vates/task/combineEvents'
|
|
|
|
|
|
|
|
|
|
const onProgress = makeOnProgress({
|
|
|
|
|
// This function is called each time a root task starts.
|
|
|
|
|
//
|
|
|
|
|
// It will be called for as many times as there are tasks created with this `onProgress` function.
|
|
|
|
|
onRootTaskStart(taskLog) {
|
|
|
|
|
// `taskLog` is an object reflecting the state of this task and all its subtasks,
|
|
|
|
|
// and will be mutated in real-time to reflect the changes of the task.
|
2023-06-07 15:23:33 +02:00
|
|
|
|
|
|
|
|
// timestamp at which the task started
|
|
|
|
|
taskLog.start
|
|
|
|
|
|
|
|
|
|
// current status of the task as described in the previous section
|
|
|
|
|
taskLog.status
|
|
|
|
|
|
|
|
|
|
// undefined or a dictionary of properties attached to the task
|
|
|
|
|
taskLog.properties
|
|
|
|
|
|
|
|
|
|
// timestamp at which the abortion was requested, undefined otherwise
|
|
|
|
|
taskLog.abortionRequestedAt
|
|
|
|
|
|
|
|
|
|
// undefined or an array of infos emitted on the task
|
|
|
|
|
taskLog.infos
|
|
|
|
|
|
|
|
|
|
// undefined or an array of warnings emitted on the task
|
|
|
|
|
taskLog.warnings
|
|
|
|
|
|
|
|
|
|
// timestamp at which the task ended, undefined otherwise
|
|
|
|
|
taskLog.end
|
|
|
|
|
|
|
|
|
|
// undefined or the result value of the task
|
|
|
|
|
taskLog.result
|
2023-04-17 11:32:02 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// This function is called each time a root task ends.
|
|
|
|
|
onRootTaskEnd(taskLog) {},
|
|
|
|
|
|
|
|
|
|
// This function is called each time a root task or a subtask is updated.
|
|
|
|
|
//
|
|
|
|
|
// `taskLog.$root` can be used to uncondionally access the root task.
|
|
|
|
|
onTaskUpdate(taskLog) {},
|
|
|
|
|
})
|
|
|
|
|
|
2023-12-21 17:38:58 +01:00
|
|
|
Task.run({ properties: { name: 'my task' }, onProgress }, asyncFn)
|
2023-04-17 11:32:02 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
It can also be fed event logs directly:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
import { makeOnProgress } from '@vates/task/combineEvents'
|
|
|
|
|
|
|
|
|
|
const onProgress = makeOnProgress({ onRootTaskStart, onRootTaskEnd, onTaskUpdate })
|
|
|
|
|
|
|
|
|
|
eventLogs.forEach(onProgress)
|
|
|
|
|
```
|