@isograph/react 0.0.0-main-7e245bae → 0.0.0-main-04cbf235

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
- export declare function areEqualWithDeepComparison(oldItem: unknown, newItem: unknown): boolean;
2
- export declare function areEqualArraysWithDeepComparison(oldItems: ReadonlyArray<unknown>, newItems: ReadonlyArray<unknown>): boolean;
3
- export declare function areEqualObjectsWithDeepComparison(oldItemObject: object, newItemObject: object): boolean;
1
+ import type { ReaderAst, ReaderLinkedField, ReaderScalarField } from './reader';
2
+ export declare function areEqualWithDeepComparison(field: ReaderScalarField | ReaderLinkedField, oldItem: unknown, newItem: unknown): boolean;
3
+ export declare function areEqualArraysWithDeepComparison(field: ReaderScalarField | ReaderLinkedField, oldItems: ReadonlyArray<unknown>, newItems: ReadonlyArray<unknown>): boolean;
4
+ export declare function areEqualObjectsWithDeepComparison(ast: ReaderAst<object>, oldItemObject: object, newItemObject: object): boolean;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.areEqualObjectsWithDeepComparison = exports.areEqualArraysWithDeepComparison = exports.areEqualWithDeepComparison = void 0;
4
- function areEqualWithDeepComparison(oldItem, newItem) {
4
+ function areEqualWithDeepComparison(field, oldItem, newItem) {
5
5
  if (newItem === null) {
6
6
  return oldItem === null;
7
7
  }
@@ -12,48 +12,72 @@ function areEqualWithDeepComparison(oldItem, newItem) {
12
12
  if (!Array.isArray(oldItem)) {
13
13
  return false;
14
14
  }
15
- return areEqualArraysWithDeepComparison(oldItem, newItem);
15
+ return areEqualArraysWithDeepComparison(field, oldItem, newItem);
16
16
  }
17
- if (typeof newItem === 'object') {
18
- if (typeof oldItem !== 'object') {
19
- return false;
20
- }
21
- if (oldItem === null) {
22
- return false;
17
+ switch (field.kind) {
18
+ case 'Scalar':
19
+ return newItem === oldItem;
20
+ case 'Linked':
21
+ if (oldItem == null) {
22
+ return false;
23
+ }
24
+ return areEqualObjectsWithDeepComparison(field.selections, oldItem, newItem);
25
+ default: {
26
+ // Ensure we have covered all variants
27
+ let _ = field;
28
+ _;
29
+ throw new Error('Unexpected case.');
23
30
  }
24
- return areEqualObjectsWithDeepComparison(oldItem, newItem);
25
31
  }
26
- return newItem === oldItem;
27
32
  }
28
33
  exports.areEqualWithDeepComparison = areEqualWithDeepComparison;
29
- function areEqualArraysWithDeepComparison(oldItems, newItems) {
34
+ function areEqualArraysWithDeepComparison(field, oldItems, newItems) {
30
35
  if (newItems.length !== oldItems.length) {
31
36
  return false;
32
37
  }
33
38
  for (let i = 0; i < newItems.length; i++) {
34
- if (!areEqualWithDeepComparison(oldItems[i], newItems[i])) {
39
+ if (!areEqualWithDeepComparison(field, oldItems[i], newItems[i])) {
35
40
  return false;
36
41
  }
37
42
  }
38
43
  return true;
39
44
  }
40
45
  exports.areEqualArraysWithDeepComparison = areEqualArraysWithDeepComparison;
41
- function areEqualObjectsWithDeepComparison(oldItemObject, newItemObject) {
42
- const oldKeys = Object.keys(oldItemObject);
43
- const newKeys = Object.keys(newItemObject);
44
- if (oldKeys.length !== newKeys.length) {
45
- return false;
46
- }
47
- for (const oldKey of oldKeys) {
48
- if (!(oldKey in newItemObject)) {
49
- return false;
50
- }
51
- // @ts-expect-error
52
- const oldValue = oldItemObject[oldKey];
53
- // @ts-expect-error
54
- const newValue = newItemObject[oldKey];
55
- if (!areEqualWithDeepComparison(oldValue, newValue)) {
56
- return false;
46
+ function areEqualObjectsWithDeepComparison(ast, oldItemObject, newItemObject) {
47
+ var _a;
48
+ for (const field of ast) {
49
+ switch (field.kind) {
50
+ case 'Scalar':
51
+ case 'Linked':
52
+ const key = (_a = field.alias) !== null && _a !== void 0 ? _a : field.fieldName;
53
+ // @ts-expect-error
54
+ const oldValue = oldItemObject[key];
55
+ // @ts-expect-error
56
+ const newValue = newItemObject[key];
57
+ if (!areEqualWithDeepComparison(field, oldValue, newValue)) {
58
+ return false;
59
+ }
60
+ break;
61
+ case 'Resolver': {
62
+ const key = field.alias;
63
+ // @ts-expect-error
64
+ const oldValue = oldItemObject[key];
65
+ // @ts-expect-error
66
+ const newValue = newItemObject[key];
67
+ if (oldValue !== newValue) {
68
+ return false;
69
+ }
70
+ break;
71
+ }
72
+ case 'ImperativelyLoadedField':
73
+ case 'LoadablySelectedField':
74
+ break;
75
+ default: {
76
+ // Ensure we have covered all variants
77
+ let _ = field;
78
+ _;
79
+ throw new Error('Unexpected case.');
80
+ }
57
81
  }
58
82
  }
59
83
  return true;
@@ -158,7 +158,7 @@ function callSubscriptions(environment, recordsEncounteredWhenNormalizing) {
158
158
  suspendIfInFlight: false,
159
159
  throwOnNetworkError: false,
160
160
  });
161
- if (!(0, areEqualWithDeepComparison_1.areEqualObjectsWithDeepComparison)(subscription.encounteredDataAndRecords.item, newEncounteredDataAndRecords.item)) {
161
+ if (!(0, areEqualWithDeepComparison_1.areEqualObjectsWithDeepComparison)(subscription.readerAst, subscription.encounteredDataAndRecords.item, newEncounteredDataAndRecords.item)) {
162
162
  // @ts-expect-error
163
163
  if (typeof window !== 'undefined' && window.__LOG) {
164
164
  console.log('Deep equality - No', {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isograph/react",
3
- "version": "0.0.0-main-7e245bae",
3
+ "version": "0.0.0-main-04cbf235",
4
4
  "description": "Use Isograph with React",
5
5
  "homepage": "https://isograph.dev",
6
6
  "main": "dist/index.js",
@@ -17,9 +17,9 @@
17
17
  "tsc": "tsc"
18
18
  },
19
19
  "dependencies": {
20
- "@isograph/disposable-types": "0.0.0-main-7e245bae",
21
- "@isograph/react-disposable-state": "0.0.0-main-7e245bae",
22
- "@isograph/reference-counted-pointer": "0.0.0-main-7e245bae"
20
+ "@isograph/disposable-types": "0.0.0-main-04cbf235",
21
+ "@isograph/react-disposable-state": "0.0.0-main-04cbf235",
22
+ "@isograph/reference-counted-pointer": "0.0.0-main-04cbf235"
23
23
  },
24
24
  "peerDependencies": {
25
25
  "react": "18.2.0"
@@ -1,4 +1,6 @@
1
+ import type { ReaderAst, ReaderLinkedField, ReaderScalarField } from './reader';
1
2
  export function areEqualWithDeepComparison(
3
+ field: ReaderScalarField | ReaderLinkedField,
2
4
  oldItem: unknown,
3
5
  newItem: unknown,
4
6
  ): boolean {
@@ -15,25 +17,33 @@ export function areEqualWithDeepComparison(
15
17
  return false;
16
18
  }
17
19
 
18
- return areEqualArraysWithDeepComparison(oldItem, newItem);
20
+ return areEqualArraysWithDeepComparison(field, oldItem, newItem);
19
21
  }
20
22
 
21
- if (typeof newItem === 'object') {
22
- if (typeof oldItem !== 'object') {
23
- return false;
24
- }
23
+ switch (field.kind) {
24
+ case 'Scalar':
25
+ return newItem === oldItem;
26
+ case 'Linked':
27
+ if (oldItem == null) {
28
+ return false;
29
+ }
25
30
 
26
- if (oldItem === null) {
27
- return false;
31
+ return areEqualObjectsWithDeepComparison(
32
+ field.selections,
33
+ oldItem,
34
+ newItem,
35
+ );
36
+ default: {
37
+ // Ensure we have covered all variants
38
+ let _: never = field;
39
+ _;
40
+ throw new Error('Unexpected case.');
28
41
  }
29
-
30
- return areEqualObjectsWithDeepComparison(oldItem, newItem);
31
42
  }
32
-
33
- return newItem === oldItem;
34
43
  }
35
44
 
36
45
  export function areEqualArraysWithDeepComparison(
46
+ field: ReaderScalarField | ReaderLinkedField,
37
47
  oldItems: ReadonlyArray<unknown>,
38
48
  newItems: ReadonlyArray<unknown>,
39
49
  ): boolean {
@@ -42,7 +52,7 @@ export function areEqualArraysWithDeepComparison(
42
52
  }
43
53
 
44
54
  for (let i = 0; i < newItems.length; i++) {
45
- if (!areEqualWithDeepComparison(oldItems[i], newItems[i])) {
55
+ if (!areEqualWithDeepComparison(field, oldItems[i], newItems[i])) {
46
56
  return false;
47
57
  }
48
58
  }
@@ -51,28 +61,46 @@ export function areEqualArraysWithDeepComparison(
51
61
  }
52
62
 
53
63
  export function areEqualObjectsWithDeepComparison(
64
+ ast: ReaderAst<object>,
54
65
  oldItemObject: object,
55
66
  newItemObject: object,
56
67
  ): boolean {
57
- const oldKeys = Object.keys(oldItemObject);
58
- const newKeys = Object.keys(newItemObject);
68
+ for (const field of ast) {
69
+ switch (field.kind) {
70
+ case 'Scalar':
71
+ case 'Linked':
72
+ const key = field.alias ?? field.fieldName;
73
+ // @ts-expect-error
74
+ const oldValue = oldItemObject[key];
75
+ // @ts-expect-error
76
+ const newValue = newItemObject[key];
77
+ if (!areEqualWithDeepComparison(field, oldValue, newValue)) {
78
+ return false;
79
+ }
80
+ break;
81
+ case 'Resolver': {
82
+ const key = field.alias;
83
+ // @ts-expect-error
84
+ const oldValue = oldItemObject[key];
85
+ // @ts-expect-error
86
+ const newValue = newItemObject[key];
59
87
 
60
- if (oldKeys.length !== newKeys.length) {
61
- return false;
62
- }
63
-
64
- for (const oldKey of oldKeys) {
65
- if (!(oldKey in newItemObject)) {
66
- return false;
67
- }
68
- // @ts-expect-error
69
- const oldValue = oldItemObject[oldKey];
70
- // @ts-expect-error
71
- const newValue = newItemObject[oldKey];
72
-
73
- if (!areEqualWithDeepComparison(oldValue, newValue)) {
74
- return false;
88
+ if (oldValue !== newValue) {
89
+ return false;
90
+ }
91
+ break;
92
+ }
93
+ case 'ImperativelyLoadedField':
94
+ case 'LoadablySelectedField':
95
+ break;
96
+ default: {
97
+ // Ensure we have covered all variants
98
+ let _: never = field;
99
+ _;
100
+ throw new Error('Unexpected case.');
101
+ }
75
102
  }
76
103
  }
104
+
77
105
  return true;
78
106
  }
package/src/core/cache.ts CHANGED
@@ -264,6 +264,7 @@ function callSubscriptions(
264
264
 
265
265
  if (
266
266
  !areEqualObjectsWithDeepComparison(
267
+ subscription.readerAst,
267
268
  subscription.encounteredDataAndRecords.item,
268
269
  newEncounteredDataAndRecords.item,
269
270
  )