forked from danielearwicker/computed-async-mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
computedAsync.ts
160 lines (131 loc) · 4.24 KB
/
computedAsync.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { Atom, autorunAsync, autorun, observable, action } from "mobx"
export function isPromiseLike<T>(result: PromiseLike<T>|T): result is PromiseLike<T> {
return result && typeof (result as any).then === "function";
}
/**
* The type returned by the `computedAsync` function. Represents the current `value`. Accessing
* the value inside a reaction will automatically listen to it, just like an `observable` or
* `computed`. The `busy` property is `true` when the asynchronous function is currently running.
*/
export interface ComputedAsyncValue<T> {
/** The current value (observable) */
readonly value: T;
/** True if an async evaluation is in progress */
readonly busy: boolean;
/** True if Promise was rejected */
readonly failed: boolean;
/** The error from the rejected promise, or undefined */
readonly error: any;
}
export interface ComputedAsyncOptions<T> {
readonly init: T;
readonly fetch: () => PromiseLike<T> | T;
readonly delay?: number;
readonly revert?: boolean;
readonly name?: string;
readonly error?: (error: any) => T;
readonly rethrow?: boolean;
}
class ComputedAsync<T> implements ComputedAsyncValue<T> {
private atom: Atom;
private cachedValue: T;
private version = 0;
private monitor: undefined | (() => void);
constructor(private options: ComputedAsyncOptions<T>) {
this.atom = new Atom(options.name || "ComputedAsync", () => this.wake(), () => this.sleep());
this.cachedValue = options.init;
}
private wake() {
setTimeout(() => {
this.sleep();
this.monitor = this.options.delay !== undefined
? autorunAsync(() => this.observe(), this.options.delay)
: autorun(() => this.observe());
});
}
private observe(): void {
const thisVersion = ++this.version;
if (this.options.revert) {
this.cachedValue = this.options.init;
this.atom.reportChanged();
}
const current = <T>(f: (arg: T) => void) => (arg: T) => {
if (this.version === thisVersion) f(arg);
};
try {
const possiblePromise = this.options.fetch();
if (!isPromiseLike(possiblePromise)) {
this.stopped(false, undefined, possiblePromise);
} else {
this.starting();
possiblePromise.then(
current((v: T) => this.stopped(false, undefined, v)),
current((e: any) => this.handleError(e)));
}
} catch (x) {
this.handleError(x);
}
}
@observable busy = false;
@observable failed = false;
@observable error: any;
@action private starting() {
this.busy = true;
}
@action private stopped(f: boolean, e: any, v: T) {
this.busy = false;
this.failed = f;
this.error = e;
if (v !== this.cachedValue) {
this.cachedValue = v;
this.atom.reportChanged();
}
}
private handleError(e: any) {
let newValue = this.options.init;
if (this.options.error) {
try {
newValue = this.options.error(e);
}
catch (x) {
console.error(x);
}
}
this.stopped(true, e, newValue);
}
private sleep() {
const monitor = this.monitor;
this.monitor = undefined;
if (monitor) {
setTimeout(() => monitor());
}
}
get value() {
this.atom.reportObserved();
if (this.failed && this.options.rethrow) {
throw this.error;
}
return this.cachedValue;
}
}
export function computedAsync<T>(
init: T,
fetch: () => PromiseLike<T> | T,
delay?: number): ComputedAsyncValue<T>;
export function computedAsync<T>(
options: ComputedAsyncOptions<T>
): ComputedAsyncValue<T>;
export function computedAsync<T>(
init: T | ComputedAsyncOptions<T>,
fetch?: () => PromiseLike<T> | T,
delay?: number
) {
if (arguments.length === 1) {
return new ComputedAsync<T>(init as ComputedAsyncOptions<T>);
}
return new ComputedAsync<T>({
init: init as T,
fetch: fetch!,
delay
});
}