Skip to content

Commit

Permalink
refactor: tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
mys1024 committed Apr 21, 2024
1 parent e33503d commit cecac01
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 121 deletions.
43 changes: 0 additions & 43 deletions src/define.ts

This file was deleted.

127 changes: 124 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,129 @@
* making it easy to call the functions defined in workers.
*/

export type { CallOptions, DefineOptions } from "./types.ts";
import {
MRpc,
type NodeWorkerOrNodeMessagePort,
type WorkerGlobalScope,
} from "@mys-x/m-rpc";
import type {
AnyFn,
CallOptions,
DefineOptions,
ProxyFn,
ProxyFns,
} from "./types.ts";

export { defineWorkerFn, defineWorkerFns } from "./define.ts";
/**
* Define a worker function.
*
* @param name - A name that identifies the worker function.
* @param fn - The worker function.
* @param options - An object containing options.
*/
function defineWorkerFn<FN extends AnyFn>(
name: string,
fn: FN,
options: DefineOptions<FN> = {},
): void {
const {
port = globalThis as unknown as WorkerGlobalScope,
...restOptions
} = options;
const rpc = MRpc.ensureMRpc(port);
rpc.defineLocalFn(name, fn, restOptions);
}

/**
* Define worker functions.
*
* @param fns - An object containing worker functions. Keys will be used as the names of the worker functions.
* @param options - An object containing options.
*/
function defineWorkerFns<FNS extends Record<string, AnyFn>>(
fns: FNS,
options: {
[NAME in keyof FNS]?: DefineOptions<FNS[NAME]>;
} = {},
): void {
for (const [name, fn] of Object.entries(fns)) {
defineWorkerFn(name, fn, options[name]);
}
}

/**
* Create a proxy function that calls the corresponding worker function.
*
* @param name - The name that identifies the worker function.
* @param worker - A worker instance.
* @param options - An object containing options.
* @returns The proxy function.
*/
function useWorkerFn<FN extends AnyFn>(
name: string,
worker: Worker | NodeWorkerOrNodeMessagePort,
options?: CallOptions<FN>,
): ProxyFn<FN> {
const rpc = MRpc.ensureMRpc(worker);
const _fn = rpc.useRemoteFn(name, options);
const fn = (async (...args) => {
try {
return await _fn(...args);
} catch (err) {
// overwrite the error message
if (
err instanceof Error &&
err.message ===
`The remote threw an error when calling the function "${name}".`
) {
err.message = `The worker function "${name}" throws an error.`;
}
throw err;
}
}) as ProxyFn<FN>;
return fn;
}

export { inspectWorker, useWorkerFn, useWorkerFns } from "./use.ts";
/**
* Create the proxy functions of all worker functions.
*
* @param worker - A worker instance.
* @param options - An object containing options.
* @returns The proxy functions.
*/
function useWorkerFns<FNS extends Record<string, AnyFn>>(
worker: Worker | NodeWorkerOrNodeMessagePort,
options?: {
[NAME in keyof FNS]?: CallOptions<FNS[NAME]>;
},
): ProxyFns<FNS> {
const rpc = MRpc.ensureMRpc(worker);
return rpc.useRemoteFns(options);
}

/**
* Inspect a worker.
*
* @param worker A worker instance.
* @returns Information about the worker.
*/
async function inspectWorker(
worker: Worker | NodeWorkerOrNodeMessagePort,
): Promise<{
names: string[] | undefined;
}> {
const rpc = MRpc.ensureMRpc(worker);
return {
names: await rpc.getRemoteFnNames(),
};
}

export {
defineWorkerFn,
defineWorkerFns,
inspectWorker,
useWorkerFn,
useWorkerFns,
};

export type { CallOptions, DefineOptions } from "./types.ts";
75 changes: 0 additions & 75 deletions src/use.ts

This file was deleted.

0 comments on commit cecac01

Please sign in to comment.