Skip to content

Commit

Permalink
Make useObject compatible with undefined objects
Browse files Browse the repository at this point in the history
* If somehow an object returns undefined, re-renders would be skipped
* Added tests for proving that re-renders occur when the database is cleared
* Removed 11.0.0-rc from peer-dependency
  • Loading branch information
takameyer committed Sep 1, 2023
1 parent 1a1a0ed commit a40af02
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/realm-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@
"react": "18.2.0",
"react-native": "0.71.7",
"react-test-renderer": "18.2.0",
"realm": "*",
"realm": "^11.0.0-rc.0",
"rollup-plugin-dts": "^5.0.0",
"ts-jest": "^29.0.5"
},
"peerDependencies": {
"react": ">=17.0.2",
"realm": "^12.0.0-browser || ^12.0.0 || ^12.0.0-rc || ^11.0.0-rc || ^11.0.0"
"realm": "^12.0.0-browser || ^12.0.0 || ^12.0.0-rc || ^11.0.0"
},
"optionalDependencies": {
"@babel/runtime": ">=7",
Expand Down
18 changes: 18 additions & 0 deletions packages/realm-react/src/__tests__/useObjectRender.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,24 @@ describe("useObject: rendering objects with a Realm.List property", () => {
fireEvent.press(rerenderButton);
expect(objectChangeCounter).toHaveBeenCalledTimes(expectedCount);
});
it("will rerender when the realm database is cleared", async () => {
await setupTest();

const initialCount = 2;

expect(objectChangeCounter).toHaveBeenCalledTimes(initialCount);

await act(async () => {
testRealm.write(() => {
testRealm.deleteAll();
});
forceSynchronousNotifications(testRealm);
});

await new Promise((resolve) => setTimeout(resolve, 1000));

expect(objectChangeCounter).toHaveBeenCalledTimes(initialCount + 1);
});
});

describe("rendering objects with a Realm.List property", () => {
Expand Down
19 changes: 19 additions & 0 deletions packages/realm-react/src/__tests__/useQueryRender.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -443,4 +443,23 @@ describe.each`
fireEvent.press(rerenderButton);
expect(queryObjectChangeCounter).toHaveBeenCalledTimes(1);
});

it("will rerender when the realm database is cleared", async () => {
await setupTest({ queryType });

const initialCount = 1;

expect(queryObjectChangeCounter).toHaveBeenCalledTimes(initialCount);

await act(async () => {
testRealm.write(() => {
testRealm.deleteAll();
});
forceSynchronousNotifications(testRealm);
});

await new Promise((resolve) => setTimeout(resolve, 1000));

expect(queryObjectChangeCounter).toHaveBeenCalledTimes(initialCount + 1);
});
});
6 changes: 3 additions & 3 deletions packages/realm-react/src/useObject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function createUseObject(useRealm: () => Realm) {
// Ref: https://github.com/facebook/react/issues/14490
const cachedObjectRef = useRef<null | CachedObject>(null);

if (cachedObjectRef.current === null) {
if (!cachedObjectRef.current) {
cachedObjectRef.current = createCachedObject({
object: originalObject ?? null,
realm,
Expand All @@ -81,7 +81,7 @@ export function createUseObject(useRealm: () => Realm) {
// Re-instantiate the cachedObject if the primaryKey has changed or the originalObject has gone from null to not null
if (
!arePrimaryKeysIdentical(primaryKey, primaryKeyRef.current) ||
(originalObjectRef.current === null && originalObject !== null)
(!originalObjectRef.current && originalObject)
) {
cachedObjectRef.current = createCachedObject({
object: originalObject ?? null,
Expand Down Expand Up @@ -138,7 +138,7 @@ export function createUseObject(useRealm: () => Realm) {
}, [realm, type, forceRerender]);

// If the object has been deleted or doesn't exist for the given primary key, just return null
if (object === null || object?.isValid() === false) {
if (!object || object?.isValid() === false) {
return null;
}

Expand Down

0 comments on commit a40af02

Please sign in to comment.