From 2391306f360ec656c9677cdebe3c82e820bd4d71 Mon Sep 17 00:00:00 2001 From: William Candillon Date: Thu, 18 Apr 2024 14:44:49 +0200 Subject: [PATCH] fix regression in reanimated module wrapper (#2383) --- package/src/external/ModuleProxy.ts | 15 +++++++++------ .../src/external/reanimated/ReanimatedProxy.ts | 14 +++++++++----- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/package/src/external/ModuleProxy.ts b/package/src/external/ModuleProxy.ts index c6207b34b9..ddb4175067 100644 --- a/package/src/external/ModuleProxy.ts +++ b/package/src/external/ModuleProxy.ts @@ -6,7 +6,6 @@ type ImportType = ReturnType; * This is useful for lazily requiring optional dependencies. */ export const createModuleProxy = ( - name: string, getModule: () => ImportType ): TModule => { const holder: { module: TModule | undefined } = { module: undefined }; @@ -14,14 +13,18 @@ export const createModuleProxy = ( const proxy = new Proxy(holder, { get: (target, property) => { if (target.module == null) { - try { - target.module = getModule() as TModule; - } catch (e) { - throw new Error(`${name} is not installed!`); - } + // lazy initialize module via require() + // caller needs to make sure the require() call is wrapped in a try/catch + target.module = getModule() as TModule; } return target.module[property as keyof typeof holder.module]; }, }); return proxy as unknown as TModule; }; + +export class OptionalDependencyNotInstalledError extends Error { + constructor(name: string) { + super(`${name} is not installed!`); + } +} diff --git a/package/src/external/reanimated/ReanimatedProxy.ts b/package/src/external/reanimated/ReanimatedProxy.ts index 4800820f1c..308edef120 100644 --- a/package/src/external/reanimated/ReanimatedProxy.ts +++ b/package/src/external/reanimated/ReanimatedProxy.ts @@ -1,14 +1,18 @@ import type * as ReanimatedT from "react-native-reanimated"; -import { createModuleProxy } from "../ModuleProxy"; +import { + OptionalDependencyNotInstalledError, + createModuleProxy, +} from "../ModuleProxy"; type TReanimated = typeof ReanimatedT; -const Reanimated = createModuleProxy( - "react-native-reanimated", - () => { +const Reanimated = createModuleProxy(() => { + try { return require("react-native-reanimated"); + } catch (e) { + throw new OptionalDependencyNotInstalledError("react-native-reanimated"); } -); +}); // eslint-disable-next-line import/no-default-export export default Reanimated;