Skip to content

Commit

Permalink
Merge pull request #65 from samchon/features/global
Browse files Browse the repository at this point in the history
Fix `NodeModule.process()` again for https://esm.sh bundling
  • Loading branch information
samchon committed Apr 2, 2024
2 parents 869d270 + 6463dc3 commit 5c5ce71
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tgrid",
"version": "0.10.1",
"version": "0.10.2",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"exports": {
Expand Down
8 changes: 4 additions & 4 deletions src/protocols/workers/internal/processes/ProcessChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import { NodeModule } from "../../../../utils/internal/NodeModule";
*/
export class ProcessChannel {
public static postMessage(message: any): void {
NodeModule.process.get().send!(message);
NodeModule.process().send!(message);
}

public static close(): void {
NodeModule.process.get().exit();
NodeModule.process().exit();
}

public static set onmessage(listener: (event: MessageEvent) => void) {
NodeModule.process.get().on("message", (msg) => {
NodeModule.process().on("message", (msg) => {
listener({ data: msg } as MessageEvent);
});
}

public static is_worker_server(): boolean {
return !!NodeModule.process.get().send;
return !!NodeModule.process().send;
}
}
2 changes: 1 addition & 1 deletion src/protocols/workers/internal/threads/ThreadPort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function ThreadPort() {
const { parentPort } = await NodeModule.thread.get();
if (!parentPort) throw new Error("This is not a worker thread.");

const process = NodeModule.process.get();
const process = NodeModule.process();
class ThreadPort {
public static postMessage(message: any): void {
parentPort!.postMessage(message);
Expand Down
9 changes: 6 additions & 3 deletions src/utils/internal/NodeModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export namespace NodeModule {
export const ws: Singleton<Promise<typeof __ws>> = new Singleton(
() => import("ws"),
);
export const process: Singleton<typeof __process> = new Singleton(() =>
is_node() ? global.process : undefined!,
);
export const process = () => {
if (__global === undefined) throw new Error("Not a node environment");
return __global.process;
};
}

const __global = is_node() ? global : undefined;

0 comments on commit 5c5ce71

Please sign in to comment.