Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

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.8-pre.0 to 1.0.0-rc.8-pre.1

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, 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>

@@ -30,5 +25,13 @@

/**
* An entity is the basic unit of shared state.
*/
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: (

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

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

@@ -48,7 +52,2 @@

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

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

/**
* Attaches a plug-in to SimplerR State
* @param pluginPkg - the plug-in package
* @param options - optional configuration object
* A plug-in extends the behavior of an entity by composing
* on top of its original `init` and `set` methods.
*/
export function plugin<O extends object>(
pluginPkg: (options: O) => Plugin<any>,
options?: O
): 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
export interface Plugin<M extends object = Record<any, any>> {
id: string
init?: (origInit: () => void, entity: Entity<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,
entity: Entity<any>,
meta: M
entity: Entity<any>
) => (...args: any[]) => void

@@ -79,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, 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.8-pre.0",
"version": "1.0.0-rc.8-pre.1",
"description": "The simplest app state management for React",

@@ -5,0 +5,0 @@ "keywords": [

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