1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.getSnapshotByObject = getSnapshotByObject;
|
7 | exports.firestoreRef = firestoreRef;
|
8 | exports.getQueryName = getQueryName;
|
9 | exports.getBaseQueryName = getBaseQueryName;
|
10 | exports.listenerExists = listenerExists;
|
11 | exports.attachListener = attachListener;
|
12 | exports.detachListener = detachListener;
|
13 | exports.queryStrToObj = queryStrToObj;
|
14 | exports.getQueryConfig = getQueryConfig;
|
15 | exports.getQueryConfigs = getQueryConfigs;
|
16 | exports.orderedFromSnap = orderedFromSnap;
|
17 | exports.dataByIdSnapshot = dataByIdSnapshot;
|
18 | exports.getPopulateChild = getPopulateChild;
|
19 | exports.populateList = populateList;
|
20 | exports.promisesForPopulate = promisesForPopulate;
|
21 | exports.dispatchListenerResponse = dispatchListenerResponse;
|
22 | exports.getPopulateActions = getPopulateActions;
|
23 | exports.snapshotCache = void 0;
|
24 |
|
25 | var _cloneDeep2 = _interopRequireDefault(require("lodash/cloneDeep"));
|
26 |
|
27 | var _set2 = _interopRequireDefault(require("lodash/set"));
|
28 |
|
29 | var _get2 = _interopRequireDefault(require("lodash/get"));
|
30 |
|
31 | var _map2 = _interopRequireDefault(require("lodash/map"));
|
32 |
|
33 | var _has2 = _interopRequireDefault(require("lodash/has"));
|
34 |
|
35 | var _forEach2 = _interopRequireDefault(require("lodash/forEach"));
|
36 |
|
37 | var _trim2 = _interopRequireDefault(require("lodash/trim"));
|
38 |
|
39 | var _isEmpty2 = _interopRequireDefault(require("lodash/isEmpty"));
|
40 |
|
41 | var _isNumber2 = _interopRequireDefault(require("lodash/isNumber"));
|
42 |
|
43 | var _isObject2 = _interopRequireDefault(require("lodash/isObject"));
|
44 |
|
45 | var _constants = require("../constants");
|
46 |
|
47 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
48 |
|
49 | function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableRest(); }
|
50 |
|
51 | function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
52 |
|
53 | function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
54 |
|
55 | function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
56 |
|
57 | function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
58 |
|
59 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
60 |
|
61 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
62 |
|
63 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
64 |
|
65 | function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
66 |
|
67 | function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
68 |
|
69 | function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
70 |
|
71 | function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
|
72 |
|
73 | function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
74 |
|
75 | function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
76 |
|
77 | var snapshotCache = new WeakMap();
|
78 | exports.snapshotCache = snapshotCache;
|
79 |
|
80 | function getSnapshotByObject(obj) {
|
81 | return snapshotCache.get(obj);
|
82 | }
|
83 |
|
84 | function addWhereToRef(ref, where) {
|
85 | if (!Array.isArray(where)) {
|
86 | throw new Error('where parameter must be an array.');
|
87 | }
|
88 |
|
89 | if (Array.isArray(where[0])) {
|
90 | return where.reduce(function (acc, whereArgs) {
|
91 | return addWhereToRef(acc, whereArgs);
|
92 | }, ref);
|
93 | }
|
94 |
|
95 | return ref.where.apply(ref, _toConsumableArray(where));
|
96 | }
|
97 |
|
98 | function addOrderByToRef(ref, orderBy) {
|
99 | if (!Array.isArray(orderBy) && !(typeof orderBy === 'string' || orderBy instanceof String)) {
|
100 | throw new Error('orderBy parameter must be an array or string.');
|
101 | }
|
102 |
|
103 | if (typeof orderBy === 'string' || orderBy instanceof String) {
|
104 | return ref.orderBy(orderBy);
|
105 | }
|
106 |
|
107 | if (typeof orderBy[0] === 'string' || orderBy[0] instanceof String) {
|
108 | return ref.orderBy.apply(ref, _toConsumableArray(orderBy));
|
109 | }
|
110 |
|
111 | return orderBy.reduce(function (acc, orderByArgs) {
|
112 | return addOrderByToRef(acc, orderByArgs);
|
113 | }, ref);
|
114 | }
|
115 |
|
116 | function arrayify(cursor) {
|
117 | return [].concat(cursor);
|
118 | }
|
119 |
|
120 | function handleSubcollections(ref, subcollectionList) {
|
121 | if (Array.isArray(subcollectionList)) {
|
122 | subcollectionList.forEach(function (subcollection) {
|
123 | if (subcollection.collection) {
|
124 | if (typeof ref.collection !== 'function') {
|
125 | throw new Error("Collection can only be run on a document. Check that query config for subcollection: \"".concat(subcollection.collection, "\" contains a doc parameter."));
|
126 | }
|
127 |
|
128 | ref = ref.collection(subcollection.collection);
|
129 | }
|
130 |
|
131 | if (subcollection.doc) ref = ref.doc(subcollection.doc);
|
132 | if (subcollection.where) ref = addWhereToRef(ref, subcollection.where);
|
133 |
|
134 | if (subcollection.orderBy) {
|
135 | ref = addOrderByToRef(ref, subcollection.orderBy);
|
136 | }
|
137 |
|
138 | if (subcollection.limit) ref = ref.limit(subcollection.limit);
|
139 |
|
140 | if (subcollection.startAt) {
|
141 | var _ref;
|
142 |
|
143 | ref = (_ref = ref).startAt.apply(_ref, _toConsumableArray(arrayify(subcollection.startAt)));
|
144 | }
|
145 |
|
146 | if (subcollection.startAfter) {
|
147 | var _ref2;
|
148 |
|
149 | ref = (_ref2 = ref).startAfter.apply(_ref2, _toConsumableArray(arrayify(subcollection.startAfter)));
|
150 | }
|
151 |
|
152 | if (subcollection.endAt) {
|
153 | var _ref3;
|
154 |
|
155 | ref = (_ref3 = ref).endAt.apply(_ref3, _toConsumableArray(arrayify(subcollection.endAt)));
|
156 | }
|
157 |
|
158 | if (subcollection.endBefore) {
|
159 | var _ref4;
|
160 |
|
161 | ref = (_ref4 = ref).endBefore.apply(_ref4, _toConsumableArray(arrayify(subcollection.endBefore)));
|
162 | }
|
163 |
|
164 | ref = handleSubcollections(ref, subcollection.subcollections);
|
165 | });
|
166 | }
|
167 |
|
168 | return ref;
|
169 | }
|
170 |
|
171 | function firestoreRef(firebase, meta) {
|
172 | var _ref5, _ref6, _ref7, _ref8;
|
173 |
|
174 | if (!firebase.firestore) {
|
175 | throw new Error('Firestore must be required and initalized.');
|
176 | }
|
177 |
|
178 | var collection = meta.collection,
|
179 | collectionGroup = meta.collectionGroup,
|
180 | doc = meta.doc,
|
181 | subcollections = meta.subcollections,
|
182 | where = meta.where,
|
183 | orderBy = meta.orderBy,
|
184 | limit = meta.limit,
|
185 | startAt = meta.startAt,
|
186 | startAfter = meta.startAfter,
|
187 | endAt = meta.endAt,
|
188 | endBefore = meta.endBefore;
|
189 | var ref = firebase.firestore();
|
190 |
|
191 | if (collection && collectionGroup) {
|
192 | throw new Error('Reference cannot contain both Collection and CollectionGroup.');
|
193 | }
|
194 |
|
195 | if (collection) ref = ref.collection(collection);
|
196 | if (collectionGroup) ref = ref.collectionGroup(collectionGroup);
|
197 | if (doc) ref = ref.doc(doc);
|
198 | ref = handleSubcollections(ref, subcollections);
|
199 | if (where) ref = addWhereToRef(ref, where);
|
200 | if (orderBy) ref = addOrderByToRef(ref, orderBy);
|
201 | if (limit) ref = ref.limit(limit);
|
202 | if (startAt) ref = (_ref5 = ref).startAt.apply(_ref5, _toConsumableArray(arrayify(startAt)));
|
203 | if (startAfter) ref = (_ref6 = ref).startAfter.apply(_ref6, _toConsumableArray(arrayify(startAfter)));
|
204 | if (endAt) ref = (_ref7 = ref).endAt.apply(_ref7, _toConsumableArray(arrayify(endAt)));
|
205 | if (endBefore) ref = (_ref8 = ref).endBefore.apply(_ref8, _toConsumableArray(arrayify(endBefore)));
|
206 | return ref;
|
207 | }
|
208 |
|
209 | function arrayToStr(key, value) {
|
210 | if (typeof value === 'string' || value instanceof String || (0, _isNumber2.default)(value)) {
|
211 | return "".concat(key, "=").concat(value);
|
212 | }
|
213 |
|
214 | if (typeof value[0] === 'string' || value[0] instanceof String) {
|
215 | return "".concat(key, "=").concat(value.join(':'));
|
216 | }
|
217 |
|
218 | if (value && typeof value.toString === 'function') {
|
219 | return "".concat(key, "=").concat(value.toString());
|
220 | }
|
221 |
|
222 | return value.map(function (val) {
|
223 | return arrayToStr(key, val);
|
224 | });
|
225 | }
|
226 |
|
227 | function pickQueryParams(obj) {
|
228 | return ['where', 'orderBy', 'limit', 'startAfter', 'startAt', 'endAt', 'endBefore'].reduce(function (acc, key) {
|
229 | return obj[key] ? _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, key, obj[key])) : acc;
|
230 | }, {});
|
231 | }
|
232 |
|
233 | function serialize(queryParams) {
|
234 | return Object.keys(queryParams).filter(function (key) {
|
235 | return queryParams[key] !== undefined;
|
236 | }).map(function (key) {
|
237 | return arrayToStr(key, queryParams[key]);
|
238 | }).join('&');
|
239 | }
|
240 |
|
241 | function getQueryName(meta) {
|
242 | if (typeof meta === 'string' || meta instanceof String) {
|
243 | return meta;
|
244 | }
|
245 |
|
246 | var collection = meta.collection,
|
247 | collectionGroup = meta.collectionGroup,
|
248 | doc = meta.doc,
|
249 | subcollections = meta.subcollections,
|
250 | storeAs = meta.storeAs,
|
251 | remainingMeta = _objectWithoutProperties(meta, ["collection", "collectionGroup", "doc", "subcollections", "storeAs"]);
|
252 |
|
253 | if (!collection && !collectionGroup) {
|
254 | throw new Error('Collection or Collection Group is required to build query name');
|
255 | }
|
256 |
|
257 | if (storeAs) {
|
258 | return storeAs;
|
259 | }
|
260 |
|
261 | var basePath = collection || collectionGroup;
|
262 |
|
263 | if (doc) {
|
264 | basePath = basePath.concat("/".concat(doc));
|
265 | }
|
266 |
|
267 | if (collection && subcollections) {
|
268 | console.error('Queries with subcollections must use "storeAs" to prevent invalid store updates. This closley matches the upcoming major release (v1), which stores subcollections at the top level by default.');
|
269 | var mappedCollections = subcollections.map(function (subcollection) {
|
270 | return getQueryName(subcollection);
|
271 | });
|
272 | basePath = "".concat(basePath, "/").concat(mappedCollections.join('/'));
|
273 | }
|
274 |
|
275 | var queryParams = pickQueryParams(remainingMeta);
|
276 |
|
277 | if (!(0, _isEmpty2.default)(queryParams)) {
|
278 | if (queryParams.where && !Array.isArray(queryParams.where)) {
|
279 | throw new Error('where parameter must be an array.');
|
280 | }
|
281 |
|
282 | basePath = basePath.concat('?', serialize(queryParams));
|
283 | }
|
284 |
|
285 | return basePath;
|
286 | }
|
287 |
|
288 | function getBaseQueryName(meta) {
|
289 | if (typeof meta === 'string' || meta instanceof String) {
|
290 | return meta;
|
291 | }
|
292 |
|
293 | var collection = meta.collection,
|
294 | collectionGroup = meta.collectionGroup,
|
295 | subcollections = meta.subcollections,
|
296 | remainingMeta = _objectWithoutProperties(meta, ["collection", "collectionGroup", "subcollections"]);
|
297 |
|
298 | if (!collection && !collectionGroup) {
|
299 | throw new Error('Collection or Collection Group is required to build query name');
|
300 | }
|
301 |
|
302 | var basePath = collection || collectionGroup;
|
303 |
|
304 | if (collection && subcollections) {
|
305 | var mappedCollections = subcollections.map(function (subcollection) {
|
306 | return getQueryName(subcollection);
|
307 | });
|
308 | basePath = "".concat(basePath, "/").concat(mappedCollections.join('/'));
|
309 | }
|
310 |
|
311 | var queryParams = pickQueryParams(remainingMeta);
|
312 |
|
313 | if (!(0, _isEmpty2.default)(queryParams)) {
|
314 | if (queryParams.where && !Array.isArray(queryParams.where)) {
|
315 | throw new Error('where parameter must be an array.');
|
316 | }
|
317 |
|
318 | basePath = basePath.concat('?', serialize(queryParams));
|
319 | }
|
320 |
|
321 | return basePath;
|
322 | }
|
323 |
|
324 | function confirmMetaAndConfig(firebase, meta) {
|
325 | if (!meta) {
|
326 | throw new Error('Meta data is required to attach listener.');
|
327 | }
|
328 |
|
329 | if (!firebase || !firebase._ || !firebase._.listeners) {
|
330 | throw new Error('Internal Firebase object required to attach listener. Confirm that reduxFirestore enhancer was added when you were creating your store');
|
331 | }
|
332 | }
|
333 |
|
334 | function listenerExists(firebase, meta) {
|
335 | confirmMetaAndConfig(firebase, meta);
|
336 | var name = getQueryName(meta);
|
337 | return !!firebase._.listeners[name];
|
338 | }
|
339 |
|
340 | function attachListener(firebase, dispatch, meta, unsubscribe) {
|
341 | confirmMetaAndConfig(firebase, meta);
|
342 | var name = getQueryName(meta);
|
343 |
|
344 | if (!firebase._.listeners[name]) {
|
345 | firebase._.listeners[name] = unsubscribe;
|
346 | }
|
347 |
|
348 | dispatch({
|
349 | type: _constants.actionTypes.SET_LISTENER,
|
350 | meta: meta,
|
351 | payload: {
|
352 | name: name
|
353 | }
|
354 | });
|
355 | return firebase._.listeners;
|
356 | }
|
357 |
|
358 | function detachListener(firebase, dispatch, meta) {
|
359 | var name = getQueryName(meta);
|
360 |
|
361 | if (firebase._.listeners[name]) {
|
362 | firebase._.listeners[name]();
|
363 |
|
364 | delete firebase._.listeners[name];
|
365 | }
|
366 |
|
367 | dispatch({
|
368 | type: _constants.actionTypes.UNSET_LISTENER,
|
369 | meta: meta,
|
370 | payload: {
|
371 | name: name
|
372 | }
|
373 | });
|
374 | }
|
375 |
|
376 | function queryStrToObj(queryPathStr, parsedPath) {
|
377 | var pathArr = parsedPath || (0, _trim2.default)(queryPathStr, ['/']).split('/');
|
378 |
|
379 | var _pathArr = _toArray(pathArr),
|
380 | collection = _pathArr[0],
|
381 | doc = _pathArr[1],
|
382 | subcollections = _pathArr.slice(2);
|
383 |
|
384 | var queryObj = {};
|
385 | if (collection) queryObj.collection = collection;
|
386 | if (doc) queryObj.doc = doc;
|
387 |
|
388 | if (subcollections.length) {
|
389 | queryObj.subcollections = [queryStrToObj('', subcollections)];
|
390 | }
|
391 |
|
392 | return queryObj;
|
393 | }
|
394 |
|
395 | function getQueryConfig(query) {
|
396 | if (typeof query === 'string' || query instanceof String) {
|
397 | return queryStrToObj(query);
|
398 | }
|
399 |
|
400 | if ((0, _isObject2.default)(query)) {
|
401 | if (!query.collection && !query.collectionGroup && !query.doc) {
|
402 | throw new Error('Collection, Collection Group and/or Doc are required parameters within query definition object.');
|
403 | }
|
404 |
|
405 | return query;
|
406 | }
|
407 |
|
408 | throw new Error('Invalid Path Definition: Only Strings and Objects are accepted.');
|
409 | }
|
410 |
|
411 | function getQueryConfigs(queries) {
|
412 | if (Array.isArray(queries)) {
|
413 | return queries.map(getQueryConfig);
|
414 | }
|
415 |
|
416 | if (typeof queries === 'string' || queries instanceof String) {
|
417 | return queryStrToObj(queries);
|
418 | }
|
419 |
|
420 | if ((0, _isObject2.default)(queries)) {
|
421 | return [getQueryConfig(queries)];
|
422 | }
|
423 |
|
424 | throw new Error('Querie(s) must be an Array or a string.');
|
425 | }
|
426 |
|
427 | function orderedFromSnap(snap) {
|
428 | var ordered = [];
|
429 |
|
430 | if (snap.data && snap.exists) {
|
431 | var obj = (0, _isObject2.default)(snap.data()) ? _objectSpread({
|
432 | id: snap.id
|
433 | }, snap.data() || snap.data) : {
|
434 | id: snap.id,
|
435 | data: snap.data()
|
436 | };
|
437 | snapshotCache.set(obj, snap);
|
438 | ordered.push(obj);
|
439 | } else if (snap.forEach) {
|
440 | snap.forEach(function (doc) {
|
441 | var obj = (0, _isObject2.default)(doc.data()) ? _objectSpread({
|
442 | id: doc.id
|
443 | }, doc.data() || doc.data) : {
|
444 | id: doc.id,
|
445 | data: doc.data()
|
446 | };
|
447 | snapshotCache.set(obj, doc);
|
448 | ordered.push(obj);
|
449 | });
|
450 | }
|
451 |
|
452 | snapshotCache.set(ordered, snap);
|
453 | return ordered;
|
454 | }
|
455 |
|
456 | function dataByIdSnapshot(snap) {
|
457 | var data = {};
|
458 |
|
459 | if (snap.data) {
|
460 | var snapData = snap.exists ? snap.data() : null;
|
461 |
|
462 | if (snapData) {
|
463 | snapshotCache.set(snapData, snap);
|
464 | }
|
465 |
|
466 | data[snap.id] = snapData;
|
467 | } else if (snap.forEach) {
|
468 | snap.forEach(function (doc) {
|
469 | var snapData = doc.data() || doc;
|
470 | snapshotCache.set(snapData, doc);
|
471 | data[doc.id] = snapData;
|
472 | });
|
473 | }
|
474 |
|
475 | if (!!data && Object.keys(data).length) {
|
476 | snapshotCache.set(data, snap);
|
477 | return data;
|
478 | }
|
479 |
|
480 | return null;
|
481 | }
|
482 |
|
483 | function getPopulateChild(firebase, populate, id) {
|
484 | return firestoreRef(firebase, {
|
485 | collection: populate.root,
|
486 | doc: id
|
487 | }).get().then(function (snap) {
|
488 | return _objectSpread({
|
489 | id: id
|
490 | }, snap.data());
|
491 | });
|
492 | }
|
493 |
|
494 | function populateList(firebase, originalObj, p, results) {
|
495 | if (!results[p.root]) {
|
496 | (0, _set2.default)(results, p.root, {});
|
497 | }
|
498 |
|
499 | return Promise.all((0, _map2.default)(originalObj, function (id, childKey) {
|
500 | var populateKey = id === true || p.populateByKey ? childKey : id;
|
501 | return getPopulateChild(firebase, p, populateKey).then(function (pc) {
|
502 | if (pc) {
|
503 | return (0, _set2.default)(results, "".concat(p.root, ".").concat(populateKey), pc);
|
504 | }
|
505 |
|
506 | return results;
|
507 | });
|
508 | }));
|
509 | }
|
510 |
|
511 | function getPopulateObj(str) {
|
512 | if (typeof str === 'string' || str instanceof String) {
|
513 | return str;
|
514 | }
|
515 |
|
516 | var strArray = str.split(':');
|
517 | return {
|
518 | child: strArray[0],
|
519 | root: strArray[1]
|
520 | };
|
521 | }
|
522 |
|
523 | function getPopulateObjs(arr) {
|
524 | if (!Array.isArray(arr)) {
|
525 | return arr;
|
526 | }
|
527 |
|
528 | return arr.map(function (o) {
|
529 | return (0, _isObject2.default)(o) ? o : getPopulateObj(o);
|
530 | });
|
531 | }
|
532 |
|
533 | function promisesForPopulate(firebase, dataKey, originalData, populatesIn) {
|
534 | var promisesArray = [];
|
535 | var results = {};
|
536 | var populatesForData = getPopulateObjs(typeof populatesIn === 'function' ? populatesIn(dataKey, originalData) : populatesIn);
|
537 | var dataHasPopulateChilds = populatesForData.some(function (populate) {
|
538 | return (0, _has2.default)(originalData, populate.child);
|
539 | });
|
540 |
|
541 | if (dataHasPopulateChilds) {
|
542 | populatesForData.forEach(function (p) {
|
543 | var childDataVal = (0, _get2.default)(originalData, p.child);
|
544 |
|
545 | if (typeof childDataVal === 'string' || childDataVal instanceof String) {
|
546 | return promisesArray.push(getPopulateChild(firebase, p, childDataVal).then(function (v) {
|
547 | if (v) {
|
548 | (0, _set2.default)(results, "".concat(p.storeAs ? p.storeAs : p.root, ".").concat(childDataVal), v);
|
549 | }
|
550 | }));
|
551 | }
|
552 |
|
553 | return promisesArray.push(populateList(firebase, childDataVal, p, results));
|
554 | });
|
555 | } else {
|
556 | (0, _forEach2.default)(originalData, function (d, key) {
|
557 | var populatesForDataItem = getPopulateObjs(typeof populatesIn === 'function' ? populatesIn(key, d) : populatesIn);
|
558 | (0, _forEach2.default)(populatesForDataItem, function (p) {
|
559 | var idOrList = (0, _get2.default)(d, p.child);
|
560 |
|
561 | if (!idOrList) {
|
562 | return;
|
563 | }
|
564 |
|
565 | if (typeof idOrList === 'string' || idOrList instanceof String) {
|
566 | return promisesArray.push(getPopulateChild(firebase, p, idOrList).then(function (v) {
|
567 | if (v) {
|
568 | (0, _set2.default)(results, "".concat(p.storeAs ? p.storeAs : p.root, ".").concat(idOrList), v);
|
569 | }
|
570 |
|
571 | return results;
|
572 | }));
|
573 | }
|
574 |
|
575 | if (Array.isArray(idOrList) || (0, _isObject2.default)(idOrList)) {
|
576 | return promisesArray.push(populateList(firebase, idOrList, p, results));
|
577 | }
|
578 | });
|
579 | });
|
580 | }
|
581 |
|
582 | return Promise.all(promisesArray).then(function () {
|
583 | return results;
|
584 | });
|
585 | }
|
586 |
|
587 | var changeTypeToEventType = {
|
588 | added: _constants.actionTypes.DOCUMENT_ADDED,
|
589 | removed: _constants.actionTypes.DOCUMENT_REMOVED,
|
590 | modified: _constants.actionTypes.DOCUMENT_MODIFIED
|
591 | };
|
592 |
|
593 | function docChangeEvent(change) {
|
594 | var originalMeta = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
595 |
|
596 | var meta = _objectSpread(_objectSpread({}, (0, _cloneDeep2.default)(originalMeta)), {}, {
|
597 | path: change.doc.ref.path
|
598 | });
|
599 |
|
600 | if (originalMeta.subcollections && !originalMeta.storeAs) {
|
601 | meta.subcollections[0] = _objectSpread(_objectSpread({}, meta.subcollections[0]), {}, {
|
602 | doc: change.doc.id
|
603 | });
|
604 | } else {
|
605 | meta.doc = change.doc.id;
|
606 | }
|
607 |
|
608 | return {
|
609 | type: changeTypeToEventType[change.type] || _constants.actionTypes.DOCUMENT_MODIFIED,
|
610 | meta: meta,
|
611 | payload: {
|
612 | data: change.doc.data(),
|
613 | ordered: {
|
614 | oldIndex: change.oldIndex,
|
615 | newIndex: change.newIndex
|
616 | }
|
617 | }
|
618 | };
|
619 | }
|
620 |
|
621 | function dispatchListenerResponse(_ref9) {
|
622 | var dispatch = _ref9.dispatch,
|
623 | docData = _ref9.docData,
|
624 | meta = _ref9.meta,
|
625 | firebase = _ref9.firebase;
|
626 |
|
627 | var _ref10 = firebase._.config || {},
|
628 | mergeOrdered = _ref10.mergeOrdered,
|
629 | mergeOrderedDocUpdates = _ref10.mergeOrderedDocUpdates,
|
630 | mergeOrderedCollectionUpdates = _ref10.mergeOrderedCollectionUpdates;
|
631 |
|
632 | var docChanges = typeof docData.docChanges === 'function' ? docData.docChanges() : docData.docChanges;
|
633 |
|
634 | if (docChanges && docChanges.length < docData.size) {
|
635 | docChanges.forEach(function (change) {
|
636 | dispatch(docChangeEvent(change, meta));
|
637 | });
|
638 | } else {
|
639 | dispatch({
|
640 | type: _constants.actionTypes.LISTENER_RESPONSE,
|
641 | meta: meta,
|
642 | payload: {
|
643 | data: dataByIdSnapshot(docData),
|
644 | ordered: orderedFromSnap(docData)
|
645 | },
|
646 | merge: {
|
647 | docs: mergeOrdered && mergeOrderedDocUpdates,
|
648 | collections: mergeOrdered && mergeOrderedCollectionUpdates
|
649 | }
|
650 | });
|
651 | }
|
652 | }
|
653 |
|
654 | function getPopulateActions(_ref11) {
|
655 | var firebase = _ref11.firebase,
|
656 | docData = _ref11.docData,
|
657 | meta = _ref11.meta;
|
658 | return promisesForPopulate(firebase, docData.id, dataByIdSnapshot(docData), meta.populates).then(function (populateResults) {
|
659 | return Object.keys(populateResults).map(function (resultKey) {
|
660 | return {
|
661 | meta: {
|
662 | collection: resultKey
|
663 | },
|
664 | payload: {
|
665 | data: populateResults[resultKey]
|
666 | },
|
667 | requesting: false,
|
668 | requested: true
|
669 | };
|
670 | });
|
671 | }).catch(function (populateErr) {
|
672 | console.error('Error with populate:', populateErr, meta);
|
673 | return Promise.reject(populateErr);
|
674 | });
|
675 | } |
\ | No newline at end of file |