Skip to content

Commit

Permalink
feat(functions): add parallel promiseFinally timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon-He95 committed Dec 18, 2022
1 parent eb993f1 commit 1f7b88b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export * from './listenStack'
export * from './copy'
export * from './curry'
export * from './sleep'
export * from './parallel'
export * from './promiseFinally'
3 changes: 3 additions & 0 deletions src/js/parallel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function parallel(tasks: any[], fn: (...args: any[]) => any) {
return Promise.all(tasks.map(fn))
}
15 changes: 15 additions & 0 deletions src/js/promiseFinally.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { isFn } from '../is/isFn'

export async function promiseFinally(
fn: Promise<any> | Function,
finalFn: Function,
) {
let result
try {
result = await (isFn(fn) ? fn() : fn)
}
finally {
finalFn()
}
return result
}
10 changes: 10 additions & 0 deletions src/js/timeout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { promiseFinally } from './promiseFinally'

export const timeout = function timeout(fn: Function, ms: number, msg: string) {
let timerId: NodeJS.Timeout
const warpPromise = promiseFinally(fn, () => clearTimeout(timerId))
const timerPromise = new Promise((resolve, reject) => {
timerId = setTimeout(() => reject(new Error(msg)), ms)
})
return Promise.race([warpPromise, timerPromise])
}
22 changes: 14 additions & 8 deletions src/node/useNodeWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import worker_threads from 'worker_threads'
import path from 'path'
import { isArray } from '../is/isArray'
import { isStr } from '../is/isStr'
import { parallel } from '../js/parallel'
import type { IShellMessage, NodeWorkerPayload } from '../types'

type NodeWorkReturn<T> = T extends NodeWorkerPayload
Expand All @@ -12,18 +13,18 @@ export async function useNodeWorker<T extends NodeWorkerPayload | string>(
payload: T,
url?: string,
): Promise<NodeWorkReturn<T>> {
url = url || path.resolve(__dirname, './node/useNodeWorkerThread.js')
// const dev = './useNodeWorkerThread.ts'
const prd = './node/useNodeWorkerThread.js'
url = url || path.resolve(__dirname, prd)
const { params, stdio } = isStr(payload)
? { params: payload, stdio: 'pipe' }
: payload
const commands = isArray(params) ? params : params.split('&&')
const result = await Promise.all(
commands.map(params =>
createWorker({
params,
stdio: stdio as 'pipe' | 'inherit',
}),
),
const result = await parallel(commands, params =>
createWorker({
params,
stdio: stdio as 'pipe' | 'inherit',
}),
)
setTimeout(process.exit) // 结束子进程
return (result.length === 1 ? result[0] : result) as NodeWorkReturn<T>
Expand All @@ -44,3 +45,8 @@ export function useProcressNodeWorker(callback: (data: any) => any) {
parentPort?.postMessage((await callback?.(data)) || (() => '')),
)
}

// useNodeWorker({
// params: 'echo "hi" && echo "hello"',
// stdio: 'inherit'
// })

0 comments on commit 1f7b88b

Please sign in to comment.