Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid direct global accessment. #64

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;