Skip to content

Commit

Permalink
improve typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Wang committed May 1, 2024
1 parent 832cc41 commit 23dcb27
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
1 change: 0 additions & 1 deletion src/FSPage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { NativeModules } from 'react-native';
import { generateUUID } from './utils';

// @ts-expect-error
const isTurboModuleEnabled = global.__turboModuleProxy != null;

type PropertiesWithoutPageName = {
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/consoleCapture.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -73,6 +72,9 @@ declare global {
fsTagName?: string;
}
}
const global: typeof globalThis & {
__turboModuleProxy: unknown;
};
}

const identifyWithProperties = (uid: string, userVars = {}) => identify(uid, userVars);
Expand Down
50 changes: 27 additions & 23 deletions src/logging/consoleCapture.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { NativeModules } from 'react-native';
import { safeStringify } from './safeStringify';

// @ts-expect-error
const isTurboModuleEnabled = global.__turboModuleProxy != null;

const FullStory = isTurboModuleEnabled
Expand Down Expand Up @@ -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<unknown>) => {
Expand All @@ -43,8 +42,6 @@ const logEvent = (level: LogLevel, args: ArrayLike<unknown>) => {
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 = {};
Expand All @@ -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 };
}
}
}
Expand Down

0 comments on commit 23dcb27

Please sign in to comment.