Skip to content

Commit

Permalink
Apply code review changes, fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
onigoetz committed Jan 3, 2024
1 parent 52a5873 commit 9311e43
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
31 changes: 10 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,40 +103,30 @@ export function extractProperties<T>(object?: T) {
}
}

export function createSyncFn<T extends AnyAsyncFn>(
export function createSyncFn<T extends AnyAsyncFn<R>, R = unknown>(
workerPath: string,
bufferSize?: number,
timeout?: number,
): Syncify<T>
export function createSyncFn<T extends AnyAsyncFn>(
workerPath: string,
options?: SynckitOptions,
): Syncify<T>
export function createSyncFn<R, T extends AnyAsyncFn<R>>(
workerPath: string,
bufferSizeOrOptions?: SynckitOptions | number,
timeout?: number,
) {
timeoutOrOptions?: SynckitOptions | number,
): Syncify<T> {
if (!path.isAbsolute(workerPath)) {
throw new Error('`workerPath` must be absolute')
}

const cachedSyncFn = syncFnCache.get(workerPath)

if (cachedSyncFn) {
return cachedSyncFn
return cachedSyncFn as Syncify<T>
}

const syncFn = startWorkerThread<R, T>(
workerPath,
/* istanbul ignore next */ typeof bufferSizeOrOptions === 'number'
? { timeout }
: bufferSizeOrOptions,
/* istanbul ignore next */ typeof timeoutOrOptions === 'number'
? { timeout: timeoutOrOptions }
: timeoutOrOptions,
)

syncFnCache.set(workerPath, syncFn)

return syncFn
return syncFn as Syncify<T>
}

const cjsRequire =
Expand Down Expand Up @@ -510,13 +500,12 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(
const syncFn = (...args: Parameters<T>): R => {
const id = nextID++

// Reset SharedArrayBuffer
Atomics.store(sharedBufferView, 0, 0)

const msg: MainToWorkerMessage<Parameters<T>> = { id, args }
worker.postMessage(msg)

const status = Atomics.wait(sharedBufferView, 0, 0, timeout)
// Reset SharedArrayBuffer for next call
Atomics.store(sharedBufferView, 0, 0)

/* istanbul ignore if */
if (!['ok', 'not-equal'].includes(status)) {
Expand Down
2 changes: 1 addition & 1 deletion test/ts-runner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ test('unknown ts runner', async () => {
const { createSyncFn } = await import('synckit')

expect(() =>
// @ts-expect-error
createSyncFn<AsyncWorkerFn>(path.resolve(_dirname, 'worker.js'), {
// @ts-expect-error
tsRunner: 'unknown',
}),
).toThrowErrorMatchingInlineSnapshot(`"Unknown ts runner: unknown"`)
Expand Down

0 comments on commit 9311e43

Please sign in to comment.