Skip to content

Commit

Permalink
fix regression in reanimated module wrapper (#2383)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon authored Apr 18, 2024
1 parent 8d54359 commit 2391306
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
15 changes: 9 additions & 6 deletions package/src/external/ModuleProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@ type ImportType = ReturnType<typeof require>;
* This is useful for lazily requiring optional dependencies.
*/
export const createModuleProxy = <TModule>(
name: string,
getModule: () => ImportType
): TModule => {
const holder: { module: TModule | undefined } = { module: undefined };

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!`);
}
}
14 changes: 9 additions & 5 deletions package/src/external/reanimated/ReanimatedProxy.ts
Original file line number Diff line number Diff line change
@@ -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<TReanimated>(
"react-native-reanimated",
() => {
const Reanimated = createModuleProxy<TReanimated>(() => {
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;

0 comments on commit 2391306

Please sign in to comment.