simpler-state
Advanced tools
Comparing version 1.2.1 to 1.2.2-rc.1
@@ -11,5 +11,3 @@ import { useEntity } from './useEntity'; | ||
newEntity._subscribe = createSubscribe(newEntity); | ||
newEntity.get = () => newEntity._value; | ||
newEntity.set = createSetter(newEntity); | ||
@@ -19,15 +17,13 @@ newEntity.init = createInit(newEntity, initialValue); | ||
applyPlugins(newEntity, plugins); | ||
newEntity.init(); // Add this entity to store (if enabled) for use with `resetAll` | ||
newEntity.init(); | ||
// Add this entity to store (if enabled) for use with `resetAll` | ||
if (isStoreEnabled()) store.add(newEntity); | ||
return newEntity; | ||
}; | ||
const createSetter = entity => (newValue, ...updaterArgs) => { | ||
if (typeof newValue === 'function') newValue = newValue(entity._value, ...updaterArgs); | ||
entity._value = newValue; | ||
entity._subscribers.forEach(cb => cb(entity._value)); | ||
}; | ||
const createInit = (entity, initialValue) => { | ||
@@ -42,10 +38,7 @@ return initialValue && typeof initialValue.then === 'function' ? () => { | ||
}; | ||
const createHook = entity => { | ||
return (...args) => useEntity(entity, ...args); | ||
}; | ||
const createSubscribe = entity => subscriberFn => { | ||
entity._subscribers.add(subscriberFn); | ||
return () => { | ||
@@ -55,7 +48,5 @@ entity._subscribers.delete(subscriberFn); | ||
}; | ||
export const applyPlugins = (entity, plugins) => { | ||
plugins.forEach(plugin => { | ||
if (typeof plugin !== 'object') throw new Error('Invalid plug-in'); | ||
const overrideMethod = method => { | ||
@@ -68,3 +59,2 @@ if (typeof plugin[method] === 'function') { | ||
}; | ||
overrideMethod('init'); | ||
@@ -71,0 +61,0 @@ overrideMethod('set'); |
@@ -5,3 +5,2 @@ export const persistence = (key, options = {}) => { | ||
if (storage === 'local') storage = getLocalStorage();else if (storage === 'session') storage = getSessionStorage();else validateCustomStorage(storage); | ||
if (!storage) { | ||
@@ -11,8 +10,8 @@ console.warn('Storage unavailable. Persistence disabled.'); | ||
} | ||
return { | ||
init: (origInit, entity) => () => { | ||
const deserialize = options.deserializeFn || JSON.parse; | ||
origInit(); // Fetch persisted value (if any) from storage | ||
origInit(); | ||
// Fetch persisted value (if any) from storage | ||
getItem(storage, key, deserialize, entity.set); | ||
@@ -22,4 +21,5 @@ }, | ||
const serialize = options.serializeFn || JSON.stringify; | ||
origSet(...args); // Persist the new value to storage | ||
origSet(...args); | ||
// Persist the new value to storage | ||
setItem(storage, key, entity.get(), serialize); | ||
@@ -29,3 +29,2 @@ } | ||
}; | ||
const getLocalStorage = () => { | ||
@@ -38,3 +37,2 @@ try { | ||
}; | ||
const getSessionStorage = () => { | ||
@@ -47,7 +45,5 @@ try { | ||
}; | ||
const validateCustomStorage = storage => { | ||
if (typeof storage.getItem !== 'function' || typeof storage.setItem !== 'function') throw new Error('Persistence: Invalid storage.'); | ||
}; | ||
const getItem = (storage, key, deserialize, callback) => { | ||
@@ -60,7 +56,5 @@ const res = storage.getItem(key); | ||
}; | ||
const setItem = (storage, key, value, serialize) => { | ||
processValue(serialize, value, res => storage.setItem(key, res)); | ||
}; | ||
const processValue = (func, value, callback) => { | ||
@@ -70,3 +64,2 @@ const res = func(value); | ||
}; | ||
export default persistence; |
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector'; | ||
import { strictEqual } from './utils'; | ||
const identity = v => v; | ||
export const useEntity = (entity, transform = identity, equality = strictEqual) => { | ||
@@ -7,0 +5,0 @@ if (!(entity._subscribers instanceof Set)) throw new Error('Invalid entity.'); |
/* Basic equality functions */ | ||
export const strictEqual = (a, b) => a === b; | ||
export const shallowEqual = (a, b) => strictEqual(a, b) || bothObjects(a, b) && equalProps(a, b); | ||
const bothObjects = (a, b) => typeof a === 'object' && a !== null && typeof b === 'object' && b !== null; | ||
const equalProps = (a, b) => { | ||
@@ -11,3 +10,2 @@ const keysOfA = Object.keys(a); | ||
if (keysOfA.length !== keysOfB.length) return false; | ||
for (let i = 0; i < keysOfA.length; i++) { | ||
@@ -17,4 +15,3 @@ const key = keysOfA[i]; | ||
} | ||
return true; | ||
}; |
"use strict"; | ||
exports.__esModule = true; | ||
exports.default = exports.applyPlugins = exports.entity = void 0; | ||
exports.entity = exports.default = exports.applyPlugins = void 0; | ||
var _useEntity = require("./useEntity"); | ||
var _store = require("./store"); | ||
var entity = function entity(initialValue, plugins) { | ||
@@ -14,3 +11,2 @@ if (plugins === void 0) { | ||
} | ||
if (initialValue === undefined) throw new Error('Entity requires an initial value.'); | ||
@@ -23,7 +19,5 @@ if (!(plugins instanceof Array)) throw new Error('Invalid plug-ins array.'); | ||
newEntity._subscribe = createSubscribe(newEntity); | ||
newEntity.get = function () { | ||
return newEntity._value; | ||
}; | ||
newEntity.set = createSetter(newEntity); | ||
@@ -33,10 +27,9 @@ newEntity.init = createInit(newEntity, initialValue); | ||
applyPlugins(newEntity, plugins); | ||
newEntity.init(); // Add this entity to store (if enabled) for use with `resetAll` | ||
newEntity.init(); | ||
// Add this entity to store (if enabled) for use with `resetAll` | ||
if ((0, _store.isStoreEnabled)()) _store.store.add(newEntity); | ||
return newEntity; | ||
}; | ||
exports.entity = entity; | ||
var createSetter = function createSetter(entity) { | ||
@@ -47,6 +40,4 @@ return function (newValue) { | ||
} | ||
if (typeof newValue === 'function') newValue = newValue.apply(void 0, [entity._value].concat(updaterArgs)); | ||
entity._value = newValue; | ||
entity._subscribers.forEach(function (cb) { | ||
@@ -57,3 +48,2 @@ return cb(entity._value); | ||
}; | ||
var createInit = function createInit(entity, initialValue) { | ||
@@ -72,3 +62,2 @@ return initialValue && typeof initialValue.then === 'function' ? function () { | ||
}; | ||
var createHook = function createHook(entity) { | ||
@@ -79,11 +68,8 @@ return function () { | ||
} | ||
return _useEntity.useEntity.apply(void 0, [entity].concat(args)); | ||
}; | ||
}; | ||
var createSubscribe = function createSubscribe(entity) { | ||
return function (subscriberFn) { | ||
entity._subscribers.add(subscriberFn); | ||
return function () { | ||
@@ -94,7 +80,5 @@ entity._subscribers.delete(subscriberFn); | ||
}; | ||
var applyPlugins = function applyPlugins(entity, plugins) { | ||
plugins.forEach(function (plugin) { | ||
if (typeof plugin !== 'object') throw new Error('Invalid plug-in'); | ||
var overrideMethod = function overrideMethod(method) { | ||
@@ -107,3 +91,2 @@ if (typeof plugin[method] === 'function') { | ||
}; | ||
overrideMethod('init'); | ||
@@ -113,5 +96,4 @@ overrideMethod('set'); | ||
}; | ||
exports.applyPlugins = applyPlugins; | ||
var _default = entity; | ||
exports.default = _default; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
exports.__esModule = true; | ||
exports.enableStore = exports.shallowEqual = exports.strictEqual = exports.resetAll = exports.persistence = exports.useEntity = exports.entity = void 0; | ||
exports.useEntity = exports.strictEqual = exports.shallowEqual = exports.resetAll = exports.persistence = exports.entity = exports.enableStore = void 0; | ||
var _entity = _interopRequireDefault(require("./entity")); | ||
exports.entity = _entity.default; | ||
var _useEntity = _interopRequireDefault(require("./useEntity")); | ||
exports.useEntity = _useEntity.default; | ||
var _utils = require("./utils"); | ||
exports.strictEqual = _utils.strictEqual; | ||
exports.shallowEqual = _utils.shallowEqual; | ||
var _persistence = _interopRequireDefault(require("./persistence")); | ||
exports.persistence = _persistence.default; | ||
var _store = require("./store"); | ||
exports.enableStore = _store.enableStore; | ||
var _resetAll = _interopRequireDefault(require("./resetAll")); | ||
exports.resetAll = _resetAll.default; |
"use strict"; | ||
exports.__esModule = true; | ||
exports.default = exports.persistence = void 0; | ||
exports.persistence = exports.default = void 0; | ||
var persistence = function persistence(key, options) { | ||
@@ -10,7 +9,5 @@ if (options === void 0) { | ||
} | ||
if (typeof key !== 'string') throw new Error('Persistence requires a string key.'); | ||
var storage = options.storage || 'local'; | ||
if (storage === 'local') storage = getLocalStorage();else if (storage === 'session') storage = getSessionStorage();else validateCustomStorage(storage); | ||
if (!storage) { | ||
@@ -20,3 +17,2 @@ console.warn('Storage unavailable. Persistence disabled.'); | ||
} | ||
return { | ||
@@ -26,4 +22,5 @@ init: function init(origInit, entity) { | ||
var deserialize = options.deserializeFn || JSON.parse; | ||
origInit(); // Fetch persisted value (if any) from storage | ||
origInit(); | ||
// Fetch persisted value (if any) from storage | ||
getItem(storage, key, deserialize, entity.set); | ||
@@ -35,4 +32,5 @@ }; | ||
var serialize = options.serializeFn || JSON.stringify; | ||
origSet.apply(void 0, arguments); // Persist the new value to storage | ||
origSet.apply(void 0, arguments); | ||
// Persist the new value to storage | ||
setItem(storage, key, entity.get(), serialize); | ||
@@ -43,5 +41,3 @@ }; | ||
}; | ||
exports.persistence = persistence; | ||
var getLocalStorage = function getLocalStorage() { | ||
@@ -54,3 +50,2 @@ try { | ||
}; | ||
var getSessionStorage = function getSessionStorage() { | ||
@@ -63,7 +58,5 @@ try { | ||
}; | ||
var validateCustomStorage = function validateCustomStorage(storage) { | ||
if (typeof storage.getItem !== 'function' || typeof storage.setItem !== 'function') throw new Error('Persistence: Invalid storage.'); | ||
}; | ||
var getItem = function getItem(storage, key, deserialize, callback) { | ||
@@ -76,3 +69,2 @@ var res = storage.getItem(key); | ||
}; | ||
var setItem = function setItem(storage, key, value, serialize) { | ||
@@ -83,3 +75,2 @@ processValue(serialize, value, function (res) { | ||
}; | ||
var processValue = function processValue(func, value, callback) { | ||
@@ -89,4 +80,3 @@ var res = func(value); | ||
}; | ||
var _default = persistence; | ||
exports.default = _default; |
"use strict"; | ||
exports.__esModule = true; | ||
exports.default = exports.resetAll = void 0; | ||
exports.resetAll = exports.default = void 0; | ||
var _store = require("./store"); | ||
var resetAll = function resetAll() { | ||
if (!(0, _store.isStoreEnabled)()) throw new Error('resetAll() requires the global store. Call enableStore() at startup.'); | ||
_store.store.forEach(function (entity) { | ||
@@ -15,5 +12,4 @@ return entity.init(); | ||
}; | ||
exports.resetAll = resetAll; | ||
var _default = resetAll; | ||
exports.default = _default; |
"use strict"; | ||
exports.__esModule = true; | ||
exports.default = exports.store = exports.enableStore = exports.isStoreEnabled = void 0; | ||
exports.store = exports.isStoreEnabled = exports.enableStore = exports.default = void 0; | ||
var storeEnabled = false; | ||
var isStoreEnabled = function isStoreEnabled() { | ||
return storeEnabled; | ||
}; | ||
exports.isStoreEnabled = isStoreEnabled; | ||
var enableStore = function enableStore(enable) { | ||
@@ -17,6 +14,4 @@ if (enable === void 0) { | ||
} | ||
storeEnabled = enable; | ||
}; | ||
exports.enableStore = enableStore; | ||
@@ -23,0 +18,0 @@ var store = new Set(); |
"use strict"; | ||
exports.__esModule = true; | ||
exports.default = exports.useEntity = void 0; | ||
exports.useEntity = exports.default = void 0; | ||
var _withSelector = require("use-sync-external-store/shim/with-selector"); | ||
var _utils = require("./utils"); | ||
var identity = function identity(v) { | ||
return v; | ||
}; | ||
var useEntity = function useEntity(entity, transform, equality) { | ||
@@ -18,13 +14,10 @@ if (transform === void 0) { | ||
} | ||
if (equality === void 0) { | ||
equality = _utils.strictEqual; | ||
} | ||
if (!(entity._subscribers instanceof Set)) throw new Error('Invalid entity.'); | ||
return (0, _withSelector.useSyncExternalStoreWithSelector)(entity._subscribe, entity.get, entity.get, transform, equality); | ||
}; | ||
exports.useEntity = useEntity; | ||
var _default = useEntity; | ||
exports.default = _default; |
"use strict"; | ||
exports.__esModule = true; | ||
exports.shallowEqual = exports.strictEqual = void 0; | ||
exports.strictEqual = exports.shallowEqual = void 0; | ||
/* Basic equality functions */ | ||
/* Basic equality functions */ | ||
var strictEqual = function strictEqual(a, b) { | ||
return a === b; | ||
}; | ||
exports.strictEqual = strictEqual; | ||
var shallowEqual = function shallowEqual(a, b) { | ||
return strictEqual(a, b) || bothObjects(a, b) && equalProps(a, b); | ||
}; | ||
exports.shallowEqual = shallowEqual; | ||
var bothObjects = function bothObjects(a, b) { | ||
return typeof a === 'object' && a !== null && typeof b === 'object' && b !== null; | ||
}; | ||
var equalProps = function equalProps(a, b) { | ||
@@ -27,3 +22,2 @@ var keysOfA = Object.keys(a); | ||
if (keysOfA.length !== keysOfB.length) return false; | ||
for (var i = 0; i < keysOfA.length; i++) { | ||
@@ -33,4 +27,3 @@ var key = keysOfA[i]; | ||
} | ||
return true; | ||
}; |
{ | ||
"name": "simpler-state", | ||
"version": "1.2.1", | ||
"version": "1.2.2-rc.1", | ||
"description": "The simplest app state management for React", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
# <img src="https://simpler-state.js.org/assets/simpler-state-logo.png" alt="SimpleR State" width="240"/> | ||
[![npm](https://img.shields.io/npm/v/simpler-state)](https://www.npmjs.com/package/simpler-state) | ||
[![build](https://img.shields.io/travis/arnelenero/simpler-state)](https://travis-ci.com/github/arnelenero/simpler-state) | ||
[![build](https://img.shields.io/github/actions/workflow/status/arnelenero/simpler-state/main-ci.yml)](https://github.com/arnelenero/simpler-state/actions/workflows/main-ci.yml) | ||
[![coverage](https://img.shields.io/coverallsCoverage/github/arnelenero/simpler-state)](https://coveralls.io/github/arnelenero/simpler-state) | ||
@@ -6,0 +6,0 @@ [![license](https://img.shields.io/github/license/arnelenero/simpler-state)](https://opensource.org/licenses/MIT) |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
25065
505
1