Skip to content

Commit

Permalink
Merge pull request #64 from samchon/features/global
Browse files Browse the repository at this point in the history
Avoid direct `global` accessment.
  • Loading branch information
samchon authored Apr 2, 2024
2 parents cb8d3c6 + 17df773 commit 869d270
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 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.0",
"version": "0.10.1",
"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().send!(message);
NodeModule.process.get().send!(message);
}

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

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

public static is_worker_server(): boolean {
return !!NodeModule.process().send;
return !!NodeModule.process.get().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();
const process = NodeModule.process.get();
class ThreadPort {
public static postMessage(message: any): void {
parentPort!.postMessage(message);
Expand Down
11 changes: 4 additions & 7 deletions src/utils/internal/NodeModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type * as __https from "https";
import import2 from "import2";
import type * as __os from "os";
import type * as __process from "process";
import { Singleton } from "tstl";
import { Singleton, is_node } from "tstl";
import type * as __thread from "worker_threads";
import type * as __ws from "ws";

Expand All @@ -31,10 +31,7 @@ export namespace NodeModule {
export const ws: Singleton<Promise<typeof __ws>> = new Singleton(
() => import("ws"),
);
export const process = () => __global.process;
export const process: Singleton<typeof __process> = new Singleton(() =>
is_node() ? global.process : undefined!,
);
}

/**
* @internal
*/
const __global = global;

0 comments on commit 869d270

Please sign in to comment.