Skip to content

Commit 1f7b88b

Browse files
committed
feat(functions): add parallel promiseFinally timeout
1 parent eb993f1 commit 1f7b88b

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

src/js/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ export * from './listenStack'
22
export * from './copy'
33
export * from './curry'
44
export * from './sleep'
5+
export * from './parallel'
6+
export * from './promiseFinally'

src/js/parallel.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function parallel(tasks: any[], fn: (...args: any[]) => any) {
2+
return Promise.all(tasks.map(fn))
3+
}

src/js/promiseFinally.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { isFn } from '../is/isFn'
2+
3+
export async function promiseFinally(
4+
fn: Promise<any> | Function,
5+
finalFn: Function,
6+
) {
7+
let result
8+
try {
9+
result = await (isFn(fn) ? fn() : fn)
10+
}
11+
finally {
12+
finalFn()
13+
}
14+
return result
15+
}

src/js/timeout.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { promiseFinally } from './promiseFinally'
2+
3+
export const timeout = function timeout(fn: Function, ms: number, msg: string) {
4+
let timerId: NodeJS.Timeout
5+
const warpPromise = promiseFinally(fn, () => clearTimeout(timerId))
6+
const timerPromise = new Promise((resolve, reject) => {
7+
timerId = setTimeout(() => reject(new Error(msg)), ms)
8+
})
9+
return Promise.race([warpPromise, timerPromise])
10+
}

src/node/useNodeWorker.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import worker_threads from 'worker_threads'
22
import path from 'path'
33
import { isArray } from '../is/isArray'
44
import { isStr } from '../is/isStr'
5+
import { parallel } from '../js/parallel'
56
import type { IShellMessage, NodeWorkerPayload } from '../types'
67

78
type NodeWorkReturn<T> = T extends NodeWorkerPayload
@@ -12,18 +13,18 @@ export async function useNodeWorker<T extends NodeWorkerPayload | string>(
1213
payload: T,
1314
url?: string,
1415
): Promise<NodeWorkReturn<T>> {
15-
url = url || path.resolve(__dirname, './node/useNodeWorkerThread.js')
16+
// const dev = './useNodeWorkerThread.ts'
17+
const prd = './node/useNodeWorkerThread.js'
18+
url = url || path.resolve(__dirname, prd)
1619
const { params, stdio } = isStr(payload)
1720
? { params: payload, stdio: 'pipe' }
1821
: payload
1922
const commands = isArray(params) ? params : params.split('&&')
20-
const result = await Promise.all(
21-
commands.map(params =>
22-
createWorker({
23-
params,
24-
stdio: stdio as 'pipe' | 'inherit',
25-
}),
26-
),
23+
const result = await parallel(commands, params =>
24+
createWorker({
25+
params,
26+
stdio: stdio as 'pipe' | 'inherit',
27+
}),
2728
)
2829
setTimeout(process.exit) // 结束子进程
2930
return (result.length === 1 ? result[0] : result) as NodeWorkReturn<T>
@@ -44,3 +45,8 @@ export function useProcressNodeWorker(callback: (data: any) => any) {
4445
parentPort?.postMessage((await callback?.(data)) || (() => '')),
4546
)
4647
}
48+
49+
// useNodeWorker({
50+
// params: 'echo "hi" && echo "hello"',
51+
// stdio: 'inherit'
52+
// })

0 commit comments

Comments
 (0)