Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement Runner proxy #106

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Dimava
Copy link
Contributor

@Dimava Dimava commented Oct 16, 2024

Runner proxy allows easily consume Typescript (and typed JS) modules the same way as if they would be imported as import * as runner
Async functions are typed the same, sync functions are mapped to async (this loses generics), non-functions are removed
worker_threads pool supports passing multiple arguments by using Object.assign(argsArray, { __tinypool_args__: true }

test('runner worker_threads test', async () => {
  const { runner } = new Tinypool({
    filename: resolve(__dirname, 'fixtures/multiple.js'),
    // runtime: both work same,
  }).withRunner<typeof import('./fixtures/multiple.js')>()

  expect((await runner.a()) satisfies 'a').toBe('a')
  expect((await runner.b()) satisfies 'b').toBe('b')
  expect(await runner.foobar({ foobar: 1 })).toBe(1)
  expect((await runner.asyncFoobar({ foobar: 1 })) satisfies 1).toBe(1)
  expect(await runner.args(1, 2, 3)).toThrow() // and type error
  expect((await runner.asyncArgs(1, 2, 3)).toThrow() // and type error
// fixtures/multiple.d.ts
export function a(): 'a'
export function b(): 'b'
export function foobar<V>(o: { foobar: V }): V
export function asyncFoobar<V>(o: { foobar: V }): Promise<V>
export function args<A>(...args: A[]): A
export function asyncArgs<A extends any[]>(...args: A): Promise<A>
export const digit: 4

Copy link

pkg-pr-new bot commented Oct 16, 2024

Open in Stackblitz

pnpm add https://pkg.pr.new/tinypool@106

commit: 30333bd

@jerome-benoit
Copy link
Contributor

What is the cost of that encapsulation?

@Dimava
Copy link
Contributor Author

Dimava commented Oct 22, 2024

@jerome-benoit: What is the cost of that encapsulation?

Runner proxy is free (Proxy creation + a couple of function calls, completely free if you cache the r.runner.fn)
Argument wrapping doesn't work with cloning (as cloning can't keep array properties) but otherwise also should be almost free

If you choose to exclude argarray from this PR I can split it into another PR

Copy link
Member

@AriPerkkio AriPerkkio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is something we want built-in into Tinypool, especially as there seems to be difference between runtime: 'worker_threads' and runtime: 'child_process' here. 🤔

Isn't this something that could be built on userland with small abstraction quite easily?

@Dimava
Copy link
Contributor Author

Dimava commented Oct 29, 2024

@AriPerkkio I've removed all the args code, now it's as simple as I could get it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants