Skip to content

Commit

Permalink
fix: immutable global variables
Browse files Browse the repository at this point in the history
  • Loading branch information
hmerritt committed Jan 28, 2024
1 parent 67e3012 commit 6e712cb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/lib/global/devTools.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { $global } from "./utils";
import { setGlobalValue } from "./utils";

// @TODO maybe remove these and add better dev tools.

Expand Down Expand Up @@ -41,6 +41,6 @@ export const getObjectOfEventListeners = () => {
};

export const injectDevTools = () => {
$global.getNumberOfEventListeners = getNumberOfEventListeners;
$global.getObjectOfEventListeners = getObjectOfEventListeners;
setGlobalValue("getNumberOfEventListeners", getNumberOfEventListeners);
setGlobalValue("getObjectOfEventListeners", getObjectOfEventListeners);
};
4 changes: 2 additions & 2 deletions src/lib/global/env.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { $global } from "./utils";
import { setGlobalValue } from "./utils";

/**
* Environment variables.
Expand Down Expand Up @@ -29,5 +29,5 @@ export type EnvObj = typeof env;
export type EnvKeys = keyof EnvObj;

export const injectEnv = () => {
$global.env = env;
setGlobalValue("env", env);
};
4 changes: 2 additions & 2 deletions src/lib/global/featureFlags.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type EnvKeys, env } from "./env";
import { $global } from "./utils";
import { setGlobalValue } from "./utils";

/**
* Returns `true` if the feature is enabled in `env` object.
Expand Down Expand Up @@ -32,7 +32,7 @@ export type FeatureOptions = {
export type FeatureFlags = EnvKeys;

export const injectFeature = () => {
$global.feature = feature;
setGlobalValue("feature", feature);
};

const isFalse = (value: unknown): value is false => {
Expand Down
12 changes: 6 additions & 6 deletions src/lib/global/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import dayjs from "dayjs";

import { padChar } from "lib/strings";

import { $global } from "./utils";
import { $global, setGlobalValue } from "./utils";

// @TODO:
// export interface Logger {
Expand Down Expand Up @@ -141,9 +141,9 @@ export const debugn = (namespace: string, logLevel: any, ...args: any[]) => {
};

export const injectLog = () => {
$global.logStore = new LogStore();
$global.log = log;
$global.logn = logn;
$global.debug = debug;
$global.debugn = debugn;
setGlobalValue("logStore", new LogStore());
setGlobalValue("log", log);
setGlobalValue("logn", logn);
setGlobalValue("debug", debug);
setGlobalValue("debugn", debugn);
};
11 changes: 11 additions & 0 deletions src/lib/global/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,15 @@ export const getGlobal = () => {
}
};

/**
* Set immutable global variable.
*/
export const setGlobalValue = (key: string, value: any) => {
Object.defineProperty(getGlobal(), key, {
value: value,
configurable: false,
writable: false
});
};

export const $global = getGlobal();

0 comments on commit 6e712cb

Please sign in to comment.