@isograph/react 0.0.0-main-ee768a61 → 0.0.0-main-d64c2813
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.
- package/dist/cache.d.ts +6 -0
- package/dist/cache.js +2 -1
- package/dist/componentCache.d.ts +1 -1
- package/dist/componentCache.js +3 -1
- package/dist/index.d.ts +2 -3
- package/dist/index.js +7 -6
- package/package.json +3 -3
- package/src/cache.ts +1 -1
- package/src/componentCache.ts +2 -2
- package/src/index.tsx +19 -12
package/dist/cache.d.ts
CHANGED
@@ -6,6 +6,12 @@ declare global {
|
|
6
6
|
__LOG: boolean;
|
7
7
|
}
|
8
8
|
}
|
9
|
+
/**
|
10
|
+
* Creates a copy of the provided value, ensuring any nested objects have their
|
11
|
+
* keys sorted such that equivalent values would have identical JSON.stringify
|
12
|
+
* results.
|
13
|
+
*/
|
14
|
+
export declare function stableCopy<T>(value: T): T;
|
9
15
|
type IsoResolver = IsographEntrypoint<any, any, any>;
|
10
16
|
export declare function getOrCreateCacheForArtifact<T>(artifact: IsographEntrypoint<any, any, T>, variables: object): ParentCache<PromiseWrapper<T>>;
|
11
17
|
declare let network: ((queryText: string, variables: object) => Promise<any>) | null;
|
package/dist/cache.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.SECOND_SPLIT_KEY = exports.FIRST_SPLIT_KEY = exports.getParentRecordKey = exports.onNextChange = exports.subscribe = exports.clearStore = exports.getStore = exports.ROOT_ID = exports.makeNetworkRequest = exports.setNetwork = exports.getOrCreateCacheForArtifact = void 0;
|
3
|
+
exports.SECOND_SPLIT_KEY = exports.FIRST_SPLIT_KEY = exports.getParentRecordKey = exports.onNextChange = exports.subscribe = exports.clearStore = exports.getStore = exports.ROOT_ID = exports.makeNetworkRequest = exports.setNetwork = exports.getOrCreateCacheForArtifact = exports.stableCopy = void 0;
|
4
4
|
const react_disposable_state_1 = require("@isograph/react-disposable-state");
|
5
5
|
const PromiseWrapper_1 = require("./PromiseWrapper");
|
6
6
|
const cache = {};
|
@@ -38,6 +38,7 @@ function stableCopy(value) {
|
|
38
38
|
}
|
39
39
|
return stable;
|
40
40
|
}
|
41
|
+
exports.stableCopy = stableCopy;
|
41
42
|
function getOrCreateCacheForArtifact(artifact, variables) {
|
42
43
|
const cacheKey = artifact.queryText + JSON.stringify(stableCopy(variables));
|
43
44
|
const factory = () => makeNetworkRequest(artifact, variables);
|
package/dist/componentCache.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
/// <reference types="react" />
|
2
2
|
import { ReaderArtifact, RefetchQueryArtifactWrapper } from "./index";
|
3
3
|
import { DataId } from "./cache";
|
4
|
-
export declare function getOrCreateCachedComponent(root: DataId, componentName: string,
|
4
|
+
export declare function getOrCreateCachedComponent(root: DataId, componentName: string, readerArtifact: ReaderArtifact<any, any, any>, variables: {
|
5
5
|
[key: string]: string;
|
6
6
|
}, resolverRefetchQueries: RefetchQueryArtifactWrapper[]): import("react").FC<any>;
|
package/dist/componentCache.js
CHANGED
@@ -2,9 +2,11 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.getOrCreateCachedComponent = void 0;
|
4
4
|
const index_1 = require("./index");
|
5
|
+
const cache_1 = require("./cache");
|
5
6
|
const cachedComponentsById = {};
|
6
|
-
function getOrCreateCachedComponent(root, componentName,
|
7
|
+
function getOrCreateCachedComponent(root, componentName, readerArtifact, variables, resolverRefetchQueries) {
|
7
8
|
var _a, _b, _c;
|
9
|
+
const stringifiedArgs = JSON.stringify((0, cache_1.stableCopy)(variables));
|
8
10
|
cachedComponentsById[root] = (_a = cachedComponentsById[root]) !== null && _a !== void 0 ? _a : {};
|
9
11
|
const componentsByName = cachedComponentsById[root];
|
10
12
|
componentsByName[componentName] = (_b = componentsByName[componentName]) !== null && _b !== void 0 ? _b : {};
|
package/dist/index.d.ts
CHANGED
@@ -90,9 +90,8 @@ export type FragmentReference<TReadFromStore extends Object, TResolverProps, TRe
|
|
90
90
|
} | null;
|
91
91
|
nestedRefetchQueries: RefetchQueryArtifactWrapper[];
|
92
92
|
};
|
93
|
-
export declare function
|
94
|
-
export declare function
|
95
|
-
export declare function useLazyReference<TReadFromStore extends Object, TResolverProps, TResolverResult>(entrypoint: IsographEntrypoint<TReadFromStore, TResolverProps, TResolverResult>, variables: object): {
|
93
|
+
export declare function iso<TResolverParameter, TResolverReturn = TResolverParameter>(_queryText: TemplateStringsArray): (x: (param: TResolverParameter) => TResolverReturn) => (param: TResolverParameter) => TResolverReturn;
|
94
|
+
export declare function useLazyReference<TReadFromStore extends Object, TResolverProps, TResolverResult>(entrypoint: IsographEntrypoint<TReadFromStore, TResolverProps, TResolverResult> | typeof iso, variables: object): {
|
96
95
|
queryReference: FragmentReference<TReadFromStore, TResolverProps, TResolverResult>;
|
97
96
|
};
|
98
97
|
export declare function read<TReadFromStore extends Object, TResolverProps, TResolverResult>(fragmentReference: FragmentReference<TReadFromStore, TResolverProps, TResolverResult>): TResolverResult;
|
package/dist/index.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.setMissingFieldHandler = exports.defaultMissingFieldHandler = exports.readButDoNotEvaluate = exports.read = exports.useLazyReference = exports.iso = exports.
|
3
|
+
exports.setMissingFieldHandler = exports.defaultMissingFieldHandler = exports.readButDoNotEvaluate = exports.read = exports.useLazyReference = exports.iso = exports.clearStore = exports.subscribe = exports.makeNetworkRequest = exports.setNetwork = 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");
|
@@ -9,10 +9,10 @@ Object.defineProperty(exports, "setNetwork", { enumerable: true, get: function (
|
|
9
9
|
Object.defineProperty(exports, "makeNetworkRequest", { enumerable: true, get: function () { return cache_2.makeNetworkRequest; } });
|
10
10
|
Object.defineProperty(exports, "subscribe", { enumerable: true, get: function () { return cache_2.subscribe; } });
|
11
11
|
Object.defineProperty(exports, "clearStore", { enumerable: true, get: function () { return cache_2.clearStore; } });
|
12
|
-
function
|
13
|
-
|
12
|
+
function assertIsEntrypoint(value) {
|
13
|
+
if (typeof value === "function")
|
14
|
+
throw new Error("Not a string");
|
14
15
|
}
|
15
|
-
exports.isoFetch = isoFetch;
|
16
16
|
function iso(_queryText) {
|
17
17
|
// The name `identity` here is a bit of a double entendre.
|
18
18
|
// First, it is the identity function, constrained to operate
|
@@ -27,6 +27,7 @@ function iso(_queryText) {
|
|
27
27
|
}
|
28
28
|
exports.iso = iso;
|
29
29
|
function useLazyReference(entrypoint, variables) {
|
30
|
+
assertIsEntrypoint(entrypoint);
|
30
31
|
// Typechecking fails here... TODO investigate
|
31
32
|
const cache = (0, cache_1.getOrCreateCacheForArtifact)(entrypoint, variables);
|
32
33
|
// TODO add comment explaining why we never use this value
|
@@ -57,7 +58,7 @@ function read(fragmentReference) {
|
|
57
58
|
}
|
58
59
|
else if (variant.kind === "Component") {
|
59
60
|
// @ts-ignore
|
60
|
-
return (0, componentCache_1.getOrCreateCachedComponent)(fragmentReference.root, variant.componentName,
|
61
|
+
return (0, componentCache_1.getOrCreateCachedComponent)(fragmentReference.root, variant.componentName, fragmentReference.readerArtifact, (_b = fragmentReference.variables) !== null && _b !== void 0 ? _b : {}, fragmentReference.nestedRefetchQueries);
|
61
62
|
}
|
62
63
|
// Why can't Typescript realize that this is unreachable??
|
63
64
|
throw new Error("This is unreachable");
|
@@ -248,7 +249,7 @@ function readData(ast, root, variables, nestedRefetchQueries) {
|
|
248
249
|
}
|
249
250
|
}
|
250
251
|
else if (variant.kind === "Component") {
|
251
|
-
target[field.alias] = (0, componentCache_1.getOrCreateCachedComponent)(root, variant.componentName,
|
252
|
+
target[field.alias] = (0, componentCache_1.getOrCreateCachedComponent)(root, variant.componentName, field.readerArtifact, variables, resolverRefetchQueries);
|
252
253
|
}
|
253
254
|
break;
|
254
255
|
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@isograph/react",
|
3
|
-
"version": "0.0.0-main-
|
3
|
+
"version": "0.0.0-main-d64c2813",
|
4
4
|
"description": "Use Isograph with React",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"types": "dist/index.d.ts",
|
@@ -15,8 +15,8 @@
|
|
15
15
|
"prepack": "yarn run test && yarn run compile"
|
16
16
|
},
|
17
17
|
"dependencies": {
|
18
|
-
"@isograph/disposable-types": "0.0.0-main-
|
19
|
-
"@isograph/react-disposable-state": "0.0.0-main-
|
18
|
+
"@isograph/disposable-types": "0.0.0-main-d64c2813",
|
19
|
+
"@isograph/react-disposable-state": "0.0.0-main-d64c2813",
|
20
20
|
"react": "^18.2.0"
|
21
21
|
},
|
22
22
|
"devDependencies": {
|
package/src/cache.ts
CHANGED
@@ -45,7 +45,7 @@ function getOrCreateCache<T>(
|
|
45
45
|
* keys sorted such that equivalent values would have identical JSON.stringify
|
46
46
|
* results.
|
47
47
|
*/
|
48
|
-
function stableCopy<T>(value: T): T {
|
48
|
+
export function stableCopy<T>(value: T): T {
|
49
49
|
if (!value || typeof value !== "object") {
|
50
50
|
return value;
|
51
51
|
}
|
package/src/componentCache.ts
CHANGED
@@ -3,7 +3,7 @@ import {
|
|
3
3
|
RefetchQueryArtifactWrapper,
|
4
4
|
readButDoNotEvaluate,
|
5
5
|
} from "./index";
|
6
|
-
import { DataId } from "./cache";
|
6
|
+
import { DataId, stableCopy } from "./cache";
|
7
7
|
|
8
8
|
type ComponentName = string;
|
9
9
|
type StringifiedArgs = string;
|
@@ -15,11 +15,11 @@ const cachedComponentsById: {
|
|
15
15
|
export function getOrCreateCachedComponent(
|
16
16
|
root: DataId,
|
17
17
|
componentName: string,
|
18
|
-
stringifiedArgs: string,
|
19
18
|
readerArtifact: ReaderArtifact<any, any, any>,
|
20
19
|
variables: { [key: string]: string },
|
21
20
|
resolverRefetchQueries: RefetchQueryArtifactWrapper[]
|
22
21
|
) {
|
22
|
+
const stringifiedArgs = JSON.stringify(stableCopy(variables));
|
23
23
|
cachedComponentsById[root] = cachedComponentsById[root] ?? {};
|
24
24
|
const componentsByName = cachedComponentsById[root];
|
25
25
|
componentsByName[componentName] = componentsByName[componentName] ?? {};
|
package/src/index.tsx
CHANGED
@@ -161,16 +161,26 @@ export type FragmentReference<
|
|
161
161
|
nestedRefetchQueries: RefetchQueryArtifactWrapper[];
|
162
162
|
};
|
163
163
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
164
|
+
function assertIsEntrypoint<
|
165
|
+
TReadFromStore extends Object,
|
166
|
+
TResolverProps,
|
167
|
+
TResolverResult
|
168
|
+
>(
|
169
|
+
value:
|
170
|
+
| IsographEntrypoint<TReadFromStore, TResolverProps, TResolverResult>
|
171
|
+
| typeof iso
|
172
|
+
): asserts value is IsographEntrypoint<
|
173
|
+
TReadFromStore,
|
174
|
+
TResolverProps,
|
175
|
+
TResolverResult
|
176
|
+
> {
|
177
|
+
if (typeof value === "function") throw new Error("Not a string");
|
168
178
|
}
|
169
179
|
|
170
180
|
export function iso<TResolverParameter, TResolverReturn = TResolverParameter>(
|
171
181
|
_queryText: TemplateStringsArray
|
172
182
|
): (
|
173
|
-
x: (
|
183
|
+
x: (param: TResolverParameter) => TResolverReturn
|
174
184
|
) => (param: TResolverParameter) => TResolverReturn {
|
175
185
|
// The name `identity` here is a bit of a double entendre.
|
176
186
|
// First, it is the identity function, constrained to operate
|
@@ -192,11 +202,9 @@ export function useLazyReference<
|
|
192
202
|
TResolverProps,
|
193
203
|
TResolverResult
|
194
204
|
>(
|
195
|
-
entrypoint:
|
196
|
-
TReadFromStore,
|
197
|
-
|
198
|
-
TResolverResult
|
199
|
-
>,
|
205
|
+
entrypoint:
|
206
|
+
| IsographEntrypoint<TReadFromStore, TResolverProps, TResolverResult>
|
207
|
+
| typeof iso,
|
200
208
|
variables: object
|
201
209
|
): {
|
202
210
|
queryReference: FragmentReference<
|
@@ -205,6 +213,7 @@ export function useLazyReference<
|
|
205
213
|
TResolverResult
|
206
214
|
>;
|
207
215
|
} {
|
216
|
+
assertIsEntrypoint(entrypoint);
|
208
217
|
// Typechecking fails here... TODO investigate
|
209
218
|
const cache = getOrCreateCacheForArtifact<TResolverResult>(
|
210
219
|
entrypoint,
|
@@ -256,7 +265,6 @@ export function read<
|
|
256
265
|
return getOrCreateCachedComponent(
|
257
266
|
fragmentReference.root,
|
258
267
|
variant.componentName,
|
259
|
-
"TODO",
|
260
268
|
fragmentReference.readerArtifact,
|
261
269
|
fragmentReference.variables ?? {},
|
262
270
|
fragmentReference.nestedRefetchQueries
|
@@ -519,7 +527,6 @@ function readData<TReadFromStore>(
|
|
519
527
|
target[field.alias] = getOrCreateCachedComponent(
|
520
528
|
root,
|
521
529
|
variant.componentName,
|
522
|
-
"TODO",
|
523
530
|
field.readerArtifact,
|
524
531
|
variables,
|
525
532
|
resolverRefetchQueries
|