Skip to content

Commit

Permalink
feat!: change the options type of defineWorkerFns()
Browse files Browse the repository at this point in the history
Also add function type support for DefineWorkerFnOpts.transfer
  • Loading branch information
mys1024 committed Mar 17, 2024
1 parent 1d1c206 commit ba3ceb8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
16 changes: 9 additions & 7 deletions src/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const _global = Function("return this")() as MsgPortNormalized;
* @param fn - The worker function.
* @param options - An object containing options.
*/
export function defineWorkerFn(
export function defineWorkerFn<FN extends AnyFn>(
name: string,
fn: AnyFn,
options: DefineWorkerFnOpts = {},
fn: FN,
options: DefineWorkerFnOpts<FN> = {},
): void {
const { transfer, port = _global } = options;
const rpcAgent = RpcAgent.getRpcAgent(port);
Expand All @@ -37,12 +37,14 @@ export function defineWorkerFn(
* @param functions - An object containing worker functions. Keys will be used as the names of the worker functions.
* @param options - An object containing options.
*/
export function defineWorkerFns(
functions: Record<string, AnyFn>,
options: DefineWorkerFnOpts = {},
export function defineWorkerFns<FNS extends Record<string, AnyFn>>(
functions: FNS,
options: {
[NAME in keyof FNS]?: DefineWorkerFnOpts<FNS[NAME]>;
} = {},
): void {
for (const [name, fn] of Object.entries(functions)) {
defineWorkerFn(name, fn, options);
defineWorkerFn(name, fn, options[name]);
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/rpc/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class RpcAgent {
string,
Map<string, {
fn: AnyFn;
transfer: boolean;
transfer: boolean | ((ctx: { ret: any }) => Transferable[]);
}>
>();

Expand Down Expand Up @@ -62,9 +62,9 @@ export class RpcAgent {
this.#startListening();
}

defineLocalFn(name: string, fn: AnyFn, options: {
defineLocalFn<FN extends AnyFn>(name: string, fn: FN, options: {
namespace?: string;
transfer?: boolean;
transfer?: boolean | ((ctx: { ret: ReturnType<FN> }) => Transferable[]);
} = {}) {
const { namespace = DEFAULT_NAMESPACE, transfer = true } = options;

Expand All @@ -83,7 +83,7 @@ export class RpcAgent {
namespace?: string;
transfer?: boolean | ((ctx: { args: Parameters<FN> }) => Transferable[]);
} = {}) {
const { namespace = DEFAULT_NAMESPACE, transfer } = options;
const { namespace = DEFAULT_NAMESPACE, transfer = true } = options;

const keyCallMap = this.#getKeyCallMap(namespace, true);

Expand Down Expand Up @@ -195,7 +195,9 @@ export class RpcAgent {
// transferables exist due to them not being able to be cloned, so to ensure in a complex object we grab all the
// transferables that can't be cloned we traverse the entire object finding all transferables, and listing them to be transferred
transfer: transfer
? getTransferables(ret, supportFlags.streams)
? transfer === true
? getTransferables(ret, supportFlags.streams)
: transfer?.({ ret })
: undefined,
});
} catch (err) {
Expand Down
12 changes: 7 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ export type InternalFns = {

/* -------------------------------------------------- options -------------------------------------------------- */

export interface DefineWorkerFnOpts {
export interface DefineWorkerFnOpts<FN extends AnyFn> {
/**
* Whether to transfer the transferable objects exist in the return value of the worker function.
* A boolean value indicating whether to transfer the transferable objects exist in the return value of the worker function,
* or a function that returns transferable objects should be transferred.
*
* @default true
* @see https://developer.mozilla.org/en-US/docs/Web/API/Worker/postMessage#transfer
*/
transfer?: boolean;
transfer?: boolean | ((ctx: { ret: ReturnType<FN> }) => Transferable[]);

/**
* The message port to communicate with the main thread.
Expand All @@ -45,10 +46,11 @@ export interface DefineWorkerFnOpts {

export interface UseWorkerFnOpts<FN extends AnyFn> {
/**
* A boolean value indicating whether to transfer the transferable objects exist in the arguments, or a function that returns transferable objects should be transferred.
* A boolean value indicating whether to transfer the transferable objects exist in the arguments,
* or a function that returns transferable objects should be transferred.
*
* @default true
* @see https://developer.mozilla.org/en-US/docs/Web/API/Worker/postMessage#transfer
* @returns Transferable objects.
*/
transfer?: boolean | ((ctx: { args: Parameters<FN> }) => Transferable[]);
}
4 changes: 3 additions & 1 deletion test/fns.test.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const fns = {
},
};

defineWorkerFns(fns);
defineWorkerFns(fns, {
fib: { transfer: false },
});

export type Fns = typeof fns;

0 comments on commit ba3ceb8

Please sign in to comment.