1 | import * as tslib_1 from "tslib";
|
2 | import { constFalse, identity, constant } from 'fp-ts/lib/function';
|
3 | import { isNone, isSome, none, some, fold as foldO, getShow as getShowOption } from 'fp-ts/lib/Option';
|
4 | import { left, right, fold as foldEither } from 'fp-ts/lib/Either';
|
5 | import { array } from 'fp-ts/lib/Array';
|
6 | import { sign } from 'fp-ts/lib/Ordering';
|
7 | import { pipe, pipeable } from 'fp-ts/lib/pipeable';
|
8 | import { showNumber } from 'fp-ts/lib/Show';
|
9 | export var URI = 'RemoteData';
|
10 |
|
11 | export var failure = function (error) { return ({
|
12 | _tag: 'RemoteFailure',
|
13 | error: error,
|
14 | }); };
|
15 | export var success = function (value) { return ({
|
16 | _tag: 'RemoteSuccess',
|
17 | value: value,
|
18 | }); };
|
19 | export var pending = {
|
20 | _tag: 'RemotePending',
|
21 | progress: none,
|
22 | };
|
23 | export var progress = function (progress) { return ({
|
24 | _tag: 'RemotePending',
|
25 | progress: some(progress),
|
26 | }); };
|
27 | export var initial = {
|
28 | _tag: 'RemoteInitial',
|
29 | };
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | export var isFailure = function (data) { return data._tag === 'RemoteFailure'; };
|
35 |
|
36 |
|
37 |
|
38 | export var isSuccess = function (data) { return data._tag === 'RemoteSuccess'; };
|
39 |
|
40 |
|
41 |
|
42 | export var isPending = function (data) { return data._tag === 'RemotePending'; };
|
43 |
|
44 |
|
45 |
|
46 | export var isInitial = function (data) { return data._tag === 'RemoteInitial'; };
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | export var getOrElse = function (f) { return function (ma) { return (isSuccess(ma) ? ma.value : f()); }; };
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | export var fold = function (onInitial, onPending, onFailure, onSuccess) { return function (ma) {
|
76 | switch (ma._tag) {
|
77 | case 'RemoteInitial': {
|
78 | return onInitial();
|
79 | }
|
80 | case 'RemotePending': {
|
81 | return onPending(ma.progress);
|
82 | }
|
83 | case 'RemoteFailure': {
|
84 | return onFailure(ma.error);
|
85 | }
|
86 | case 'RemoteSuccess': {
|
87 | return onSuccess(ma.value);
|
88 | }
|
89 | }
|
90 | }; };
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | export var toNullable = function (ma) { return (isSuccess(ma) ? ma.value : null); };
|
108 | export var toUndefined = function (ma) { return (isSuccess(ma) ? ma.value : undefined); };
|
109 | export function fromOption(option, error) {
|
110 | if (isNone(option)) {
|
111 | return failure(error());
|
112 | }
|
113 | else {
|
114 | return success(option.value);
|
115 | }
|
116 | }
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | export function toOption(data) {
|
129 | return data._tag === 'RemoteSuccess' ? some(data.value) : none;
|
130 | }
|
131 |
|
132 |
|
133 |
|
134 | export var fromEither = foldEither(failure, success);
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | export function toEither(onInitial, onPending) {
|
154 | return function (data) {
|
155 | return pipe(data, fold(function () { return left(onInitial()); }, function () { return left(onPending()); }, left, right));
|
156 | };
|
157 | }
|
158 | export function fromPredicate(predicate, whenFalse) {
|
159 | return function (a) { return (predicate(a) ? success(a) : failure(whenFalse(a))); };
|
160 | }
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | export function fromProgressEvent(event) {
|
166 | return progress({
|
167 | loaded: event.loaded,
|
168 | total: event.lengthComputable ? some(event.total) : none,
|
169 | });
|
170 | }
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 | export function elem(E) {
|
180 | return function (a, fa) { return fa._tag === 'RemoteSuccess' && E.equals(a, fa.value); };
|
181 | }
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | export function exists(p) {
|
187 | return function (fa) { return fa._tag === 'RemoteSuccess' && p(fa.value); };
|
188 | }
|
189 |
|
190 |
|
191 |
|
192 | export function recover(f) {
|
193 | var r = recoverMap(f, identity);
|
194 | return function (fa) { return (fa._tag === 'RemoteFailure' ? r(fa) : fa); };
|
195 | }
|
196 |
|
197 |
|
198 |
|
199 |
|
200 | export function recoverMap(f, g) {
|
201 | return function (fa) {
|
202 | switch (fa._tag) {
|
203 | case 'RemoteInitial': {
|
204 | return fa;
|
205 | }
|
206 | case 'RemotePending': {
|
207 | return fa;
|
208 | }
|
209 | case 'RemoteFailure': {
|
210 | var b = f(fa.error);
|
211 | return b._tag === 'Some' ? success(b.value) : fa;
|
212 | }
|
213 | case 'RemoteSuccess': {
|
214 | return success(g(fa.value));
|
215 | }
|
216 | }
|
217 | };
|
218 | }
|
219 | var concatPendings = function (a, b) {
|
220 | if (isSome(a.progress) && isSome(b.progress)) {
|
221 | var progressA = a.progress.value;
|
222 | var progressB = b.progress.value;
|
223 | if (isNone(progressA.total) || isNone(progressB.total)) {
|
224 | return progress({
|
225 | loaded: progressA.loaded + progressB.loaded,
|
226 | total: none,
|
227 | });
|
228 | }
|
229 | var totalA = progressA.total.value;
|
230 | var totalB = progressB.total.value;
|
231 | var total = totalA + totalB;
|
232 | var loaded = (progressA.loaded * totalA + progressB.loaded * totalB) / (total * total);
|
233 | return progress({
|
234 | loaded: loaded,
|
235 | total: some(total),
|
236 | });
|
237 | }
|
238 | var noA = isNone(a.progress);
|
239 | var noB = isNone(b.progress);
|
240 | if (noA && !noB) {
|
241 | return b;
|
242 | }
|
243 | if (!noA && noB) {
|
244 | return a;
|
245 | }
|
246 | return pending;
|
247 | };
|
248 |
|
249 | export var remoteData = {
|
250 |
|
251 | URI: URI,
|
252 |
|
253 | of: function (value) { return success(value); },
|
254 | ap: function (fab, fa) {
|
255 | switch (fa._tag) {
|
256 | case 'RemoteInitial': {
|
257 | return isFailure(fab) ? fab : initial;
|
258 | }
|
259 | case 'RemotePending': {
|
260 | return isPending(fab) ? concatPendings(fa, fab) : isSuccess(fab) ? fa : fab;
|
261 | }
|
262 | case 'RemoteFailure': {
|
263 | return isFailure(fab) ? fab : fa;
|
264 | }
|
265 | case 'RemoteSuccess': {
|
266 | return isSuccess(fab) ? success(fab.value(fa.value)) : fab;
|
267 | }
|
268 | }
|
269 | },
|
270 | map: function (fa, f) {
|
271 | return isSuccess(fa) ? success(f(fa.value)) : fa;
|
272 | },
|
273 | chain: function (fa, f) {
|
274 | return isSuccess(fa) ? f(fa.value) : fa;
|
275 | },
|
276 |
|
277 | reduce: function (fa, b, f) {
|
278 | return pipe(fa, fold(function () { return b; }, function () { return b; }, function () { return b; }, function (a) { return f(b, a); }));
|
279 | },
|
280 | reduceRight: function (fa, b, f) { return (isSuccess(fa) ? f(fa.value, b) : b); },
|
281 | foldMap: function (M) { return function (fa, f) {
|
282 | return isSuccess(fa) ? f(fa.value) : M.empty;
|
283 | }; },
|
284 |
|
285 | traverse: function (F) { return function (ta, f) {
|
286 | if (isSuccess(ta)) {
|
287 | return F.map(f(ta.value), function (a) { return remoteData.of(a); });
|
288 | }
|
289 | else {
|
290 | return F.of(ta);
|
291 | }
|
292 | }; },
|
293 | sequence: function (F) { return function (ta) {
|
294 | return remoteData.traverse(F)(ta, identity);
|
295 | }; },
|
296 |
|
297 | bimap: function (fla, f, g) {
|
298 | return pipe(fla, fold(function () { return initial; }, foldO(function () { return pending; }, progress), function (e) { return failure(f(e)); }, function (a) { return success(g(a)); }));
|
299 | },
|
300 | mapLeft: function (fla, f) {
|
301 | return fold(function () { return initial; }, foldO(function () { return pending; }, progress), function (e) { return failure(f(e)); }, function () { return fla; })(fla);
|
302 | },
|
303 |
|
304 | alt: function (fx, fy) { return fold(fy, fy, fy, function () { return fx; })(fx); },
|
305 |
|
306 | zero: function () { return initial; },
|
307 |
|
308 | extend: function (fla, f) {
|
309 | return pipe(fla, fold(function () { return initial; }, foldO(function () { return pending; }, progress), function () { return fla; }, function () { return success(f(fla)); }));
|
310 | },
|
311 | };
|
312 |
|
313 | export var getEq = function (EE, EA) {
|
314 | return {
|
315 | equals: function (x, y) {
|
316 | return pipe(x, fold(function () { return isInitial(y); }, function () { return isPending(y); }, function (xError) {
|
317 | return pipe(y, fold(constFalse, constFalse, function (yError) { return EE.equals(xError, yError); }, constFalse));
|
318 | }, function (ax) {
|
319 | return pipe(y, fold(constFalse, constFalse, constFalse, function (ay) { return EA.equals(ax, ay); }));
|
320 | }));
|
321 | },
|
322 | };
|
323 | };
|
324 |
|
325 | var constLt = constant(-1);
|
326 | var constEq = constant(0);
|
327 | var constGt = constant(1);
|
328 | export var getOrd = function (OE, OA) {
|
329 | return tslib_1.__assign({}, getEq(OE, OA), { compare: function (x, y) {
|
330 | return sign(pipe(x, fold(function () {
|
331 | return pipe(y, fold(constEq, constLt, constLt, constLt));
|
332 | }, function () {
|
333 | return pipe(y, fold(constGt, constEq, constLt, constLt));
|
334 | }, function (xError) {
|
335 | return pipe(y, fold(constGt, constGt, function (yError) { return OE.compare(xError, yError); }, constLt));
|
336 | }, function (xValue) {
|
337 | return pipe(y, fold(constGt, constGt, constGt, function (yValue) { return OA.compare(xValue, yValue); }));
|
338 | })));
|
339 | } });
|
340 | };
|
341 |
|
342 | export var getSemigroup = function (SE, SA) {
|
343 | return {
|
344 | concat: function (x, y) {
|
345 | var constX = constant(x);
|
346 | var constY = constant(y);
|
347 | return pipe(x, fold(function () {
|
348 | return pipe(y, fold(constY, constY, constY, constY));
|
349 | }, function () {
|
350 | return pipe(y, fold(constX, function () { return concatPendings(x, y); }, constY, constY));
|
351 | }, function (xError) {
|
352 | return pipe(y, fold(constX, constX, function (yError) { return failure(SE.concat(xError, yError)); }, function () { return y; }));
|
353 | }, function (xValue) {
|
354 | return pipe(y, fold(constX, constX, function () { return x; }, function (yValue) { return success(SA.concat(xValue, yValue)); }));
|
355 | }));
|
356 | },
|
357 | };
|
358 | };
|
359 |
|
360 | export var getMonoid = function (SL, SA) {
|
361 | return tslib_1.__assign({}, getSemigroup(SL, SA), { empty: initial });
|
362 | };
|
363 | var showOptionNumber = getShowOption(showNumber);
|
364 |
|
365 | export var getShow = function (SE, SA) { return ({
|
366 | show: fold(function () { return 'initial'; }, foldO(function () { return 'pending'; }, function (progress) {
|
367 | return "progress({ loaded: " + showNumber.show(progress.loaded) + ", total: " + showOptionNumber.show(progress.total) + " })";
|
368 | }), function (e) { return "failure(" + SE.show(e) + ")"; }, function (a) { return "success(" + SA.show(a) + ")"; }),
|
369 | }); };
|
370 | var _a = pipeable(remoteData), alt = _a.alt, ap = _a.ap, apFirst = _a.apFirst, apSecond = _a.apSecond, bimap = _a.bimap, chain = _a.chain, chainFirst = _a.chainFirst, duplicate = _a.duplicate, extend = _a.extend, flatten = _a.flatten, foldMap = _a.foldMap, map = _a.map, mapLeft = _a.mapLeft, reduce = _a.reduce, reduceRight = _a.reduceRight;
|
371 | export { alt, ap, apFirst, apSecond, bimap, chain, chainFirst, duplicate, extend, flatten, foldMap, map, mapLeft, reduce, reduceRight, };
|
372 | export function combine() {
|
373 | var list = [];
|
374 | for (var _i = 0; _i < arguments.length; _i++) {
|
375 | list[_i] = arguments[_i];
|
376 | }
|
377 | if (list.length === 0) {
|
378 | return remoteData.of([]);
|
379 | }
|
380 | return array.sequence(remoteData)(list);
|
381 | }
|