Skip to content

Commit

Permalink
Merge 07764c6 into 4462095
Browse files Browse the repository at this point in the history
  • Loading branch information
takameyer committed Aug 22, 2023
2 parents 4462095 + 07764c6 commit 6db5283
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/realm-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* None

### Enhancements
* None
* Add flag to keep realm open on unmount of RealmProvider. ([#6023](https://github.com/realm/realm-js/issues/6023))

### Fixed
* <How to hit and notice issue? what was the impact?> ([#????](https://github.com/realm/realm-js/issues/????), since v?.?.?)
Expand Down
9 changes: 6 additions & 3 deletions packages/realm-react/src/RealmProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type PartialRealmConfiguration = Omit<Partial<Realm.Configuration>, "sync"> & {

type ProviderProps = PartialRealmConfiguration & {
fallback?: React.ComponentType<unknown> | React.ReactElement | null | undefined;
closeOnUnmount?: boolean;
realmRef?: React.MutableRefObject<Realm | null>;
children: React.ReactNode;
};
Expand Down Expand Up @@ -65,7 +66,7 @@ export function createRealmProvider(
* For example, to override the `path` config value, use a prop named `path`,
* e.g. `path="newPath.realm"`
*/
return ({ children, fallback: Fallback, realmRef, ...restProps }) => {
return ({ children, fallback: Fallback, closeOnUnmount = true, realmRef, ...restProps }) => {
const [realm, setRealm] = useState<Realm | null>(() =>
realmConfig.sync === undefined && restProps.sync === undefined
? new Realm(mergeRealmConfiguration(realmConfig, restProps))
Expand Down Expand Up @@ -129,11 +130,13 @@ export function createRealmProvider(

return () => {
if (realm) {
realm.close();
if (closeOnUnmount) {
realm.close();
}
setRealm(null);
}
};
}, [configVersion, realm, setRealm]);
}, [configVersion, realm, setRealm, closeOnUnmount]);

if (!realm) {
if (typeof Fallback === "function") {
Expand Down
13 changes: 13 additions & 0 deletions packages/realm-react/src/__tests__/RealmProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ describe("RealmProvider", () => {
expect(realm.isClosed).toBe(true);
});

it("returns the configured realm with useRealm and stays open if flagged", async () => {
const wrapper = ({ children }: { children: React.ReactNode }) => (
<RealmProvider closeOnUnmount={false}>{children}</RealmProvider>
);
const { result, unmount } = renderHook(() => useRealm(), { wrapper });
await waitFor(() => expect(result.current).not.toBe(null));
const realm = result.current;
expect(realm).not.toBe(null);
expect(realm.schema[0].name).toBe("dog");
unmount();
expect(realm.isClosed).toBe(false);
});

it("will override the the configuration provided in createRealmContext", async () => {
const wrapper = ({ children }: { children: React.ReactNode }) => (
<RealmProvider schema={[catSchema]}>{children}</RealmProvider>
Expand Down

0 comments on commit 6db5283

Please sign in to comment.