Socket
Socket
Sign inDemoInstall

simpler-state

Package Overview
Dependencies
Maintainers
1
Versions
62
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simpler-state - npm Package Compare versions

Comparing version 1.0.0-rc.7 to 1.0.0-rc.8

es/persistence.js

22

es/entity.js
import { useEntity } from './useEntity';
import { store } from './store';
import { plugins } from './plugin';
export var entity = function entity(initialValue, meta) {
if (meta === void 0) {
meta = {};
export var entity = function entity(initialValue, plugins) {
if (plugins === void 0) {
plugins = [];
}
if (initialValue === undefined) throw new Error('Entity requires an initial value.');
if (typeof meta !== 'object') throw new Error('Entity metadata should be an object.');
if (!(plugins instanceof Array)) throw new Error('Invalid plug-ins array.');
var newEntity = {

@@ -24,3 +23,3 @@ _value: undefined,

newEntity.use = createHook(newEntity);
applyPlugins(newEntity, meta);
applyPlugins(newEntity, plugins);
newEntity.init(); // Save reference to this entity for use with `resetAll`

@@ -49,2 +48,3 @@

return typeof initialValue.then === 'function' ? function () {
// Call the setter so that any bound components are updated
initialValue.then(function (value) {

@@ -54,3 +54,3 @@ return entity.set(value);

} : function () {
entity.set(initialValue);
entity._value = initialValue;
};

@@ -79,8 +79,10 @@ };

export var applyPlugins = function applyPlugins(entity, meta) {
export 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) {
if (typeof plugin[method] === 'function') {
var override = plugin[method](entity[method], entity.get, meta);
if (typeof override !== 'function') throw new Error("Invalid override for '" + method + "' in plug-in '" + plugin.id + "'.");
var override = plugin[method](entity[method], entity);
if (typeof override !== 'function') throw new Error("Invalid override for '" + method + "' in plug-in.");
entity[method] = override;

@@ -87,0 +89,0 @@ }

export { default as entity } from './entity';
export { default as useEntity } from './useEntity';
export { strictEqual, shallowEqual } from './utils';
export { default as plugin } from './plugin';
export { default as persistence } from './persistence';
export { default as resetAll } from './resetAll';
/**
* Creates and returns a new entity
* @param initialValue - required default value
* @param meta - optional metadata object (for plug-ins)
* @param plugins - optional array list of plug-ins
*/
export function entity<T = any>(initialValue: T): Entity<T>
export function entity<T = any>(initialValue: Promise<T>): Entity<T | undefined>
export function entity<T = any, M extends object = Record<any, any>>(
initialValue: T,
meta: M
): Entity<T>
export function entity<T = any, M extends object = Record<any, any>>(
export function entity<T = any>(initialValue: T, plugins?: Plugin[]): Entity<T>
export function entity<T = any>(
initialValue: Promise<T>,
meta: M
plugins?: Plugin[]
): Entity<T | undefined>

@@ -31,15 +26,12 @@

/**
* Binds an entity to the component as a ref
* @param entity - the entity
* @param transform - optional data transformation function
* An entity is the basic unit of shared state.
*/
export function useEntityRef<T>(entity: Entity<T>): T
export function useEntityRef<T, C>(
entity: Entity<T>,
transform?: (value: T) => C
): C
export interface Entity<T> {
/** Sets the entity to its initial value */
init: () => void
/** Returns the current value of the entity */
get: () => T
/** Updates the value of the entity */
set: (

@@ -49,4 +41,5 @@ newValue: T | ((value: T, ...args: any[]) => T),

) => void
/** Binds the entity value to component state */
use: EntityHook<T>
useRef: EntityRefHook<T>
}

@@ -59,7 +52,2 @@

export type EntityRefHook<T> = {
(): T
<C>(transform?: (value: T) => C): C
}
export function strictEqual(a: any, b: any): boolean

@@ -69,23 +57,21 @@ export function shallowEqual(a: any, b: any): boolean

/**
* Resets the values of all entities (for testing)
* A plug-in extends the behavior of an entity by composing
* on top of its original `init` and `set` methods.
*/
export function resetAll(): void
export interface Plugin {
/**
* Returns an override for the entity's `init` method
* @param origSet - original `init`
* @param entity - target entity
*/
init?: (origInit: () => void, entity: Entity<any>) => () => void
/**
* Attaches a plug-in to SimplerR State
* @param pluginPkg - the plug-in package
* @param options - optional configuration object
*/
export function plugin<O extends object>(
pluginPkg: (options: O) => Plugin<any>,
options?: O
): void
export interface Plugin<M extends object = Record<any, any>> {
id: string
init?: (origInit: () => void, get: () => any, meta: M) => () => void
/**
* Returns an override for the entity's `set` method
* @param origSet - original `set`
* @param entity - target entity
*/
set?: (
origSet: (...args: any[]) => void,
get: () => any,
meta: M
entity: Entity<any>
) => (...args: any[]) => void

@@ -95,4 +81,29 @@ }

/**
* Persistence plug-in enables storing entity values to
* localStorage (default), sessionStorage or custom storage
* (must implement the Web Storage API).
* @param key - unique identifier
* @param options - optional config for storage and serialization/deserialization
*/
export function persistence(
key: string,
options?: {
storage?: 'local' | 'session' | Storage
serializeFn?: (value: any) => string | Promise<string>
deserializeFn?: (value: string) => any | Promise<any>
}
): Plugin
/**
* Storage implements both `getItem` and `setItem` methods
* of the Web Storage API.
*/
export interface Storage {
getItem: (key: string) => string | null | Promise<string> | Promise<null>
setItem: (key: string, value: string) => void | Promise<void>
}
/**
* Resets all entities to initial value
*/
export function resetAll(): void

@@ -10,11 +10,9 @@ "use strict";

var _plugin = require("./plugin");
var entity = function entity(initialValue, meta) {
if (meta === void 0) {
meta = {};
var entity = function entity(initialValue, plugins) {
if (plugins === void 0) {
plugins = [];
}
if (initialValue === undefined) throw new Error('Entity requires an initial value.');
if (typeof meta !== 'object') throw new Error('Entity metadata should be an object.');
if (!(plugins instanceof Array)) throw new Error('Invalid plug-ins array.');
var newEntity = {

@@ -33,3 +31,3 @@ _value: undefined,

newEntity.use = createHook(newEntity);
applyPlugins(newEntity, meta);
applyPlugins(newEntity, plugins);
newEntity.init(); // Save reference to this entity for use with `resetAll`

@@ -61,2 +59,3 @@

return typeof initialValue.then === 'function' ? function () {
// Call the setter so that any bound components are updated
initialValue.then(function (value) {

@@ -66,3 +65,3 @@ return entity.set(value);

} : function () {
entity.set(initialValue);
entity._value = initialValue;
};

@@ -91,8 +90,10 @@ };

var applyPlugins = function applyPlugins(entity, meta) {
_plugin.plugins.forEach(function (plugin) {
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) {
if (typeof plugin[method] === 'function') {
var override = plugin[method](entity[method], entity.get, meta);
if (typeof override !== 'function') throw new Error("Invalid override for '" + method + "' in plug-in '" + plugin.id + "'.");
var override = plugin[method](entity[method], entity);
if (typeof override !== 'function') throw new Error("Invalid override for '" + method + "' in plug-in.");
entity[method] = override;

@@ -99,0 +100,0 @@ }

@@ -6,3 +6,3 @@ "use strict";

exports.__esModule = true;
exports.shallowEqual = exports.strictEqual = exports.resetAll = exports.plugin = exports.useEntity = exports.entity = void 0;
exports.shallowEqual = exports.strictEqual = exports.resetAll = exports.persistence = exports.useEntity = exports.entity = void 0;

@@ -22,5 +22,5 @@ var _entity = _interopRequireDefault(require("./entity"));

var _plugin = _interopRequireDefault(require("./plugin"));
var _persistence = _interopRequireDefault(require("./persistence"));
exports.plugin = _plugin["default"];
exports.persistence = _persistence["default"];

@@ -27,0 +27,0 @@ var _resetAll = _interopRequireDefault(require("./resetAll"));

{
"name": "simpler-state",
"version": "1.0.0-rc.7",
"version": "1.0.0-rc.8",
"description": "The simplest app state management for React",

@@ -28,4 +28,4 @@ "keywords": [

"scripts": {
"build:lib": "cross-env BABEL_ENV=commonjs babel src --out-dir lib --ignore **/__tests__",
"build:es": "babel src --out-dir es --ignore **/__tests__",
"build:lib": "cross-env BABEL_ENV=commonjs babel src --out-dir lib --ignore **/*.test.js",
"build:es": "babel src --out-dir es --ignore **/*.test.js",
"build": "npm run build:lib && npm run build:es",

@@ -69,2 +69,3 @@ "clean": "rimraf lib es",

"jest": "^24.9.0",
"jest-localstorage-mock": "^2.4.8",
"react": "^16.14.0",

@@ -77,2 +78,6 @@ "react-dom": "^16.14.0",

"jest": {
"resetMocks": false,
"setupFiles": [
"jest-localstorage-mock"
],
"setupFilesAfterEnv": [

@@ -79,0 +84,0 @@ "<rootDir>/test-setup.js"

@@ -42,3 +42,3 @@ # <img src="https://simpler-state.js.org/assets/simpler-state-logo.png" alt="SimpleR State" width="240"/>

counter.set(value => value + by)
// Alternatively: counter.set(counter.get() + by)
// --OR--> counter.set(counter.get() + by)
}

@@ -54,2 +54,3 @@ ```

const count = counter.use()
// --OR--> const count = useEntity(counter)

@@ -56,0 +57,0 @@ return (

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc