simpler-state
Advanced tools
Comparing version 2.0.0-rc.11 to 2.0.0-rc.12
@@ -42,2 +42,3 @@ import { strictEqual } from './equality'; | ||
return function (valueOrUpdaterFn, alias) { | ||
var _window$__inspector; | ||
if (typeof valueOrUpdaterFn === 'function') valueOrUpdaterFn = valueOrUpdaterFn(entity._value); | ||
@@ -48,3 +49,3 @@ entity._value = valueOrUpdaterFn; | ||
}); | ||
window.__onSetEntity == null ? void 0 : window.__onSetEntity(entity, alias != null ? alias : '<anonymous>'); | ||
(_window$__inspector = window.__inspector) == null ? void 0 : _window$__inspector.onSet(entity, alias != null ? alias : '<anonymous>'); | ||
}; | ||
@@ -55,2 +56,3 @@ } | ||
return function () { | ||
var _window$__inspector2; | ||
if (!entity.name) entity.name = "entity" + nextId++; | ||
@@ -62,3 +64,3 @@ if (initialValue instanceof Promise) initialValue.then(function (value) { | ||
});else entity._value = initialValue; | ||
window.__onInitEntity == null ? void 0 : window.__onInitEntity(entity); | ||
(_window$__inspector2 = window.__inspector) == null ? void 0 : _window$__inspector2.onInit(entity); | ||
}; | ||
@@ -65,0 +67,0 @@ } |
@@ -1,35 +0,1 @@ | ||
function getLocalStorage() { | ||
try { | ||
return localStorage; | ||
} catch (err) { | ||
return null; | ||
} | ||
} | ||
function getSessionStorage() { | ||
try { | ||
return sessionStorage; | ||
} catch (err) { | ||
return null; | ||
} | ||
} | ||
function validateCustomStorage(storage) { | ||
if (typeof storage.getItem !== 'function' || typeof storage.setItem !== 'function') throw new Error('Persistence: Invalid storage.'); | ||
return storage; | ||
} | ||
function getItem(storage, key, deserialize, callback) { | ||
var res = storage.getItem(key); | ||
if (res === null) return; | ||
if (typeof res !== 'string' && typeof res.then === 'function') res.then(function (val) { | ||
if (val != null) processValue(deserialize, val, callback); | ||
});else processValue(deserialize, res, callback); | ||
} | ||
function setItem(storage, key, value, serialize) { | ||
processValue(serialize, value, function (res) { | ||
return storage.setItem(key, res); | ||
}); | ||
} | ||
function processValue(func, value, callback) { | ||
var res = func(value); | ||
if (res && typeof res.then === 'function') res.then(callback);else callback(res); | ||
} | ||
export function persistence(key, options) { | ||
@@ -41,6 +7,4 @@ var _options$storage; | ||
if (typeof key !== 'string') throw new Error('Persistence requires a string key.'); | ||
var storage; | ||
var storageOption = (_options$storage = options.storage) != null ? _options$storage : 'local'; | ||
if (storageOption === 'local') storage = getLocalStorage();else if (storageOption === 'session') storage = getSessionStorage();else storage = validateCustomStorage(storageOption); | ||
if (!storage) { | ||
var storage = (_options$storage = options.storage) != null ? _options$storage : window.localStorage; | ||
if (!isValidStorage(storage)) { | ||
console.warn('Storage unavailable. Persistence disabled.'); | ||
@@ -65,2 +29,21 @@ return {}; | ||
}; | ||
} | ||
function isValidStorage(storage) { | ||
return storage && typeof storage.getItem === 'function' && typeof storage.setItem === 'function'; | ||
} | ||
function getItem(storage, key, deserialize, callback) { | ||
var res = storage.getItem(key); | ||
if (res === null) return; | ||
if (typeof res !== 'string' && typeof res.then === 'function') res.then(function (val) { | ||
if (val != null) processValue(deserialize, val, callback); | ||
});else processValue(deserialize, res, callback); | ||
} | ||
function setItem(storage, key, value, serialize) { | ||
processValue(serialize, value, function (res) { | ||
return storage.setItem(key, res); | ||
}); | ||
} | ||
function processValue(func, value, callback) { | ||
var res = func(value); | ||
if (res && typeof res.then === 'function') res.then(callback);else callback(res); | ||
} |
@@ -14,4 +14,6 @@ import _extends from "@babel/runtime/helpers/esm/extends"; | ||
isInspectorEnabled = condition; | ||
window.__onInitEntity = condition ? onInit : undefined; | ||
window.__onSetEntity = condition ? onSet : undefined; | ||
window.__inspector = condition ? { | ||
onInit: onInit, | ||
onSet: onSet | ||
} : undefined; | ||
} | ||
@@ -18,0 +20,0 @@ export var features = { |
import { getStore, isStoreEnabled } from './store'; | ||
export default function resetAll() { | ||
export function resetAll() { | ||
if (!isStoreEnabled()) throw new Error('resetAll() requires the entity store. Call enableStore() at startup.'); | ||
@@ -4,0 +4,0 @@ getStore().forEach(function (entity) { |
@@ -46,2 +46,3 @@ "use strict"; | ||
return function (valueOrUpdaterFn, alias) { | ||
var _window$__inspector; | ||
if (typeof valueOrUpdaterFn === 'function') valueOrUpdaterFn = valueOrUpdaterFn(entity._value); | ||
@@ -52,3 +53,3 @@ entity._value = valueOrUpdaterFn; | ||
}); | ||
window.__onSetEntity == null ? void 0 : window.__onSetEntity(entity, alias != null ? alias : '<anonymous>'); | ||
(_window$__inspector = window.__inspector) == null ? void 0 : _window$__inspector.onSet(entity, alias != null ? alias : '<anonymous>'); | ||
}; | ||
@@ -59,2 +60,3 @@ } | ||
return function () { | ||
var _window$__inspector2; | ||
if (!entity.name) entity.name = "entity" + nextId++; | ||
@@ -66,3 +68,3 @@ if (initialValue instanceof Promise) initialValue.then(function (value) { | ||
});else entity._value = initialValue; | ||
window.__onInitEntity == null ? void 0 : window.__onInitEntity(entity); | ||
(_window$__inspector2 = window.__inspector) == null ? void 0 : _window$__inspector2.onInit(entity); | ||
}; | ||
@@ -69,0 +71,0 @@ } |
{ | ||
"name": "simpler-state", | ||
"version": "2.0.0-rc.11", | ||
"version": "2.0.0-rc.12", | ||
"description": "The simplest app state management for React", | ||
@@ -37,5 +37,2 @@ "keywords": [ | ||
}, | ||
"peerDependencies": { | ||
"react": ">=16.8.0" | ||
}, | ||
"dependencies": { | ||
@@ -48,5 +45,6 @@ "@babel/runtime": "^7.21.0", | ||
"@babel/core": "^7.21.4", | ||
"@testing-library/react": "^14.0.0", | ||
"@types/jest": "^29.5.1", | ||
"@types/react": "^16.14.25", | ||
"@types/react-dom": "^16.9.15", | ||
"@types/react": "^18.2.0", | ||
"@types/react-dom": "^18.2.1", | ||
"@types/use-sync-external-store": "^0.0.3", | ||
@@ -57,4 +55,2 @@ "babel-jest": "^29.5.0", | ||
"cross-env": "^5.2.1", | ||
"enzyme": "^3.11.0", | ||
"enzyme-adapter-react-16": "^1.15.2", | ||
"eslint": "^8.39.0", | ||
@@ -67,4 +63,4 @@ "eslint-config-prettier": "^8.8.0", | ||
"prettier": "^2.8.7", | ||
"react": "^16.14.0", | ||
"react-dom": "^16.14.0", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"rimraf": "^2.7.1", | ||
@@ -71,0 +67,0 @@ "ts-jest": "^29.1.0", |
@@ -18,5 +18,5 @@ import type { Plugin } from '../entity'; | ||
export declare function persistence(key: string, options?: { | ||
storage?: Storage | AsyncStorage | 'local' | 'session'; | ||
storage?: Storage | AsyncStorage; | ||
serializeFn?: (value: any) => string | Promise<string>; | ||
deserializeFn?: (value: string) => any | Promise<any>; | ||
}): Plugin; |
@@ -5,36 +5,2 @@ "use strict"; | ||
exports.persistence = persistence; | ||
function getLocalStorage() { | ||
try { | ||
return localStorage; | ||
} catch (err) { | ||
return null; | ||
} | ||
} | ||
function getSessionStorage() { | ||
try { | ||
return sessionStorage; | ||
} catch (err) { | ||
return null; | ||
} | ||
} | ||
function validateCustomStorage(storage) { | ||
if (typeof storage.getItem !== 'function' || typeof storage.setItem !== 'function') throw new Error('Persistence: Invalid storage.'); | ||
return storage; | ||
} | ||
function getItem(storage, key, deserialize, callback) { | ||
var res = storage.getItem(key); | ||
if (res === null) return; | ||
if (typeof res !== 'string' && typeof res.then === 'function') res.then(function (val) { | ||
if (val != null) processValue(deserialize, val, callback); | ||
});else processValue(deserialize, res, callback); | ||
} | ||
function setItem(storage, key, value, serialize) { | ||
processValue(serialize, value, function (res) { | ||
return storage.setItem(key, res); | ||
}); | ||
} | ||
function processValue(func, value, callback) { | ||
var res = func(value); | ||
if (res && typeof res.then === 'function') res.then(callback);else callback(res); | ||
} | ||
function persistence(key, options) { | ||
@@ -46,6 +12,4 @@ var _options$storage; | ||
if (typeof key !== 'string') throw new Error('Persistence requires a string key.'); | ||
var storage; | ||
var storageOption = (_options$storage = options.storage) != null ? _options$storage : 'local'; | ||
if (storageOption === 'local') storage = getLocalStorage();else if (storageOption === 'session') storage = getSessionStorage();else storage = validateCustomStorage(storageOption); | ||
if (!storage) { | ||
var storage = (_options$storage = options.storage) != null ? _options$storage : window.localStorage; | ||
if (!isValidStorage(storage)) { | ||
console.warn('Storage unavailable. Persistence disabled.'); | ||
@@ -70,2 +34,21 @@ return {}; | ||
}; | ||
} | ||
function isValidStorage(storage) { | ||
return storage && typeof storage.getItem === 'function' && typeof storage.setItem === 'function'; | ||
} | ||
function getItem(storage, key, deserialize, callback) { | ||
var res = storage.getItem(key); | ||
if (res === null) return; | ||
if (typeof res !== 'string' && typeof res.then === 'function') res.then(function (val) { | ||
if (val != null) processValue(deserialize, val, callback); | ||
});else processValue(deserialize, res, callback); | ||
} | ||
function setItem(storage, key, value, serialize) { | ||
processValue(serialize, value, function (res) { | ||
return storage.setItem(key, res); | ||
}); | ||
} | ||
function processValue(func, value, callback) { | ||
var res = func(value); | ||
if (res && typeof res.then === 'function') res.then(callback);else callback(res); | ||
} |
@@ -35,4 +35,6 @@ import type { MutableMap } from './registry'; | ||
}; | ||
__onInitEntity?: typeof onInit; | ||
__onSetEntity?: typeof onSet; | ||
__inspector?: { | ||
onInit: typeof onInit; | ||
onSet: typeof onSet; | ||
}; | ||
} | ||
@@ -39,0 +41,0 @@ } |
@@ -23,4 +23,6 @@ "use strict"; | ||
isInspectorEnabled = condition; | ||
window.__onInitEntity = condition ? onInit : undefined; | ||
window.__onSetEntity = condition ? onSet : undefined; | ||
window.__inspector = condition ? { | ||
onInit: onInit, | ||
onSet: onSet | ||
} : undefined; | ||
} | ||
@@ -27,0 +29,0 @@ var features = { |
@@ -10,2 +10,2 @@ /** | ||
*/ | ||
export default function resetAll(): void; | ||
export declare function resetAll(): void; |
"use strict"; | ||
exports.__esModule = true; | ||
exports.default = resetAll; | ||
exports.resetAll = resetAll; | ||
var _store = require("./store"); | ||
@@ -6,0 +6,0 @@ function resetAll() { |
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
2
23
35655
1000