From 23dcb2753514a71e65aef5ddeb7e1f71830886db Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Wed, 1 May 2024 11:10:02 -0400 Subject: [PATCH] improve typing --- src/FSPage.ts | 1 - src/__tests__/consoleCapture.test.ts | 1 + src/index.ts | 4 ++- src/logging/consoleCapture.ts | 50 +++++++++++++++------------- 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/FSPage.ts b/src/FSPage.ts index 220b2c8..5c6954d 100644 --- a/src/FSPage.ts +++ b/src/FSPage.ts @@ -1,7 +1,6 @@ import { NativeModules } from 'react-native'; import { generateUUID } from './utils'; -// @ts-expect-error const isTurboModuleEnabled = global.__turboModuleProxy != null; type PropertiesWithoutPageName = { diff --git a/src/__tests__/consoleCapture.test.ts b/src/__tests__/consoleCapture.test.ts index 466ac90..f747575 100644 --- a/src/__tests__/consoleCapture.test.ts +++ b/src/__tests__/consoleCapture.test.ts @@ -2,6 +2,7 @@ import { describe, it, expect, afterEach } from '@jest/globals'; import { NativeModules } from 'react-native'; import consoleWatcher, { LogLevel } from '../logging/consoleCapture'; +// must run in 'silent' mode or console.trace will call into console.error describe('consoleCapture', () => { const oldConsoleLog = console.log; const oldConsoleTrace = console.trace; diff --git a/src/index.ts b/src/index.ts index d8de3f2..eb5f320 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,6 @@ import type { ViewProps } from 'react-native/Libraries/Components/View/ViewPropT import { ForwardedRef } from 'react'; import consoleWatcher, { LogLevel } from './logging/consoleCapture'; -// @ts-expect-error const isTurboModuleEnabled = global.__turboModuleProxy != null; interface NativeProps extends ViewProps { @@ -73,6 +72,9 @@ declare global { fsTagName?: string; } } + const global: typeof globalThis & { + __turboModuleProxy: unknown; + }; } const identifyWithProperties = (uid: string, userVars = {}) => identify(uid, userVars); diff --git a/src/logging/consoleCapture.ts b/src/logging/consoleCapture.ts index dee7386..40deb26 100644 --- a/src/logging/consoleCapture.ts +++ b/src/logging/consoleCapture.ts @@ -1,7 +1,6 @@ import { NativeModules } from 'react-native'; import { safeStringify } from './safeStringify'; -// @ts-expect-error const isTurboModuleEnabled = global.__turboModuleProxy != null; const FullStory = isTurboModuleEnabled @@ -30,7 +29,7 @@ type ConsoleLevels = keyof typeof consoleLevelMap; type MethodShim = Record<'native' | 'override', Function>; type ConsoleShims = { - [K in keyof typeof consoleLevelMap]?: MethodShim | null; + [key in ConsoleLevels]?: MethodShim; }; const logEvent = (level: LogLevel, args: ArrayLike) => { @@ -43,8 +42,6 @@ const logEvent = (level: LogLevel, args: ArrayLike) => { FullStory.log(level, payload.join(' ')); }; -// A naive version of ConsoleWatcher on web. -// See https://github.com/cowpaths/mn/blob/865353f374b687e481d3b230e7eec8e1d7be2eb4/projects/fullstory/packages/recording/src/consolewatcher.ts class ConsoleWatcher { private _isActive = false; private _shims: ConsoleShims = {}; @@ -59,44 +56,51 @@ class ConsoleWatcher { } disable() { - if (this._isActive) { - this._isActive = false; - for (const logLevel in this._shims as ConsoleShims) { - if (!this._shims[logLevel as ConsoleLevels]) { - return; - } + if (!this._isActive) { + return; + } - // If possible, restore the original logger function - const { override, native } = this._shims[logLevel as ConsoleLevels] as MethodShim; - // If our override has not been replaced by a 3rd party, revert back to native function - if (console[logLevel as ConsoleLevels] === override) { - console[logLevel as ConsoleLevels] = native as any; - this._shims[logLevel as ConsoleLevels] = undefined; - } + this._isActive = false; + let logLevel: ConsoleLevels; + for (logLevel in this._shims) { + if (!this._shims[logLevel]) { + return; + } + + // If possible, restore the original logger function + const { override, native } = this._shims[logLevel] as MethodShim; + // If our override has not been replaced by a 3rd party, revert back to native function + if (console[logLevel] === override) { + console[logLevel] = native as any; + this._shims[logLevel] = undefined; } } } private _makeShim() { - for (const rnLogLevel in consoleLevelMap) { + let rnLogLevel: ConsoleLevels; + for (rnLogLevel in consoleLevelMap) { + // copied by value for override func + const _rnLogLevel = rnLogLevel; + if (!(rnLogLevel in console)) { continue; } - const originalLogger = console[rnLogLevel as ConsoleLevels]; + const originalLogger = console[rnLogLevel]; const override = (...args: any[]) => { // call FS log with the mapped level if (this._isActive) { - logEvent(consoleLevelMap[rnLogLevel as ConsoleLevels], args); + logEvent(consoleLevelMap[_rnLogLevel], args); } // call original logger - originalLogger.apply(console, [...args]); + originalLogger.apply(console, args); }; - console[rnLogLevel as ConsoleLevels] = override; - this._shims[rnLogLevel as ConsoleLevels] = { override, native: originalLogger }; + console[rnLogLevel] = override; + this._shims[rnLogLevel] = { override, native: originalLogger }; } } }