UNPKG

6.1 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.shouldLoad = exports.hasPrev = exports.hasNext = exports.getMeta = exports.getId = exports.shouldRefresh = exports.isError = exports.getModificationTime = exports.isBusy = exports.isInitialized = exports.isValid = exports.getTransformation = exports.hasStatus = exports.getStatus = exports.cloneStatus = exports.setStatus = exports.updateStatus = exports.createStatus = exports.busyStatus = exports.validationStatus = exports.STATUS = undefined;
7
8var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
9
10exports.isExpired = isExpired;
11
12var _lodash = require('lodash');
13
14var _lodash2 = _interopRequireDefault(_lodash);
15
16function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
18function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
19
20var STATUS = exports.STATUS = '@@redux-api-state/status';
21
22var validationStatus = exports.validationStatus = Object.freeze({
23 NONE: 'none',
24 INVALID: 'invalid',
25 VALID: 'valid'
26});
27
28var busyStatus = exports.busyStatus = Object.freeze({
29 IDLE: 'idle',
30 BUSY: 'busy'
31});
32
33var createStatus = exports.createStatus = function createStatus() {
34 var description = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
35 return _extends({}, description, {
36 validationStatus: validationStatus.NONE,
37 busyStatus: busyStatus.IDLE,
38 error: false,
39 modifiedTimestamp: Date.now(),
40 transformation: {}
41 });
42};
43
44var updateStatus = exports.updateStatus = function updateStatus(status, update) {
45 var markChange = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
46
47 var timestamp = markChange ? { modifiedTimestamp: Date.now() } : {};
48 return _lodash2.default.merge({}, status, update, timestamp);
49};
50
51var setStatus = exports.setStatus = function setStatus(obj, status) {
52 if (_lodash2.default.has(obj, STATUS)) {
53 // eslint-disable-next-line no-param-reassign
54 obj[STATUS] = status;
55 } else {
56 Object.defineProperty(obj, STATUS, {
57 value: status,
58 enumerable: false,
59 writable: true
60 });
61 }
62};
63
64var cloneStatus = exports.cloneStatus = function cloneStatus(sourceObject, destinationObject) {
65 var markChange = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
66
67 if (!sourceObject[STATUS]) {
68 return;
69 }
70 setStatus(destinationObject, updateStatus(sourceObject[STATUS], {}, markChange));
71};
72
73function statusProp(obj, prop) {
74 var propPath = _lodash2.default.isArray(prop) ? prop : [prop];
75 return _lodash2.default.get(obj, [STATUS].concat(_toConsumableArray(propPath)));
76}
77
78var getStatus = exports.getStatus = function getStatus(obj) {
79 return _lodash2.default.get(obj, [STATUS]);
80};
81
82var hasStatus = exports.hasStatus = function hasStatus(obj) {
83 return _lodash2.default.has(obj, [STATUS]);
84};
85
86var getTransformation = exports.getTransformation = function getTransformation(obj) {
87 return statusProp(obj, 'transformation');
88};
89
90var isValid = exports.isValid = function isValid(obj) {
91 return !!getStatus(obj) && statusProp(obj, 'validationStatus') === validationStatus.VALID;
92};
93
94var isInitialized = exports.isInitialized = function isInitialized(obj) {
95 return !!getStatus(obj) && statusProp(obj, 'validationStatus') !== validationStatus.NONE;
96};
97
98var isBusy = exports.isBusy = function isBusy(obj) {
99 return !!(statusProp(obj, 'busyStatus') === busyStatus.BUSY);
100};
101
102var getModificationTime = exports.getModificationTime = function getModificationTime(obj) {
103 return statusProp(obj, 'modifiedTimestamp');
104};
105
106var isError = exports.isError = function isError(obj) {
107 return !!statusProp(obj, 'error');
108};
109
110function isExpired(reference) {
111 var status = getStatus(reference);
112 if (!status) {
113 return false;
114 }
115
116 var expirationTime = status.expirationTime,
117 modifiedTimestamp = status.modifiedTimestamp;
118
119 if (!expirationTime) {
120 return false;
121 }
122
123 var referenceLifetime = Date.now() - modifiedTimestamp;
124 // TODO: When `expiration` becomes rio plugin, save milliseconds in
125 // the status to avoid conversation
126 return expirationTime * 1000 < referenceLifetime;
127}
128
129var shouldRefresh = exports.shouldRefresh = function shouldRefresh(obj) {
130 var ignoreError = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
131 return (!isValid(obj) || isExpired(obj)) && !isBusy(obj) && (!isError(obj) || ignoreError);
132};
133
134var getId = exports.getId = function getId(obj) {
135 return statusProp(obj, 'id');
136};
137
138var getMeta = exports.getMeta = function getMeta(obj) {
139 return statusProp(obj, 'meta');
140};
141
142var hasNext = exports.hasNext = function hasNext(obj) {
143 return !!statusProp(obj, ['links', 'next']);
144};
145
146var hasPrev = exports.hasPrev = function hasPrev(obj) {
147 return !!statusProp(obj, ['links', 'prev']);
148};
149
150/**
151 * Checks whether a load is needed for selected propName on given set of new and current props.
152 * @param nextProps New props
153 * @param props Current props
154 * @param propName Prop to check
155 * @param ignoreError flag indicating whether shouldRefresh function returns 'true' for
156 * objects in error
157 */
158var shouldLoad = exports.shouldLoad = function shouldLoad(nextProps, props, propName) {
159 var ignoreError = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
160
161 if (!nextProps || !propName) {
162 throw Error('Invalid shouldLoad call. Check provided arguments.');
163 }
164
165 var prop = _lodash2.default.get(props, propName);
166 var nextProp = _lodash2.default.get(nextProps, propName);
167
168 // if both props point to same object, nothing has changed
169 if (nextProp && nextProp === prop) {
170 return false;
171 }
172
173 // finally check whether refresh is needed
174 return shouldRefresh(nextProp, ignoreError);
175};
\No newline at end of file