Skip to content

Commit

Permalink
one issue at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
kommander committed May 8, 2024
1 parent 9a7fb68 commit 2ffed75
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/gc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-env jest */
import path from 'path'
import { createThreadPool } from './index'
import debug from 'debug'
import { WorkerWithCallback } from './__tests__/workers/callback'
Expand Down
13 changes: 7 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export interface PoolInterface extends EventEmitter {
}

type ProxyWorkerTarget = Record<string, any>
type ProxyWorkerHandler = {
get: (proxyTarget: ProxyWorkerTarget, key: string) => any;
}

type FilterType<Base, Condition> = Pick<Base, {
[Key in keyof Base]: Base[Key] extends Condition ? Key : never
Expand All @@ -68,10 +71,11 @@ type WrapReturnType<Base extends TypeWithMethods> = {
: (...a: Parameters<Base[Key]>) => Promise<ReturnType<Base[Key]>>;
};

// @ts-ignore
// TODO: Fix usage of interface vs. type
// Even though this complains, the type is inferred from the template correctly,
// working for classes/interfaces and types
// @ts-expect-error Using function type explicitly here
// eslint-disable-next-line @typescript-eslint/ban-types
type FilterAndWrap<Base> = WrapReturnType<FilterType<Required<Base>, Function>>

export type WrapWorkerType<Base> = FilterAndWrap<Base> & BaseWorker & { all: FilterAndWrap<Base> }
Expand Down Expand Up @@ -372,9 +376,7 @@ export async function createThreadPool<T> (workerPath: string, {
}

// Intermediate, so the proxy can return itself
let proxy: ExtendedWorkerType

const handler = {
const proxy: ExtendedWorkerType = new PoolProxy<TargetWorkerType, ProxyWorkerHandler, ExtendedWorkerType> (target, {
get: (proxyTarget: ProxyWorkerTarget, key: string) => {
// NOTE: If the proxy is returned from an async function,
// the engine checks if it is a thenable by checking existence of a then method
Expand All @@ -400,8 +402,7 @@ export async function createThreadPool<T> (workerPath: string, {
return result
}
}
}
})

proxy = new PoolProxy<typeof target, typeof handler, ExtendedWorkerType> (target, handler)
return proxy
}
2 changes: 1 addition & 1 deletion src/thread-pool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ describe('Chaining', () => {
it('returns the proxy when a worker object returns itself', async () => {
const worker = await createThreadPool<ChainWorkerClass>('./__tests__/workers/this')

const result = (await worker.chain()).follow()
const result = await (await worker.chain()).follow()
worker.pool.terminate()

expect(result).toEqual('works')
Expand Down

0 comments on commit 2ffed75

Please sign in to comment.