Skip to content

Commit

Permalink
feat: options for useWorkerFns()
Browse files Browse the repository at this point in the history
  • Loading branch information
mys1024 committed Mar 9, 2024
1 parent 762d90f commit a841a0e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ export function useWorkerFn<FN extends AnyFn>(
* Invoke this function in the main thread to create proxy functions of all worker functions.
*
* @param worker - A Worker instance.
* @param options - An object containing options.
* @returns Proxy functions.
*/
export function useWorkerFns<FNS extends Record<string, AnyFn>>(
worker: MsgPort,
options: {
[NAME in keyof FNS]?: UseWorkerFnOpts<FNS[NAME]>;
} = {},
): ProxyFns<FNS> {
const memo = new Map<string, ProxyFn<AnyFn>>();
const fns = new Proxy({}, {
Expand All @@ -56,7 +60,7 @@ export function useWorkerFns<FNS extends Record<string, AnyFn>>(
if (memo.has(name)) {
return memo.get(name);
}
const fn = useWorkerFn(name, worker);
const fn = useWorkerFn(name, worker, options[name]);
memo.set(name, fn);
return fn;
},
Expand Down
6 changes: 5 additions & 1 deletion test/fns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ Deno.test("fns", async (t) => {
type: "module",
},
);
const { add, fib } = useWorkerFns<Fns>(worker);
const { add, fib } = useWorkerFns<Fns>(worker, {
fib: {
transfer: () => [],
},
});
assertEquals(await add(1, 2), 3);
assertEquals(await add(5, 5), 10);
assertEquals(await add(10, 20), 30);
Expand Down

0 comments on commit a841a0e

Please sign in to comment.