@isograph/react 0.0.0-main-945e49cc → 0.0.0-main-1bfd646b

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,7 +1,6 @@
1
- import { ReactNode } from 'react';
2
- import * as React from 'react';
1
+ /// <reference types="react" />
3
2
  import { ParentCache } from '@isograph/isograph-react-disposable-state';
4
- export declare const IsographEnvironmentContext: React.Context<IsographEnvironment | null>;
3
+ import { RetainedQuery } from './garbageCollection';
5
4
  type ComponentName = string;
6
5
  type StringifiedArgs = string;
7
6
  type ComponentCache = {
@@ -22,6 +21,9 @@ export type IsographEnvironment = {
22
21
  componentCache: ComponentCache;
23
22
  subscriptions: Subscriptions;
24
23
  suspenseCache: SuspenseCache;
24
+ retainedQueries: Set<RetainedQuery>;
25
+ gcBuffer: Array<RetainedQuery>;
26
+ gcBufferSize: number;
25
27
  };
26
28
  export type MissingFieldHandler = (storeRecord: StoreRecord, root: DataId, fieldName: string, arguments_: {
27
29
  [index: string]: any;
@@ -43,12 +45,6 @@ export type IsographStore = {
43
45
  [index: DataId]: StoreRecord | null;
44
46
  __ROOT: StoreRecord;
45
47
  };
46
- export type IsographEnvironmentProviderProps = {
47
- environment: IsographEnvironment;
48
- children: ReactNode;
49
- };
50
- export declare function IsographEnvironmentProvider({ environment, children, }: IsographEnvironmentProviderProps): React.JSX.Element;
51
- export declare function useIsographEnvironment(): IsographEnvironment;
52
48
  export declare function createIsographEnvironment(store: IsographStore, networkFunction: IsographNetworkFunction, missingFieldHandler?: MissingFieldHandler): IsographEnvironment;
53
49
  export declare function createIsographStore(): {
54
50
  __ROOT: {};
@@ -1,46 +1,8 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.createIsographStore = exports.createIsographEnvironment = exports.useIsographEnvironment = exports.IsographEnvironmentProvider = exports.ROOT_ID = exports.IsographEnvironmentContext = void 0;
27
- const react_1 = require("react");
28
- const React = __importStar(require("react"));
29
- exports.IsographEnvironmentContext = (0, react_1.createContext)(null);
3
+ exports.createIsographStore = exports.createIsographEnvironment = exports.ROOT_ID = void 0;
30
4
  exports.ROOT_ID = '__ROOT';
31
- function IsographEnvironmentProvider({ environment, children, }) {
32
- return (React.createElement(exports.IsographEnvironmentContext.Provider, { value: environment }, children));
33
- }
34
- exports.IsographEnvironmentProvider = IsographEnvironmentProvider;
35
- function useIsographEnvironment() {
36
- const context = (0, react_1.useContext)(exports.IsographEnvironmentContext);
37
- if (context == null) {
38
- throw new Error('Unexpected null environment context. Make sure to render ' +
39
- 'this component within an IsographEnvironmentProvider component');
40
- }
41
- return context;
42
- }
43
- exports.useIsographEnvironment = useIsographEnvironment;
5
+ const DEFAULT_GC_BUFFER_SIZE = 10;
44
6
  function createIsographEnvironment(store, networkFunction, missingFieldHandler) {
45
7
  return {
46
8
  store,
@@ -49,6 +11,9 @@ function createIsographEnvironment(store, networkFunction, missingFieldHandler)
49
11
  componentCache: {},
50
12
  subscriptions: new Set(),
51
13
  suspenseCache: {},
14
+ retainedQueries: new Set(),
15
+ gcBuffer: [],
16
+ gcBufferSize: DEFAULT_GC_BUFFER_SIZE,
52
17
  };
53
18
  }
54
19
  exports.createIsographEnvironment = createIsographEnvironment;
@@ -0,0 +1,10 @@
1
+ import * as React from 'react';
2
+ import { ReactNode } from 'react';
3
+ import { type IsographEnvironment } from './IsographEnvironment';
4
+ export declare const IsographEnvironmentContext: React.Context<IsographEnvironment | null>;
5
+ export type IsographEnvironmentProviderProps = {
6
+ environment: IsographEnvironment;
7
+ children: ReactNode;
8
+ };
9
+ export declare function IsographEnvironmentProvider({ environment, children, }: IsographEnvironmentProviderProps): React.JSX.Element;
10
+ export declare function useIsographEnvironment(): IsographEnvironment;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.useIsographEnvironment = exports.IsographEnvironmentProvider = exports.IsographEnvironmentContext = void 0;
27
+ const React = __importStar(require("react"));
28
+ const react_1 = require("react");
29
+ exports.IsographEnvironmentContext = (0, react_1.createContext)(null);
30
+ function IsographEnvironmentProvider({ environment, children, }) {
31
+ return (React.createElement(exports.IsographEnvironmentContext.Provider, { value: environment }, children));
32
+ }
33
+ exports.IsographEnvironmentProvider = IsographEnvironmentProvider;
34
+ function useIsographEnvironment() {
35
+ const context = (0, react_1.useContext)(exports.IsographEnvironmentContext);
36
+ if (context == null) {
37
+ throw new Error('Unexpected null environment context. Make sure to render ' +
38
+ 'this component within an IsographEnvironmentProvider component');
39
+ }
40
+ return context;
41
+ }
42
+ exports.useIsographEnvironment = useIsographEnvironment;
package/dist/cache.js CHANGED
@@ -4,6 +4,7 @@ exports.SECOND_SPLIT_KEY = exports.FIRST_SPLIT_KEY = exports.getParentRecordKey
4
4
  const react_disposable_state_1 = require("@isograph/react-disposable-state");
5
5
  const PromiseWrapper_1 = require("./PromiseWrapper");
6
6
  const IsographEnvironment_1 = require("./IsographEnvironment");
7
+ const garbageCollection_1 = require("./garbageCollection");
7
8
  function getOrCreateCache(environment, index, factory) {
8
9
  if (typeof window !== 'undefined' && window.__LOG) {
9
10
  console.log('getting cache for', {
@@ -49,34 +50,60 @@ function makeNetworkRequest(environment, artifact, variables) {
49
50
  if (typeof window !== 'undefined' && window.__LOG) {
50
51
  console.log('make network request', artifact, variables);
51
52
  }
53
+ let status = {
54
+ kind: 'UndisposedIncomplete',
55
+ };
56
+ // This should be an observable, not a promise
52
57
  const promise = environment
53
58
  .networkFunction(artifact.queryText, variables)
54
59
  .then((networkResponse) => {
55
60
  if (typeof window !== 'undefined' && window.__LOG) {
56
61
  console.log('network response', artifact, artifact);
57
62
  }
58
- normalizeData(environment, artifact.normalizationAst, networkResponse.data, variables, artifact.nestedRefetchQueries);
59
- return networkResponse.data;
63
+ if (status.kind === 'UndisposedIncomplete') {
64
+ normalizeData(environment, artifact.normalizationAst, networkResponse.data, variables, artifact.nestedRefetchQueries);
65
+ const retainedQuery = {
66
+ normalizationAst: artifact.normalizationAst,
67
+ variables,
68
+ };
69
+ status = {
70
+ kind: 'UndisposedComplete',
71
+ retainedQuery,
72
+ };
73
+ (0, garbageCollection_1.retainQuery)(environment, retainedQuery);
74
+ }
75
+ // TODO return null
76
+ return networkResponse;
60
77
  });
61
78
  const wrapper = (0, PromiseWrapper_1.wrapPromise)(promise);
62
79
  const response = [
63
80
  wrapper,
64
81
  () => {
65
- // delete from cache
82
+ if (status.kind === 'UndisposedComplete') {
83
+ const didUnretainSomeQuery = (0, garbageCollection_1.unretainQuery)(environment, status.retainedQuery);
84
+ if (didUnretainSomeQuery) {
85
+ (0, garbageCollection_1.garbageCollectEnvironment)(environment);
86
+ }
87
+ }
88
+ status = {
89
+ kind: 'Disposed',
90
+ };
66
91
  },
67
92
  ];
68
93
  return response;
69
94
  }
70
95
  exports.makeNetworkRequest = makeNetworkRequest;
71
96
  function normalizeData(environment, normalizationAst, networkResponse, variables, nestedRefetchQueries) {
97
+ const encounteredIds = new Set();
72
98
  if (typeof window !== 'undefined' && window.__LOG) {
73
99
  console.log('about to normalize', normalizationAst, networkResponse, variables);
74
100
  }
75
- normalizeDataIntoRecord(environment, normalizationAst, networkResponse, environment.store.__ROOT, IsographEnvironment_1.ROOT_ID, variables, nestedRefetchQueries);
101
+ normalizeDataIntoRecord(environment, normalizationAst, networkResponse, environment.store.__ROOT, IsographEnvironment_1.ROOT_ID, variables, nestedRefetchQueries, encounteredIds);
76
102
  if (typeof window !== 'undefined' && window.__LOG) {
77
103
  console.log('after normalization', { store: environment.store });
78
104
  }
79
105
  callSubscriptions(environment);
106
+ return encounteredIds;
80
107
  }
81
108
  function subscribe(environment, callback) {
82
109
  environment.subscriptions.add(callback);
@@ -98,7 +125,8 @@ function callSubscriptions(environment) {
98
125
  /**
99
126
  * Mutate targetParentRecord according to the normalizationAst and networkResponseParentRecord.
100
127
  */
101
- function normalizeDataIntoRecord(environment, normalizationAst, networkResponseParentRecord, targetParentRecord, targetParentRecordId, variables, nestedRefetchQueries) {
128
+ function normalizeDataIntoRecord(environment, normalizationAst, networkResponseParentRecord, targetParentRecord, targetParentRecordId, variables, nestedRefetchQueries, mutableEncounteredIds) {
129
+ mutableEncounteredIds.add(targetParentRecordId);
102
130
  for (const normalizationNode of normalizationAst) {
103
131
  switch (normalizationNode.kind) {
104
132
  case 'Scalar': {
@@ -106,7 +134,7 @@ function normalizeDataIntoRecord(environment, normalizationAst, networkResponseP
106
134
  break;
107
135
  }
108
136
  case 'Linked': {
109
- normalizeLinkedField(environment, normalizationNode, networkResponseParentRecord, targetParentRecord, targetParentRecordId, variables, nestedRefetchQueries);
137
+ normalizeLinkedField(environment, normalizationNode, networkResponseParentRecord, targetParentRecord, targetParentRecordId, variables, nestedRefetchQueries, mutableEncounteredIds);
110
138
  break;
111
139
  }
112
140
  }
@@ -127,7 +155,7 @@ function normalizeScalarField(astNode, networkResponseParentRecord, targetStoreR
127
155
  /**
128
156
  * Mutate targetParentRecord with a given linked field ast node.
129
157
  */
130
- function normalizeLinkedField(environment, astNode, networkResponseParentRecord, targetParentRecord, targetParentRecordId, variables, nestedRefetchQueries) {
158
+ function normalizeLinkedField(environment, astNode, networkResponseParentRecord, targetParentRecord, targetParentRecordId, variables, nestedRefetchQueries, mutableEncounteredIds) {
131
159
  const networkResponseKey = getNetworkResponseKey(astNode);
132
160
  const networkResponseData = networkResponseParentRecord[networkResponseKey];
133
161
  const parentRecordKey = getParentRecordKey(astNode, variables);
@@ -143,24 +171,24 @@ function normalizeLinkedField(environment, astNode, networkResponseParentRecord,
143
171
  const dataIds = [];
144
172
  for (let i = 0; i < networkResponseData.length; i++) {
145
173
  const networkResponseObject = networkResponseData[i];
146
- const newStoreRecordId = normalizeNetworkResponseObject(environment, astNode, networkResponseObject, targetParentRecordId, variables, i, nestedRefetchQueries);
174
+ const newStoreRecordId = normalizeNetworkResponseObject(environment, astNode, networkResponseObject, targetParentRecordId, variables, i, nestedRefetchQueries, mutableEncounteredIds);
147
175
  dataIds.push({ __link: newStoreRecordId });
148
176
  }
149
177
  targetParentRecord[parentRecordKey] = dataIds;
150
178
  }
151
179
  else {
152
- const newStoreRecordId = normalizeNetworkResponseObject(environment, astNode, networkResponseData, targetParentRecordId, variables, null, nestedRefetchQueries);
180
+ const newStoreRecordId = normalizeNetworkResponseObject(environment, astNode, networkResponseData, targetParentRecordId, variables, null, nestedRefetchQueries, mutableEncounteredIds);
153
181
  targetParentRecord[parentRecordKey] = {
154
182
  __link: newStoreRecordId,
155
183
  };
156
184
  }
157
185
  }
158
- function normalizeNetworkResponseObject(environment, astNode, networkResponseData, targetParentRecordId, variables, index, nestedRefetchQueries) {
186
+ function normalizeNetworkResponseObject(environment, astNode, networkResponseData, targetParentRecordId, variables, index, nestedRefetchQueries, mutableEncounteredIds) {
159
187
  var _a;
160
188
  const newStoreRecordId = getDataIdOfNetworkResponse(targetParentRecordId, networkResponseData, astNode, variables, index);
161
189
  const newStoreRecord = (_a = environment.store[newStoreRecordId]) !== null && _a !== void 0 ? _a : {};
162
190
  environment.store[newStoreRecordId] = newStoreRecord;
163
- normalizeDataIntoRecord(environment, astNode.selections, networkResponseData, newStoreRecord, newStoreRecordId, variables, nestedRefetchQueries);
191
+ normalizeDataIntoRecord(environment, astNode.selections, networkResponseData, newStoreRecord, newStoreRecordId, variables, nestedRefetchQueries, mutableEncounteredIds);
164
192
  return newStoreRecordId;
165
193
  }
166
194
  function isScalarOrEmptyArray(data) {
@@ -0,0 +1,32 @@
1
+ import { Arguments } from './index';
2
+ import { ReaderArtifact } from './reader';
3
+ export type IsographEntrypoint<TReadFromStore extends Object, TResolverResult> = {
4
+ kind: 'Entrypoint';
5
+ queryText: string;
6
+ normalizationAst: NormalizationAst;
7
+ readerArtifact: ReaderArtifact<TReadFromStore, TResolverResult>;
8
+ nestedRefetchQueries: RefetchQueryArtifactWrapper[];
9
+ };
10
+ export type NormalizationAstNode = NormalizationScalarField | NormalizationLinkedField;
11
+ export type NormalizationAst = NormalizationAstNode[];
12
+ export type NormalizationScalarField = {
13
+ kind: 'Scalar';
14
+ fieldName: string;
15
+ arguments: Arguments | null;
16
+ };
17
+ export type NormalizationLinkedField = {
18
+ kind: 'Linked';
19
+ fieldName: string;
20
+ arguments: Arguments | null;
21
+ selections: NormalizationAst;
22
+ };
23
+ export type RefetchQueryArtifact = {
24
+ kind: 'RefetchQuery';
25
+ queryText: string;
26
+ normalizationAst: NormalizationAst;
27
+ };
28
+ export type RefetchQueryArtifactWrapper = {
29
+ artifact: RefetchQueryArtifact;
30
+ allowedVariables: string[];
31
+ };
32
+ export declare function assertIsEntrypoint<TReadFromStore extends Object, TResolverResult>(value: IsographEntrypoint<TReadFromStore, TResolverResult> | ((_: any) => any) | any): asserts value is IsographEntrypoint<TReadFromStore, TResolverResult>;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertIsEntrypoint = void 0;
4
+ function assertIsEntrypoint(value) {
5
+ if (typeof value === 'function')
6
+ throw new Error('Not a string');
7
+ }
8
+ exports.assertIsEntrypoint = assertIsEntrypoint;
@@ -0,0 +1,11 @@
1
+ import { IsographEnvironment } from './IsographEnvironment';
2
+ import { NormalizationAst } from './index';
3
+ export type RetainedQuery = {
4
+ normalizationAst: NormalizationAst;
5
+ variables: {};
6
+ };
7
+ type DidUnretainSomeQuery = boolean;
8
+ export declare function unretainQuery(environment: IsographEnvironment, retainedQuery: RetainedQuery): DidUnretainSomeQuery;
9
+ export declare function retainQuery(environment: IsographEnvironment, queryToRetain: RetainedQuery): void;
10
+ export declare function garbageCollectEnvironment(environment: IsographEnvironment): void;
11
+ export {};
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.garbageCollectEnvironment = exports.retainQuery = exports.unretainQuery = void 0;
4
+ const IsographEnvironment_1 = require("./IsographEnvironment");
5
+ const cache_1 = require("./cache");
6
+ function unretainQuery(environment, retainedQuery) {
7
+ environment.retainedQueries.delete(retainedQuery);
8
+ environment.gcBuffer.push(retainedQuery);
9
+ if (environment.gcBuffer.length > environment.gcBufferSize) {
10
+ environment.gcBuffer.shift();
11
+ return true;
12
+ }
13
+ return false;
14
+ }
15
+ exports.unretainQuery = unretainQuery;
16
+ function retainQuery(environment, queryToRetain) {
17
+ environment.retainedQueries.add(queryToRetain);
18
+ // TODO can we remove this query from the buffer somehow?
19
+ // We are relying on === equality, but we really should be comparing
20
+ // id + variables
21
+ }
22
+ exports.retainQuery = retainQuery;
23
+ function garbageCollectEnvironment(environment) {
24
+ const retainedIds = new Set([IsographEnvironment_1.ROOT_ID]);
25
+ for (const query of environment.retainedQueries) {
26
+ recordReachableIds(environment.store, query, retainedIds);
27
+ }
28
+ for (const query of environment.gcBuffer) {
29
+ recordReachableIds(environment.store, query, retainedIds);
30
+ }
31
+ for (const dataId in environment.store) {
32
+ if (!retainedIds.has(dataId)) {
33
+ delete environment.store[dataId];
34
+ }
35
+ }
36
+ }
37
+ exports.garbageCollectEnvironment = garbageCollectEnvironment;
38
+ function recordReachableIds(store, retainedQuery, mutableRetainedIds) {
39
+ recordReachableIdsFromRecord(store, store[IsographEnvironment_1.ROOT_ID], mutableRetainedIds, retainedQuery.normalizationAst, retainedQuery.variables);
40
+ }
41
+ function getLinkedId(data) {
42
+ // @ts-expect-error
43
+ if (data.__link != null) {
44
+ // @ts-expect-error
45
+ return data.__link;
46
+ }
47
+ else {
48
+ throw new Error('Record in an invalid state');
49
+ }
50
+ }
51
+ function recordReachableIdsFromRecord(store, currentRecord, mutableRetainedIds, selections, variables) {
52
+ for (const selection of selections) {
53
+ switch (selection.kind) {
54
+ case 'Linked':
55
+ const linkKey = (0, cache_1.getParentRecordKey)(selection, variables !== null && variables !== void 0 ? variables : {});
56
+ const linkedFieldOrFields = currentRecord[linkKey];
57
+ const ids = [];
58
+ if (Array.isArray(linkedFieldOrFields)) {
59
+ for (const link of linkedFieldOrFields) {
60
+ if (link != null) {
61
+ const id = getLinkedId(link);
62
+ ids.push(id);
63
+ }
64
+ }
65
+ }
66
+ else {
67
+ if (linkedFieldOrFields != null) {
68
+ const id = getLinkedId(linkedFieldOrFields);
69
+ ids.push(id);
70
+ }
71
+ }
72
+ for (const nextRecordId of ids) {
73
+ const nextRecord = store[nextRecordId];
74
+ if (nextRecord != null) {
75
+ mutableRetainedIds.add(nextRecordId);
76
+ recordReachableIdsFromRecord(store, nextRecord, mutableRetainedIds, selection.selections, variables);
77
+ }
78
+ }
79
+ continue;
80
+ case 'Scalar':
81
+ continue;
82
+ }
83
+ }
84
+ }
package/dist/index.d.ts CHANGED
@@ -1,85 +1,16 @@
1
1
  import { DataId, IsographEnvironment, Link, StoreRecord } from './IsographEnvironment';
2
+ import { ReaderArtifact } from './reader';
3
+ import { IsographEntrypoint, RefetchQueryArtifactWrapper } from './entrypoint';
4
+ export { retainQuery, unretainQuery, type RetainedQuery, garbageCollectEnvironment, } from './garbageCollection';
2
5
  export { type PromiseWrapper } from './PromiseWrapper';
3
6
  export { makeNetworkRequest, subscribe } from './cache';
4
- export { IsographEnvironmentContext, ROOT_ID, type DataId, type DataTypeValue, type IsographEnvironment, IsographEnvironmentProvider, type IsographEnvironmentProviderProps, type IsographNetworkFunction, type IsographStore, type Link, type StoreRecord, useIsographEnvironment, createIsographEnvironment, createIsographStore, } from './IsographEnvironment';
7
+ export { ROOT_ID, type DataId, type DataTypeValue, type IsographEnvironment, type IsographNetworkFunction, type IsographStore, type Link, type StoreRecord, createIsographEnvironment, createIsographStore, } from './IsographEnvironment';
8
+ export { IsographEnvironmentProvider, useIsographEnvironment, type IsographEnvironmentProviderProps, } from './IsographEnvironmentProvider';
5
9
  export { useImperativeReference } from './useImperativeReference';
6
10
  export { EntrypointReader } from './EntrypointReader';
7
- export type IsographEntrypoint<TReadFromStore extends Object, TResolverResult> = {
8
- kind: 'Entrypoint';
9
- queryText: string;
10
- normalizationAst: NormalizationAst;
11
- readerArtifact: ReaderArtifact<TReadFromStore, TResolverResult>;
12
- nestedRefetchQueries: RefetchQueryArtifactWrapper[];
13
- };
14
- export type ReaderArtifact<TReadFromStore extends Object, TResolverResult> = {
15
- kind: 'ReaderArtifact';
16
- readerAst: ReaderAst<TReadFromStore>;
17
- resolver: (data: TReadFromStore, runtimeProps: any) => TResolverResult;
18
- variant: ReaderResolverVariant;
19
- };
20
- export type ReaderAstNode = ReaderScalarField | ReaderLinkedField | ReaderResolverField | ReaderRefetchField | ReaderMutationField;
21
- export type ReaderAst<TReadFromStore> = ReaderAstNode[];
11
+ export { type ReaderArtifact, ReaderAst, ReaderAstNode, ReaderLinkedField, ReaderMutationField, ReaderRefetchField, ReaderResolverField, ReaderResolverVariant, ReaderScalarField, } from './reader';
12
+ export { NormalizationAst, NormalizationAstNode, NormalizationLinkedField, NormalizationScalarField, IsographEntrypoint, assertIsEntrypoint, RefetchQueryArtifact, RefetchQueryArtifactWrapper, } from './entrypoint';
22
13
  export type ExtractSecondParam<T extends (arg1: any, arg2: any) => any> = T extends (arg1: any, arg2: infer P) => any ? P : never;
23
- export type ReaderScalarField = {
24
- kind: 'Scalar';
25
- fieldName: string;
26
- alias: string | null;
27
- arguments: Arguments | null;
28
- };
29
- export type ReaderLinkedField = {
30
- kind: 'Linked';
31
- fieldName: string;
32
- alias: string | null;
33
- selections: ReaderAst<unknown>;
34
- arguments: Arguments | null;
35
- };
36
- export type ReaderResolverVariant = {
37
- kind: 'Eager';
38
- } | {
39
- kind: 'Component';
40
- componentName: string;
41
- };
42
- export type ReaderResolverField = {
43
- kind: 'Resolver';
44
- alias: string;
45
- readerArtifact: ReaderArtifact<any, any>;
46
- arguments: Arguments | null;
47
- usedRefetchQueries: number[];
48
- };
49
- export type ReaderRefetchField = {
50
- kind: 'RefetchField';
51
- alias: string;
52
- readerArtifact: ReaderArtifact<any, any>;
53
- refetchQuery: number;
54
- };
55
- export type ReaderMutationField = {
56
- kind: 'MutationField';
57
- alias: string;
58
- readerArtifact: ReaderArtifact<any, any>;
59
- refetchQuery: number;
60
- };
61
- export type NormalizationAstNode = NormalizationScalarField | NormalizationLinkedField;
62
- export type NormalizationAst = NormalizationAstNode[];
63
- export type NormalizationScalarField = {
64
- kind: 'Scalar';
65
- fieldName: string;
66
- arguments: Arguments | null;
67
- };
68
- export type NormalizationLinkedField = {
69
- kind: 'Linked';
70
- fieldName: string;
71
- arguments: Arguments | null;
72
- selections: NormalizationAst;
73
- };
74
- export type RefetchQueryArtifact = {
75
- kind: 'RefetchQuery';
76
- queryText: string;
77
- normalizationAst: NormalizationAst;
78
- };
79
- export type RefetchQueryArtifactWrapper = {
80
- artifact: RefetchQueryArtifact;
81
- allowedVariables: string[];
82
- };
83
14
  export type Arguments = Argument[];
84
15
  export type Argument = [ArgumentName, ArgumentValue];
85
16
  export type ArgumentName = string;
package/dist/index.js CHANGED
@@ -1,36 +1,40 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.defaultMissingFieldHandler = exports.readButDoNotEvaluate = exports.read = exports.useResult = exports.useLazyReference = exports.EntrypointReader = exports.useImperativeReference = exports.createIsographStore = exports.createIsographEnvironment = exports.useIsographEnvironment = exports.IsographEnvironmentProvider = exports.ROOT_ID = exports.IsographEnvironmentContext = exports.subscribe = exports.makeNetworkRequest = void 0;
3
+ exports.defaultMissingFieldHandler = exports.readButDoNotEvaluate = exports.read = exports.useResult = exports.useLazyReference = exports.assertIsEntrypoint = exports.EntrypointReader = exports.useImperativeReference = exports.useIsographEnvironment = exports.IsographEnvironmentProvider = exports.createIsographStore = exports.createIsographEnvironment = exports.ROOT_ID = exports.subscribe = exports.makeNetworkRequest = exports.garbageCollectEnvironment = exports.unretainQuery = exports.retainQuery = void 0;
4
4
  const cache_1 = require("./cache");
5
5
  const react_disposable_state_1 = require("@isograph/react-disposable-state");
6
6
  const componentCache_1 = require("./componentCache");
7
7
  const IsographEnvironment_1 = require("./IsographEnvironment");
8
8
  const react_1 = require("react");
9
+ const IsographEnvironmentProvider_1 = require("./IsographEnvironmentProvider");
10
+ const entrypoint_1 = require("./entrypoint");
11
+ var garbageCollection_1 = require("./garbageCollection");
12
+ Object.defineProperty(exports, "retainQuery", { enumerable: true, get: function () { return garbageCollection_1.retainQuery; } });
13
+ Object.defineProperty(exports, "unretainQuery", { enumerable: true, get: function () { return garbageCollection_1.unretainQuery; } });
14
+ Object.defineProperty(exports, "garbageCollectEnvironment", { enumerable: true, get: function () { return garbageCollection_1.garbageCollectEnvironment; } });
9
15
  var cache_2 = require("./cache");
10
16
  Object.defineProperty(exports, "makeNetworkRequest", { enumerable: true, get: function () { return cache_2.makeNetworkRequest; } });
11
17
  Object.defineProperty(exports, "subscribe", { enumerable: true, get: function () { return cache_2.subscribe; } });
12
18
  var IsographEnvironment_2 = require("./IsographEnvironment");
13
- Object.defineProperty(exports, "IsographEnvironmentContext", { enumerable: true, get: function () { return IsographEnvironment_2.IsographEnvironmentContext; } });
14
19
  Object.defineProperty(exports, "ROOT_ID", { enumerable: true, get: function () { return IsographEnvironment_2.ROOT_ID; } });
15
- Object.defineProperty(exports, "IsographEnvironmentProvider", { enumerable: true, get: function () { return IsographEnvironment_2.IsographEnvironmentProvider; } });
16
- Object.defineProperty(exports, "useIsographEnvironment", { enumerable: true, get: function () { return IsographEnvironment_2.useIsographEnvironment; } });
17
20
  Object.defineProperty(exports, "createIsographEnvironment", { enumerable: true, get: function () { return IsographEnvironment_2.createIsographEnvironment; } });
18
21
  Object.defineProperty(exports, "createIsographStore", { enumerable: true, get: function () { return IsographEnvironment_2.createIsographStore; } });
22
+ var IsographEnvironmentProvider_2 = require("./IsographEnvironmentProvider");
23
+ Object.defineProperty(exports, "IsographEnvironmentProvider", { enumerable: true, get: function () { return IsographEnvironmentProvider_2.IsographEnvironmentProvider; } });
24
+ Object.defineProperty(exports, "useIsographEnvironment", { enumerable: true, get: function () { return IsographEnvironmentProvider_2.useIsographEnvironment; } });
19
25
  var useImperativeReference_1 = require("./useImperativeReference");
20
26
  Object.defineProperty(exports, "useImperativeReference", { enumerable: true, get: function () { return useImperativeReference_1.useImperativeReference; } });
21
27
  var EntrypointReader_1 = require("./EntrypointReader");
22
28
  Object.defineProperty(exports, "EntrypointReader", { enumerable: true, get: function () { return EntrypointReader_1.EntrypointReader; } });
23
- function assertIsEntrypoint(value) {
24
- if (typeof value === 'function')
25
- throw new Error('Not a string');
26
- }
29
+ var entrypoint_2 = require("./entrypoint");
30
+ Object.defineProperty(exports, "assertIsEntrypoint", { enumerable: true, get: function () { return entrypoint_2.assertIsEntrypoint; } });
27
31
  // Note: we cannot write TEntrypoint extends IsographEntrypoint<any, any, any>, or else
28
32
  // if we do not explicitly pass a type, the read out type will be any.
29
33
  // We cannot write TEntrypoint extends IsographEntrypoint<never, never, never>, or else
30
34
  // any actual Entrypoint we pass will not be valid.
31
35
  function useLazyReference(entrypoint, variables) {
32
- const environment = (0, IsographEnvironment_1.useIsographEnvironment)();
33
- assertIsEntrypoint(entrypoint);
36
+ const environment = (0, IsographEnvironmentProvider_1.useIsographEnvironment)();
37
+ (0, entrypoint_1.assertIsEntrypoint)(entrypoint);
34
38
  const cache = (0, cache_1.getOrCreateCacheForArtifact)(environment, entrypoint, variables);
35
39
  // TODO add comment explaining why we never use this value
36
40
  // @ts-ignore
@@ -47,7 +51,7 @@ function useLazyReference(entrypoint, variables) {
47
51
  }
48
52
  exports.useLazyReference = useLazyReference;
49
53
  function useResult(fragmentReference) {
50
- const environment = (0, IsographEnvironment_1.useIsographEnvironment)();
54
+ const environment = (0, IsographEnvironmentProvider_1.useIsographEnvironment)();
51
55
  const [, setState] = (0, react_1.useState)();
52
56
  (0, react_1.useEffect)(() => {
53
57
  return (0, cache_1.subscribe)(environment, () => {
@@ -0,0 +1,47 @@
1
+ import { Arguments } from './index';
2
+ export type ReaderArtifact<TReadFromStore extends Object, TResolverResult> = {
3
+ kind: 'ReaderArtifact';
4
+ readerAst: ReaderAst<TReadFromStore>;
5
+ resolver: (data: TReadFromStore, runtimeProps: any) => TResolverResult;
6
+ variant: ReaderResolverVariant;
7
+ };
8
+ export type ReaderAstNode = ReaderScalarField | ReaderLinkedField | ReaderResolverField | ReaderRefetchField | ReaderMutationField;
9
+ export type ReaderAst<TReadFromStore> = ReaderAstNode[];
10
+ export type ReaderScalarField = {
11
+ kind: 'Scalar';
12
+ fieldName: string;
13
+ alias: string | null;
14
+ arguments: Arguments | null;
15
+ };
16
+ export type ReaderLinkedField = {
17
+ kind: 'Linked';
18
+ fieldName: string;
19
+ alias: string | null;
20
+ selections: ReaderAst<unknown>;
21
+ arguments: Arguments | null;
22
+ };
23
+ export type ReaderResolverVariant = {
24
+ kind: 'Eager';
25
+ } | {
26
+ kind: 'Component';
27
+ componentName: string;
28
+ };
29
+ export type ReaderResolverField = {
30
+ kind: 'Resolver';
31
+ alias: string;
32
+ readerArtifact: ReaderArtifact<any, any>;
33
+ arguments: Arguments | null;
34
+ usedRefetchQueries: number[];
35
+ };
36
+ export type ReaderRefetchField = {
37
+ kind: 'RefetchField';
38
+ alias: string;
39
+ readerArtifact: ReaderArtifact<any, any>;
40
+ refetchQuery: number;
41
+ };
42
+ export type ReaderMutationField = {
43
+ kind: 'MutationField';
44
+ alias: string;
45
+ readerArtifact: ReaderArtifact<any, any>;
46
+ refetchQuery: number;
47
+ };
package/dist/reader.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ // TODO this should probably be at least three distinct types, for @component,
3
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isograph/react",
3
- "version": "0.0.0-main-945e49cc",
3
+ "version": "0.0.0-main-1bfd646b",
4
4
  "description": "Use Isograph with React",
5
5
  "homepage": "https://isograph.dev",
6
6
  "main": "dist/index.js",
@@ -16,8 +16,8 @@
16
16
  "prepack": "yarn run test && yarn run compile"
17
17
  },
18
18
  "dependencies": {
19
- "@isograph/disposable-types": "0.0.0-main-945e49cc",
20
- "@isograph/react-disposable-state": "0.0.0-main-945e49cc",
19
+ "@isograph/disposable-types": "0.0.0-main-1bfd646b",
20
+ "@isograph/react-disposable-state": "0.0.0-main-1bfd646b",
21
21
  "react": "^18.2.0"
22
22
  },
23
23
  "devDependencies": {