@isograph/react 0.0.0-main-f49c67bb → 0.0.0-main-35c553b5
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/{context.d.ts → IsographEnvironment.d.ts} +22 -0
- package/dist/{context.js → IsographEnvironment.js} +19 -2
- package/dist/cache.d.ts +3 -3
- package/dist/cache.js +17 -19
- package/dist/componentCache.d.ts +2 -2
- package/dist/componentCache.js +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +14 -12
- package/package.json +3 -3
- package/src/{context.tsx → IsographEnvironment.tsx} +37 -1
- package/src/cache.tsx +19 -19
- package/src/componentCache.ts +2 -9
- package/src/index.tsx +6 -4
@@ -1,10 +1,27 @@
|
|
1
1
|
import { ReactNode } from 'react';
|
2
2
|
import * as React from 'react';
|
3
|
+
import { ParentCache } from '@isograph/isograph-react-disposable-state';
|
3
4
|
export declare const IsographEnvironmentContext: React.Context<IsographEnvironment | null>;
|
5
|
+
type ComponentName = string;
|
6
|
+
type StringifiedArgs = string;
|
7
|
+
type ComponentCache = {
|
8
|
+
[key: DataId]: {
|
9
|
+
[key: ComponentName]: {
|
10
|
+
[key: StringifiedArgs]: React.FC<any>;
|
11
|
+
};
|
12
|
+
};
|
13
|
+
};
|
14
|
+
export type Subscriptions = Set<() => void>;
|
15
|
+
type SuspenseCache = {
|
16
|
+
[index: string]: ParentCache<any>;
|
17
|
+
};
|
4
18
|
export type IsographEnvironment = {
|
5
19
|
store: IsographStore;
|
6
20
|
networkFunction: IsographNetworkFunction;
|
7
21
|
missingFieldHandler: MissingFieldHandler | null;
|
22
|
+
componentCache: ComponentCache;
|
23
|
+
subscriptions: Subscriptions;
|
24
|
+
suspenseCache: SuspenseCache;
|
8
25
|
};
|
9
26
|
export type MissingFieldHandler = (storeRecord: StoreRecord, root: DataId, fieldName: string, arguments_: {
|
10
27
|
[index: string]: any;
|
@@ -32,3 +49,8 @@ export type IsographEnvironmentProviderProps = {
|
|
32
49
|
};
|
33
50
|
export declare function IsographEnvironmentProvider({ environment, children, }: IsographEnvironmentProviderProps): React.JSX.Element;
|
34
51
|
export declare function useIsographEnvironment(): IsographEnvironment;
|
52
|
+
export declare function createIsographEnvironment(store: IsographStore, networkFunction: IsographNetworkFunction, missingFieldHandler?: MissingFieldHandler): IsographEnvironment;
|
53
|
+
export declare function createIsographStore(): {
|
54
|
+
__ROOT: {};
|
55
|
+
};
|
56
|
+
export {};
|
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
23
23
|
return result;
|
24
24
|
};
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
26
|
-
exports.useIsographEnvironment = exports.IsographEnvironmentProvider = exports.ROOT_ID = exports.IsographEnvironmentContext = void 0;
|
26
|
+
exports.createIsographStore = exports.createIsographEnvironment = exports.useIsographEnvironment = exports.IsographEnvironmentProvider = exports.ROOT_ID = exports.IsographEnvironmentContext = void 0;
|
27
27
|
const react_1 = require("react");
|
28
28
|
const React = __importStar(require("react"));
|
29
29
|
const cache_1 = require("./cache");
|
@@ -32,7 +32,7 @@ exports.ROOT_ID = '__ROOT';
|
|
32
32
|
function IsographEnvironmentProvider({ environment, children, }) {
|
33
33
|
const [, setState] = React.useState();
|
34
34
|
React.useEffect(() => {
|
35
|
-
return (0, cache_1.subscribe)(() => setState({}));
|
35
|
+
return (0, cache_1.subscribe)(environment, () => setState({}));
|
36
36
|
}, []);
|
37
37
|
return (React.createElement(exports.IsographEnvironmentContext.Provider, { value: environment }, children));
|
38
38
|
}
|
@@ -46,3 +46,20 @@ function useIsographEnvironment() {
|
|
46
46
|
return context;
|
47
47
|
}
|
48
48
|
exports.useIsographEnvironment = useIsographEnvironment;
|
49
|
+
function createIsographEnvironment(store, networkFunction, missingFieldHandler) {
|
50
|
+
return {
|
51
|
+
store,
|
52
|
+
networkFunction,
|
53
|
+
missingFieldHandler: missingFieldHandler !== null && missingFieldHandler !== void 0 ? missingFieldHandler : null,
|
54
|
+
componentCache: {},
|
55
|
+
subscriptions: new Set(),
|
56
|
+
suspenseCache: {},
|
57
|
+
};
|
58
|
+
}
|
59
|
+
exports.createIsographEnvironment = createIsographEnvironment;
|
60
|
+
function createIsographStore() {
|
61
|
+
return {
|
62
|
+
[exports.ROOT_ID]: {},
|
63
|
+
};
|
64
|
+
}
|
65
|
+
exports.createIsographStore = createIsographStore;
|
package/dist/cache.d.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
import { ItemCleanupPair, ParentCache } from '@isograph/react-disposable-state';
|
2
2
|
import { PromiseWrapper } from './PromiseWrapper';
|
3
3
|
import { IsographEntrypoint, NormalizationLinkedField, NormalizationScalarField, ReaderLinkedField, ReaderScalarField } from './index';
|
4
|
-
import { type IsographEnvironment } from './
|
4
|
+
import { type IsographEnvironment } from './IsographEnvironment';
|
5
5
|
declare global {
|
6
6
|
interface Window {
|
7
7
|
__LOG: boolean;
|
@@ -16,8 +16,8 @@ export declare function stableCopy<T>(value: T): T;
|
|
16
16
|
type IsoResolver = IsographEntrypoint<any, any, any>;
|
17
17
|
export declare function getOrCreateCacheForArtifact<T>(environment: IsographEnvironment, artifact: IsographEntrypoint<any, any, T>, variables: object): ParentCache<PromiseWrapper<T>>;
|
18
18
|
export declare function makeNetworkRequest<T>(environment: IsographEnvironment, artifact: IsoResolver, variables: object): ItemCleanupPair<PromiseWrapper<T>>;
|
19
|
-
export declare function subscribe(callback: () => void): () => void;
|
20
|
-
export declare function onNextChange(): Promise<void>;
|
19
|
+
export declare function subscribe(environment: IsographEnvironment, callback: () => void): () => void;
|
20
|
+
export declare function onNextChange(environment: IsographEnvironment): Promise<void>;
|
21
21
|
export declare function getParentRecordKey(astNode: NormalizationLinkedField | NormalizationScalarField | ReaderLinkedField | ReaderScalarField, variables: {
|
22
22
|
[index: string]: string;
|
23
23
|
}): string;
|
package/dist/cache.js
CHANGED
@@ -3,20 +3,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SECOND_SPLIT_KEY = exports.FIRST_SPLIT_KEY = exports.getParentRecordKey = exports.onNextChange = exports.subscribe = exports.makeNetworkRequest = 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
|
-
const
|
7
|
-
|
8
|
-
function getOrCreateCache(index, factory) {
|
6
|
+
const IsographEnvironment_1 = require("./IsographEnvironment");
|
7
|
+
function getOrCreateCache(environment, index, factory) {
|
9
8
|
if (typeof window !== 'undefined' && window.__LOG) {
|
10
9
|
console.log('getting cache for', {
|
11
10
|
index,
|
12
|
-
cache: Object.keys(
|
13
|
-
found: !!
|
11
|
+
cache: Object.keys(environment.suspenseCache),
|
12
|
+
found: !!environment.suspenseCache[index],
|
14
13
|
});
|
15
14
|
}
|
16
|
-
if (
|
17
|
-
|
15
|
+
if (environment.suspenseCache[index] == null) {
|
16
|
+
environment.suspenseCache[index] = new react_disposable_state_1.ParentCache(factory);
|
18
17
|
}
|
19
|
-
return
|
18
|
+
return environment.suspenseCache[index];
|
20
19
|
}
|
21
20
|
/**
|
22
21
|
* Creates a copy of the provided value, ensuring any nested objects have their
|
@@ -43,7 +42,7 @@ exports.stableCopy = stableCopy;
|
|
43
42
|
function getOrCreateCacheForArtifact(environment, artifact, variables) {
|
44
43
|
const cacheKey = artifact.queryText + JSON.stringify(stableCopy(variables));
|
45
44
|
const factory = () => makeNetworkRequest(environment, artifact, variables);
|
46
|
-
return getOrCreateCache(cacheKey, factory);
|
45
|
+
return getOrCreateCache(environment, cacheKey, factory);
|
47
46
|
}
|
48
47
|
exports.getOrCreateCacheForArtifact = getOrCreateCacheForArtifact;
|
49
48
|
function makeNetworkRequest(environment, artifact, variables) {
|
@@ -73,29 +72,28 @@ function normalizeData(environment, normalizationAst, networkResponse, variables
|
|
73
72
|
if (typeof window !== 'undefined' && window.__LOG) {
|
74
73
|
console.log('about to normalize', normalizationAst, networkResponse, variables);
|
75
74
|
}
|
76
|
-
normalizeDataIntoRecord(environment, normalizationAst, networkResponse, environment.store.__ROOT,
|
75
|
+
normalizeDataIntoRecord(environment, normalizationAst, networkResponse, environment.store.__ROOT, IsographEnvironment_1.ROOT_ID, variables, nestedRefetchQueries);
|
77
76
|
if (typeof window !== 'undefined' && window.__LOG) {
|
78
77
|
console.log('after normalization', { store: environment.store });
|
79
78
|
}
|
80
|
-
callSubscriptions();
|
79
|
+
callSubscriptions(environment);
|
81
80
|
}
|
82
|
-
function subscribe(callback) {
|
83
|
-
subscriptions.add(callback);
|
84
|
-
return () => subscriptions.delete(callback);
|
81
|
+
function subscribe(environment, callback) {
|
82
|
+
environment.subscriptions.add(callback);
|
83
|
+
return () => environment.subscriptions.delete(callback);
|
85
84
|
}
|
86
85
|
exports.subscribe = subscribe;
|
87
|
-
function onNextChange() {
|
86
|
+
function onNextChange(environment) {
|
88
87
|
return new Promise((resolve) => {
|
89
|
-
const unsubscribe = subscribe(() => {
|
88
|
+
const unsubscribe = subscribe(environment, () => {
|
90
89
|
unsubscribe();
|
91
90
|
resolve();
|
92
91
|
});
|
93
92
|
});
|
94
93
|
}
|
95
94
|
exports.onNextChange = onNextChange;
|
96
|
-
|
97
|
-
|
98
|
-
subscriptions.forEach((callback) => callback());
|
95
|
+
function callSubscriptions(environment) {
|
96
|
+
environment.subscriptions.forEach((callback) => callback());
|
99
97
|
}
|
100
98
|
/**
|
101
99
|
* Mutate targetParentRecord according to the normalizationAst and networkResponseParentRecord.
|
package/dist/componentCache.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
/// <reference types="react" />
|
2
|
-
import {
|
3
|
-
import { IsographEnvironment } from '
|
2
|
+
import { ReaderArtifact, RefetchQueryArtifactWrapper } from './index';
|
3
|
+
import { IsographEnvironment, DataId } from './IsographEnvironment';
|
4
4
|
export declare function getOrCreateCachedComponent(environment: IsographEnvironment, 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
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getOrCreateCachedComponent = void 0;
|
4
4
|
const index_1 = require("./index");
|
5
5
|
const cache_1 = require("./cache");
|
6
|
-
const cachedComponentsById = {};
|
7
6
|
function getOrCreateCachedComponent(environment, root, componentName, readerArtifact, variables, resolverRefetchQueries) {
|
8
7
|
var _a, _b, _c;
|
8
|
+
const cachedComponentsById = environment.componentCache;
|
9
9
|
const stringifiedArgs = JSON.stringify((0, cache_1.stableCopy)(variables));
|
10
10
|
cachedComponentsById[root] = (_a = cachedComponentsById[root]) !== null && _a !== void 0 ? _a : {};
|
11
11
|
const componentsByName = cachedComponentsById[root];
|
package/dist/index.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
import { DataId, IsographEnvironment, Link, StoreRecord } from './
|
1
|
+
import { DataId, IsographEnvironment, Link, StoreRecord } from './IsographEnvironment';
|
2
2
|
export { makeNetworkRequest, subscribe } from './cache';
|
3
|
-
export { IsographEnvironmentContext, ROOT_ID, type DataId, type DataTypeValue, type IsographEnvironment, IsographEnvironmentProvider, type IsographEnvironmentProviderProps, type IsographNetworkFunction, type IsographStore, type Link, type StoreRecord, useIsographEnvironment, } from './
|
3
|
+
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';
|
4
4
|
export { iso } from './iso';
|
5
5
|
export type IsographEntrypoint<TReadFromStore extends Object, TResolverProps, TResolverResult> = {
|
6
6
|
kind: 'Entrypoint';
|
package/dist/index.js
CHANGED
@@ -1,18 +1,20 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.defaultMissingFieldHandler = exports.readButDoNotEvaluate = exports.read = exports.useRead = exports.useLazyReference = exports.iso = exports.useIsographEnvironment = exports.IsographEnvironmentProvider = exports.ROOT_ID = exports.IsographEnvironmentContext = exports.subscribe = exports.makeNetworkRequest = void 0;
|
3
|
+
exports.defaultMissingFieldHandler = exports.readButDoNotEvaluate = exports.read = exports.useRead = exports.useLazyReference = exports.iso = exports.createIsographStore = exports.createIsographEnvironment = exports.useIsographEnvironment = exports.IsographEnvironmentProvider = exports.ROOT_ID = exports.IsographEnvironmentContext = exports.subscribe = exports.makeNetworkRequest = 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
|
-
const
|
7
|
+
const IsographEnvironment_1 = require("./IsographEnvironment");
|
8
8
|
var cache_2 = require("./cache");
|
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
|
-
var
|
12
|
-
Object.defineProperty(exports, "IsographEnvironmentContext", { enumerable: true, get: function () { return
|
13
|
-
Object.defineProperty(exports, "ROOT_ID", { enumerable: true, get: function () { return
|
14
|
-
Object.defineProperty(exports, "IsographEnvironmentProvider", { enumerable: true, get: function () { return
|
15
|
-
Object.defineProperty(exports, "useIsographEnvironment", { enumerable: true, get: function () { return
|
11
|
+
var IsographEnvironment_2 = require("./IsographEnvironment");
|
12
|
+
Object.defineProperty(exports, "IsographEnvironmentContext", { enumerable: true, get: function () { return IsographEnvironment_2.IsographEnvironmentContext; } });
|
13
|
+
Object.defineProperty(exports, "ROOT_ID", { enumerable: true, get: function () { return IsographEnvironment_2.ROOT_ID; } });
|
14
|
+
Object.defineProperty(exports, "IsographEnvironmentProvider", { enumerable: true, get: function () { return IsographEnvironment_2.IsographEnvironmentProvider; } });
|
15
|
+
Object.defineProperty(exports, "useIsographEnvironment", { enumerable: true, get: function () { return IsographEnvironment_2.useIsographEnvironment; } });
|
16
|
+
Object.defineProperty(exports, "createIsographEnvironment", { enumerable: true, get: function () { return IsographEnvironment_2.createIsographEnvironment; } });
|
17
|
+
Object.defineProperty(exports, "createIsographStore", { enumerable: true, get: function () { return IsographEnvironment_2.createIsographStore; } });
|
16
18
|
var iso_1 = require("./iso");
|
17
19
|
Object.defineProperty(exports, "iso", { enumerable: true, get: function () { return iso_1.iso; } });
|
18
20
|
function assertIsEntrypoint(value) {
|
@@ -24,7 +26,7 @@ function assertIsEntrypoint(value) {
|
|
24
26
|
// We cannot write TEntrypoint extends IsographEntrypoint<never, never, never>, or else
|
25
27
|
// any actual Entrypoint we pass will not be valid.
|
26
28
|
function useLazyReference(entrypoint, variables) {
|
27
|
-
const environment = (0,
|
29
|
+
const environment = (0, IsographEnvironment_1.useIsographEnvironment)();
|
28
30
|
assertIsEntrypoint(entrypoint);
|
29
31
|
// Typechecking fails here... TODO investigate
|
30
32
|
const cache = (0, cache_1.getOrCreateCacheForArtifact)(environment, entrypoint, variables);
|
@@ -35,7 +37,7 @@ function useLazyReference(entrypoint, variables) {
|
|
35
37
|
queryReference: {
|
36
38
|
kind: 'FragmentReference',
|
37
39
|
readerArtifact: entrypoint.readerArtifact,
|
38
|
-
root:
|
40
|
+
root: IsographEnvironment_1.ROOT_ID,
|
39
41
|
variables,
|
40
42
|
nestedRefetchQueries: entrypoint.nestedRefetchQueries,
|
41
43
|
},
|
@@ -43,7 +45,7 @@ function useLazyReference(entrypoint, variables) {
|
|
43
45
|
}
|
44
46
|
exports.useLazyReference = useLazyReference;
|
45
47
|
function useRead(fragmentReference) {
|
46
|
-
const environment = (0,
|
48
|
+
const environment = (0, IsographEnvironment_1.useIsographEnvironment)();
|
47
49
|
return read(environment, fragmentReference);
|
48
50
|
}
|
49
51
|
exports.useRead = useRead;
|
@@ -53,7 +55,7 @@ function read(environment, fragmentReference) {
|
|
53
55
|
if (variant.kind === 'Eager') {
|
54
56
|
const data = readData(environment, fragmentReference.readerArtifact.readerAst, fragmentReference.root, (_a = fragmentReference.variables) !== null && _a !== void 0 ? _a : {}, fragmentReference.nestedRefetchQueries);
|
55
57
|
if (data.kind === 'MissingData') {
|
56
|
-
throw (0, cache_1.onNextChange)();
|
58
|
+
throw (0, cache_1.onNextChange)(environment);
|
57
59
|
}
|
58
60
|
else {
|
59
61
|
return fragmentReference.readerArtifact.resolver(data.data);
|
@@ -74,7 +76,7 @@ function readButDoNotEvaluate(environment, reference) {
|
|
74
76
|
console.log('done reading', { response });
|
75
77
|
}
|
76
78
|
if (response.kind === 'MissingData') {
|
77
|
-
throw (0, cache_1.onNextChange)();
|
79
|
+
throw (0, cache_1.onNextChange)(environment);
|
78
80
|
}
|
79
81
|
else {
|
80
82
|
return response.data;
|
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-35c553b5",
|
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-35c553b5",
|
19
|
+
"@isograph/react-disposable-state": "0.0.0-main-35c553b5",
|
20
20
|
"react": "^18.2.0"
|
21
21
|
},
|
22
22
|
"devDependencies": {
|
@@ -1,14 +1,29 @@
|
|
1
1
|
import { ReactNode, createContext, useContext } from 'react';
|
2
2
|
import * as React from 'react';
|
3
3
|
import { subscribe } from './cache';
|
4
|
+
import { ParentCache } from '@isograph/isograph-react-disposable-state';
|
4
5
|
|
5
6
|
export const IsographEnvironmentContext =
|
6
7
|
createContext<IsographEnvironment | null>(null);
|
7
8
|
|
9
|
+
type ComponentName = string;
|
10
|
+
type StringifiedArgs = string;
|
11
|
+
type ComponentCache = {
|
12
|
+
[key: DataId]: {
|
13
|
+
[key: ComponentName]: { [key: StringifiedArgs]: React.FC<any> };
|
14
|
+
};
|
15
|
+
};
|
16
|
+
|
17
|
+
export type Subscriptions = Set<() => void>;
|
18
|
+
type SuspenseCache = { [index: string]: ParentCache<any> };
|
19
|
+
|
8
20
|
export type IsographEnvironment = {
|
9
21
|
store: IsographStore;
|
10
22
|
networkFunction: IsographNetworkFunction;
|
11
23
|
missingFieldHandler: MissingFieldHandler | null;
|
24
|
+
componentCache: ComponentCache;
|
25
|
+
subscriptions: Subscriptions;
|
26
|
+
suspenseCache: SuspenseCache;
|
12
27
|
};
|
13
28
|
|
14
29
|
export type MissingFieldHandler = (
|
@@ -68,7 +83,7 @@ export function IsographEnvironmentProvider({
|
|
68
83
|
}: IsographEnvironmentProviderProps) {
|
69
84
|
const [, setState] = React.useState<object | void>();
|
70
85
|
React.useEffect(() => {
|
71
|
-
return subscribe(() => setState({}));
|
86
|
+
return subscribe(environment, () => setState({}));
|
72
87
|
}, []);
|
73
88
|
|
74
89
|
return (
|
@@ -88,3 +103,24 @@ export function useIsographEnvironment(): IsographEnvironment {
|
|
88
103
|
}
|
89
104
|
return context;
|
90
105
|
}
|
106
|
+
|
107
|
+
export function createIsographEnvironment(
|
108
|
+
store: IsographStore,
|
109
|
+
networkFunction: IsographNetworkFunction,
|
110
|
+
missingFieldHandler?: MissingFieldHandler,
|
111
|
+
): IsographEnvironment {
|
112
|
+
return {
|
113
|
+
store,
|
114
|
+
networkFunction,
|
115
|
+
missingFieldHandler: missingFieldHandler ?? null,
|
116
|
+
componentCache: {},
|
117
|
+
subscriptions: new Set(),
|
118
|
+
suspenseCache: {},
|
119
|
+
};
|
120
|
+
}
|
121
|
+
|
122
|
+
export function createIsographStore() {
|
123
|
+
return {
|
124
|
+
[ROOT_ID]: {},
|
125
|
+
};
|
126
|
+
}
|
package/src/cache.tsx
CHANGED
@@ -21,7 +21,7 @@ import {
|
|
21
21
|
StoreRecord,
|
22
22
|
Link,
|
23
23
|
type IsographEnvironment,
|
24
|
-
} from './
|
24
|
+
} from './IsographEnvironment';
|
25
25
|
|
26
26
|
declare global {
|
27
27
|
interface Window {
|
@@ -29,24 +29,23 @@ declare global {
|
|
29
29
|
}
|
30
30
|
}
|
31
31
|
|
32
|
-
const cache: { [index: string]: ParentCache<any> } = {};
|
33
|
-
|
34
32
|
function getOrCreateCache<T>(
|
33
|
+
environment: IsographEnvironment,
|
35
34
|
index: string,
|
36
35
|
factory: Factory<T>,
|
37
36
|
): ParentCache<T> {
|
38
37
|
if (typeof window !== 'undefined' && window.__LOG) {
|
39
38
|
console.log('getting cache for', {
|
40
39
|
index,
|
41
|
-
cache: Object.keys(
|
42
|
-
found: !!
|
40
|
+
cache: Object.keys(environment.suspenseCache),
|
41
|
+
found: !!environment.suspenseCache[index],
|
43
42
|
});
|
44
43
|
}
|
45
|
-
if (
|
46
|
-
|
44
|
+
if (environment.suspenseCache[index] == null) {
|
45
|
+
environment.suspenseCache[index] = new ParentCache(factory);
|
47
46
|
}
|
48
47
|
|
49
|
-
return
|
48
|
+
return environment.suspenseCache[index];
|
50
49
|
}
|
51
50
|
|
52
51
|
/**
|
@@ -81,7 +80,7 @@ export function getOrCreateCacheForArtifact<T>(
|
|
81
80
|
const cacheKey = artifact.queryText + JSON.stringify(stableCopy(variables));
|
82
81
|
const factory: Factory<PromiseWrapper<T>> = () =>
|
83
82
|
makeNetworkRequest<T>(environment, artifact, variables);
|
84
|
-
return getOrCreateCache<PromiseWrapper<T>>(cacheKey, factory);
|
83
|
+
return getOrCreateCache<PromiseWrapper<T>>(environment, cacheKey, factory);
|
85
84
|
}
|
86
85
|
|
87
86
|
export function makeNetworkRequest<T>(
|
@@ -160,27 +159,28 @@ function normalizeData(
|
|
160
159
|
if (typeof window !== 'undefined' && window.__LOG) {
|
161
160
|
console.log('after normalization', { store: environment.store });
|
162
161
|
}
|
163
|
-
callSubscriptions();
|
162
|
+
callSubscriptions(environment);
|
164
163
|
}
|
165
164
|
|
166
|
-
export function subscribe(
|
167
|
-
|
168
|
-
|
165
|
+
export function subscribe(
|
166
|
+
environment: IsographEnvironment,
|
167
|
+
callback: () => void,
|
168
|
+
): () => void {
|
169
|
+
environment.subscriptions.add(callback);
|
170
|
+
return () => environment.subscriptions.delete(callback);
|
169
171
|
}
|
170
172
|
|
171
|
-
export function onNextChange(): Promise<void> {
|
173
|
+
export function onNextChange(environment: IsographEnvironment): Promise<void> {
|
172
174
|
return new Promise((resolve) => {
|
173
|
-
const unsubscribe = subscribe(() => {
|
175
|
+
const unsubscribe = subscribe(environment, () => {
|
174
176
|
unsubscribe();
|
175
177
|
resolve();
|
176
178
|
});
|
177
179
|
});
|
178
180
|
}
|
179
181
|
|
180
|
-
|
181
|
-
|
182
|
-
function callSubscriptions() {
|
183
|
-
subscriptions.forEach((callback) => callback());
|
182
|
+
function callSubscriptions(environment: IsographEnvironment) {
|
183
|
+
environment.subscriptions.forEach((callback) => callback());
|
184
184
|
}
|
185
185
|
|
186
186
|
/**
|
package/src/componentCache.ts
CHANGED
@@ -1,19 +1,11 @@
|
|
1
1
|
import {
|
2
|
-
DataId,
|
3
2
|
ReaderArtifact,
|
4
3
|
RefetchQueryArtifactWrapper,
|
5
4
|
readButDoNotEvaluate,
|
6
5
|
} from './index';
|
7
6
|
import { stableCopy } from './cache';
|
8
|
-
import { IsographEnvironment } from '
|
7
|
+
import { IsographEnvironment, DataId } from './IsographEnvironment';
|
9
8
|
|
10
|
-
type ComponentName = string;
|
11
|
-
type StringifiedArgs = string;
|
12
|
-
const cachedComponentsById: {
|
13
|
-
[key: DataId]: {
|
14
|
-
[key: ComponentName]: { [key: StringifiedArgs]: React.FC<any> };
|
15
|
-
};
|
16
|
-
} = {};
|
17
9
|
export function getOrCreateCachedComponent(
|
18
10
|
environment: IsographEnvironment,
|
19
11
|
root: DataId,
|
@@ -22,6 +14,7 @@ export function getOrCreateCachedComponent(
|
|
22
14
|
variables: { [key: string]: string },
|
23
15
|
resolverRefetchQueries: RefetchQueryArtifactWrapper[],
|
24
16
|
) {
|
17
|
+
const cachedComponentsById = environment.componentCache;
|
25
18
|
const stringifiedArgs = JSON.stringify(stableCopy(variables));
|
26
19
|
cachedComponentsById[root] = cachedComponentsById[root] ?? {};
|
27
20
|
const componentsByName = cachedComponentsById[root];
|
package/src/index.tsx
CHANGED
@@ -14,7 +14,7 @@ import {
|
|
14
14
|
ROOT_ID,
|
15
15
|
StoreRecord,
|
16
16
|
useIsographEnvironment,
|
17
|
-
} from './
|
17
|
+
} from './IsographEnvironment';
|
18
18
|
|
19
19
|
export { makeNetworkRequest, subscribe } from './cache';
|
20
20
|
export {
|
@@ -30,7 +30,9 @@ export {
|
|
30
30
|
type Link,
|
31
31
|
type StoreRecord,
|
32
32
|
useIsographEnvironment,
|
33
|
-
|
33
|
+
createIsographEnvironment,
|
34
|
+
createIsographStore,
|
35
|
+
} from './IsographEnvironment';
|
34
36
|
|
35
37
|
export { iso } from './iso';
|
36
38
|
|
@@ -285,7 +287,7 @@ export function read<
|
|
285
287
|
fragmentReference.nestedRefetchQueries,
|
286
288
|
);
|
287
289
|
if (data.kind === 'MissingData') {
|
288
|
-
throw onNextChange();
|
290
|
+
throw onNextChange(environment);
|
289
291
|
} else {
|
290
292
|
return fragmentReference.readerArtifact.resolver(data.data);
|
291
293
|
}
|
@@ -319,7 +321,7 @@ export function readButDoNotEvaluate<TReadFromStore extends Object>(
|
|
319
321
|
console.log('done reading', { response });
|
320
322
|
}
|
321
323
|
if (response.kind === 'MissingData') {
|
322
|
-
throw onNextChange();
|
324
|
+
throw onNextChange(environment);
|
323
325
|
} else {
|
324
326
|
return response.data;
|
325
327
|
}
|