-
Notifications
You must be signed in to change notification settings - Fork 11
/
storeFetch.js
240 lines (190 loc) · 5.29 KB
/
storeFetch.js
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
let log = require('../core/logger');
let _ = require('../mindash');
let warnings = require('../core/warnings');
let fetchResult = require('./fetch');
let StoreEvents = require('./storeEvents');
let CompoundError = require('../errors/compoundError');
let NotFoundError = require('../errors/notFoundError');
let FetchConstants = require('./fetchConstants');
function fetch(id, local, remote) {
let store = this;
let app = this.app;
let options, result, error, cacheError;
if (_.isObject(id)) {
options = id;
} else {
options = {
id: id,
locally: local,
remotely: remote
};
}
_.defaults(options, {
locally: _.noop,
remotely: _.noop
});
if (!options || _.isUndefined(options.id)) {
throw new Error('must specify an id');
}
result = dependencyResult(this, options);
if (result) {
return result;
}
cacheError = _.isUndefined(options.cacheError) || options.cacheError;
if (cacheError) {
error = store.__failedFetches[options.id];
if (error) {
return fetchFailed(error);
}
}
if (store.__fetchInProgress[options.id]) {
return fetchResult.pending(options.id, store);
}
if (app && app.fetchStarted) {
app.fetchStarted(store.id, options.id);
}
return tryAndGetLocally() || tryAndGetRemotely();
function tryAndGetLocally(remoteCalled) {
let result = options.locally.call(store);
if (_.isUndefined(result)) {
return;
}
if (_.isNull(result)) {
return fetchNotFound();
}
if (!remoteCalled) {
finished();
}
return fetchFinished(result);
}
function tryAndGetRemotely() {
result = options.remotely.call(store);
if (result) {
if (_.isFunction(result.then)) {
store.__fetchInProgress[options.id] = true;
result.then(function () {
store.__fetchHistory[options.id] = true;
result = tryAndGetLocally(true);
if (result) {
fetchFinished();
store.hasChanged();
} else {
fetchNotFound();
store.hasChanged();
}
}).catch(function (error) {
fetchFailed(error);
store.hasChanged();
store.app.dispatcher.dispatchAction({
type: FetchConstants.FETCH_FAILED,
arguments: [
error,
options.id,
store
]
});
});
return fetchPending();
} else {
store.__fetchHistory[options.id] = true;
result = tryAndGetLocally(true);
if (result) {
return result;
}
}
}
if (warnings.promiseNotReturnedFromRemotely) {
log.warn(promiseNotReturnedWarning());
}
return fetchNotFound();
}
function promiseNotReturnedWarning() {
let inStore = '';
if (store.displayName) {
inStore = ' in ' + store.displayName;
}
return `The remote fetch for '${options.id}' ${inStore} ` +
'did not return a promise and the state was ' +
'not present after remotely finished executing. ' +
'This might be because you forgot to return a promise.';
}
function finished() {
store.__fetchHistory[options.id] = true;
delete store.__fetchInProgress[options.id];
}
function fetchPending() {
return fetchResult.pending(options.id, store);
}
function fetchFinished(result) {
finished();
if (app && app.fetchFinished && result) {
app.fetchFinished(store.id, options.id, 'DONE', {
result: result
});
}
return fetchChanged(fetchResult.done(result, options.id, store));
}
function fetchFailed(error) {
if (cacheError) {
store.__failedFetches[options.id] = error;
}
finished();
if (app && app.fetchFinished) {
app.fetchFinished(store.id, options.id, 'FAILED', {
error: error
});
}
return fetchChanged(fetchResult.failed(error, options.id, store));
}
function fetchNotFound() {
return fetchFailed(new NotFoundError(), options.id, store);
}
function fetchChanged(fetch) {
store.__emitter.emit(StoreEvents.FETCH_CHANGE_EVENT, fetch);
return fetch;
}
}
function dependencyResult(store, options) {
let pending = false;
let errors = [];
let dependencies = options.dependsOn;
if (!dependencies) {
return;
}
if (!_.isArray(dependencies)) {
dependencies = [dependencies];
}
_.each(dependencies, (dependency) => {
switch (dependency.status) {
case FetchConstants.PENDING.toString():
pending = true;
break;
case FetchConstants.FAILED.toString():
errors.push(dependency.error);
break;
}
});
if (errors.length) {
let error = errors.length === 1 ? errors[0] : new CompoundError(errors);
return fetchResult.failed(error, options.id, store);
}
if (pending) {
// Wait for all dependencies to be done and then notify listeners
Promise
.all(_.invoke(dependencies, 'toPromise'))
.then(() => {
store.fetch(options);
store.hasChanged();
})
.catch(() => {
store.fetch(options);
store.hasChanged();
});
return fetchResult.pending(options.id, store);
}
}
fetch.done = fetchResult.done;
fetch.failed = fetchResult.failed;
fetch.pending = fetchResult.pending;
fetch.notFound = fetchResult.notFound;
module.exports = fetch;