@isograph/react 0.0.0-main-8b64a409 → 0.0.0-main-752db914

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.
@@ -5,6 +5,7 @@ import { WithEncounteredRecords } from './read';
5
5
  import { FragmentReference, Variables } from './FragmentReference';
6
6
  import { PromiseWrapper } from './PromiseWrapper';
7
7
  import { IsographEntrypoint } from './entrypoint';
8
+ import type { ReaderAst } from './reader';
8
9
  export type ComponentOrFieldName = string;
9
10
  export type StringifiedArgs = string;
10
11
  type ComponentCache = {
@@ -20,6 +21,7 @@ export type FragmentSubscription<TReadFromStore extends Object> = {
20
21
  /** The value read out from the previous call to readButDoNotEvaluate */
21
22
  readonly encounteredDataAndRecords: WithEncounteredRecords<TReadFromStore>;
22
23
  readonly fragmentReference: FragmentReference<TReadFromStore, any>;
24
+ readonly readerAst: ReaderAst<TReadFromStore>;
23
25
  };
24
26
  type AnyRecordSubscription = {
25
27
  readonly kind: 'AnyRecords';
@@ -1,7 +1,7 @@
1
1
  import { Factory, ParentCache } from '@isograph/react-disposable-state';
2
2
  import { DataId, type IsographEnvironment } from './IsographEnvironment';
3
3
  import { IsographEntrypoint, NormalizationAst, NormalizationLinkedField, NormalizationScalarField, RefetchQueryNormalizationArtifactWrapper } from '../core/entrypoint';
4
- import { ReaderLinkedField, ReaderScalarField } from './reader';
4
+ import { ReaderLinkedField, ReaderScalarField, type ReaderAst } from './reader';
5
5
  import { WithEncounteredRecords } from './read';
6
6
  import { FragmentReference, Variables } from './FragmentReference';
7
7
  export declare function getOrCreateItemInSuspenseCache<TReadFromStore extends Object, TClientFieldValue>(environment: IsographEnvironment, index: string, factory: Factory<FragmentReference<TReadFromStore, TClientFieldValue>>): ParentCache<FragmentReference<TReadFromStore, TClientFieldValue>>;
@@ -20,7 +20,7 @@ type NetworkResponseObject = {
20
20
  };
21
21
  export declare function normalizeData(environment: IsographEnvironment, normalizationAst: NormalizationAst, networkResponse: NetworkResponseObject, variables: Variables, nestedRefetchQueries: RefetchQueryNormalizationArtifactWrapper[]): Set<DataId>;
22
22
  export declare function subscribeToAnyChange(environment: IsographEnvironment, callback: () => void): () => void;
23
- export declare function subscribe<TReadFromStore extends Object>(environment: IsographEnvironment, encounteredDataAndRecords: WithEncounteredRecords<TReadFromStore>, fragmentReference: FragmentReference<TReadFromStore, any>, callback: (newEncounteredDataAndRecords: WithEncounteredRecords<TReadFromStore>) => void): () => void;
23
+ export declare function subscribe<TReadFromStore extends Object>(environment: IsographEnvironment, encounteredDataAndRecords: WithEncounteredRecords<TReadFromStore>, fragmentReference: FragmentReference<TReadFromStore, any>, callback: (newEncounteredDataAndRecords: WithEncounteredRecords<TReadFromStore>) => void, readerAst: ReaderAst<TReadFromStore>): () => void;
24
24
  export declare function onNextChange(environment: IsographEnvironment): Promise<void>;
25
25
  export declare function getParentRecordKey(astNode: NormalizationLinkedField | NormalizationScalarField | ReaderLinkedField | ReaderScalarField, variables: Variables): string;
26
26
  export declare const FIRST_SPLIT_KEY = "____";
@@ -97,12 +97,13 @@ function subscribeToAnyChange(environment, callback) {
97
97
  }
98
98
  exports.subscribeToAnyChange = subscribeToAnyChange;
99
99
  // TODO we should re-read and call callback if the value has changed
100
- function subscribe(environment, encounteredDataAndRecords, fragmentReference, callback) {
100
+ function subscribe(environment, encounteredDataAndRecords, fragmentReference, callback, readerAst) {
101
101
  const fragmentSubscription = {
102
102
  kind: 'FragmentSubscription',
103
103
  callback,
104
104
  encounteredDataAndRecords,
105
105
  fragmentReference,
106
+ readerAst,
106
107
  };
107
108
  // @ts-expect-error
108
109
  environment.subscriptions.add(fragmentSubscription);
@@ -19,7 +19,8 @@ function getOrCreateCachedComponent(environment, componentName, fragmentReferenc
19
19
  byArgs[stringifiedArgs] =
20
20
  (_c = byArgs[stringifiedArgs]) !== null && _c !== void 0 ? _c : (() => {
21
21
  function Component(additionalRuntimeProps) {
22
- const data = (0, useReadAndSubscribe_1.useReadAndSubscribe)(fragmentReference, networkRequestOptions);
22
+ const readerWithRefetchQueries = (0, PromiseWrapper_1.readPromise)(fragmentReference.readerWithRefetchQueries);
23
+ const data = (0, useReadAndSubscribe_1.useReadAndSubscribe)(fragmentReference, networkRequestOptions, readerWithRefetchQueries.readerArtifact.readerAst);
23
24
  // @ts-expect-error
24
25
  if (typeof window !== 'undefined' && window.__LOG) {
25
26
  console.log('Component re-rendered: ' +
@@ -27,7 +28,6 @@ function getOrCreateCachedComponent(environment, componentName, fragmentReferenc
27
28
  ' ' +
28
29
  fragmentReference.root);
29
30
  }
30
- const readerWithRefetchQueries = (0, PromiseWrapper_1.readPromise)(fragmentReference.readerWithRefetchQueries);
31
31
  return readerWithRefetchQueries.readerArtifact.resolver(data, additionalRuntimeProps);
32
32
  }
33
33
  Component.displayName = `${componentName} (id: ${fragmentReference.root}) @component`;
@@ -1,11 +1,13 @@
1
1
  import { FragmentReference } from '../core/FragmentReference';
2
2
  import { NetworkRequestReaderOptions, WithEncounteredRecords } from '../core/read';
3
+ import type { ReaderAst } from '../core/reader';
3
4
  /**
4
5
  * Read the data from a fragment reference and subscribe to updates.
5
6
  */
6
- export declare function useReadAndSubscribe<TReadFromStore extends Object>(fragmentReference: FragmentReference<TReadFromStore, any>, networkRequestOptions: NetworkRequestReaderOptions): TReadFromStore;
7
+ export declare function useReadAndSubscribe<TReadFromStore extends Object>(fragmentReference: FragmentReference<TReadFromStore, any>, networkRequestOptions: NetworkRequestReaderOptions, readerAst: ReaderAst<TReadFromStore>): TReadFromStore;
7
8
  export declare function useSubscribeToMultiple<TReadFromStore extends Object>(items: ReadonlyArray<{
8
9
  records: WithEncounteredRecords<TReadFromStore>;
9
10
  callback: (updatedRecords: WithEncounteredRecords<TReadFromStore>) => void;
10
11
  fragmentReference: FragmentReference<TReadFromStore, any>;
12
+ readerAst: ReaderAst<TReadFromStore>;
11
13
  }>): void;
@@ -10,18 +10,18 @@ const cache_1 = require("../core/cache");
10
10
  /**
11
11
  * Read the data from a fragment reference and subscribe to updates.
12
12
  */
13
- function useReadAndSubscribe(fragmentReference, networkRequestOptions) {
13
+ function useReadAndSubscribe(fragmentReference, networkRequestOptions, readerAst) {
14
14
  const environment = (0, IsographEnvironmentProvider_1.useIsographEnvironment)();
15
15
  const [readOutDataAndRecords, setReadOutDataAndRecords] = (0, react_1.useState)(() => (0, read_1.readButDoNotEvaluate)(environment, fragmentReference, networkRequestOptions));
16
- (0, useRerenderOnChange_1.useRerenderOnChange)(readOutDataAndRecords, fragmentReference, setReadOutDataAndRecords);
16
+ (0, useRerenderOnChange_1.useRerenderOnChange)(readOutDataAndRecords, fragmentReference, setReadOutDataAndRecords, readerAst);
17
17
  return readOutDataAndRecords.item;
18
18
  }
19
19
  exports.useReadAndSubscribe = useReadAndSubscribe;
20
20
  function useSubscribeToMultiple(items) {
21
21
  const environment = (0, IsographEnvironmentProvider_1.useIsographEnvironment)();
22
22
  (0, react_1.useEffect)(() => {
23
- const cleanupFns = items.map(({ records, callback, fragmentReference }) => {
24
- return (0, cache_1.subscribe)(environment, records, fragmentReference, callback);
23
+ const cleanupFns = items.map(({ records, callback, fragmentReference, readerAst }) => {
24
+ return (0, cache_1.subscribe)(environment, records, fragmentReference, callback, readerAst);
25
25
  });
26
26
  return () => {
27
27
  cleanupFns.forEach((loader) => {
@@ -1,3 +1,4 @@
1
1
  import { WithEncounteredRecords } from '../core/read';
2
2
  import { FragmentReference } from '../core/FragmentReference';
3
- export declare function useRerenderOnChange<TReadFromStore extends Object>(encounteredDataAndRecords: WithEncounteredRecords<TReadFromStore>, fragmentReference: FragmentReference<any, any>, setEncounteredDataAndRecords: (data: WithEncounteredRecords<TReadFromStore>) => void): void;
3
+ import type { ReaderAst } from '../core/reader';
4
+ export declare function useRerenderOnChange<TReadFromStore extends Object>(encounteredDataAndRecords: WithEncounteredRecords<TReadFromStore>, fragmentReference: FragmentReference<any, any>, setEncounteredDataAndRecords: (data: WithEncounteredRecords<TReadFromStore>) => void, readerAst: ReaderAst<TReadFromStore>): void;
@@ -6,12 +6,12 @@ const cache_1 = require("../core/cache");
6
6
  const IsographEnvironmentProvider_1 = require("./IsographEnvironmentProvider");
7
7
  // TODO add unit tests for this. Add integration tests that test
8
8
  // behavior when the encounteredRecords underneath a fragment change.
9
- function useRerenderOnChange(encounteredDataAndRecords, fragmentReference, setEncounteredDataAndRecords) {
9
+ function useRerenderOnChange(encounteredDataAndRecords, fragmentReference, setEncounteredDataAndRecords, readerAst) {
10
10
  const environment = (0, IsographEnvironmentProvider_1.useIsographEnvironment)();
11
11
  (0, react_1.useEffect)(() => {
12
12
  return (0, cache_1.subscribe)(environment, encounteredDataAndRecords, fragmentReference, (newEncounteredDataAndRecords) => {
13
13
  setEncounteredDataAndRecords(newEncounteredDataAndRecords);
14
- });
14
+ }, readerAst);
15
15
  // Note: this is an empty array on purpose:
16
16
  // - the fragment reference is stable for the life of the component
17
17
  // - ownership of encounteredDataAndRecords is transferred into the
@@ -17,7 +17,7 @@ function useResult(fragmentReference, partialNetworkRequestOptions) {
17
17
  return (0, componentCache_1.getOrCreateCachedComponent)(environment, readerWithRefetchQueries.readerArtifact.componentName, fragmentReference, networkRequestOptions);
18
18
  }
19
19
  case 'EagerReaderArtifact': {
20
- const data = (0, useReadAndSubscribe_1.useReadAndSubscribe)(fragmentReference, networkRequestOptions);
20
+ const data = (0, useReadAndSubscribe_1.useReadAndSubscribe)(fragmentReference, networkRequestOptions, readerWithRefetchQueries.readerArtifact.readerAst);
21
21
  return readerWithRefetchQueries.readerArtifact.resolver(data);
22
22
  }
23
23
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isograph/react",
3
- "version": "0.0.0-main-8b64a409",
3
+ "version": "0.0.0-main-752db914",
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-8b64a409",
21
- "@isograph/react-disposable-state": "0.0.0-main-8b64a409",
22
- "@isograph/reference-counted-pointer": "0.0.0-main-8b64a409"
20
+ "@isograph/disposable-types": "0.0.0-main-752db914",
21
+ "@isograph/react-disposable-state": "0.0.0-main-752db914",
22
+ "@isograph/reference-counted-pointer": "0.0.0-main-752db914"
23
23
  },
24
24
  "peerDependencies": {
25
25
  "react": "18.2.0"
@@ -4,6 +4,7 @@ import { WithEncounteredRecords } from './read';
4
4
  import { FragmentReference, Variables } from './FragmentReference';
5
5
  import { PromiseWrapper, wrapPromise } from './PromiseWrapper';
6
6
  import { IsographEntrypoint } from './entrypoint';
7
+ import type { ReaderAst } from './reader';
7
8
 
8
9
  export type ComponentOrFieldName = string;
9
10
  export type StringifiedArgs = string;
@@ -21,6 +22,7 @@ export type FragmentSubscription<TReadFromStore extends Object> = {
21
22
  /** The value read out from the previous call to readButDoNotEvaluate */
22
23
  readonly encounteredDataAndRecords: WithEncounteredRecords<TReadFromStore>;
23
24
  readonly fragmentReference: FragmentReference<TReadFromStore, any>;
25
+ readonly readerAst: ReaderAst<TReadFromStore>;
24
26
  };
25
27
  type AnyRecordSubscription = {
26
28
  readonly kind: 'AnyRecords';
package/src/core/cache.ts CHANGED
@@ -21,7 +21,7 @@ import {
21
21
  NormalizationScalarField,
22
22
  RefetchQueryNormalizationArtifactWrapper,
23
23
  } from '../core/entrypoint';
24
- import { ReaderLinkedField, ReaderScalarField } from './reader';
24
+ import { ReaderLinkedField, ReaderScalarField, type ReaderAst } from './reader';
25
25
  import { Argument, ArgumentValue } from './util';
26
26
  import { WithEncounteredRecords, readButDoNotEvaluate } from './read';
27
27
  import { FragmentReference, Variables } from './FragmentReference';
@@ -187,12 +187,14 @@ export function subscribe<TReadFromStore extends Object>(
187
187
  callback: (
188
188
  newEncounteredDataAndRecords: WithEncounteredRecords<TReadFromStore>,
189
189
  ) => void,
190
+ readerAst: ReaderAst<TReadFromStore>,
190
191
  ): () => void {
191
192
  const fragmentSubscription: FragmentSubscription<TReadFromStore> = {
192
193
  kind: 'FragmentSubscription',
193
194
  callback,
194
195
  encounteredDataAndRecords,
195
196
  fragmentReference,
197
+ readerAst,
196
198
  };
197
199
  // @ts-expect-error
198
200
  environment.subscriptions.add(fragmentSubscription);
@@ -30,9 +30,14 @@ export function getOrCreateCachedComponent(
30
30
  byArgs[stringifiedArgs] ??
31
31
  (() => {
32
32
  function Component(additionalRuntimeProps: { [key: string]: any }) {
33
+ const readerWithRefetchQueries = readPromise(
34
+ fragmentReference.readerWithRefetchQueries,
35
+ );
36
+
33
37
  const data = useReadAndSubscribe(
34
38
  fragmentReference,
35
39
  networkRequestOptions,
40
+ readerWithRefetchQueries.readerArtifact.readerAst,
36
41
  );
37
42
 
38
43
  // @ts-expect-error
@@ -45,10 +50,6 @@ export function getOrCreateCachedComponent(
45
50
  );
46
51
  }
47
52
 
48
- const readerWithRefetchQueries = readPromise(
49
- fragmentReference.readerWithRefetchQueries,
50
- );
51
-
52
53
  return readerWithRefetchQueries.readerArtifact.resolver(
53
54
  data,
54
55
  additionalRuntimeProps,
@@ -11,6 +11,7 @@ import {
11
11
  import { useRerenderOnChange } from './useRerenderOnChange';
12
12
  import { useIsographEnvironment } from './IsographEnvironmentProvider';
13
13
  import { subscribe } from '../core/cache';
14
+ import type { ReaderAst } from '../core/reader';
14
15
 
15
16
  /**
16
17
  * Read the data from a fragment reference and subscribe to updates.
@@ -18,6 +19,7 @@ import { subscribe } from '../core/cache';
18
19
  export function useReadAndSubscribe<TReadFromStore extends Object>(
19
20
  fragmentReference: FragmentReference<TReadFromStore, any>,
20
21
  networkRequestOptions: NetworkRequestReaderOptions,
22
+ readerAst: ReaderAst<TReadFromStore>,
21
23
  ): TReadFromStore {
22
24
  const environment = useIsographEnvironment();
23
25
  const [readOutDataAndRecords, setReadOutDataAndRecords] = useState(() =>
@@ -27,6 +29,7 @@ export function useReadAndSubscribe<TReadFromStore extends Object>(
27
29
  readOutDataAndRecords,
28
30
  fragmentReference,
29
31
  setReadOutDataAndRecords,
32
+ readerAst,
30
33
  );
31
34
  return readOutDataAndRecords.item;
32
35
  }
@@ -36,14 +39,21 @@ export function useSubscribeToMultiple<TReadFromStore extends Object>(
36
39
  records: WithEncounteredRecords<TReadFromStore>;
37
40
  callback: (updatedRecords: WithEncounteredRecords<TReadFromStore>) => void;
38
41
  fragmentReference: FragmentReference<TReadFromStore, any>;
42
+ readerAst: ReaderAst<TReadFromStore>;
39
43
  }>,
40
44
  ) {
41
45
  const environment = useIsographEnvironment();
42
46
  useEffect(
43
47
  () => {
44
48
  const cleanupFns = items.map(
45
- ({ records, callback, fragmentReference }) => {
46
- return subscribe(environment, records, fragmentReference, callback);
49
+ ({ records, callback, fragmentReference, readerAst }) => {
50
+ return subscribe(
51
+ environment,
52
+ records,
53
+ fragmentReference,
54
+ callback,
55
+ readerAst,
56
+ );
47
57
  },
48
58
  );
49
59
  return () => {
@@ -3,6 +3,7 @@ import { subscribe } from '../core/cache';
3
3
  import { WithEncounteredRecords } from '../core/read';
4
4
  import { FragmentReference } from '../core/FragmentReference';
5
5
  import { useIsographEnvironment } from './IsographEnvironmentProvider';
6
+ import type { ReaderAst } from '../core/reader';
6
7
 
7
8
  // TODO add unit tests for this. Add integration tests that test
8
9
  // behavior when the encounteredRecords underneath a fragment change.
@@ -12,6 +13,7 @@ export function useRerenderOnChange<TReadFromStore extends Object>(
12
13
  setEncounteredDataAndRecords: (
13
14
  data: WithEncounteredRecords<TReadFromStore>,
14
15
  ) => void,
16
+ readerAst: ReaderAst<TReadFromStore>,
15
17
  ) {
16
18
  const environment = useIsographEnvironment();
17
19
  useEffect(() => {
@@ -22,6 +24,7 @@ export function useRerenderOnChange<TReadFromStore extends Object>(
22
24
  (newEncounteredDataAndRecords) => {
23
25
  setEncounteredDataAndRecords(newEncounteredDataAndRecords);
24
26
  },
27
+ readerAst,
25
28
  );
26
29
  // Note: this is an empty array on purpose:
27
30
  // - the fragment reference is stable for the life of the component
@@ -43,6 +43,7 @@ export function useResult<TReadFromStore extends Object, TClientFieldValue>(
43
43
  const data = useReadAndSubscribe(
44
44
  fragmentReference,
45
45
  networkRequestOptions,
46
+ readerWithRefetchQueries.readerArtifact.readerAst,
46
47
  );
47
48
  return readerWithRefetchQueries.readerArtifact.resolver(data);
48
49
  }