Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TypeScript errors in @realm/react #4847

Merged
merged 1 commit into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/realm-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 7 additions & 7 deletions packages/realm-react/src/cachedObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions packages/realm-react/src/useObject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
Expand All @@ -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>;
};
}