-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.js
239 lines (228 loc) · 7 KB
/
actions.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
/**
* @module redux action creators
* @name actions
*/
/* eslint-env browser */
//@flow
import type {JSONType} from './types';
import {isSubscribedToValue} from './selectors';
import {CONFIG} from './config';
import {normalizePath, getPathSpecKey} from './util';
import {
RECEIVE_SNAPSHOTS,
UNSUBSCRIBE_FROM_VALUES,
SUBSCRIBE_TO_VALUES,
DEFAULT_CACHE_PREFIX,
REHYDRATE,
} from './constants';
export type FSA =
| {type: 'FIREBASE/RECEIVE_SNAPSHOTS', path: string, value: JSONType}
| {type: 'FIREBASE/UNSUBSCRIBE_FROM_VALUES', paths: string[]}
| {type: 'FIREBASE/SUBSCRIBE_TO_VALUES', paths: string[]};
export type Action = FSA | (<R>(d: Dispatch, getState: <S>() => S) => ?R);
export type PathConfig = {
path: string,
filter?: {
limitToLast?: number,
limitToFirst?: number,
endAt?: number,
startAt?: number,
equalTo?: any, // TODO: specify this more carefully?
},
orderByChild?: string,
orderByKey?: boolean,
orderByValue?: boolean,
};
export type PathSpec = string | PathConfig;
export function receiveSnapshots(snapshots) {
const values = {};
snapshots.forEach(({pathSpec, snapshot}) => {
values[getPathSpecKey(pathSpec)] = {
pathSpec,
value: snapshot.val(),
};
});
return {
type: RECEIVE_SNAPSHOTS,
values,
};
}
export const _moduleState = {
dispatchTimeout: null,
receiveQueue: [],
};
/**
* Subscribe to `'value'` changes for the specified list of paths in firebase.
* @param {string[]} paths - the list of paths to subscribe to.
*/
export function subscribeToValues<S>(paths: PathSpec[]) {
return (dispatch: Dispatch, getState: () => S) => {
paths = paths.filter(path => !isSubscribedToValue(getState(), path));
if (paths.length == 0) {
return;
}
if (CONFIG.persistToLocalStorage) {
dispatch(loadValuesFromCache(paths, CONFIG.persistToLocalStorage));
}
dispatch({
type: SUBSCRIBE_TO_VALUES,
paths,
});
const dispatchSnapshot = pathSpec => snapshot => {
if (_moduleState.dispatchTimeout) {
_moduleState.receiveQueue.push({pathSpec, snapshot});
} else {
dispatch(receiveSnapshots([{pathSpec, snapshot}]));
_moduleState.dispatchTimeout = setTimeout(() => {
_moduleState.dispatchTimeout = null;
const snapshots = _moduleState.receiveQueue;
_moduleState.receiveQueue = [];
if (snapshots.length > 0) {
dispatch(receiveSnapshots(snapshots));
}
}, CONFIG.syncInterval);
}
};
paths.forEach(pathSpec => {
let ref;
if (typeof pathSpec === 'string') {
ref = CONFIG.getFirebase().database().ref(pathSpec);
} else {
ref = CONFIG.getFirebase().database().ref(pathSpec.path);
if (pathSpec.orderByKey) {
ref = ref.orderByKey();
} else if (pathSpec.orderByValue) {
ref = ref.orderByValue();
} else if (pathSpec.orderByChild) {
ref = ref.orderByChild(pathSpec.orderByChild);
}
if (pathSpec.filter) {
Object.keys(pathSpec.filter).forEach(key => {
ref = ref[key](pathSpec.filter[key]);
});
}
}
ref.on('value', dispatchSnapshot(pathSpec));
});
};
}
/**
* Load values from cache, possibly asynchronously.
* @param {PathSpec[]} pathSpecs - the list of pathSpecs to load from cache
* @param {PersistanceConfig} [config] - configuration for the cache
*/
export function loadValuesFromCache(pathSpecs: PathSpec[], config) {
return dispatch => {
if (config) {
const storagePrefix = config.storagePrefix || DEFAULT_CACHE_PREFIX;
const storage = config.storage || localStorage;
let valuesToDispatch = {};
let numReceived = 0;
let timeout = null;
const dispatchValues = () => {
const values = valuesToDispatch;
valuesToDispatch = {};
clearTimeout(timeout);
dispatch({
type: RECEIVE_SNAPSHOTS,
values,
fromCache: true,
});
};
return pathSpecs.map(pathSpec => {
const pathSpecKey = getPathSpecKey(pathSpec);
let valueOrPromise = storage.getItem(storagePrefix + pathSpecKey);
const receiveFromCache = cached => {
numReceived++;
if (!cached) {
return;
}
try {
cached = JSON.parse(cached);
} catch (e) {
// failed to parse json, just skip this.
return;
}
valuesToDispatch[pathSpecKey] = {
pathSpec: pathSpec,
value: cached,
};
if (numReceived === pathSpecs.length) {
// once we have received everything from cache, dispatch the results
clearTimeout(timeout);
dispatch({
type: RECEIVE_SNAPSHOTS,
values: valuesToDispatch,
fromCache: true,
});
} else {
// wait sync interval before dispatching results
// so we don't dispatch too often and block the browser.
if (!timeout) {
timeout = setTimeout(dispatchValues, CONFIG.syncInterval);
}
}
return cached;
};
if (valueOrPromise instanceof Promise) {
return valueOrPromise.then(receiveFromCache);
} else {
return receiveFromCache(valueOrPromise);
}
});
}
};
}
/**
* Perform a one-time fetch from firebase of all the given paths.
* @param {string[]} paths - the list of paths to subscribe to.
* @param {callback} [callback] - function to call when all the data has been fetched.
* @callback callback
* @returns {Promise} - a Promise which resolves when the fetch is completed
*/
export function fetchValues(paths: string[], callback: ?() => void) {
return (dispatch: Dispatch) => {
let numLeft = paths.length;
return new Promise(resolve => {
const dispatchSnapshot = pathSpec => snapshot => {
dispatch(receiveSnapshots([{pathSpec, snapshot}]));
numLeft--;
if (numLeft === 0) {
callback && callback();
resolve();
}
};
paths.forEach(path =>
CONFIG.getFirebase()
.database()
.ref(path)
.once('value', dispatchSnapshot(path))
);
});
};
}
/**
* Unsubscribe from `'value'` changes for the specified list of paths in firebase.
*
* @param {string[]} paths - the list of paths to unsubscribe from.
* @returns {void}
*/
export function unsubscribeFromValues(paths: string[]) {
return (dispatch: Dispatch, getState: () => *) => {
paths = paths.filter(path => isSubscribedToValue(getState(), path));
if (paths.length === 0) {
return;
}
paths.forEach(path =>
CONFIG.getFirebase().database().ref(path).off('value')
);
dispatch({type: UNSUBSCRIBE_FROM_VALUES, paths});
};
}
/**
* Rehydrate the state of the firebase mirror with the given data.
* Note that this will not cause new subscritions to be created.
*/
export function rehydrate(data) {
return {type: REHYDRATE, data};
}