Skip to content

Commit

Permalink
fix(preact): Check operations for key difference only (#3266)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten authored Jun 12, 2023
1 parent 3d6b0c8 commit 3526e2a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/yellow-shrimps-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/preact': patch
---

Apply shallow difference patch from React bindings to `@urql/preact` (See: #3195)
18 changes: 17 additions & 1 deletion packages/preact-urql/src/hooks/useSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,26 @@ type Updater<T> = (input: T) => void;

let currentInit = false;

// Two operations are considered equal if they have the same key
const areOperationsEqual = (
a: { key: number } | undefined,
b: { key: number } | undefined
) => {
return a === b || !!(a && b && a.key === b.key);
};

const isShallowDifferent = (a: any, b: any) => {
if (typeof a != 'object' || typeof b != 'object') return a !== b;
for (const x in a) if (!(x in b)) return true;
for (const x in b) if (a[x] !== b[x]) return true;
for (const key in b) {
if (
key === 'operation'
? !areOperationsEqual(a[key], b[key])
: a[key] !== b[key]
) {
return true;
}
}
return false;
};

Expand Down

0 comments on commit 3526e2a

Please sign in to comment.