async-task-schedule
Advanced tools
Comparing version 0.1.3 to 1.0.0
@@ -171,3 +171,3 @@ function $parcel$export(e, n, v, s) { | ||
if (!val) throw new Error("not found"); | ||
acc.push(val); | ||
acc.push(val[1]); | ||
return acc; | ||
@@ -203,7 +203,7 @@ }, result); | ||
const defaultValue = new Error("not found"); | ||
doneArray = tasks.map((t)=>{ | ||
const taskResult = result.find((item)=>this.isSameTask(item[0], t)); | ||
doneArray = tasks.map((t, idx)=>{ | ||
const taskResult = result.length > idx ? result[idx] : defaultValue; | ||
return { | ||
task: t, | ||
value: taskResult ? taskResult[1] : defaultValue, | ||
value: taskResult ? taskResult : defaultValue, | ||
time: now | ||
@@ -287,6 +287,3 @@ }; | ||
const result = results[idx]; | ||
return [ | ||
t, | ||
result.status === "fulfilled" ? result.value : result.reason | ||
]; | ||
return result.status === "fulfilled" ? result.value : result.reason; | ||
}); | ||
@@ -293,0 +290,0 @@ }; |
@@ -170,3 +170,3 @@ function $parcel$export(e, n, v, s) { | ||
if (!val) throw new Error("not found"); | ||
acc.push(val); | ||
acc.push(val[1]); | ||
return acc; | ||
@@ -202,7 +202,7 @@ }, result); | ||
const defaultValue = new Error("not found"); | ||
doneArray = tasks.map((t)=>{ | ||
const taskResult = result.find((item)=>this.isSameTask(item[0], t)); | ||
doneArray = tasks.map((t, idx)=>{ | ||
const taskResult = result.length > idx ? result[idx] : defaultValue; | ||
return { | ||
task: t, | ||
value: taskResult ? taskResult[1] : defaultValue, | ||
value: taskResult ? taskResult : defaultValue, | ||
time: now | ||
@@ -286,6 +286,3 @@ }; | ||
const result = results[idx]; | ||
return [ | ||
t, | ||
result.status === "fulfilled" ? result.value : result.reason | ||
]; | ||
return result.status === "fulfilled" ? result.value : result.reason; | ||
}); | ||
@@ -292,0 +289,0 @@ }; |
@@ -13,3 +13,3 @@ export type ITaskExecStrategy = 'parallel' | 'serial'; | ||
*/ | ||
batchDoTasks?: (tasks: Task[]) => Promise<Array<[Task, Result | Error]>> | Array<[Task, Result | Error]>; | ||
batchDoTasks?: (tasks: Task[]) => Promise<Array<Result | Error>> | Array<Result | Error>; | ||
/** | ||
@@ -60,3 +60,3 @@ * action to do single task | ||
dispatch<T extends readonly Task[] | []>(tasks: T): Promise<{ | ||
[k in keyof T]: [Task, Result | Error]; | ||
[k in keyof T]: Result | Error; | ||
}>; | ||
@@ -98,3 +98,3 @@ /** | ||
*/ | ||
static wrapDoTask<T, R>(doTask: (t: T) => Promise<R> | R): (tasks: T[]) => Promise<Array<[T, R | Error]>>; | ||
static wrapDoTask<T, R>(doTask: (t: T) => Promise<R> | R): (tasks: T[]) => Promise<Array<R | Error>>; | ||
/** | ||
@@ -101,0 +101,0 @@ * check whether the given values are equal (with deep comparison) |
{ | ||
"name": "async-task-schedule", | ||
"version": "0.1.3", | ||
"version": "1.0.0", | ||
"description": "schedule async tasks", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -8,3 +8,3 @@ # async-task-schedule | ||
</a> | ||
<img src="https://img.shields.io/badge/coverage-100%25-brightgreen" alt="use it with confident"> | ||
<img src="https://img.shields.io/badge/coverage-100%25-brightgreen?logo=codecov" alt="use it with confident"> | ||
<a href="#readme"> | ||
@@ -41,8 +41,13 @@ <img src="https://badgen.net/badge/Built%20With/TypeScript/blue" alt="code with typescript" height="20"> | ||
import TaskSchedule from 'async-task-schedule' | ||
let count = 0 | ||
const taskSchedule = new TaskSchedule({ | ||
batchDoTasks: async (names: string[]) => { | ||
count += 1 | ||
return names.map((n) => ([n, `${n}${count}`] as [string, string])) | ||
}, | ||
doTask: async (name: string) => { | ||
count += 1 | ||
return `${name}${count}` | ||
}, | ||
// or use this will do the same | ||
// batchDoTasks: async (names: string[]) => { | ||
// count += 1 | ||
// return names.map((n) => `${n}${count}`) | ||
// }, | ||
}) | ||
@@ -82,6 +87,6 @@ | ||
* | ||
* batchDoTasks should receive multitasks, and return tuple of task and response or error in array | ||
* batchDoTasks should receive multitasks, and return result or error in order | ||
* one of batchDoTasks/doTask must be specified, batchDoTasks will take priority | ||
*/ | ||
batchDoTasks: (tasks: Task[]) => Promise<Array<[Task, Result | Error ]>> | Array<[Task, Result | Error ]> | ||
batchDoTasks: (tasks: Task[]) => Promise<Array<Result | Error>> | Array<Result | Error> | ||
@@ -97,3 +102,3 @@ /** | ||
* it helps to avoid duplicated tasks | ||
* default: (a, b) => a === b | ||
* default: AsyncTask.isEqual (deep equal) | ||
*/ | ||
@@ -135,5 +140,5 @@ isSameTask?: (a: Task, b: Task) => boolean | ||
* task result caching duration(in milliseconds), default to 1000ms (1s) | ||
* >`undefined` or `0` for unlimited | ||
* >set to minimum value `1` to disable caching | ||
* >`function` to specified specified each task's validity | ||
* > `undefined` or `0` for unlimited | ||
* > set to minimum value `1` to disable caching | ||
* > `function` to specified specified each task's validity | ||
* | ||
@@ -165,3 +170,3 @@ * *cache is lazy cleaned after invalid* | ||
// get first result | ||
const resultOf1 = result[0][1] // 1 | ||
const resultOf1 = result[0] // 1 | ||
// doTask won't be called | ||
@@ -177,5 +182,5 @@ const result11 = await taskSchedule.dispatch(1) // 1 | ||
### dispatch(tasks: Task[]):Promise<Array<[Task, Result | Error]>> | ||
dispatch multitasks at a time, will get tuple of task and response in array with corresponding order of `tasks` | ||
this method won't throw any error, it will fulfil even partially failed, you can check whether its success by `tuple[1] instanceof Error` | ||
### dispatch(tasks: Task[]):Promise<Array<Result | Error>> | ||
dispatch multitasks at a time, will get response with corresponding order of `tasks` | ||
this method won't throw any error, it will fulfil even partially failed, you can check whether its success by `response instanceof Error` | ||
@@ -196,5 +201,5 @@ ```ts | ||
// get first result | ||
const resultOf1 = result[0][1] // 1 | ||
const resultOf1 = result[0] // 1 | ||
// second result is error | ||
const isError = result[1][1] instanceof Error // error object | ||
const isError = result[1] instanceof Error // error object | ||
@@ -251,9 +256,9 @@ | ||
await Promise.all([ | ||
taskSchedule.dispatch([1,2,3,1,2]), | ||
taskSchedule.dispatch([1,9,10,12,22]), | ||
taskSchedule.dispatch([1, 2, 3, 1, 2]), | ||
taskSchedule.dispatch([1, 9, 10, 12, 22]), | ||
]) | ||
// clean all cached result | ||
taskSchedule.cleanCache() | ||
// second result is error | ||
const result1 = await taskSchedule.dispatch(1), | ||
// task will execute again | ||
const result = await taskSchedule.dispatch(1), | ||
@@ -296,3 +301,3 @@ ``` | ||
### example 1: cache `fetch` | ||
#### example 1: cache `fetch` | ||
suppose we use browser native `fetch` to send request, we can do so to make an improvement: | ||
@@ -336,3 +341,3 @@ | ||
```ts | ||
getUsers(userIds: string[]) -> Promise<[{id: string, name: string, email: string}]> | ||
getUsers(userIds: string[]) => Promise<[{code: string, message: string, id?: string, name?: string, email?: string}]> | ||
``` | ||
@@ -345,4 +350,4 @@ | ||
const users = await getUsers(userIds) | ||
// convert users array into tuple of user id and user info in array | ||
return users.map(user => ([user.id, user])) | ||
// convert invalid users to error | ||
return users.map(user => (user.code === 'failed' ? new Error(user.message) : user)) | ||
} | ||
@@ -367,2 +372,3 @@ | ||
If you got a batch version function, you just need to make sure it throw an error when error occurred. | ||
@@ -369,0 +375,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
41022
1
375
730