UNPKG

3.01 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.isComposable = isComposable;
7exports.default = queriesReducer;
8
9var _unset2 = _interopRequireDefault(require("lodash/unset"));
10
11var _get2 = _interopRequireDefault(require("lodash/get"));
12
13var _set2 = _interopRequireDefault(require("lodash/set"));
14
15var _immer = _interopRequireDefault(require("immer"));
16
17var _constants = require("../constants");
18
19var _query = require("../utils/query");
20
21function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
23function 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; }
24
25function _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; }
26
27function _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; }
28
29function isComposable(action) {
30 return !!(0, _get2.default)(action, 'meta.where') && !!(0, _get2.default)(action, 'meta.collection');
31}
32
33function queriesReducer() {
34 var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
35 var action = arguments.length > 1 ? arguments[1] : undefined;
36 return (0, _immer.default)(state, function (draft) {
37 if (!isComposable(action)) {
38 return state;
39 }
40
41 var key = (0, _query.getBaseQueryName)(action.meta);
42
43 switch (action.type) {
44 case _constants.actionTypes.GET_SUCCESS:
45 case _constants.actionTypes.LISTENER_RESPONSE:
46 draft[key] = _objectSpread({
47 data: action.payload.data
48 }, action.meta);
49 return draft;
50
51 case _constants.actionTypes.UNSET_LISTENER:
52 if (draft[key]) {
53 draft[key].data = undefined;
54 }
55
56 return draft;
57
58 case _constants.actionTypes.DOCUMENT_ADDED:
59 case _constants.actionTypes.DOCUMENT_MODIFIED:
60 (0, _set2.default)(draft, [key, 'data', action.meta.doc], action.payload.data);
61 return draft;
62
63 case _constants.actionTypes.DOCUMENT_REMOVED:
64 case _constants.actionTypes.DELETE_SUCCESS:
65 (0, _unset2.default)(draft, [key, 'data', action.meta.doc]);
66 return draft;
67
68 default:
69 return state;
70 }
71 });
72}
\No newline at end of file