redux-toolkit-optimistic
Version:
Helper library for Redux Toolkit to perform optimistic updates.
138 lines (108 loc) • 6 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.performOptimisticUpdate = performOptimisticUpdate;
exports.performManyOptimisticUpdates = performManyOptimisticUpdates;
exports.revertOptimisticUpdate = revertOptimisticUpdate;
exports.revertManyOptimisticUpdates = revertManyOptimisticUpdates;
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
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); }
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; }
/**
* @param {*} state The current state of the given slice
* @param {*} adapter The entity adapter for the slice
* @param {{ id: string, changes: object}} update The update object
*/
function performOptimisticUpdate(state, adapter, update) {
var incorrectUpdateArgError = new Error('Incorrect `update` arg sent to `performOptimisticUpdate`. Expected: { id: <string>, changes: <object> }');
if (!update || (0, _typeof2.default)(update) !== 'object') {
throw incorrectUpdateArgError;
}
var id = update.id,
changes = update.changes;
if (typeof id !== 'string' || (0, _typeof2.default)(changes) !== 'object') {
throw incorrectUpdateArgError;
} // Store the previous data to handle rejection of optimistic update
state.entities[id].previousChanges = Object.keys(changes).reduce(function (acc, key) {
acc[key] = state.entities[id][key];
return acc;
}, {});
adapter.updateOne(state, update);
}
/**
* @param {*} state The current state of the given slice
* @param {*} adapter The entity adapter for the slice
* @param {{ id: string, changes: object}[]} update The update object
*/
function performManyOptimisticUpdates(state, adapter, update) {
var incorrectUpdateArgError = new Error('Incorrect `update` arg sent to `performManyOptimisticUpdates`. Expected: [{ id: <string>, changes: <object> }, ...]');
if (!update || !Array.isArray(update)) {
throw incorrectUpdateArgError;
} // Store the previous data to handle rejection of optimistic updates
var _iterator = _createForOfIteratorHelper(update),
_step;
try {
var _loop = function _loop() {
var entity = _step.value;
if ((0, _typeof2.default)(entity) !== 'object' || typeof entity.id !== 'string' || (0, _typeof2.default)(entity.changes) !== 'object') {
throw incorrectUpdateArgError;
}
var id = entity.id,
changes = entity.changes;
state.entities[id].previousChanges = Object.keys(changes).reduce(function (acc, key) {
acc[key] = state.entities[id][key];
return acc;
}, {});
};
for (_iterator.s(); !(_step = _iterator.n()).done;) {
_loop();
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
adapter.updateMany(state, update);
}
/**
* @param {*} state The current state of the given slice
* @param {*} adapter The entity adapter for the slice
* @param {string} id The ID of the entity to revert to its prior state
*/
function revertOptimisticUpdate(state, adapter, id) {
var _state$entities$id;
if (typeof id !== 'string') {
throw new Error('Incorrect `id` arg sent to `revertOptimisticUpdate`. Expected: <string>');
}
var previousChanges = state === null || state === void 0 ? void 0 : (_state$entities$id = state.entities[id]) === null || _state$entities$id === void 0 ? void 0 : _state$entities$id.previousChanges;
if (!previousChanges) return;
adapter.updateOne(state, {
id: id,
changes: previousChanges
});
}
/**
* Redux toolkit helper function that reverts multiple optimistic updates
* @param {*} state The current state of the given slice
* @param {*} adapter The entity adapter for the slice
* @param {string[]} ids The list of IDs to revert to prior state
*/
function revertManyOptimisticUpdates(state, adapter, ids) {
if (!Array.isArray(ids)) {
throw new Error('Incorrect `ids` arg sent to `revertManyOptimisticUpdates`. Expected: [<string>]');
}
var update = ids.map(function (id) {
var _state$entities$id2;
var changes = state === null || state === void 0 ? void 0 : (_state$entities$id2 = state.entities[id]) === null || _state$entities$id2 === void 0 ? void 0 : _state$entities$id2.previousChanges;
if (!changes) return null;
return {
id: id,
changes: changes
};
}).filter(Boolean);
if (!update.length) return;
adapter.updateMany(state, update);
}