@khanacademy/wonder-blocks-data 10.0.5 → 10.1.1
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/CHANGELOG.md +38 -0
- package/dist/components/data.d.ts +52 -0
- package/dist/components/data.js.flow +63 -0
- package/dist/components/gql-router.d.ts +24 -0
- package/dist/components/gql-router.js.flow +33 -0
- package/dist/components/intercept-context.d.ts +10 -0
- package/dist/components/intercept-context.js.flow +19 -0
- package/dist/components/intercept-requests.d.ts +42 -0
- package/dist/components/intercept-requests.js.flow +51 -0
- package/dist/components/track-data.d.ts +11 -0
- package/dist/components/track-data.js.flow +18 -0
- package/dist/es/index.js +196 -214
- package/dist/hooks/use-cached-effect.d.ts +70 -0
- package/dist/hooks/use-cached-effect.js.flow +85 -0
- package/dist/hooks/use-gql-router-context.d.ts +5 -0
- package/dist/hooks/use-gql-router-context.js.flow +15 -0
- package/dist/hooks/use-gql.d.ts +12 -0
- package/dist/hooks/use-gql.js.flow +29 -0
- package/dist/hooks/use-hydratable-effect.d.ts +102 -0
- package/dist/hooks/use-hydratable-effect.js.flow +125 -0
- package/dist/hooks/use-request-interception.d.ts +14 -0
- package/dist/hooks/use-request-interception.js.flow +25 -0
- package/dist/hooks/use-server-effect.d.ts +39 -0
- package/dist/hooks/use-server-effect.js.flow +51 -0
- package/dist/hooks/use-shared-cache.d.ts +32 -0
- package/dist/hooks/use-shared-cache.js.flow +43 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +198 -219
- package/dist/index.js.flow +48 -2
- package/dist/util/data-error.d.ts +45 -0
- package/dist/util/data-error.js.flow +64 -0
- package/dist/util/get-gql-data-from-response.d.ts +4 -0
- package/dist/util/get-gql-data-from-response.js.flow +13 -0
- package/dist/util/get-gql-request-id.d.ts +5 -0
- package/dist/util/get-gql-request-id.js.flow +20 -0
- package/dist/util/gql-error.d.ts +28 -0
- package/dist/util/gql-error.js.flow +43 -0
- package/dist/util/gql-router-context.d.ts +3 -0
- package/dist/util/gql-router-context.js.flow +10 -0
- package/dist/util/gql-types.d.ts +34 -0
- package/dist/util/gql-types.js.flow +53 -0
- package/dist/util/graphql-document-node-parser.d.ts +18 -0
- package/dist/util/graphql-document-node-parser.js.flow +31 -0
- package/dist/util/graphql-types.d.ts +19 -0
- package/dist/util/graphql-types.js.flow +30 -0
- package/dist/util/hydration-cache-api.d.ts +17 -0
- package/dist/util/hydration-cache-api.js.flow +30 -0
- package/dist/util/merge-gql-context.d.ts +8 -0
- package/dist/util/merge-gql-context.js.flow +19 -0
- package/dist/util/purge-caches.d.ts +8 -0
- package/dist/util/purge-caches.js.flow +15 -0
- package/dist/util/request-api.d.ts +28 -0
- package/dist/util/request-api.js.flow +34 -0
- package/dist/util/request-fulfillment.d.ts +37 -0
- package/dist/util/request-fulfillment.js.flow +50 -0
- package/dist/util/request-tracking.d.ts +62 -0
- package/dist/util/request-tracking.js.flow +81 -0
- package/dist/util/result-from-cache-response.d.ts +5 -0
- package/dist/util/result-from-cache-response.js.flow +15 -0
- package/dist/util/scoped-in-memory-cache.d.ts +38 -0
- package/dist/util/scoped-in-memory-cache.js.flow +57 -0
- package/dist/util/serializable-in-memory-cache.d.ts +16 -0
- package/dist/util/serializable-in-memory-cache.js.flow +26 -0
- package/dist/util/ssr-cache.d.ts +51 -0
- package/dist/util/ssr-cache.js.flow +87 -0
- package/dist/util/status.d.ts +10 -0
- package/dist/util/status.js.flow +19 -0
- package/dist/util/to-gql-operation.d.ts +32 -0
- package/dist/util/to-gql-operation.js.flow +45 -0
- package/dist/util/types.d.ts +111 -0
- package/dist/util/types.js.flow +151 -0
- package/package.json +5 -6
- package/src/components/__tests__/{data.test.js → data.test.tsx} +50 -16
- package/src/components/__tests__/{gql-router.test.js → gql-router.test.tsx} +6 -7
- package/src/components/__tests__/{intercept-requests.test.js → intercept-requests.test.tsx} +4 -5
- package/src/components/__tests__/{track-data.test.js → track-data.test.tsx} +4 -5
- package/src/components/{data.js → data.ts} +13 -21
- package/src/components/{gql-router.js → gql-router.tsx} +14 -16
- package/src/components/{intercept-context.js → intercept-context.ts} +5 -4
- package/src/components/{intercept-requests.js → intercept-requests.tsx} +9 -10
- package/src/components/{track-data.js → track-data.tsx} +5 -6
- package/src/hooks/__tests__/{use-cached-effect.test.js → use-cached-effect.test.tsx} +65 -63
- package/src/hooks/__tests__/{use-gql-router-context.test.js → use-gql-router-context.test.tsx} +9 -9
- package/src/hooks/__tests__/{use-gql.test.js → use-gql.test.tsx} +23 -24
- package/src/hooks/__tests__/{use-hydratable-effect.test.js → use-hydratable-effect.test.ts} +52 -54
- package/src/hooks/__tests__/{use-request-interception.test.js → use-request-interception.test.tsx} +7 -5
- package/src/hooks/__tests__/{use-server-effect.test.js → use-server-effect.test.ts} +16 -10
- package/src/hooks/__tests__/{use-shared-cache.test.js → use-shared-cache.test.ts} +13 -13
- package/src/hooks/{use-cached-effect.js → use-cached-effect.ts} +34 -31
- package/src/hooks/{use-gql-router-context.js → use-gql-router-context.ts} +6 -7
- package/src/hooks/{use-gql.js → use-gql.ts} +9 -9
- package/src/hooks/{use-hydratable-effect.js → use-hydratable-effect.ts} +60 -67
- package/src/hooks/{use-request-interception.js → use-request-interception.ts} +6 -6
- package/src/hooks/{use-server-effect.js → use-server-effect.ts} +12 -14
- package/src/hooks/{use-shared-cache.js → use-shared-cache.ts} +16 -11
- package/src/index.ts +46 -0
- package/src/util/__tests__/{get-gql-data-from-response.test.js → get-gql-data-from-response.test.ts} +1 -2
- package/src/util/__tests__/{get-gql-request-id.test.js → get-gql-request-id.test.ts} +10 -12
- package/src/util/__tests__/{graphql-document-node-parser.test.js → graphql-document-node-parser.test.ts} +12 -13
- package/src/util/__tests__/{hydration-cache-api.test.js → hydration-cache-api.test.ts} +3 -4
- package/src/util/__tests__/{merge-gql-context.test.js → merge-gql-context.test.ts} +5 -6
- package/src/util/__tests__/{purge-caches.test.js → purge-caches.test.ts} +3 -4
- package/src/util/__tests__/{request-api.test.js → request-api.test.ts} +5 -5
- package/src/util/__tests__/{request-fulfillment.test.js → request-fulfillment.test.ts} +2 -3
- package/src/util/__tests__/{request-tracking.test.js → request-tracking.test.tsx} +15 -8
- package/src/util/__tests__/{result-from-cache-response.test.js → result-from-cache-response.test.ts} +3 -5
- package/src/util/__tests__/{scoped-in-memory-cache.test.js → scoped-in-memory-cache.test.ts} +5 -6
- package/src/util/__tests__/{serializable-in-memory-cache.test.js → serializable-in-memory-cache.test.ts} +8 -8
- package/src/util/__tests__/{ssr-cache.test.js → ssr-cache.test.ts} +5 -4
- package/src/util/__tests__/{to-gql-operation.test.js → to-gql-operation.test.ts} +5 -4
- package/src/util/{data-error.js → data-error.ts} +3 -4
- package/src/util/{get-gql-data-from-response.js → get-gql-data-from-response.ts} +3 -8
- package/src/util/{get-gql-request-id.js → get-gql-request-id.ts} +13 -17
- package/src/util/{gql-error.js → gql-error.ts} +3 -4
- package/src/util/gql-router-context.ts +6 -0
- package/src/util/{gql-types.js → gql-types.ts} +27 -23
- package/src/util/{graphql-document-node-parser.js → graphql-document-node-parser.ts} +8 -9
- package/src/util/graphql-types.ts +27 -0
- package/src/util/{hydration-cache-api.js → hydration-cache-api.ts} +6 -4
- package/src/util/{merge-gql-context.js → merge-gql-context.ts} +3 -3
- package/src/util/{purge-caches.js → purge-caches.ts} +2 -3
- package/src/util/{request-api.js → request-api.ts} +4 -5
- package/src/util/{request-fulfillment.js → request-fulfillment.ts} +15 -14
- package/src/util/{request-tracking.js → request-tracking.ts} +15 -16
- package/src/util/{result-from-cache-response.js → result-from-cache-response.ts} +6 -7
- package/src/util/{scoped-in-memory-cache.js → scoped-in-memory-cache.ts} +3 -4
- package/src/util/{serializable-in-memory-cache.js → serializable-in-memory-cache.ts} +5 -6
- package/src/util/{ssr-cache.js → ssr-cache.ts} +21 -20
- package/src/util/{status.js → status.ts} +5 -6
- package/src/util/{to-gql-operation.js → to-gql-operation.ts} +4 -5
- package/src/util/{types.js → types.ts} +41 -49
- package/tsconfig.json +11 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/src/__docs__/_overview_.stories.mdx +0 -18
- package/src/__docs__/_overview_graphql.stories.mdx +0 -35
- package/src/__docs__/_overview_ssr_.stories.mdx +0 -185
- package/src/__docs__/_overview_testing_.stories.mdx +0 -123
- package/src/__docs__/exports.abort-inflight-requests.stories.mdx +0 -20
- package/src/__docs__/exports.data-error.stories.mdx +0 -23
- package/src/__docs__/exports.data-errors.stories.mdx +0 -23
- package/src/__docs__/exports.data.stories.mdx +0 -146
- package/src/__docs__/exports.fetch-tracked-requests.stories.mdx +0 -24
- package/src/__docs__/exports.get-gql-request-id.stories.mdx +0 -24
- package/src/__docs__/exports.gql-error.stories.mdx +0 -23
- package/src/__docs__/exports.gql-errors.stories.mdx +0 -20
- package/src/__docs__/exports.gql-router.stories.mdx +0 -29
- package/src/__docs__/exports.has-tracked-requests-to-be-fetched.stories.mdx +0 -20
- package/src/__docs__/exports.intercept-requests.stories.mdx +0 -69
- package/src/__docs__/exports.intialize-hydration-cache.stories.mdx +0 -29
- package/src/__docs__/exports.purge-caches.stories.mdx +0 -23
- package/src/__docs__/exports.purge-hydration-cache.stories.mdx +0 -24
- package/src/__docs__/exports.scoped-in-memory-cache.stories.mdx +0 -92
- package/src/__docs__/exports.serializable-in-memory-cache.stories.mdx +0 -112
- package/src/__docs__/exports.shared-cache.stories.mdx +0 -16
- package/src/__docs__/exports.status.stories.mdx +0 -31
- package/src/__docs__/exports.track-data.stories.mdx +0 -209
- package/src/__docs__/exports.use-cached-effect.stories.mdx +0 -44
- package/src/__docs__/exports.use-gql.stories.mdx +0 -41
- package/src/__docs__/exports.use-hydratable-effect.stories.mdx +0 -43
- package/src/__docs__/exports.use-server-effect.stories.mdx +0 -50
- package/src/__docs__/exports.use-shared-cache.stories.mdx +0 -30
- package/src/__docs__/exports.when-client-side.stories.mdx +0 -33
- package/src/__docs__/types.cached-response.stories.mdx +0 -29
- package/src/__docs__/types.error-options.stories.mdx +0 -21
- package/src/__docs__/types.fetch-policy.stories.mdx +0 -44
- package/src/__docs__/types.gql-context.stories.mdx +0 -20
- package/src/__docs__/types.gql-fetch-fn.stories.mdx +0 -24
- package/src/__docs__/types.gql-fetch-options.stories.mdx +0 -24
- package/src/__docs__/types.gql-operation-type.stories.mdx +0 -24
- package/src/__docs__/types.gql-operation.stories.mdx +0 -67
- package/src/__docs__/types.raw-scoped-cache.stories.mdx +0 -27
- package/src/__docs__/types.response-cache.stories.mdx +0 -33
- package/src/__docs__/types.result.stories.mdx +0 -39
- package/src/__docs__/types.scoped-cache.stories.mdx +0 -114
- package/src/__docs__/types.valid-cache-data.stories.mdx +0 -23
- package/src/index.js +0 -55
- package/src/util/gql-router-context.js +0 -6
- package/src/util/graphql-types.js +0 -30
- /package/src/hooks/__tests__/__snapshots__/{use-shared-cache.test.js.snap → use-shared-cache.test.ts.snap} +0 -0
- /package/src/util/__tests__/__snapshots__/{scoped-in-memory-cache.test.js.snap → scoped-in-memory-cache.test.ts.snap} +0 -0
- /package/src/util/__tests__/__snapshots__/{serializable-in-memory-cache.test.js.snap → serializable-in-memory-cache.test.ts.snap} +0 -0
package/dist/index.js
CHANGED
|
@@ -4,11 +4,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var wonderBlocksCore = require('@khanacademy/wonder-blocks-core');
|
|
6
6
|
var wonderStuffCore = require('@khanacademy/wonder-stuff-core');
|
|
7
|
-
var _extends = require('@babel/runtime/helpers/extends');
|
|
8
7
|
var React = require('react');
|
|
9
8
|
|
|
10
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
11
|
-
|
|
12
9
|
function _interopNamespace(e) {
|
|
13
10
|
if (e && e.__esModule) return e;
|
|
14
11
|
var n = Object.create(null);
|
|
@@ -27,10 +24,72 @@ function _interopNamespace(e) {
|
|
|
27
24
|
return Object.freeze(n);
|
|
28
25
|
}
|
|
29
26
|
|
|
30
|
-
var _extends__default = /*#__PURE__*/_interopDefaultLegacy(_extends);
|
|
31
27
|
var React__namespace = /*#__PURE__*/_interopNamespace(React);
|
|
32
28
|
|
|
33
|
-
const FetchPolicy =
|
|
29
|
+
const FetchPolicy = {
|
|
30
|
+
CacheBeforeNetwork: "CacheBeforeNetwork",
|
|
31
|
+
CacheAndNetwork: "CacheAndNetwork",
|
|
32
|
+
CacheOnly: "CacheOnly",
|
|
33
|
+
NetworkOnly: "NetworkOnly"
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
function _typeof(obj) {
|
|
37
|
+
"@babel/helpers - typeof";
|
|
38
|
+
|
|
39
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
|
|
40
|
+
return typeof obj;
|
|
41
|
+
} : function (obj) {
|
|
42
|
+
return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
|
|
43
|
+
}, _typeof(obj);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function _toPrimitive(input, hint) {
|
|
47
|
+
if (_typeof(input) !== "object" || input === null) return input;
|
|
48
|
+
var prim = input[Symbol.toPrimitive];
|
|
49
|
+
if (prim !== undefined) {
|
|
50
|
+
var res = prim.call(input, hint || "default");
|
|
51
|
+
if (_typeof(res) !== "object") return res;
|
|
52
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
53
|
+
}
|
|
54
|
+
return (hint === "string" ? String : Number)(input);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function _toPropertyKey(arg) {
|
|
58
|
+
var key = _toPrimitive(arg, "string");
|
|
59
|
+
return _typeof(key) === "symbol" ? key : String(key);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function _defineProperties(target, props) {
|
|
63
|
+
for (var i = 0; i < props.length; i++) {
|
|
64
|
+
var descriptor = props[i];
|
|
65
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
66
|
+
descriptor.configurable = true;
|
|
67
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
68
|
+
Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function _createClass(Constructor, protoProps, staticProps) {
|
|
72
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
73
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
74
|
+
Object.defineProperty(Constructor, "prototype", {
|
|
75
|
+
writable: false
|
|
76
|
+
});
|
|
77
|
+
return Constructor;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function _setPrototypeOf(o, p) {
|
|
81
|
+
_setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
|
|
82
|
+
o.__proto__ = p;
|
|
83
|
+
return o;
|
|
84
|
+
};
|
|
85
|
+
return _setPrototypeOf(o, p);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function _inheritsLoose(subClass, superClass) {
|
|
89
|
+
subClass.prototype = Object.create(superClass.prototype);
|
|
90
|
+
subClass.prototype.constructor = subClass;
|
|
91
|
+
_setPrototypeOf(subClass, superClass);
|
|
92
|
+
}
|
|
34
93
|
|
|
35
94
|
const DataErrors = Object.freeze({
|
|
36
95
|
Unknown: "Unknown",
|
|
@@ -41,116 +100,106 @@ const DataErrors = Object.freeze({
|
|
|
41
100
|
Parse: "Parse",
|
|
42
101
|
Hydrated: "Hydrated"
|
|
43
102
|
});
|
|
44
|
-
|
|
45
|
-
|
|
103
|
+
let DataError = function (_KindError) {
|
|
104
|
+
_inheritsLoose(DataError, _KindError);
|
|
105
|
+
function DataError(message, kind, {
|
|
46
106
|
metadata,
|
|
47
107
|
cause
|
|
48
108
|
} = {}) {
|
|
49
|
-
|
|
109
|
+
return _KindError.call(this, message, kind, {
|
|
50
110
|
metadata,
|
|
51
111
|
cause,
|
|
52
112
|
name: "Data"
|
|
53
|
-
});
|
|
113
|
+
}) || this;
|
|
54
114
|
}
|
|
115
|
+
return DataError;
|
|
116
|
+
}(wonderStuffCore.KindError);
|
|
55
117
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
constructor(initialCache = {}) {
|
|
118
|
+
let ScopedInMemoryCache = function () {
|
|
119
|
+
function ScopedInMemoryCache(initialCache = {}) {
|
|
120
|
+
this._cache = void 0;
|
|
60
121
|
this._cache = initialCache;
|
|
61
122
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
return Object.keys(this._cache).length > 0;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
set(scope, id, value) {
|
|
123
|
+
var _proto = ScopedInMemoryCache.prototype;
|
|
124
|
+
_proto.set = function set(scope, id, value) {
|
|
68
125
|
var _this$_cache$scope;
|
|
69
|
-
|
|
70
126
|
if (!id || typeof id !== "string") {
|
|
71
127
|
throw new DataError("id must be non-empty string", DataErrors.InvalidInput);
|
|
72
128
|
}
|
|
73
|
-
|
|
74
129
|
if (!scope || typeof scope !== "string") {
|
|
75
130
|
throw new DataError("scope must be non-empty string", DataErrors.InvalidInput);
|
|
76
131
|
}
|
|
77
|
-
|
|
78
132
|
if (typeof value === "function") {
|
|
79
133
|
throw new DataError("value must be a non-function value", DataErrors.InvalidInput);
|
|
80
134
|
}
|
|
81
|
-
|
|
82
135
|
this._cache[scope] = (_this$_cache$scope = this._cache[scope]) != null ? _this$_cache$scope : {};
|
|
83
136
|
this._cache[scope][id] = value;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
get(scope, id) {
|
|
137
|
+
};
|
|
138
|
+
_proto.get = function get(scope, id) {
|
|
87
139
|
var _this$_cache$scope$id, _this$_cache$scope2;
|
|
88
|
-
|
|
89
140
|
return (_this$_cache$scope$id = (_this$_cache$scope2 = this._cache[scope]) == null ? void 0 : _this$_cache$scope2[id]) != null ? _this$_cache$scope$id : null;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
purge(scope, id) {
|
|
141
|
+
};
|
|
142
|
+
_proto.purge = function purge(scope, id) {
|
|
93
143
|
var _this$_cache$scope3;
|
|
94
|
-
|
|
95
144
|
if (!((_this$_cache$scope3 = this._cache[scope]) != null && _this$_cache$scope3[id])) {
|
|
96
145
|
return;
|
|
97
146
|
}
|
|
98
|
-
|
|
99
147
|
delete this._cache[scope][id];
|
|
100
|
-
|
|
101
148
|
if (Object.keys(this._cache[scope]).length === 0) {
|
|
102
149
|
delete this._cache[scope];
|
|
103
150
|
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
purgeScope(scope, predicate) {
|
|
151
|
+
};
|
|
152
|
+
_proto.purgeScope = function purgeScope(scope, predicate) {
|
|
107
153
|
if (!this._cache[scope]) {
|
|
108
154
|
return;
|
|
109
155
|
}
|
|
110
|
-
|
|
111
156
|
if (predicate == null) {
|
|
112
157
|
delete this._cache[scope];
|
|
113
158
|
return;
|
|
114
159
|
}
|
|
115
|
-
|
|
116
160
|
for (const key of Object.keys(this._cache[scope])) {
|
|
117
161
|
if (predicate(key, this._cache[scope][key])) {
|
|
118
162
|
delete this._cache[scope][key];
|
|
119
163
|
}
|
|
120
164
|
}
|
|
121
|
-
|
|
122
165
|
if (Object.keys(this._cache[scope]).length === 0) {
|
|
123
166
|
delete this._cache[scope];
|
|
124
167
|
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
purgeAll(predicate) {
|
|
168
|
+
};
|
|
169
|
+
_proto.purgeAll = function purgeAll(predicate) {
|
|
128
170
|
if (predicate == null) {
|
|
129
171
|
this._cache = {};
|
|
130
172
|
return;
|
|
131
173
|
}
|
|
132
|
-
|
|
133
174
|
for (const scope of Object.keys(this._cache)) {
|
|
134
175
|
this.purgeScope(scope, (id, value) => predicate(scope, id, value));
|
|
135
176
|
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
177
|
+
};
|
|
178
|
+
_createClass(ScopedInMemoryCache, [{
|
|
179
|
+
key: "inUse",
|
|
180
|
+
get: function () {
|
|
181
|
+
return Object.keys(this._cache).length > 0;
|
|
182
|
+
}
|
|
183
|
+
}]);
|
|
184
|
+
return ScopedInMemoryCache;
|
|
185
|
+
}();
|
|
186
|
+
|
|
187
|
+
let SerializableInMemoryCache = function (_ScopedInMemoryCache) {
|
|
188
|
+
_inheritsLoose(SerializableInMemoryCache, _ScopedInMemoryCache);
|
|
189
|
+
function SerializableInMemoryCache(initialCache = {}) {
|
|
190
|
+
var _this;
|
|
142
191
|
try {
|
|
143
|
-
|
|
192
|
+
_this = _ScopedInMemoryCache.call(this, wonderStuffCore.clone(initialCache)) || this;
|
|
144
193
|
} catch (e) {
|
|
145
194
|
throw new DataError(`An error occurred trying to initialize from a response cache snapshot: ${e}`, DataErrors.InvalidInput);
|
|
146
195
|
}
|
|
196
|
+
return _this;
|
|
147
197
|
}
|
|
148
|
-
|
|
149
|
-
set(scope, id, value) {
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
clone() {
|
|
198
|
+
var _proto = SerializableInMemoryCache.prototype;
|
|
199
|
+
_proto.set = function set(scope, id, value) {
|
|
200
|
+
_ScopedInMemoryCache.prototype.set.call(this, scope, id, Object.freeze(wonderStuffCore.clone(value)));
|
|
201
|
+
};
|
|
202
|
+
_proto.clone = function clone() {
|
|
154
203
|
try {
|
|
155
204
|
return wonderStuffCore.clone(this._cache);
|
|
156
205
|
} catch (e) {
|
|
@@ -158,79 +207,57 @@ class SerializableInMemoryCache extends ScopedInMemoryCache {
|
|
|
158
207
|
cause: e
|
|
159
208
|
});
|
|
160
209
|
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
}
|
|
210
|
+
};
|
|
211
|
+
return SerializableInMemoryCache;
|
|
212
|
+
}(ScopedInMemoryCache);
|
|
164
213
|
|
|
165
214
|
const DefaultScope$2 = "default";
|
|
166
|
-
|
|
167
215
|
let _default$2;
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
_default$2 = new SsrCache();
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
return _default$2;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
constructor(hydrationCache = null, ssrOnlyCache = null) {
|
|
216
|
+
let SsrCache = function () {
|
|
217
|
+
function SsrCache(hydrationCache = null, ssrOnlyCache = null) {
|
|
218
|
+
this._hydrationCache = void 0;
|
|
219
|
+
this._ssrOnlyCache = void 0;
|
|
179
220
|
this.initialize = source => {
|
|
180
221
|
if (this._hydrationCache.inUse) {
|
|
181
222
|
throw new Error("Cannot initialize data response cache more than once");
|
|
182
223
|
}
|
|
183
|
-
|
|
184
224
|
this._hydrationCache = new SerializableInMemoryCache({
|
|
185
225
|
[DefaultScope$2]: source
|
|
186
226
|
});
|
|
187
227
|
};
|
|
188
|
-
|
|
189
228
|
this.cacheData = (id, data, hydrate) => this._setCachedResponse(id, {
|
|
190
229
|
data
|
|
191
230
|
}, hydrate);
|
|
192
|
-
|
|
193
231
|
this.cacheError = (id, error, hydrate) => {
|
|
194
232
|
const errorMessage = typeof error === "string" ? error : error.message;
|
|
195
233
|
return this._setCachedResponse(id, {
|
|
196
234
|
error: errorMessage
|
|
197
235
|
}, hydrate);
|
|
198
236
|
};
|
|
199
|
-
|
|
200
237
|
this.getEntry = id => {
|
|
201
238
|
const ssrEntry = wonderBlocksCore.Server.isServerSide() ? this._ssrOnlyCache.get(DefaultScope$2, id) : null;
|
|
202
239
|
const internalEntry = ssrEntry != null ? ssrEntry : this._hydrationCache.get(DefaultScope$2, id);
|
|
203
|
-
|
|
204
240
|
if (!wonderBlocksCore.Server.isServerSide() && internalEntry != null) {
|
|
205
241
|
this._hydrationCache.purge(DefaultScope$2, id);
|
|
206
242
|
}
|
|
207
|
-
|
|
208
243
|
return internalEntry;
|
|
209
244
|
};
|
|
210
|
-
|
|
211
245
|
this.purgeData = predicate => {
|
|
212
246
|
const realPredicate = predicate ? (_, key, cachedEntry) => predicate(key, cachedEntry) : undefined;
|
|
213
|
-
|
|
214
247
|
this._hydrationCache.purgeAll(realPredicate);
|
|
215
|
-
|
|
216
248
|
this._ssrOnlyCache.purgeAll(realPredicate);
|
|
217
249
|
};
|
|
218
|
-
|
|
219
250
|
this.cloneHydratableData = () => {
|
|
220
251
|
var _cache$DefaultScope;
|
|
221
|
-
|
|
222
252
|
const cache = this._hydrationCache.clone();
|
|
223
|
-
|
|
224
253
|
return (_cache$DefaultScope = cache[DefaultScope$2]) != null ? _cache$DefaultScope : {};
|
|
225
254
|
};
|
|
226
|
-
|
|
227
255
|
this._ssrOnlyCache = ssrOnlyCache || new SerializableInMemoryCache();
|
|
228
256
|
this._hydrationCache = hydrationCache || new SerializableInMemoryCache();
|
|
229
257
|
}
|
|
230
|
-
|
|
231
|
-
_setCachedResponse(id, entry, hydrate) {
|
|
258
|
+
var _proto = SsrCache.prototype;
|
|
259
|
+
_proto._setCachedResponse = function _setCachedResponse(id, entry, hydrate) {
|
|
232
260
|
const frozenEntry = Object.freeze(entry);
|
|
233
|
-
|
|
234
261
|
if (wonderBlocksCore.Server.isServerSide()) {
|
|
235
262
|
if (hydrate) {
|
|
236
263
|
this._hydrationCache.set(DefaultScope$2, id, frozenEntry);
|
|
@@ -238,31 +265,50 @@ class SsrCache {
|
|
|
238
265
|
this._ssrOnlyCache.set(DefaultScope$2, id, frozenEntry);
|
|
239
266
|
}
|
|
240
267
|
}
|
|
241
|
-
|
|
242
268
|
return frozenEntry;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
|
|
269
|
+
};
|
|
270
|
+
_createClass(SsrCache, null, [{
|
|
271
|
+
key: "Default",
|
|
272
|
+
get: function () {
|
|
273
|
+
if (!_default$2) {
|
|
274
|
+
_default$2 = new SsrCache();
|
|
275
|
+
}
|
|
276
|
+
return _default$2;
|
|
277
|
+
}
|
|
278
|
+
}]);
|
|
279
|
+
return SsrCache;
|
|
280
|
+
}();
|
|
246
281
|
|
|
247
282
|
const initializeHydrationCache = source => SsrCache.Default.initialize(source);
|
|
248
283
|
const purgeHydrationCache = predicate => SsrCache.Default.purgeData(predicate);
|
|
249
284
|
|
|
250
|
-
|
|
285
|
+
function _extends() {
|
|
286
|
+
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
287
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
288
|
+
var source = arguments[i];
|
|
289
|
+
for (var key in source) {
|
|
290
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
291
|
+
target[key] = source[key];
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return target;
|
|
296
|
+
};
|
|
297
|
+
return _extends.apply(this, arguments);
|
|
298
|
+
}
|
|
251
299
|
|
|
252
|
-
|
|
253
|
-
|
|
300
|
+
let _default$1;
|
|
301
|
+
let RequestFulfillment = function () {
|
|
302
|
+
function RequestFulfillment() {
|
|
254
303
|
this._requests = {};
|
|
255
|
-
|
|
256
304
|
this.fulfill = (id, {
|
|
257
305
|
handler,
|
|
258
306
|
hydrate: _hydrate = true
|
|
259
307
|
}) => {
|
|
260
308
|
const inflight = this._requests[id];
|
|
261
|
-
|
|
262
309
|
if (inflight) {
|
|
263
310
|
return inflight;
|
|
264
311
|
}
|
|
265
|
-
|
|
266
312
|
const request = handler().then(data => ({
|
|
267
313
|
status: "success",
|
|
268
314
|
data
|
|
@@ -272,13 +318,11 @@ class RequestFulfillment {
|
|
|
272
318
|
unexpectedError: error
|
|
273
319
|
}
|
|
274
320
|
}) : error;
|
|
275
|
-
|
|
276
321
|
if (actualError.name === "AbortError") {
|
|
277
322
|
return {
|
|
278
323
|
status: "aborted"
|
|
279
324
|
};
|
|
280
325
|
}
|
|
281
|
-
|
|
282
326
|
return {
|
|
283
327
|
status: "error",
|
|
284
328
|
error: actualError
|
|
@@ -289,42 +333,32 @@ class RequestFulfillment {
|
|
|
289
333
|
this._requests[id] = request;
|
|
290
334
|
return request;
|
|
291
335
|
};
|
|
292
|
-
|
|
293
336
|
this.abort = id => {
|
|
294
337
|
delete this._requests[id];
|
|
295
338
|
};
|
|
296
|
-
|
|
297
339
|
this.abortAll = () => {
|
|
298
340
|
Object.keys(this._requests).forEach(id => this.abort(id));
|
|
299
341
|
};
|
|
300
342
|
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
_default$1
|
|
343
|
+
_createClass(RequestFulfillment, null, [{
|
|
344
|
+
key: "Default",
|
|
345
|
+
get: function () {
|
|
346
|
+
if (!_default$1) {
|
|
347
|
+
_default$1 = new RequestFulfillment();
|
|
348
|
+
}
|
|
349
|
+
return _default$1;
|
|
305
350
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
}
|
|
351
|
+
}]);
|
|
352
|
+
return RequestFulfillment;
|
|
353
|
+
}();
|
|
311
354
|
|
|
312
355
|
const TrackerContext = React__namespace.createContext(null);
|
|
313
|
-
|
|
314
356
|
let _default;
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
static get Default() {
|
|
318
|
-
if (!_default) {
|
|
319
|
-
_default = new RequestTracker();
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
return _default;
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
constructor(responseCache = undefined) {
|
|
357
|
+
let RequestTracker = function () {
|
|
358
|
+
function RequestTracker(responseCache = undefined) {
|
|
326
359
|
this._trackedRequests = {};
|
|
327
|
-
|
|
360
|
+
this._responseCache = void 0;
|
|
361
|
+
this._requestFulfillment = void 0;
|
|
328
362
|
this.trackDataRequest = (id, handler, hydrate) => {
|
|
329
363
|
if (this._trackedRequests[id] == null) {
|
|
330
364
|
this._trackedRequests[id] = {
|
|
@@ -333,82 +367,78 @@ class RequestTracker {
|
|
|
333
367
|
};
|
|
334
368
|
}
|
|
335
369
|
};
|
|
336
|
-
|
|
337
370
|
this.reset = () => {
|
|
338
371
|
this._trackedRequests = {};
|
|
339
372
|
};
|
|
340
|
-
|
|
341
373
|
this.fulfillTrackedRequests = () => {
|
|
342
374
|
const promises = [];
|
|
343
375
|
const {
|
|
344
376
|
cacheData,
|
|
345
377
|
cacheError
|
|
346
378
|
} = this._responseCache;
|
|
347
|
-
|
|
348
379
|
for (const requestKey of Object.keys(this._trackedRequests)) {
|
|
349
380
|
const options = this._trackedRequests[requestKey];
|
|
350
|
-
|
|
351
381
|
try {
|
|
352
|
-
promises.push(this._requestFulfillment.fulfill(requestKey,
|
|
382
|
+
promises.push(this._requestFulfillment.fulfill(requestKey, _extends({}, options)).then(result => {
|
|
353
383
|
switch (result.status) {
|
|
354
384
|
case "success":
|
|
355
385
|
cacheData(requestKey, result.data, options.hydrate);
|
|
356
386
|
break;
|
|
357
|
-
|
|
358
387
|
case "error":
|
|
359
388
|
cacheError(requestKey, result.error, options.hydrate);
|
|
360
389
|
break;
|
|
361
390
|
}
|
|
362
|
-
|
|
363
391
|
return;
|
|
364
392
|
}));
|
|
365
393
|
} catch (e) {
|
|
366
394
|
promises.push(Promise.resolve(cacheError(requestKey, e, options.hydrate)));
|
|
367
395
|
}
|
|
368
396
|
}
|
|
369
|
-
|
|
370
397
|
this.reset();
|
|
371
398
|
return Promise.all(promises).then(() => this._responseCache.cloneHydratableData());
|
|
372
399
|
};
|
|
373
|
-
|
|
374
400
|
this._responseCache = responseCache || SsrCache.Default;
|
|
375
401
|
this._requestFulfillment = new RequestFulfillment();
|
|
376
402
|
}
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
}
|
|
403
|
+
_createClass(RequestTracker, [{
|
|
404
|
+
key: "hasUnfulfilledRequests",
|
|
405
|
+
get: function () {
|
|
406
|
+
return Object.keys(this._trackedRequests).length > 0;
|
|
407
|
+
}
|
|
408
|
+
}], [{
|
|
409
|
+
key: "Default",
|
|
410
|
+
get: function () {
|
|
411
|
+
if (!_default) {
|
|
412
|
+
_default = new RequestTracker();
|
|
413
|
+
}
|
|
414
|
+
return _default;
|
|
415
|
+
}
|
|
416
|
+
}]);
|
|
417
|
+
return RequestTracker;
|
|
418
|
+
}();
|
|
383
419
|
|
|
384
420
|
const SSRCheck = () => {
|
|
385
421
|
if (wonderBlocksCore.Server.isServerSide()) {
|
|
386
422
|
return null;
|
|
387
423
|
}
|
|
388
|
-
|
|
389
424
|
if (process.env.NODE_ENV === "production") {
|
|
390
425
|
return new DataError("No CSR tracking", DataErrors.NotAllowed);
|
|
391
426
|
} else {
|
|
392
427
|
return new DataError("Data requests are not tracked for fulfillment when when client-side", DataErrors.NotAllowed);
|
|
393
428
|
}
|
|
394
429
|
};
|
|
395
|
-
|
|
396
430
|
const fetchTrackedRequests = () => {
|
|
397
431
|
const ssrCheck = SSRCheck();
|
|
398
|
-
|
|
399
432
|
if (ssrCheck != null) {
|
|
400
433
|
return Promise.reject(ssrCheck);
|
|
401
434
|
}
|
|
402
|
-
|
|
403
435
|
return RequestTracker.Default.fulfillTrackedRequests();
|
|
404
436
|
};
|
|
405
437
|
const hasTrackedRequestsToBeFetched = () => {
|
|
406
438
|
const ssrCheck = SSRCheck();
|
|
407
|
-
|
|
408
439
|
if (ssrCheck != null) {
|
|
409
440
|
throw ssrCheck;
|
|
410
441
|
}
|
|
411
|
-
|
|
412
442
|
return RequestTracker.Default.hasUnfulfilledRequests;
|
|
413
443
|
};
|
|
414
444
|
const abortInflightRequests = () => {
|
|
@@ -421,23 +451,18 @@ const useSharedCache = (id, scope, initialValue) => {
|
|
|
421
451
|
if (!id || typeof id !== "string") {
|
|
422
452
|
throw new DataError("id must be a non-empty string", DataErrors.InvalidInput);
|
|
423
453
|
}
|
|
424
|
-
|
|
425
454
|
if (!scope || typeof scope !== "string") {
|
|
426
455
|
throw new DataError("scope must be a non-empty string", DataErrors.InvalidInput);
|
|
427
456
|
}
|
|
428
|
-
|
|
429
457
|
const cacheValue = React__namespace.useCallback(value => value == null ? cache$1.purge(scope, id) : cache$1.set(scope, id, value), [id, scope]);
|
|
430
458
|
let currentValue = cache$1.get(scope, id);
|
|
431
|
-
|
|
432
459
|
if (currentValue == null && initialValue !== undefined) {
|
|
433
460
|
const value = typeof initialValue === "function" ? initialValue() : initialValue;
|
|
434
|
-
|
|
435
461
|
if (value != null) {
|
|
436
462
|
cacheValue(value);
|
|
437
463
|
currentValue = value;
|
|
438
464
|
}
|
|
439
465
|
}
|
|
440
|
-
|
|
441
466
|
return [currentValue, cacheValue];
|
|
442
467
|
};
|
|
443
468
|
|
|
@@ -446,18 +471,22 @@ const purgeCaches = () => {
|
|
|
446
471
|
purgeHydrationCache();
|
|
447
472
|
};
|
|
448
473
|
|
|
449
|
-
|
|
450
|
-
|
|
474
|
+
let TrackData = function (_React$Component) {
|
|
475
|
+
_inheritsLoose(TrackData, _React$Component);
|
|
476
|
+
function TrackData() {
|
|
477
|
+
return _React$Component.apply(this, arguments) || this;
|
|
478
|
+
}
|
|
479
|
+
var _proto = TrackData.prototype;
|
|
480
|
+
_proto.render = function render() {
|
|
451
481
|
if (!wonderBlocksCore.Server.isServerSide()) {
|
|
452
482
|
throw new Error("This component is not for use during client-side rendering");
|
|
453
483
|
}
|
|
454
|
-
|
|
455
484
|
return React__namespace.createElement(TrackerContext.Provider, {
|
|
456
485
|
value: RequestTracker.Default.trackDataRequest
|
|
457
486
|
}, this.props.children);
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
}
|
|
487
|
+
};
|
|
488
|
+
return TrackData;
|
|
489
|
+
}(React__namespace.Component);
|
|
461
490
|
|
|
462
491
|
const loadingStatus = Object.freeze({
|
|
463
492
|
status: "loading"
|
|
@@ -482,20 +511,16 @@ const resultFromCachedResponse = cacheEntry => {
|
|
|
482
511
|
if (cacheEntry == null) {
|
|
483
512
|
return null;
|
|
484
513
|
}
|
|
485
|
-
|
|
486
514
|
const {
|
|
487
515
|
data,
|
|
488
516
|
error
|
|
489
517
|
} = cacheEntry;
|
|
490
|
-
|
|
491
518
|
if (error != null) {
|
|
492
519
|
return Status.error(new DataError(error, DataErrors.Hydrated));
|
|
493
520
|
}
|
|
494
|
-
|
|
495
521
|
if (data != null) {
|
|
496
522
|
return Status.success(data);
|
|
497
523
|
}
|
|
498
|
-
|
|
499
524
|
return Status.aborted();
|
|
500
525
|
};
|
|
501
526
|
|
|
@@ -508,7 +533,6 @@ const useRequestInterception = (requestId, handler) => {
|
|
|
508
533
|
if (prev != null) {
|
|
509
534
|
return prev;
|
|
510
535
|
}
|
|
511
|
-
|
|
512
536
|
return interceptor(requestId);
|
|
513
537
|
}, null);
|
|
514
538
|
return interceptResponse != null ? interceptResponse : handler();
|
|
@@ -524,18 +548,15 @@ const useServerEffect = (requestId, handler, options = {}) => {
|
|
|
524
548
|
const interceptedHandler = useRequestInterception(requestId, handler);
|
|
525
549
|
const cachedResult = SsrCache.Default.getEntry(requestId);
|
|
526
550
|
const maybeTrack = React.useContext(TrackerContext);
|
|
527
|
-
|
|
528
551
|
if (!skip && cachedResult == null && wonderBlocksCore.Server.isServerSide()) {
|
|
529
552
|
maybeTrack == null ? void 0 : maybeTrack(requestId, interceptedHandler, hydrate);
|
|
530
553
|
}
|
|
531
|
-
|
|
532
554
|
return cachedResult == null ? null : resultFromCachedResponse(cachedResult);
|
|
533
555
|
};
|
|
534
556
|
|
|
535
557
|
const DefaultScope$1 = "useCachedEffect";
|
|
536
558
|
const useCachedEffect = (requestId, handler, options = {}) => {
|
|
537
559
|
var _ref;
|
|
538
|
-
|
|
539
560
|
const {
|
|
540
561
|
fetchPolicy = FetchPolicy.CacheBeforeNetwork,
|
|
541
562
|
skip: hardSkip = false,
|
|
@@ -550,73 +571,57 @@ const useCachedEffect = (requestId, handler, options = {}) => {
|
|
|
550
571
|
const currentRequestRef = React__namespace.useRef();
|
|
551
572
|
const fetchRequest = React__namespace.useMemo(() => {
|
|
552
573
|
var _currentRequestRef$cu;
|
|
553
|
-
|
|
554
574
|
(_currentRequestRef$cu = currentRequestRef.current) == null ? void 0 : _currentRequestRef$cu.cancel();
|
|
555
575
|
currentRequestRef.current = null;
|
|
556
576
|
networkResultRef.current = null;
|
|
557
|
-
|
|
558
577
|
const fetchFn = () => {
|
|
559
578
|
var _currentRequestRef$cu2, _currentRequestRef$cu3;
|
|
560
|
-
|
|
561
579
|
if (fetchPolicy === FetchPolicy.CacheOnly) {
|
|
562
580
|
throw new DataError("Cannot fetch with CacheOnly policy", DataErrors.NotAllowed);
|
|
563
581
|
}
|
|
564
|
-
|
|
565
582
|
const request = RequestFulfillment.Default.fulfill(`${requestId}|${scope}`, {
|
|
566
583
|
handler: interceptedHandler
|
|
567
584
|
});
|
|
568
|
-
|
|
569
585
|
if (request === ((_currentRequestRef$cu2 = currentRequestRef.current) == null ? void 0 : _currentRequestRef$cu2.request)) {
|
|
570
586
|
return;
|
|
571
587
|
}
|
|
572
|
-
|
|
573
588
|
networkResultRef.current = null;
|
|
574
589
|
(_currentRequestRef$cu3 = currentRequestRef.current) == null ? void 0 : _currentRequestRef$cu3.cancel();
|
|
575
590
|
let cancel = false;
|
|
576
591
|
request.then(result => {
|
|
577
592
|
currentRequestRef.current = null;
|
|
578
|
-
|
|
579
593
|
if (cancel) {
|
|
580
594
|
return;
|
|
581
595
|
}
|
|
582
|
-
|
|
583
596
|
setMostRecentResult(result);
|
|
584
597
|
networkResultRef.current = result;
|
|
585
|
-
|
|
586
598
|
if (onResultChanged != null) {
|
|
587
599
|
onResultChanged(result);
|
|
588
600
|
} else {
|
|
589
601
|
forceUpdate();
|
|
590
602
|
}
|
|
591
|
-
|
|
592
603
|
return;
|
|
593
604
|
});
|
|
594
605
|
currentRequestRef.current = {
|
|
595
606
|
requestId,
|
|
596
607
|
request,
|
|
597
|
-
|
|
598
608
|
cancel() {
|
|
599
609
|
cancel = true;
|
|
600
610
|
RequestFulfillment.Default.abort(requestId);
|
|
601
611
|
}
|
|
602
|
-
|
|
603
612
|
};
|
|
604
613
|
};
|
|
605
|
-
|
|
606
614
|
return fetchFn;
|
|
607
615
|
}, [requestId, onResultChanged, forceUpdate, setMostRecentResult, fetchPolicy]);
|
|
608
616
|
const shouldFetch = React__namespace.useMemo(() => {
|
|
609
617
|
if (hardSkip) {
|
|
610
618
|
return false;
|
|
611
619
|
}
|
|
612
|
-
|
|
613
620
|
switch (fetchPolicy) {
|
|
614
621
|
case FetchPolicy.CacheOnly:
|
|
615
622
|
return false;
|
|
616
|
-
|
|
617
623
|
case FetchPolicy.CacheBeforeNetwork:
|
|
618
624
|
return mostRecentResult == null;
|
|
619
|
-
|
|
620
625
|
case FetchPolicy.CacheAndNetwork:
|
|
621
626
|
case FetchPolicy.NetworkOnly:
|
|
622
627
|
return networkResultRef.current == null;
|
|
@@ -626,11 +631,9 @@ const useCachedEffect = (requestId, handler, options = {}) => {
|
|
|
626
631
|
if (!shouldFetch) {
|
|
627
632
|
return;
|
|
628
633
|
}
|
|
629
|
-
|
|
630
634
|
fetchRequest();
|
|
631
635
|
return () => {
|
|
632
636
|
var _currentRequestRef$cu4;
|
|
633
|
-
|
|
634
637
|
(_currentRequestRef$cu4 = currentRequestRef.current) == null ? void 0 : _currentRequestRef$cu4.cancel();
|
|
635
638
|
currentRequestRef.current = null;
|
|
636
639
|
};
|
|
@@ -642,7 +645,12 @@ const useCachedEffect = (requestId, handler, options = {}) => {
|
|
|
642
645
|
return [result, fetchRequest];
|
|
643
646
|
};
|
|
644
647
|
|
|
645
|
-
const WhenClientSide =
|
|
648
|
+
const WhenClientSide = {
|
|
649
|
+
DoNotHydrate: "DoNotHydrate",
|
|
650
|
+
ExecuteWhenNoResult: "ExecuteWhenNoResult",
|
|
651
|
+
ExecuteWhenNoSuccessResult: "ExecuteWhenNoSuccessResult",
|
|
652
|
+
AlwaysExecute: "AlwaysExecute"
|
|
653
|
+
};
|
|
646
654
|
const DefaultScope = "useHydratableEffect";
|
|
647
655
|
const useHydratableEffect = (requestId, handler, options = {}) => {
|
|
648
656
|
const {
|
|
@@ -661,15 +669,12 @@ const useHydratableEffect = (requestId, handler, options = {}) => {
|
|
|
661
669
|
case WhenClientSide.DoNotHydrate:
|
|
662
670
|
case WhenClientSide.AlwaysExecute:
|
|
663
671
|
return null;
|
|
664
|
-
|
|
665
672
|
case WhenClientSide.ExecuteWhenNoResult:
|
|
666
673
|
return serverResult;
|
|
667
|
-
|
|
668
674
|
case WhenClientSide.ExecuteWhenNoSuccessResult:
|
|
669
675
|
if ((serverResult == null ? void 0 : serverResult.status) === "success") {
|
|
670
676
|
return serverResult;
|
|
671
677
|
}
|
|
672
|
-
|
|
673
678
|
return null;
|
|
674
679
|
}
|
|
675
680
|
}, [serverResult]);
|
|
@@ -711,11 +716,9 @@ const InterceptRequests = ({
|
|
|
711
716
|
|
|
712
717
|
const toString = value => {
|
|
713
718
|
var _JSON$stringify;
|
|
714
|
-
|
|
715
719
|
if (typeof value === "string") {
|
|
716
720
|
return value;
|
|
717
721
|
}
|
|
718
|
-
|
|
719
722
|
if (typeof value === "object" && value != null) {
|
|
720
723
|
if (value instanceof Date) {
|
|
721
724
|
return value.toISOString();
|
|
@@ -723,14 +726,11 @@ const toString = value => {
|
|
|
723
726
|
return value.toString();
|
|
724
727
|
}
|
|
725
728
|
}
|
|
726
|
-
|
|
727
729
|
return (_JSON$stringify = JSON.stringify(value)) != null ? _JSON$stringify : "";
|
|
728
730
|
};
|
|
729
|
-
|
|
730
731
|
const toStringifiedVariables = (acc, key, value) => {
|
|
731
732
|
if (typeof value === "object" && value !== null) {
|
|
732
733
|
const subValues = wonderStuffCore.entries(value);
|
|
733
|
-
|
|
734
734
|
if (subValues.length !== 0) {
|
|
735
735
|
return subValues.reduce((innerAcc, [i, v]) => {
|
|
736
736
|
const subKey = `${key}.${i}`;
|
|
@@ -738,18 +738,15 @@ const toStringifiedVariables = (acc, key, value) => {
|
|
|
738
738
|
}, acc);
|
|
739
739
|
}
|
|
740
740
|
}
|
|
741
|
-
|
|
742
741
|
acc[key] = toString(value);
|
|
743
742
|
return acc;
|
|
744
743
|
};
|
|
745
|
-
|
|
746
744
|
const getGqlRequestId = (operation, variables, context) => {
|
|
747
745
|
const parts = [];
|
|
748
746
|
const sortableContext = new URLSearchParams(context);
|
|
749
747
|
sortableContext.sort();
|
|
750
748
|
parts.push(sortableContext.toString());
|
|
751
749
|
parts.push(operation.id);
|
|
752
|
-
|
|
753
750
|
if (variables != null) {
|
|
754
751
|
const stringifiedVariables = Object.keys(variables).reduce((acc, key) => {
|
|
755
752
|
const value = variables[key];
|
|
@@ -759,7 +756,6 @@ const getGqlRequestId = (operation, variables, context) => {
|
|
|
759
756
|
sortableVariables.sort();
|
|
760
757
|
parts.push(sortableVariables.toString());
|
|
761
758
|
}
|
|
762
|
-
|
|
763
759
|
return parts.join("|");
|
|
764
760
|
};
|
|
765
761
|
|
|
@@ -768,24 +764,24 @@ const GqlErrors = Object.freeze({
|
|
|
768
764
|
BadResponse: "BadResponse",
|
|
769
765
|
ErrorResult: "ErrorResult"
|
|
770
766
|
});
|
|
771
|
-
|
|
772
|
-
|
|
767
|
+
let GqlError = function (_KindError) {
|
|
768
|
+
_inheritsLoose(GqlError, _KindError);
|
|
769
|
+
function GqlError(message, kind, {
|
|
773
770
|
metadata,
|
|
774
771
|
cause
|
|
775
772
|
} = {}) {
|
|
776
|
-
|
|
773
|
+
return _KindError.call(this, message, kind, {
|
|
777
774
|
metadata,
|
|
778
775
|
cause,
|
|
779
776
|
name: "Gql"
|
|
780
|
-
});
|
|
777
|
+
}) || this;
|
|
781
778
|
}
|
|
782
|
-
|
|
783
|
-
}
|
|
779
|
+
return GqlError;
|
|
780
|
+
}(wonderStuffCore.KindError);
|
|
784
781
|
|
|
785
782
|
const getGqlDataFromResponse = async response => {
|
|
786
783
|
const bodyText = await response.text();
|
|
787
784
|
let result;
|
|
788
|
-
|
|
789
785
|
try {
|
|
790
786
|
result = JSON.parse(bodyText);
|
|
791
787
|
} catch (e) {
|
|
@@ -797,7 +793,6 @@ const getGqlDataFromResponse = async response => {
|
|
|
797
793
|
cause: e
|
|
798
794
|
});
|
|
799
795
|
}
|
|
800
|
-
|
|
801
796
|
if (response.status >= 300) {
|
|
802
797
|
throw new DataError("Response unsuccessful", DataErrors.Network, {
|
|
803
798
|
metadata: {
|
|
@@ -806,7 +801,6 @@ const getGqlDataFromResponse = async response => {
|
|
|
806
801
|
}
|
|
807
802
|
});
|
|
808
803
|
}
|
|
809
|
-
|
|
810
804
|
if (!Object.prototype.hasOwnProperty.call(result, "data") && !Object.prototype.hasOwnProperty.call(result, "errors")) {
|
|
811
805
|
throw new GqlError("Server response missing", GqlErrors.BadResponse, {
|
|
812
806
|
metadata: {
|
|
@@ -815,7 +809,6 @@ const getGqlDataFromResponse = async response => {
|
|
|
815
809
|
}
|
|
816
810
|
});
|
|
817
811
|
}
|
|
818
|
-
|
|
819
812
|
if (result.errors != null && Array.isArray(result.errors) && result.errors.length > 0) {
|
|
820
813
|
throw new GqlError("GraphQL errors", GqlErrors.ErrorResult, {
|
|
821
814
|
metadata: {
|
|
@@ -824,7 +817,6 @@ const getGqlDataFromResponse = async response => {
|
|
|
824
817
|
}
|
|
825
818
|
});
|
|
826
819
|
}
|
|
827
|
-
|
|
828
820
|
return result.data;
|
|
829
821
|
};
|
|
830
822
|
|
|
@@ -835,13 +827,10 @@ const DocumentTypes = Object.freeze({
|
|
|
835
827
|
const cache = new Map();
|
|
836
828
|
function graphQLDocumentNodeParser(document) {
|
|
837
829
|
var _definition$name;
|
|
838
|
-
|
|
839
830
|
const cached = cache.get(document);
|
|
840
|
-
|
|
841
831
|
if (cached) {
|
|
842
832
|
return cached;
|
|
843
833
|
}
|
|
844
|
-
|
|
845
834
|
if (!(document != null && document.kind)) {
|
|
846
835
|
if (process.env.NODE_ENV === "production") {
|
|
847
836
|
throw new DataError("Bad DocumentNode", DataErrors.InvalidInput);
|
|
@@ -849,12 +838,10 @@ function graphQLDocumentNodeParser(document) {
|
|
|
849
838
|
throw new DataError(`Argument of ${JSON.stringify(document)} passed to parser was not a valid GraphQL ` + `DocumentNode. You may need to use 'graphql-tag' or another method ` + `to convert your operation into a document`, DataErrors.InvalidInput);
|
|
850
839
|
}
|
|
851
840
|
}
|
|
852
|
-
|
|
853
841
|
const fragments = document.definitions.filter(x => x.kind === "FragmentDefinition");
|
|
854
842
|
const queries = document.definitions.filter(x => x.kind === "OperationDefinition" && x.operation === "query");
|
|
855
843
|
const mutations = document.definitions.filter(x => x.kind === "OperationDefinition" && x.operation === "mutation");
|
|
856
844
|
const subscriptions = document.definitions.filter(x => x.kind === "OperationDefinition" && x.operation === "subscription");
|
|
857
|
-
|
|
858
845
|
if (fragments.length && !queries.length && !mutations.length) {
|
|
859
846
|
if (process.env.NODE_ENV === "production") {
|
|
860
847
|
throw new DataError("Fragment only", DataErrors.InvalidInput);
|
|
@@ -862,7 +849,6 @@ function graphQLDocumentNodeParser(document) {
|
|
|
862
849
|
throw new DataError(`Passing only a fragment to 'graphql' is not supported. ` + `You must include a query or mutation as well`, DataErrors.InvalidInput);
|
|
863
850
|
}
|
|
864
851
|
}
|
|
865
|
-
|
|
866
852
|
if (subscriptions.length) {
|
|
867
853
|
if (process.env.NODE_ENV === "production") {
|
|
868
854
|
throw new DataError("No subscriptions", DataErrors.InvalidInput);
|
|
@@ -870,7 +856,6 @@ function graphQLDocumentNodeParser(document) {
|
|
|
870
856
|
throw new DataError(`We do not support subscriptions. ` + `${JSON.stringify(document)} had ${subscriptions.length} subscriptions`, DataErrors.InvalidInput);
|
|
871
857
|
}
|
|
872
858
|
}
|
|
873
|
-
|
|
874
859
|
if (queries.length + mutations.length > 1) {
|
|
875
860
|
if (process.env.NODE_ENV === "production") {
|
|
876
861
|
throw new DataError("Too many ops", DataErrors.InvalidInput);
|
|
@@ -878,7 +863,6 @@ function graphQLDocumentNodeParser(document) {
|
|
|
878
863
|
throw new DataError(`We only support one query or mutation per component. ` + `${JSON.stringify(document)} had ${queries.length} queries and ` + `${mutations.length} mutations. `, DataErrors.InvalidInput);
|
|
879
864
|
}
|
|
880
865
|
}
|
|
881
|
-
|
|
882
866
|
const type = queries.length ? DocumentTypes.query : DocumentTypes.mutation;
|
|
883
867
|
const definitions = queries.length ? queries : mutations;
|
|
884
868
|
const definition = definitions[0];
|
|
@@ -927,18 +911,15 @@ const mergeGqlContext = (defaultContext, overrides) => {
|
|
|
927
911
|
acc[key] = overrides[key];
|
|
928
912
|
}
|
|
929
913
|
}
|
|
930
|
-
|
|
931
914
|
return acc;
|
|
932
|
-
},
|
|
915
|
+
}, _extends({}, defaultContext));
|
|
933
916
|
};
|
|
934
917
|
|
|
935
918
|
const useGqlRouterContext = (contextOverrides = {}) => {
|
|
936
919
|
const gqlRouterContext = React.useContext(GqlRouterContext);
|
|
937
|
-
|
|
938
920
|
if (gqlRouterContext == null) {
|
|
939
921
|
throw new GqlError("No GqlRouter", GqlErrors.Internal);
|
|
940
922
|
}
|
|
941
|
-
|
|
942
923
|
const {
|
|
943
924
|
fetch,
|
|
944
925
|
defaultContext
|
|
@@ -948,11 +929,9 @@ const useGqlRouterContext = (contextOverrides = {}) => {
|
|
|
948
929
|
const refKeys = Object.keys(contextRef.current);
|
|
949
930
|
const mergedKeys = Object.keys(mergedContext);
|
|
950
931
|
const shouldWeUpdateRef = refKeys.length !== mergedKeys.length || mergedKeys.every(key => contextRef.current[key] !== mergedContext[key]);
|
|
951
|
-
|
|
952
932
|
if (shouldWeUpdateRef) {
|
|
953
933
|
contextRef.current = mergedContext;
|
|
954
934
|
}
|
|
955
|
-
|
|
956
935
|
const finalContext = contextRef.current;
|
|
957
936
|
const finalRouterContext = React.useMemo(() => ({
|
|
958
937
|
fetch,
|