Skip to content

Commit 9a5abd3

Browse files
committed
feat: replace globalThis in misc used to retrieve litCore config with a function and module scoped variable
1 parent cf4ea12 commit 9a5abd3

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

packages/core/src/lib/lit-core.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import {
5050
logWithRequestId,
5151
mostCommonString,
5252
sendRequest,
53+
setMiscLitConfig,
5354
} from '@lit-protocol/misc';
5455
import {
5556
AuthSig,
@@ -197,8 +198,8 @@ export class LitCore {
197198
// -- set bootstrapUrls to match the network litNetwork unless it's set to custom
198199
this.setCustomBootstrapUrls();
199200

200-
// -- set global variables
201-
globalThis.litConfig = this.config;
201+
// -- set misc global variables
202+
setMiscLitConfig(this.config);
202203
bootstrapLogManager('core');
203204

204205
// -- configure local storage if not present
@@ -387,7 +388,7 @@ export class LitCore {
387388

388389
this._stopListeningForNewEpoch();
389390
// this._stopNetworkPolling();
390-
if (globalThis.litConfig) delete globalThis.litConfig;
391+
setMiscLitConfig(undefined);
391392
}
392393

393394
// _stopNetworkPolling() {
@@ -532,9 +533,6 @@ export class LitCore {
532533
// this._scheduleNetworkSync();
533534
this._listenForNewEpoch();
534535

535-
// FIXME: don't create global singleton; multiple instances of `core` should not all write to global
536-
// @ts-expect-error typeof globalThis is not defined. We're going to get rid of the global soon.
537-
globalThis.litNodeClient = this;
538536
this.ready = true;
539537

540538
log(`🔥 lit is ready. "litNodeClient" variable is ready to use globally.`);

packages/misc/src/lib/misc.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { LitNodeClientConfig } from '@lit-protocol/types';
12
import { Contract } from '@ethersproject/contracts';
23
import { JsonRpcProvider } from '@ethersproject/providers';
34
import Ajv, { JSONSchemaType } from 'ajv';
@@ -31,6 +32,13 @@ import {
3132
const logBuffer: any[][] = [];
3233
const ajv = new Ajv();
3334

35+
// Module scoped variable to store the LitNodeClientConfig passed to LitCore
36+
let litConfig: LitNodeClientConfig | undefined;
37+
38+
export const setMiscLitConfig = (config: LitNodeClientConfig | undefined) => {
39+
litConfig = config;
40+
};
41+
3442
/**
3543
*
3644
* Print error message based on Error interface
@@ -95,7 +103,6 @@ export const findMostCommonResponse = (responses: object[]): object => {
95103
};
96104

97105
declare global {
98-
var litConfig: any;
99106
var wasmExport: any;
100107
var wasmECDSA: any;
101108
var logger: any;
@@ -148,13 +155,13 @@ export const log = (...args: any): void => {
148155
}
149156

150157
// check if config is loaded yet
151-
if (!globalThis?.litConfig) {
158+
if (!litConfig) {
152159
// config isn't loaded yet, push into buffer
153160
logBuffer.push(args);
154161
return;
155162
}
156163

157-
if (globalThis?.litConfig?.debug !== true) {
164+
if (litConfig?.debug !== true) {
158165
return;
159166
}
160167
// config is loaded, and debug is true
@@ -176,13 +183,13 @@ export const logWithRequestId = (id: string, ...args: any) => {
176183
}
177184

178185
// check if config is loaded yet
179-
if (!globalThis?.litConfig) {
186+
if (!litConfig) {
180187
// config isn't loaded yet, push into buffer
181188
logBuffer.push(args);
182189
return;
183190
}
184191

185-
if (globalThis?.litConfig?.debug !== true) {
192+
if (litConfig?.debug !== true) {
186193
return;
187194
}
188195
// config is loaded, and debug is true
@@ -206,13 +213,13 @@ export const logErrorWithRequestId = (id: string, ...args: any) => {
206213
}
207214

208215
// check if config is loaded yet
209-
if (!globalThis?.litConfig) {
216+
if (!litConfig) {
210217
// config isn't loaded yet, push into buffer
211218
logBuffer.push(args);
212219
return;
213220
}
214221

215-
if (globalThis?.litConfig?.debug !== true) {
222+
if (litConfig?.debug !== true) {
216223
return;
217224
}
218225
// config is loaded, and debug is true
@@ -236,13 +243,13 @@ export const logError = (...args: any) => {
236243
}
237244

238245
// check if config is loaded yet
239-
if (!globalThis?.litConfig) {
246+
if (!litConfig) {
240247
// config isn't loaded yet, push into buffer
241248
logBuffer.push(args);
242249
return;
243250
}
244251

245-
if (globalThis?.litConfig?.debug !== true) {
252+
if (litConfig?.debug !== true) {
246253
return;
247254
}
248255
// config is loaded, and debug is true

0 commit comments

Comments
 (0)