Skip to content

Commit

Permalink
feat!: refactor with @mys/m-rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
mys1024 committed Apr 21, 2024
1 parent 4201959 commit 4792a41
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 492 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
"denoland",
"lcov",
"minzip",
"tsup",
"mrpc",
"okikio",
"pnpx",
"runtimes",
"Transferables",
"tsup",
"typeof"
]
}
6 changes: 4 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
"run:watch": "deno run --watch src/main.ts",
"cache": "deno cache --lock-write src/**/*.ts test/**/*.ts",
"cache:reload": "deno cache --lock-write --reload src/**/*.ts test/**/*.ts",
"check": "deno check src/**/*.ts test/**/*.ts && deno lint && deno fmt --check",
"test": "deno test -A",
"test:watch": "deno test -A --watch",
"test:lcov": "deno test -A --coverage && deno coverage --lcov --output=cov.lcov",
"bump": "deno task check && deno task test && deno publish --dry-run && pnpm i -C npm && pnpm run -C npm build && pnpm publish -C npm --no-git-checks --dry-run && pnpm run -C npm clean && echo && echo ✅ Checks passed, start bumping... && echo && deno run -A jsr:@mys/bump@1"
"check:lint": "deno check src/**/*.ts test/**/*.ts && deno lint && deno fmt --check",
"check:all": "deno task check:lint && deno task test && deno publish --allow-dirty --dry-run && cd npm && pnpm i && pnpm run build && pnpm publish --no-git-checks --dry-run && pnpm run clean && echo && echo ✅ All checks passed && echo",
"bump": "deno task check:all && deno run -A jsr:@mys/bump@1"
},
"imports": {
"@mys/m-rpc": "jsr:@mys/m-rpc@^0.12.1",
"@okikio/transferables": "jsr:@okikio/transferables@^1.0.2",
"@std/assert": "jsr:@std/[email protected]"
},
Expand Down
8 changes: 8 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
"types": "./dist/main.d.ts"
}
},
"dependencies": {
"@mys/m-rpc": "npm:@mys-x/m-rpc@^0.12.1"
},
"devDependencies": {
"@okikio/transferables": "npm:@jsr/okikio__transferables@^1.0.2",
"tsup": "^8.0.2",
Expand Down
9 changes: 9 additions & 0 deletions npm/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions npm/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"allowImportingTsExtensions": true
}
}
53 changes: 10 additions & 43 deletions src/define.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import type { AnyFn, DefineWorkerFnOpts, InternalFns } from "./types.ts";
import type { MsgPort, MsgPortNormalized } from "./rpc/types.ts";
import { RpcAgent } from "./rpc/rpc.ts";

/* -------------------------------------------------- common -------------------------------------------------- */

const _global = Function("return this")() as MsgPortNormalized;
import { MRpc } from "@mys/m-rpc";
import type { AnyFn, DefineOptions, WorkerGlobalScope } from "./types.ts";

/* -------------------------------------------------- defineWorkerFn() -------------------------------------------------- */

Expand All @@ -18,15 +13,14 @@ const _global = Function("return this")() as MsgPortNormalized;
export function defineWorkerFn<FN extends AnyFn>(
name: string,
fn: FN,
options: DefineWorkerFnOpts<FN> = {},
options: DefineOptions<FN> = {},
): void {
const { transfer, port = _global } = options;
const rpcAgent = RpcAgent.getRpcAgent(port);
ensureInternalFns(port);
rpcAgent.defineLocalFn(name, fn, {
namespace: "fn",
transfer,
});
const {
port = globalThis as unknown as WorkerGlobalScope,
...restOptions
} = options;
const rpc = MRpc.ensureMRpc(port);
rpc.defineLocalFn(name, fn, restOptions);
}

/* -------------------------------------------------- defineWorkerFns() -------------------------------------------------- */
Expand All @@ -40,37 +34,10 @@ export function defineWorkerFn<FN extends AnyFn>(
export function defineWorkerFns<FNS extends Record<string, AnyFn>>(
functions: FNS,
options: {
[NAME in keyof FNS]?: DefineWorkerFnOpts<FNS[NAME]>;
[NAME in keyof FNS]?: DefineOptions<FNS[NAME]>;
} = {},
): void {
for (const [name, fn] of Object.entries(functions)) {
defineWorkerFn(name, fn, options[name]);
}
}

/* -------------------------------------------------- internal functions -------------------------------------------------- */

const INTERNAL_FNS_DEFINED = Symbol("internalFnsDefined");

/**
* Ensure that internal functions are defined.
*/
function ensureInternalFns(port: MsgPort) {
// prevent double usage
if ((port as any)[INTERNAL_FNS_DEFINED]) {
return;
}
(port as any)[INTERNAL_FNS_DEFINED] = true;
// get RpcAgent instance
const rpcAgent = RpcAgent.getRpcAgent(port);
// define internal functions
const internalFns: InternalFns = {
/**
* @returns Names of defined worker functions
*/
names: () => rpcAgent.getLocalFnNames("fn"),
};
for (const [name, fn] of Object.entries(internalFns)) {
rpcAgent.defineLocalFn(name, fn, { namespace: "fn-internal" });
}
}
Loading

0 comments on commit 4792a41

Please sign in to comment.