@live-change/task-service 0.8.119 → 0.8.120

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/index.js CHANGED
@@ -4,7 +4,7 @@ const app = App.app()
4
4
  import definition from './definition.js'
5
5
 
6
6
  import './model.js'
7
- import task from './task.js'
7
+ import task from './task.ts'
8
8
 
9
9
  export { task }
10
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/task-service",
3
- "version": "0.8.119",
3
+ "version": "0.8.120",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -22,8 +22,8 @@
22
22
  },
23
23
  "type": "module",
24
24
  "dependencies": {
25
- "@live-change/framework": "^0.8.119",
26
- "@live-change/relations-plugin": "^0.8.119"
25
+ "@live-change/framework": "^0.8.120",
26
+ "@live-change/relations-plugin": "^0.8.120"
27
27
  },
28
- "gitHead": "1f0bb577d5bfe8b8d355775d7adbd73c789cd1d5"
28
+ "gitHead": "adf31fd8365df0655d88293f96a93734058d3dd4"
29
29
  }
@@ -1,7 +1,8 @@
1
1
  import App from '@live-change/framework'
2
+ // @ts-ignore:next-line
2
3
  const app = App.app()
3
4
 
4
- import crypto from 'crypto'
5
+ import * as crypto from 'crypto'
5
6
 
6
7
  import PQueue from 'p-queue'
7
8
 
@@ -87,10 +88,68 @@ async function startTask(taskFunction, props, causeType, cause){
87
88
  return { task: taskObject.id, taskObject, promise, causeType, cause }
88
89
  }
89
90
 
90
- export default function task(definition, serviceDefinition) {
91
+ interface TaskExecuteApi {
92
+ id: string,
93
+ run: (taskFunction, props, progressFactor) => Promise<any>,
94
+ progress: (current, total, action, opts) => void,
95
+ trigger: (trigger, props) => Promise<any>,
96
+ triggerService: (trigger, props, returnArray) => Promise<any>
97
+ }
98
+
99
+ interface TaskExecuteContext {
100
+ task: TaskExecuteApi,
101
+ trigger: (trigger, props) => Promise<any>,
102
+ triggerService: (trigger, props, returnArray) => Promise<any>,
103
+ causeType: string,
104
+ cause: string
105
+ }
106
+
107
+ interface TaskDefinition {
108
+ /**
109
+ * Task name
110
+ */
111
+ name: string,
112
+
113
+ /**
114
+ * Maximum number of retries
115
+ */
116
+ maxRetries?: number,
117
+
118
+ /**
119
+ * Task execution function
120
+ * @param props - task properties/parameters
121
+ * @param context - task context
122
+ * @param emit - event emitter function
123
+ * @returns {Promise<any>} - task result promise
124
+ */
125
+ execute: (props, context: TaskExecuteContext, emit) => Promise<any>,
126
+
127
+ /**
128
+ * Cleanup function
129
+ * @param props - task properties/parameters
130
+ * @param context - task context
131
+ * @returns {Promise<void>} - cleanup result promise
132
+ */
133
+ cleanup?: (props, context: TaskExecuteContext) => Promise<void>,
134
+
135
+ /**
136
+ * Fallback function
137
+ * @param props - task properties/parameters
138
+ * @param context - task context
139
+ * @param error - error object
140
+ * @returns {Promise<any>} - fallback result
141
+ */
142
+ fallback?: (props, context: TaskExecuteContext, error) => any
143
+ }
144
+
145
+ type TaskFunction = (props, context: TaskExecuteContext, emit, reportProgress) => Promise<any>
146
+
147
+ export default function task(definition:TaskDefinition, serviceDefinition) {
91
148
  if(!definition) throw new Error('Task definition is not defined')
92
149
  if(!serviceDefinition) throw new Error('Service definition is not defined')
93
- const taskFunction = async (props, context, emit, reportProgress = () => {}) => {
150
+ const taskFunction = async (props, context,
151
+ emit = events => app.emitEvents(definition.name, Array.isArray(events) ? events : [events], {}),
152
+ reportProgress = (current, total, selfProgress) => {}) => {
94
153
  if(!emit) emit = (events) =>
95
154
  app.emitEvents(definition.name, Array.isArray(events) ? events : [events], {})
96
155
 
@@ -127,7 +186,7 @@ export default function task(definition, serviceDefinition) {
127
186
  })
128
187
  }
129
188
 
130
- let selfProgress = { current: 0, total: 0 }
189
+ let selfProgress = { current: 0, total: 0, action: undefined }
131
190
  const subtasksProgress = []
132
191
  let progressUpdateTimer, lastProgressUpdate = 0
133
192
  const progressThrottleTime = 400
@@ -159,7 +218,7 @@ export default function task(definition, serviceDefinition) {
159
218
  ...context,
160
219
  task: {
161
220
  id: taskObject.id,
162
- async run(taskFunction, props, progressFactor = 1) {
221
+ async run(taskFunction: TaskFunction, props, progressFactor = 1) {
163
222
  if(typeof taskFunction !== 'function') {
164
223
  console.log("TASK FUNCTION", taskFunction)
165
224
  throw new Error('Task function is not a function')