simpler-state
Advanced tools
Comparing version 1.0.0-rc.9-pre.2 to 1.0.0-rc.9-pre.3
@@ -15,8 +15,8 @@ export const persistence = (key, options = {}) => { | ||
const getItem = async () => { | ||
const value = await storage.getItem(key); | ||
if (value) { | ||
entity.set(await deserialize(value)); | ||
} | ||
const getItem = () => { | ||
_promise(storage.getItem(key)).then(value => { | ||
if (value) { | ||
_promise(deserialize(value)).then(value => entity.set(value)); | ||
} | ||
}); | ||
}; | ||
@@ -31,5 +31,6 @@ | ||
const setItem = async () => { | ||
const value = await serialize(entity.get()); | ||
return storage.setItem(key, value); | ||
const setItem = () => { | ||
_promise(serialize(entity.get())).then(value => { | ||
storage.setItem(key, value); | ||
}); | ||
}; | ||
@@ -64,2 +65,8 @@ | ||
export default persistence; | ||
export default persistence; | ||
/** Turns a value into a Promise (if it's not already) */ | ||
const _promise = val => { | ||
const notPromise = typeof val !== 'object' || val === null || typeof val.then !== 'function'; | ||
return notPromise ? new Promise(resolve => resolve(val)) : val; | ||
}; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
exports.__esModule = true; | ||
exports.default = exports.persistence = void 0; | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); | ||
var persistence = function persistence(key, options) { | ||
@@ -31,42 +25,12 @@ if (options === void 0) { | ||
var getItem = /*#__PURE__*/function () { | ||
var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() { | ||
var value; | ||
return _regenerator.default.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return storage.getItem(key); | ||
var getItem = function getItem() { | ||
_promise(storage.getItem(key)).then(function (value) { | ||
if (value) { | ||
_promise(deserialize(value)).then(function (value) { | ||
return entity.set(value); | ||
}); | ||
} | ||
}); | ||
}; | ||
case 2: | ||
value = _context.sent; | ||
if (!value) { | ||
_context.next = 9; | ||
break; | ||
} | ||
_context.t0 = entity; | ||
_context.next = 7; | ||
return deserialize(value); | ||
case 7: | ||
_context.t1 = _context.sent; | ||
_context.t0.set.call(_context.t0, _context.t1); | ||
case 9: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
return function getItem() { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
origInit(); // Fetch persisted value (if any) from storage | ||
@@ -81,29 +45,8 @@ | ||
var setItem = /*#__PURE__*/function () { | ||
var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() { | ||
var value; | ||
return _regenerator.default.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_context2.next = 2; | ||
return serialize(entity.get()); | ||
var setItem = function setItem() { | ||
_promise(serialize(entity.get())).then(function (value) { | ||
storage.setItem(key, value); | ||
}); | ||
}; | ||
case 2: | ||
value = _context2.sent; | ||
return _context2.abrupt("return", storage.setItem(key, value)); | ||
case 4: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2); | ||
})); | ||
return function setItem() { | ||
return _ref2.apply(this, arguments); | ||
}; | ||
}(); | ||
origSet.apply(void 0, arguments); // Persist the new value to storage | ||
@@ -140,2 +83,11 @@ | ||
var _default = persistence; | ||
exports.default = _default; | ||
/** Turns a value into a Promise (if it's not already) */ | ||
exports.default = _default; | ||
var _promise = function _promise(val) { | ||
var notPromise = typeof val !== 'object' || val === null || typeof val.then !== 'function'; | ||
return notPromise ? new Promise(function (resolve) { | ||
return resolve(val); | ||
}) : val; | ||
}; |
{ | ||
"name": "simpler-state", | ||
"version": "1.0.0-rc.9-pre.2", | ||
"version": "1.0.0-rc.9-pre.3", | ||
"description": "The simplest app state management for React", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24119
480