Skip to content

Commit

Permalink
Apply review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
MatiPl01 committed Aug 12, 2024
1 parent bc75fcb commit dbbc18b
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,10 @@ ShareableObject::ShareableObject(
jsi::Value ShareableObject::toJSValue(jsi::Runtime &rt) {
auto obj = jsi::Object(rt);
for (size_t i = 0, size = data_.size(); i < size; i++) {
obj.setProperty(rt, jsi::String::createFromUtf8(rt, data_[i].first), data_[i].second->toJSValue(rt));
obj.setProperty(
rt,
jsi::String::createFromUtf8(rt, data_[i].first),
data_[i].second->toJSValue(rt));
}
#if SUPPORTS_NATIVE_STATE
if (nativeState_ != nullptr) {
Expand Down
26 changes: 17 additions & 9 deletions packages/react-native-reanimated/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
'use strict';
import type { WorkletStackDetails } from './commonTypes';

export class ReanimatedError extends Error {
constructor(message?: string) {
super(`[Reanimated] ${message}`);
export interface ReanimatedError extends Error {
// eslint-disable-next-line @typescript-eslint/no-misused-new
new (message?: string): ReanimatedError;
}

// Set the name of the error to the class name
this.name = this.constructor.name;
export function ReanimatedError(message: string): ReanimatedError {
const errorInstance = new Error(`[Reanimated] ${message}`);
Object.setPrototypeOf(errorInstance, ReanimatedError.prototype);
errorInstance.name = 'ReanimatedError';

if (Error.captureStackTrace) {
// Exclude constructor from stack trace
Error.captureStackTrace(this, this.constructor);
}
// Exclude constructor from stack trace if captureStackTrace is available
if (Error.captureStackTrace) {
Error.captureStackTrace(errorInstance, ReanimatedError);
}

return errorInstance as ReanimatedError;
}

// Ensure the prototype chain is correct
ReanimatedError.prototype = Object.create(Error.prototype);
ReanimatedError.prototype.constructor = ReanimatedError;

const _workletStackDetails = new Map<number, WorkletStackDetails>();

export function registerWorkletStackDetails(
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-reanimated/src/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,5 @@ declare global {
shadowNodeWrapper: ShadowNodeWrapper,
propName: string
) => string;
var ReanimatedError: ReanimatedError;
}
6 changes: 6 additions & 0 deletions packages/react-native-reanimated/src/logger/LogBox.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
'use strict';
/**
* Copied from:
* react-native/Libraries/LogBox/Data/LogBoxData.js
* react-native/Libraries/LogBox/Data/parseLogBoxLog.js
*/

import type { LogBoxStatic } from 'react-native';
import { LogBox as RNLogBox } from 'react-native';

Expand Down
21 changes: 15 additions & 6 deletions packages/react-native-reanimated/src/logger/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
'use strict';
import { shareableMappingCache } from '../shareableMappingCache';
import { makeShareableCloneRecursive } from '../shareables';
import { addLogBoxLog } from './LogBox';
import type { LogLevel, LogData } from './LogBox';

Expand All @@ -16,11 +18,6 @@ function logToConsole(data: LogData) {
}
}

export function logToLogBoxAndConsole(data: LogData) {
addLogBoxLog(data);
logToConsole(data);
}

function formatMessage(message: string) {
'worklet';
return `[Reanimated] ${message}`;
Expand All @@ -43,10 +40,22 @@ function createLog(level: LogLevel, message: string): LogData {
};
}

export const loggerImpl = {
const loggerImpl = {
logFunction: logToConsole,
};

export function logToLogBoxAndConsole(data: LogData) {
addLogBoxLog(data);
logToConsole(data);
}

export function replaceLoggerImplementation(
logFunction: (data: LogData) => void
) {
loggerImpl.logFunction = logFunction;
shareableMappingCache.set(logger, makeShareableCloneRecursive(logger));
}

export const logger = {
warn(message: string) {
'worklet';
Expand Down
6 changes: 0 additions & 6 deletions packages/react-native-reanimated/src/publicGlobals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
import type { ReanimatedError as ReanimatedErrorClass } from './errors';

/* eslint-disable no-var */
export {};
Expand All @@ -23,9 +22,4 @@ declare global {
* which is the Reanimated UI runtime.
*/
var _WORKLET_RUNTIME: ArrayBuffer;

/**
* This is a custom error class that is thrown by Reanimated.
*/
var ReanimatedError: typeof ReanimatedErrorClass;
}
13 changes: 4 additions & 9 deletions packages/react-native-reanimated/src/threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import {
makeShareableCloneRecursive,
} from './shareables';
import { isWorkletFunction } from './commonTypes';
import type { LogData } from './logger';
import { logger, loggerImpl, logToLogBoxAndConsole } from './logger';
import { shareableMappingCache } from './shareableMappingCache';
import { logToLogBoxAndConsole, replaceLoggerImplementation } from './logger';

const IS_JEST = isJest();
const SHOULD_BE_USE_WEB = shouldBeUseWeb();
Expand Down Expand Up @@ -260,9 +258,6 @@ export function runOnJS<Args extends unknown[], ReturnValue>(
}

// Override the logFunction implementation with the one that adds logs
// with better stack traces to the LogBox
loggerImpl.logFunction = (data: LogData) => {
'worklet';
runOnJS(logToLogBoxAndConsole)(data);
};
shareableMappingCache.set(logger, makeShareableCloneRecursive(logger));
// with better stack traces to the LogBox (need to override it after runOnJS
// is defined).
replaceLoggerImplementation(runOnJS(logToLogBoxAndConsole));
2 changes: 1 addition & 1 deletion packages/react-native-reanimated/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"react-native-reanimated": ["./src"]
}
},
"include": ["src"]
"include": ["src", "metro-config"]
}

0 comments on commit dbbc18b

Please sign in to comment.