Skip to content

Commit

Permalink
Fix TypeScript errors in @realm/react (#4847)
Browse files Browse the repository at this point in the history
Changes to class based models caused @realm/react to break.
This addresses these changes.
takameyer authored Aug 31, 2022
1 parent f75302f commit a3747c1
Showing 3 changed files with 11 additions and 10 deletions.
3 changes: 2 additions & 1 deletion packages/realm-react/package.json
Original file line number Diff line number Diff line change
@@ -38,7 +38,8 @@
"prettier": "^2.3.2",
"react": "^17.0.2",
"react-native": "^0.65.1",
"react-test-renderer": "^17.0.2"
"react-test-renderer": "^17.0.2",
"realm": "*"
},
"peerDependencies": {
"react": ">=16.8.0",
14 changes: 7 additions & 7 deletions packages/realm-react/src/cachedObject.ts
Original file line number Diff line number Diff line change
@@ -21,11 +21,11 @@ import { createCachedCollection } from "./cachedCollection";
/**
* Arguments object for `cachedObject`.
*/
type CachedObjectArgs<T> = {
type CachedObjectArgs = {
/**
* The {@link Realm.Object} to proxy
*/
object: T | null;
object: Realm.Object | null;
/**
* The {@link Realm} instance
*/
@@ -49,11 +49,11 @@ type CachedObjectArgs<T> = {
* @param args - {@link CachedObjectArgs} object arguments
* @returns Proxy object wrapping the {@link Realm.Object}
*/
export function createCachedObject<T extends Realm.Object>({
export function createCachedObject({
object,
realm,
updateCallback,
}: CachedObjectArgs<T>): { object: T | null; tearDown: () => void } {
}: CachedObjectArgs): { object: Realm.Object | null; tearDown: () => void } {
const listCaches = new Map();
const listTearDowns: Array<() => void> = [];
// If the object doesn't exist, just return it with an noop tearDown
@@ -64,7 +64,7 @@ export function createCachedObject<T extends Realm.Object>({
// This Proxy handler intercepts any accesses into properties of the cached object
// of type `Realm.List`, and returns a `cachedCollection` wrapping those properties
// to allow changes in the list to trigger re-renders
const cachedObjectHandler: ProxyHandler<T & Realm.Object> = {
const cachedObjectHandler: ProxyHandler<Realm.Object> = {
get: function (target, key) {
const value = Reflect.get(target, key);
// Pass methods through
@@ -92,14 +92,14 @@ export function createCachedObject<T extends Realm.Object>({
};

const cachedObjectResult = new Proxy(object, cachedObjectHandler);
const listenerCallback: Realm.ObjectChangeCallback<T> = (obj, changes) => {
const listenerCallback: Realm.ObjectChangeCallback<any> = (obj, changes) => {
if (changes.deleted) {
updateCallback();
} else if (changes.changedProperties.length > 0) {
// Don't force a second re-render if any of the changed properties is a Realm.List,
// as the List's cachedCollection will force a re-render itself
const anyListPropertyModified = changes.changedProperties.some((property) => {
return obj[property as keyof T] instanceof Realm.List;
return obj[property] instanceof Realm.List;
});
const shouldRerender = !anyListPropertyModified;

4 changes: 2 additions & 2 deletions packages/realm-react/src/useObject.tsx
Original file line number Diff line number Diff line change
@@ -61,7 +61,7 @@ export function createUseObject(useRealm: () => Realm) {
// When this is implemented, remove `?? null`
() =>
createCachedObject({
object: realm.objectForPrimaryKey(type, primaryKey) ?? null,
object: realm.objectForPrimaryKey<unknown>(type, primaryKey) ?? null,
realm,
updateCallback: forceRerender,
}),
@@ -79,6 +79,6 @@ export function createUseObject(useRealm: () => Realm) {
}

// Wrap object in a proxy to update the reference on rerender ( should only rerender when something has changed )
return new Proxy(object, {});
return new Proxy(object, {}) as T & Realm.Object<T>;
};
}

0 comments on commit a3747c1

Please sign in to comment.