Skip to content

Commit

Permalink
refactor: use globalThis instead of global for public globals
Browse files Browse the repository at this point in the history
global does not work in web environments, whereas globalThis does

for things that need to work on web at minimum we need globalThis then

all uses of global. should move to globalThis. at some point but that's
a larger question
  • Loading branch information
mikehardy committed Feb 10, 2025
1 parent 454625f commit 4c38caa
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 23 deletions.
4 changes: 2 additions & 2 deletions docs/analytics/usage/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ When running on Android in debug, events won't be logged by default. If you want

## Other / Web

To mark your events as "Debug" events for platforms using react-native-firebase "other" platform support, you need to set the global debug flag `global.RNFBDebug` to `true` then reload the app.
To mark your events as "Debug" events for platforms using react-native-firebase "other" platform support, you need to set the global debug flag `globalThis.RNFBDebug` to `true` then reload the app.

This toggle must be set to the value you want before accessing the analytics instance for the first time, so you should do it as early in your app's bootstrap sequence as possible.

Expand All @@ -271,7 +271,7 @@ import { name as appName } from './app.json';

// \/ Add these lines below
// Enable debug mode for react-native-firebase:
if (__DEV__) global.RNFBDebug = true;
if (__DEV__) globalThis.RNFBDebug = true;
// /\ Add these lines above

AppRegistry.registerComponent(appName, () => App);
Expand Down
2 changes: 1 addition & 1 deletion packages/analytics/lib/web/RNFBAnalyticsModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function getAnalyticsApi() {
}
if (!analyticsInstances[measurementId]) {
analyticsInstances[measurementId] = new AnalyticsApi('[DEFAULT]', measurementId);
if (global.RNFBDebug) {
if (globalThis.RNFBDebug) {
analyticsInstances[measurementId].setDebug(true);
}
}
Expand Down
10 changes: 5 additions & 5 deletions packages/analytics/lib/web/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class AnalyticsApi {
this.currentScreen = null;

this._getInstallationId().catch(error => {
if (global.RNFBDebug) {
if (globalThis.RNFBDebug) {
console.debug('[RNFB->Analytics][🔴] Error getting Firebase Installation Id:', error);
} else {
// No-op. This is a non-critical error.
Expand Down Expand Up @@ -128,7 +128,7 @@ class AnalyticsApi {
const app = getApp(this.appName);
const installations = getInstallations(app);
const id = await getId(installations);
if (global.RNFBDebug) {
if (globalThis.RNFBDebug) {
console.debug('[RNFB->Analytics][📊] Firebase Installation Id:', id);
}
this.installationId = id;
Expand Down Expand Up @@ -299,7 +299,7 @@ For example, to use React Native Async Storage:

try {
const url = `https://www.google-analytics.com/g/collect?${queryParams.toString()}`;
if (global.RNFBDebug) {
if (globalThis.RNFBDebug) {
console.debug(`[RNFB-->Fetch][📊] Sending analytics call: ${url}`);
}
const response = await fetch(url, {
Expand All @@ -319,11 +319,11 @@ For example, to use React Native Async Storage:
'user-agent': 'react-native-firebase',
},
});
if (global.RNFBDebug) {
if (globalThis.RNFBDebug) {
console.debug(`[RNFB<--Fetch][📊] Response: ${response.status}`);
}
} catch (error) {
if (global.RNFBDebug) {
if (globalThis.RNFBDebug) {
console.debug('[RNFB<--Fetch][🔴] Error sending Analytics event:', error);
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/app/lib/internal/RNFBNativeEventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,24 @@ class RNFBNativeEventEmitter extends NativeEventEmitter {
this.ready = true;
}
RNFBAppModule.eventsAddListener(eventType);
if (global.RNFBDebug) {
if (globalThis.RNFBDebug) {
// eslint-disable-next-line no-console
console.debug(`[RNFB-->Event][👂] ${eventType} -> listening`);
}
const listenerDebugger = (...args) => {
if (global.RNFBDebug) {
if (globalThis.RNFBDebug) {
// eslint-disable-next-line no-console
console.debug(`[RNFB<--Event][📣] ${eventType} <-`, JSON.stringify(args[0]));
// Possible leaking test if events are still being received after the test.
// This is not super accurate but it's better than nothing, e.g. if doing setup/teardown
// logic outside of a test this may cause false positives.
if (global.RNFBTest && !global.RNFBDebugInTestLeakDetection) {
if (globalThis.RNFBTest && !globalThis.RNFBDebugInTestLeakDetection) {
// eslint-disable-next-line no-console
console.debug(
`[TEST--->Leak][💡] Possible leaking test detected! An event (☝️) ` +
`was received outside of any running tests which may indicates that some ` +
`listeners/event subscriptions that have not been unsubscribed from in your ` +
`test code. The last test that ran was: "${global.RNFBDebugLastTest}".`,
`test code. The last test that ran was: "${globalThis.RNFBDebugLastTest}".`,
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/app/lib/internal/nativeModuleAndroidIos.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { NativeModules } from 'react-native';
*/
export function getReactNativeModule(moduleName) {
const nativeModule = NativeModules[moduleName];
if (!global.RNFBDebug) {
if (!globalThis.RNFBDebug) {
return nativeModule;
}
return new Proxy(nativeModule, {
Expand Down
3 changes: 2 additions & 1 deletion packages/app/lib/internal/nativeModuleWeb.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ export function getReactNativeModule(moduleName) {
if (!nativeModule) {
throw new Error(`Native module ${moduleName} is not registered.`);
}
if (!global.RNFBDebug) {
if (!globalThis.RNFBDebug) {
return nativeModule;
}
return new Proxy(nativeModule, {
ownKeys(target) {
// FIXME - test in new arch context - I don't think Object.keys works
return Object.keys(target);
},
get: (_, name) => {
Expand Down
6 changes: 3 additions & 3 deletions packages/storage/e2e/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const PATH = `${PATH_ROOT}/${ID}`;
const WRITE_ONLY_NAME = 'writeOnly.jpeg';

exports.seed = async function seed(path) {
let leakDetectCurrent = global.RNFBDebugInTestLeakDetection;
global.RNFBDebugInTestLeakDetection = false;
let leakDetectCurrent = globalThis.RNFBDebugInTestLeakDetection;
globalThis.RNFBDebugInTestLeakDetection = false;

try {
// Add a write only file
Expand All @@ -27,7 +27,7 @@ exports.seed = async function seed(path) {
console.error('unable to seed storage service with test fixtures');
throw e;
} finally {
global.RNFBDebugInTestLeakDetection = leakDetectCurrent;
globalThis.RNFBDebugInTestLeakDetection = leakDetectCurrent;
}
};

Expand Down
8 changes: 4 additions & 4 deletions tests/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ ErrorUtils.setGlobalHandler((err, isFatal) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function loadTests(_) {
describe('React Native Firebase', function () {
if (!global.RNFBDebug) {
if (!globalThis.RNFBDebug) {
// Only retry tests if not debugging locally,
// otherwise it gets annoying to debug.
this.retries(4);
Expand Down Expand Up @@ -96,8 +96,8 @@ function loadTests(_) {
});

afterEach(async function afterEachTest() {
global.RNFBDebugLastTest = this.currentTest.title;
global.RNFBDebugInTestLeakDetection = false;
globalThis.RNFBDebugLastTest = this.currentTest.title;
globalThis.RNFBDebugInTestLeakDetection = false;
if (RNFBDebug) {
const emoji = this.currentTest.state === 'passed' ? '✅' : '❌';
console.debug(`[TEST->Finish][${emoji}] ${this.currentTest.title}`);
Expand Down Expand Up @@ -129,7 +129,7 @@ function loadTests(_) {
// Allow time for things to settle between tests.
await Utils.sleep(50);
}
global.RNFBDebugInTestLeakDetection = true;
globalThis.RNFBDebugInTestLeakDetection = true;
});

// Load tests for each Firebase module.
Expand Down
4 changes: 2 additions & 2 deletions tests/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import shouldMatchers from 'should';
// [RNFB<--Event][📣] storage_event <- {...}
// [RNFB<-Native][🟢] RNFBStorageModule.putString <- {...}
// [TEST->Finish][✅] uploads a base64url string
global.RNFBDebug = false;
globalThis.RNFBDebug = false;

// RNFB packages.
import '@react-native-firebase/analytics';
Expand Down Expand Up @@ -465,4 +465,4 @@ global.jet = {
// TODO toggle this correct in CI only.
global.isCI = true;
// Used to tell our internals that we are running tests.
global.RNFBTest = true;
globalThis.RNFBTest = true;

0 comments on commit 4c38caa

Please sign in to comment.