@typed/async-data 0.7.1 → 0.9.0
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/cjs/AsyncData.js +51 -8
- package/dist/cjs/AsyncData.js.map +1 -1
- package/dist/cjs/Progress.js +3 -3
- package/dist/cjs/Progress.js.map +1 -1
- package/dist/cjs/Schema.js +212 -199
- package/dist/cjs/Schema.js.map +1 -1
- package/dist/cjs/internal/async-data.js +1 -1
- package/dist/cjs/internal/async-data.js.map +1 -1
- package/dist/dts/AsyncData.d.ts +16 -4
- package/dist/dts/AsyncData.d.ts.map +1 -1
- package/dist/dts/Schema.d.ts +4 -4
- package/dist/dts/Schema.d.ts.map +1 -1
- package/dist/esm/AsyncData.js +43 -7
- package/dist/esm/AsyncData.js.map +1 -1
- package/dist/esm/Progress.js +1 -1
- package/dist/esm/Progress.js.map +1 -1
- package/dist/esm/Schema.js +171 -158
- package/dist/esm/Schema.js.map +1 -1
- package/package.json +4 -4
- package/src/AsyncData.ts +52 -10
- package/src/Progress.ts +1 -1
- package/src/Schema.ts +244 -235
package/dist/cjs/Schema.js
CHANGED
|
@@ -3,23 +3,24 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.
|
|
6
|
+
exports.asyncDataFromJson = exports.Progress = exports.AsyncDataFromSelf = exports.AsyncData = void 0;
|
|
7
7
|
var Arbitrary = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/schema/Arbitrary"));
|
|
8
8
|
var AST = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/schema/AST"));
|
|
9
9
|
var Eq = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/schema/Equivalence"));
|
|
10
|
-
var Parser = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/schema/Parser"));
|
|
11
10
|
var ParseResult = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/schema/ParseResult"));
|
|
12
11
|
var Pretty = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/schema/Pretty"));
|
|
13
12
|
var Schema = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/schema/Schema"));
|
|
14
|
-
var AsyncData = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@typed/async-data/AsyncData"));
|
|
15
|
-
var _effect = /*#__PURE__*/require("effect");
|
|
16
13
|
var Cause = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Cause"));
|
|
14
|
+
var Data = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Data"));
|
|
17
15
|
var Effect = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Effect"));
|
|
16
|
+
var Equal = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Equal"));
|
|
17
|
+
var FiberId = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/FiberId"));
|
|
18
18
|
var Option = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Option"));
|
|
19
19
|
var _Predicate = /*#__PURE__*/require("effect/Predicate");
|
|
20
|
+
var _AsyncData = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("./AsyncData.js"));
|
|
20
21
|
var P = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("./Progress.js"));
|
|
21
22
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
22
|
-
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u &&
|
|
23
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
23
24
|
/**
|
|
24
25
|
* @since 1.0.0
|
|
25
26
|
*/
|
|
@@ -38,13 +39,13 @@ const SUCCESS_PRETTY = print => success => Option.match(success.refreshing, {
|
|
|
38
39
|
onSome: () => `AsyncData.Success(timestamp=${success.timestamp}, refreshing=true, value=${print(success.value)})`
|
|
39
40
|
});
|
|
40
41
|
const OPTIMISTIC_PRETTY = (printValue, printError) => optimistic => `AsyncData.Optimistic(timestamp=${optimistic.timestamp}, value=${printValue(optimistic.value)}, previous=${asyncDataPretty(printValue, printError)(optimistic.previous)})`;
|
|
41
|
-
const ProgressSchemaJson = /*#__PURE__*/Schema.
|
|
42
|
-
loaded: Schema.
|
|
43
|
-
total: /*#__PURE__*/Schema.optional(Schema.
|
|
42
|
+
const ProgressSchemaJson = /*#__PURE__*/Schema.Struct({
|
|
43
|
+
loaded: Schema.BigInt,
|
|
44
|
+
total: /*#__PURE__*/Schema.optional(Schema.BigInt)
|
|
44
45
|
});
|
|
45
|
-
const ProgressSchema = /*#__PURE__*/Schema.
|
|
46
|
-
loaded: Schema.
|
|
47
|
-
total: /*#__PURE__*/Schema.
|
|
46
|
+
const ProgressSchema = /*#__PURE__*/Schema.Data( /*#__PURE__*/Schema.Struct({
|
|
47
|
+
loaded: Schema.BigIntFromSelf,
|
|
48
|
+
total: /*#__PURE__*/Schema.OptionFromSelf(Schema.BigIntFromSelf)
|
|
48
49
|
}));
|
|
49
50
|
const progressArbitrary = fc => fc.bigInt().chain(loaded => fc.option(fc.bigInt({
|
|
50
51
|
min: loaded
|
|
@@ -52,20 +53,23 @@ const progressArbitrary = fc => fc.bigInt().chain(loaded => fc.option(fc.bigInt(
|
|
|
52
53
|
/**
|
|
53
54
|
* @since 1.0.0
|
|
54
55
|
*/
|
|
55
|
-
const Progress = exports.Progress = /*#__PURE__*/ProgressSchemaJson.pipe( /*#__PURE__*/Schema.transform(ProgressSchema,
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
const Progress = exports.Progress = /*#__PURE__*/ProgressSchemaJson.pipe( /*#__PURE__*/Schema.transform(ProgressSchema, {
|
|
57
|
+
decode: json => P.make(json.loaded, json.total),
|
|
58
|
+
encode: progress => ({
|
|
59
|
+
loaded: progress.loaded,
|
|
60
|
+
total: Option.getOrUndefined(progress.total)
|
|
61
|
+
})
|
|
62
|
+
}), /*#__PURE__*/Schema.annotations({
|
|
59
63
|
[AST.IdentifierAnnotationId]: "Progress",
|
|
60
64
|
[Pretty.PrettyHookId]: () => "Progress",
|
|
61
65
|
[Arbitrary.ArbitraryHookId]: () => progressArbitrary,
|
|
62
|
-
[Eq.EquivalenceHookId]: () =>
|
|
66
|
+
[Eq.EquivalenceHookId]: () => Equal.equals
|
|
63
67
|
}));
|
|
64
|
-
const loadingArbitrary = fc => fc.option(progressArbitrary(fc)).map(progress =>
|
|
68
|
+
const loadingArbitrary = fc => fc.option(progressArbitrary(fc)).map(progress => _AsyncData.loading({
|
|
65
69
|
timestamp: Date.now(),
|
|
66
70
|
progress: progress || undefined
|
|
67
71
|
}));
|
|
68
|
-
const failureArbitrary = cause => fc => fc.option(loadingArbitrary(fc)).chain(refreshing => cause(fc).chain(cause => fc.date().map(date =>
|
|
72
|
+
const failureArbitrary = cause => fc => fc.option(loadingArbitrary(fc)).chain(refreshing => cause(fc).chain(cause => fc.date().map(date => _AsyncData.failCause(cause, {
|
|
69
73
|
timestamp: date.getTime(),
|
|
70
74
|
refreshing: refreshing || undefined
|
|
71
75
|
}))));
|
|
@@ -83,7 +87,7 @@ const FailureFrom = (cause, timestamp, refreshing) => {
|
|
|
83
87
|
}
|
|
84
88
|
return base;
|
|
85
89
|
};
|
|
86
|
-
const successArbitrary = value => fc => fc.option(loadingArbitrary(fc)).chain(refreshing => value(fc).chain(a => fc.date().map(date =>
|
|
90
|
+
const successArbitrary = value => fc => fc.option(loadingArbitrary(fc)).chain(refreshing => value(fc).chain(a => fc.date().map(date => _AsyncData.success(a, {
|
|
87
91
|
timestamp: date.getTime(),
|
|
88
92
|
refreshing: refreshing || undefined
|
|
89
93
|
}))));
|
|
@@ -107,7 +111,7 @@ const OptimisticFrom = (value, timestamp, previous) => ({
|
|
|
107
111
|
timestamp,
|
|
108
112
|
previous
|
|
109
113
|
});
|
|
110
|
-
const optimisticArbitrary = (valueArb, causeArb) => fc => asyncDataArbitrary(valueArb, causeArb)(fc).chain(previous => valueArb(fc).chain(value => fc.date().map(date =>
|
|
114
|
+
const optimisticArbitrary = (valueArb, causeArb) => fc => asyncDataArbitrary(valueArb, causeArb)(fc).chain(previous => valueArb(fc).chain(value => fc.date().map(date => _AsyncData.optimistic(previous, value, {
|
|
111
115
|
timestamp: date.getTime()
|
|
112
116
|
}))));
|
|
113
117
|
const fromEq = (a, b) => {
|
|
@@ -121,25 +125,25 @@ const fromEq = (a, b) => {
|
|
|
121
125
|
if (a.timestamp !== loadingB.timestamp) return false;
|
|
122
126
|
if (a.progress === undefined && loadingB.progress === undefined) return true;
|
|
123
127
|
if (a.progress === undefined || loadingB.progress === undefined) return false;
|
|
124
|
-
return
|
|
128
|
+
return Equal.equals(Data.struct(a.progress), Data.struct(loadingB.progress));
|
|
125
129
|
}
|
|
126
130
|
case "Failure":
|
|
127
131
|
{
|
|
128
132
|
const failureB = b;
|
|
129
|
-
if (!(
|
|
133
|
+
if (!(Equal.equals(Data.struct(a.cause), Data.struct(failureB.cause)) && a.timestamp === failureB.timestamp)) return false;
|
|
130
134
|
if (a.refreshing === undefined && failureB.refreshing === undefined) return true;
|
|
131
135
|
if (a.refreshing === undefined || failureB.refreshing === undefined) return false;
|
|
132
|
-
return
|
|
136
|
+
return Equal.equals(Data.struct(a.refreshing), Data.struct(failureB.refreshing));
|
|
133
137
|
}
|
|
134
138
|
case "Success":
|
|
135
139
|
{
|
|
136
140
|
const successB = b;
|
|
137
|
-
return
|
|
141
|
+
return Equal.equals(a.value, successB.value) && a.timestamp === successB.timestamp && Equal.equals(a.refreshing, successB.refreshing);
|
|
138
142
|
}
|
|
139
143
|
case "Optimistic":
|
|
140
144
|
{
|
|
141
145
|
const optimisticB = b;
|
|
142
|
-
return
|
|
146
|
+
return Equal.equals(a.value, optimisticB.value) && a.timestamp === optimisticB.timestamp && fromEq(a.previous, optimisticB.previous);
|
|
143
147
|
}
|
|
144
148
|
}
|
|
145
149
|
};
|
|
@@ -156,12 +160,12 @@ function isProgressFrom(value) {
|
|
|
156
160
|
function isLoadingFrom(value) {
|
|
157
161
|
return (0, _Predicate.hasProperty)(value, "_tag") && value._tag === "Loading" && (0, _Predicate.hasProperty)(value, "timestamp") && typeof value.timestamp === "number" && ((0, _Predicate.hasProperty)(value, "progress") ? isProgressFrom(value.progress) : true);
|
|
158
162
|
}
|
|
159
|
-
const
|
|
160
|
-
defect: Schema.
|
|
161
|
-
error: Schema.
|
|
163
|
+
const isCauseEncoded = /*#__PURE__*/Schema.is( /*#__PURE__*/Schema.encodedSchema( /*#__PURE__*/Schema.Cause({
|
|
164
|
+
defect: Schema.Unknown,
|
|
165
|
+
error: Schema.Unknown
|
|
162
166
|
})));
|
|
163
167
|
function isFailureFrom(value) {
|
|
164
|
-
return (0, _Predicate.hasProperty)(value, "_tag") && value._tag === "Failure" && (0, _Predicate.hasProperty)(value, "cause") &&
|
|
168
|
+
return (0, _Predicate.hasProperty)(value, "_tag") && value._tag === "Failure" && (0, _Predicate.hasProperty)(value, "cause") && isCauseEncoded(value.cause) && (0, _Predicate.hasProperty)(value, "timestamp") && typeof value.timestamp === "number" && ((0, _Predicate.hasProperty)(value, "refreshing") ? value.refreshing === undefined || isLoadingFrom(value.refreshing) : true);
|
|
165
169
|
}
|
|
166
170
|
function isSuccessFrom(value) {
|
|
167
171
|
return (0, _Predicate.hasProperty)(value, "_tag") && value._tag === "Success" && (0, _Predicate.hasProperty)(value, "value") && (0, _Predicate.hasProperty)(value, "timestamp") && typeof value.timestamp === "number" && ((0, _Predicate.hasProperty)(value, "refreshing") ? value.refreshing === undefined || isLoadingFrom(value.refreshing) : true);
|
|
@@ -176,73 +180,76 @@ function isAsyncDataFrom(value) {
|
|
|
176
180
|
* @since 1.0.0
|
|
177
181
|
*/
|
|
178
182
|
const asyncDataFromJson = (value, error) => {
|
|
179
|
-
const schema = Schema.declare([value, Schema.
|
|
183
|
+
const schema = Schema.declare([value, Schema.Cause({
|
|
180
184
|
error,
|
|
181
|
-
defect: Schema.
|
|
182
|
-
})],
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
185
|
+
defect: Schema.Unknown
|
|
186
|
+
})], {
|
|
187
|
+
decode: (valueSchema, causeSchema) => {
|
|
188
|
+
const parseCause = ParseResult.decode(causeSchema);
|
|
189
|
+
const parseValue = ParseResult.decode(valueSchema);
|
|
190
|
+
const parseAsyncData = (input, options) => {
|
|
191
|
+
return Effect.gen(function* () {
|
|
192
|
+
if (!isAsyncDataFrom(input)) {
|
|
193
|
+
return yield* Effect.fail(new ParseResult.Forbidden(schema.ast, input));
|
|
194
|
+
}
|
|
195
|
+
switch (input._tag) {
|
|
196
|
+
case "NoData":
|
|
197
|
+
case "Loading":
|
|
198
|
+
return input;
|
|
199
|
+
case "Failure":
|
|
200
|
+
{
|
|
201
|
+
const cause = yield* parseCause(input.cause, options);
|
|
202
|
+
return FailureFrom(cause, input.timestamp, input.refreshing);
|
|
203
|
+
}
|
|
204
|
+
case "Success":
|
|
205
|
+
{
|
|
206
|
+
const a = yield* parseValue(input.value, options);
|
|
207
|
+
return SuccessFrom(a, input.timestamp, input.refreshing);
|
|
208
|
+
}
|
|
209
|
+
case "Optimistic":
|
|
210
|
+
{
|
|
211
|
+
const previous = yield* parseAsyncData(input.previous, options);
|
|
212
|
+
const value = yield* parseValue(input.value, options);
|
|
213
|
+
return OptimisticFrom(value, input.timestamp, previous);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
};
|
|
218
|
+
return parseAsyncData;
|
|
219
|
+
},
|
|
220
|
+
encode: (valueSchema, causeSchema) => {
|
|
221
|
+
const parseCause = ParseResult.encode(causeSchema);
|
|
222
|
+
const parseValue = ParseResult.encode(valueSchema);
|
|
223
|
+
const parseAsyncData = (input, options) => {
|
|
224
|
+
return Effect.gen(function* () {
|
|
225
|
+
if (!isAsyncDataFrom(input)) {
|
|
226
|
+
return yield* Effect.fail(new ParseResult.Forbidden(schema.ast, input));
|
|
227
|
+
}
|
|
228
|
+
switch (input._tag) {
|
|
229
|
+
case "NoData":
|
|
230
|
+
case "Loading":
|
|
231
|
+
return input;
|
|
232
|
+
case "Failure":
|
|
233
|
+
{
|
|
234
|
+
const cause = yield* parseCause(causeFromToCause(input.cause), options);
|
|
235
|
+
return FailureFrom(cause, input.timestamp, input.refreshing);
|
|
236
|
+
}
|
|
237
|
+
case "Success":
|
|
238
|
+
{
|
|
239
|
+
const a = yield* parseValue(input.value, options);
|
|
240
|
+
return SuccessFrom(a, input.timestamp, input.refreshing);
|
|
241
|
+
}
|
|
242
|
+
case "Optimistic":
|
|
243
|
+
{
|
|
244
|
+
const previous = yield* parseAsyncData(input.previous, options);
|
|
245
|
+
const value = yield* parseValue(input.value, options);
|
|
246
|
+
return OptimisticFrom(value, input.timestamp, previous);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
};
|
|
251
|
+
return parseAsyncData;
|
|
252
|
+
}
|
|
246
253
|
}, {
|
|
247
254
|
title: "AsyncDataFrom",
|
|
248
255
|
equivalence: () => fromEq,
|
|
@@ -255,109 +262,115 @@ const asyncDataFromJson = (value, error) => {
|
|
|
255
262
|
* @since 1.0.0
|
|
256
263
|
*/
|
|
257
264
|
exports.asyncDataFromJson = asyncDataFromJson;
|
|
258
|
-
const
|
|
265
|
+
const AsyncData = (valueSchema, errorSchema) => {
|
|
259
266
|
const from = asyncDataFromJson(valueSchema, errorSchema);
|
|
260
|
-
const to =
|
|
261
|
-
return from.pipe(Schema.transform(to,
|
|
267
|
+
const to = AsyncDataFromSelf(Schema.typeSchema(valueSchema), Schema.typeSchema(errorSchema));
|
|
268
|
+
return from.pipe(Schema.transform(to, {
|
|
269
|
+
decode: asyncDataFromToAsyncData,
|
|
270
|
+
encode: asyncDataToAsyncDataFrom
|
|
271
|
+
}));
|
|
262
272
|
};
|
|
263
273
|
/**
|
|
264
274
|
* @since 1.0.0
|
|
265
275
|
*/
|
|
266
|
-
exports.
|
|
267
|
-
const
|
|
268
|
-
const schema = Schema.declare([value, Schema.
|
|
276
|
+
exports.AsyncData = AsyncData;
|
|
277
|
+
const AsyncDataFromSelf = (value, error) => {
|
|
278
|
+
const schema = Schema.declare([value, Schema.CauseFromSelf({
|
|
269
279
|
error
|
|
270
|
-
})],
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
280
|
+
})], {
|
|
281
|
+
decode: (valueSchema, causeSchema) => {
|
|
282
|
+
const parseCause = ParseResult.decode(causeSchema);
|
|
283
|
+
const parseValue = ParseResult.decode(valueSchema);
|
|
284
|
+
const parseAsyncData = (input, options) => {
|
|
285
|
+
return Effect.gen(function* () {
|
|
286
|
+
if (!_AsyncData.isAsyncData(input)) {
|
|
287
|
+
return yield* Effect.fail(new ParseResult.Forbidden(schema.ast, input));
|
|
288
|
+
}
|
|
289
|
+
switch (input._tag) {
|
|
290
|
+
case "NoData":
|
|
291
|
+
case "Loading":
|
|
292
|
+
return input;
|
|
293
|
+
case "Failure":
|
|
294
|
+
{
|
|
295
|
+
const cause = yield* parseCause(input.cause, options);
|
|
296
|
+
return _AsyncData.failCause(cause, {
|
|
297
|
+
timestamp: input.timestamp,
|
|
298
|
+
refreshing: Option.getOrUndefined(input.refreshing)
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
case "Success":
|
|
302
|
+
{
|
|
303
|
+
const a = yield* parseValue(input.value, options);
|
|
304
|
+
return _AsyncData.success(a, {
|
|
305
|
+
timestamp: input.timestamp,
|
|
306
|
+
refreshing: Option.getOrUndefined(input.refreshing)
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
case "Optimistic":
|
|
310
|
+
{
|
|
311
|
+
const previous = yield* parseAsyncData(input.previous, options);
|
|
312
|
+
const value = yield* parseValue(input.value, options);
|
|
313
|
+
return _AsyncData.optimistic(previous, value, {
|
|
314
|
+
timestamp: input.timestamp
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
};
|
|
320
|
+
return parseAsyncData;
|
|
321
|
+
},
|
|
322
|
+
encode: (valueSchema, causeSchema) => {
|
|
323
|
+
const parseCause = ParseResult.encode(causeSchema);
|
|
324
|
+
const parseValue = ParseResult.encode(valueSchema);
|
|
325
|
+
const parseAsyncData = (input, options) => {
|
|
326
|
+
return Effect.gen(function* () {
|
|
327
|
+
if (!_AsyncData.isAsyncData(input)) {
|
|
328
|
+
return yield* Effect.fail(new ParseResult.Forbidden(schema.ast, input));
|
|
329
|
+
}
|
|
330
|
+
switch (input._tag) {
|
|
331
|
+
case "NoData":
|
|
332
|
+
case "Loading":
|
|
333
|
+
return input;
|
|
334
|
+
case "Failure":
|
|
335
|
+
{
|
|
336
|
+
const cause = yield* parseCause(input.cause, options);
|
|
337
|
+
return _AsyncData.failCause(cause, {
|
|
338
|
+
timestamp: input.timestamp,
|
|
339
|
+
refreshing: Option.getOrUndefined(input.refreshing)
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
case "Success":
|
|
343
|
+
{
|
|
344
|
+
const a = yield* parseValue(input.value, options);
|
|
345
|
+
return _AsyncData.success(a, {
|
|
346
|
+
timestamp: input.timestamp,
|
|
347
|
+
refreshing: Option.getOrUndefined(input.refreshing)
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
case "Optimistic":
|
|
351
|
+
{
|
|
352
|
+
const previous = yield* parseAsyncData(input.previous, options);
|
|
353
|
+
const value = yield* parseValue(input.value, options);
|
|
354
|
+
return _AsyncData.optimistic(previous, value, {
|
|
355
|
+
timestamp: input.timestamp
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
};
|
|
361
|
+
return parseAsyncData;
|
|
362
|
+
}
|
|
350
363
|
}, {
|
|
351
364
|
title: "AsyncData",
|
|
352
365
|
pretty: asyncDataPretty,
|
|
353
366
|
arbitrary: asyncDataArbitrary,
|
|
354
|
-
equivalence: () =>
|
|
367
|
+
equivalence: () => Equal.equals
|
|
355
368
|
});
|
|
356
369
|
return schema;
|
|
357
370
|
};
|
|
358
|
-
exports.
|
|
371
|
+
exports.AsyncDataFromSelf = AsyncDataFromSelf;
|
|
359
372
|
function asyncDataPretty(A, E) {
|
|
360
|
-
return
|
|
373
|
+
return _AsyncData.match({
|
|
361
374
|
NoData: () => NO_DATA_PRETTY,
|
|
362
375
|
Loading: LOADING_PRETTY,
|
|
363
376
|
Failure: (_, data) => FAILURE_PRETTY(E)(data),
|
|
@@ -369,7 +382,7 @@ function asyncDataArbitrary(A, E) {
|
|
|
369
382
|
const failureArb = failureArbitrary(E);
|
|
370
383
|
const successArb = successArbitrary(A);
|
|
371
384
|
const optimisticArb = optimisticArbitrary(A, E);
|
|
372
|
-
return fc => fc.oneof(fc.constant(
|
|
385
|
+
return fc => fc.oneof(fc.constant(_AsyncData.noData()), fc.constant(_AsyncData.loading()), failureArb(fc), successArb(fc), optimisticArb(fc));
|
|
373
386
|
}
|
|
374
387
|
function progressFromJson(json) {
|
|
375
388
|
if (json === undefined) return Option.none();
|
|
@@ -384,7 +397,7 @@ function progressToJson(progres) {
|
|
|
384
397
|
}
|
|
385
398
|
function loadingFromJson(json) {
|
|
386
399
|
if (json === undefined) return;
|
|
387
|
-
return
|
|
400
|
+
return _AsyncData.loading({
|
|
388
401
|
timestamp: json.timestamp,
|
|
389
402
|
progress: Option.getOrUndefined(progressFromJson(json.progress))
|
|
390
403
|
});
|
|
@@ -421,14 +434,14 @@ function causeFromToCause(from) {
|
|
|
421
434
|
function fiberIdFromToFiberId(id) {
|
|
422
435
|
switch (id._tag) {
|
|
423
436
|
case "None":
|
|
424
|
-
return
|
|
437
|
+
return FiberId.none;
|
|
425
438
|
case "Runtime":
|
|
426
|
-
return
|
|
439
|
+
return FiberId.runtime(id.id, id.startTimeMillis);
|
|
427
440
|
case "Composite":
|
|
428
|
-
return
|
|
441
|
+
return FiberId.composite(fiberIdFromToFiberId(id.left), fiberIdFromToFiberId(id.right));
|
|
429
442
|
}
|
|
430
443
|
}
|
|
431
|
-
function
|
|
444
|
+
function causeToCauseEncoded(cause) {
|
|
432
445
|
switch (cause._tag) {
|
|
433
446
|
case "Die":
|
|
434
447
|
return {
|
|
@@ -452,14 +465,14 @@ function causeToCauseFrom(cause) {
|
|
|
452
465
|
case "Parallel":
|
|
453
466
|
return {
|
|
454
467
|
_tag: "Parallel",
|
|
455
|
-
left:
|
|
456
|
-
right:
|
|
468
|
+
left: causeToCauseEncoded(cause.left),
|
|
469
|
+
right: causeToCauseEncoded(cause.right)
|
|
457
470
|
};
|
|
458
471
|
case "Sequential":
|
|
459
472
|
return {
|
|
460
473
|
_tag: "Sequential",
|
|
461
|
-
left:
|
|
462
|
-
right:
|
|
474
|
+
left: causeToCauseEncoded(cause.left),
|
|
475
|
+
right: causeToCauseEncoded(cause.right)
|
|
463
476
|
};
|
|
464
477
|
}
|
|
465
478
|
}
|
|
@@ -493,7 +506,7 @@ function asyncDataToAsyncDataFrom(data) {
|
|
|
493
506
|
case "Loading":
|
|
494
507
|
return loadingToJson(data);
|
|
495
508
|
case "Failure":
|
|
496
|
-
return FailureFrom(
|
|
509
|
+
return FailureFrom(causeToCauseEncoded(data.cause), data.timestamp, Option.getOrUndefined(Option.map(data.refreshing, loadingToJson)));
|
|
497
510
|
case "Success":
|
|
498
511
|
return SuccessFrom(data.value, data.timestamp, Option.getOrUndefined(Option.map(data.refreshing, loadingToJson)));
|
|
499
512
|
case "Optimistic":
|
|
@@ -503,21 +516,21 @@ function asyncDataToAsyncDataFrom(data) {
|
|
|
503
516
|
function asyncDataFromToAsyncData(data) {
|
|
504
517
|
switch (data._tag) {
|
|
505
518
|
case "NoData":
|
|
506
|
-
return
|
|
519
|
+
return _AsyncData.noData();
|
|
507
520
|
case "Loading":
|
|
508
521
|
return loadingFromJson(data);
|
|
509
522
|
case "Failure":
|
|
510
|
-
return
|
|
523
|
+
return _AsyncData.failCause(causeFromToCause(data.cause), {
|
|
511
524
|
timestamp: data.timestamp,
|
|
512
525
|
refreshing: loadingFromJson(data.refreshing)
|
|
513
526
|
});
|
|
514
527
|
case "Success":
|
|
515
|
-
return
|
|
528
|
+
return _AsyncData.success(data.value, {
|
|
516
529
|
timestamp: data.timestamp,
|
|
517
530
|
refreshing: loadingFromJson(data.refreshing)
|
|
518
531
|
});
|
|
519
532
|
case "Optimistic":
|
|
520
|
-
return
|
|
533
|
+
return _AsyncData.optimistic(asyncDataFromToAsyncData(data.previous), data.value, {
|
|
521
534
|
timestamp: data.timestamp
|
|
522
535
|
});
|
|
523
536
|
}
|