Comparing version 1.1.1 to 1.2.1
@@ -6,7 +6,7 @@ // Generated by dts-bundle-generator v6.4.0 | ||
} : T; | ||
export declare type GlobalStateSetter<T> = (fn: (oldvalue: FullReadOnly<T>) => T) => void; | ||
export interface GlobalState<T extends {}> { | ||
export declare type LStateSetter<T> = (fn: (oldvalue: FullReadOnly<T>) => T) => void; | ||
export interface LState<T extends {}> { | ||
$: { | ||
get(): FullReadOnly<T>; | ||
set: GlobalStateSetter<T>; | ||
set: LStateSetter<T>; | ||
subscribe(subscription: (value: FullReadOnly<T>) => void): () => void; | ||
@@ -16,19 +16,76 @@ destroy(): void; | ||
} | ||
export interface GlobalStateReducers { | ||
export interface LComputed<T extends {}> { | ||
$: { | ||
get(): FullReadOnly<T>; | ||
subscribe(subscription: (value: FullReadOnly<T>) => void): () => void; | ||
destroy(): void; | ||
}; | ||
} | ||
export declare type LCollectionOf<T extends { | ||
id: string; | ||
}> = { | ||
[id: string]: Omit<T, "id">; | ||
}; | ||
export declare type LCollectionList<T extends { | ||
id: string; | ||
}> = T[]; | ||
export interface LCollection<T extends { | ||
id: string; | ||
}> { | ||
$: { | ||
get(): FullReadOnly<LCollectionOf<T>>; | ||
set: LStateSetter<LCollectionOf<T>>; | ||
load(data: FullReadOnly<LCollectionList<T>>): void; | ||
subscribe(subscription: (collectionOf: FullReadOnly<LCollectionOf<T>>) => void): () => void; | ||
subscribeList(subscription: (items: FullReadOnly<LCollectionList<T>>) => void): () => void; | ||
subscribeQuery<R = LCollectionList<T>>(initial: R, reducer: (last: R, item: FullReadOnly<T>) => R, subscription: (reduced: FullReadOnly<R>) => void): () => void; | ||
subscribeItem(id: string, subscription: (item?: FullReadOnly<LCollectionOf<T>>) => void): () => void; | ||
upsert(id: string, fn: (old?: Omit<T, "id">) => Omit<T, "id">): void; | ||
remove(id: string): void; | ||
destroy(): void; | ||
}; | ||
} | ||
export declare type LAnyState = LState<any> | LComputed<any> | LCollection<any>; | ||
export declare type ExtractLAnyState<T> = T extends LState<infer T1> ? T1 : T extends LComputed<infer T2> ? T2 : T extends LCollection<infer T3> ? T3 : unknown; | ||
export declare type ExtractLAnyStates<T> = { | ||
[key in keyof T]: ExtractLAnyState<T[key]>; | ||
}; | ||
export interface LStateActions { | ||
[key: string]: (...value: any[]) => any; | ||
} | ||
export declare type DependencyCompute<T extends {}> = (setter: GlobalStateSetter<T>) => void; | ||
export interface GlobalStateDef<T extends {}, REDUCERS extends GlobalStateReducers> { | ||
export declare type DependencyCompute<T extends {}, DEPS extends LAnyState[]> = (setter: LStateSetter<T>, ...deps: ExtractLAnyStates<DEPS>) => void; | ||
export interface LStateDef<T extends {}, ACTIONS extends LStateActions> { | ||
initial: T; | ||
reducers: (setter: GlobalStateSetter<T>) => REDUCERS; | ||
dependencies?: [ | ||
...Array<GlobalState<any>>, | ||
DependencyCompute<T> | ||
]; | ||
actions: (setter: LStateSetter<T>) => ACTIONS; | ||
compare?: (a: FullReadOnly<T>, b: T) => boolean; | ||
} | ||
export declare function createGlobalState<T extends {}, REDUCERS extends GlobalStateReducers>(def: GlobalStateDef<T, REDUCERS>): GlobalState<T> & REDUCERS; | ||
export declare function useGlobalState<T>(state: GlobalState<T>): FullReadOnly<T>; | ||
export declare function isGlobalState(state: any): state is GlobalState<any>; | ||
export interface LComputedDef<T extends {}, DEPS extends LAnyState[]> { | ||
default: T; | ||
dependencies: DEPS; | ||
compute: DependencyCompute<T, DEPS>; | ||
compare?: (a: FullReadOnly<T>, b: T) => boolean; | ||
} | ||
export interface LCollectionDef<T extends { | ||
id: string; | ||
}, ACTIONS extends LStateActions> { | ||
default: LCollectionOf<T>; | ||
actions: (dml: { | ||
setter: LStateSetter<LCollectionOf<T>>; | ||
upsert(id: string, fn: (old?: Omit<T, "id">) => Omit<T, "id">): void; | ||
remove(id: string): void; | ||
}) => ACTIONS; | ||
} | ||
export declare function createLState<T extends {}, ACTIONS extends LStateActions>(def: LStateDef<T, ACTIONS>): LState<T> & ACTIONS; | ||
export declare function createLState<T extends {}, DEPS extends LAnyState[]>(def: LComputedDef<T, DEPS>): LComputed<T>; | ||
export declare function createLState<T extends { | ||
id: string; | ||
}, ACTIONS extends LStateActions>(def: LCollectionDef<T, ACTIONS>): LCollection<T> & ACTIONS; | ||
export declare function useLState<T>(state: LState<T>): FullReadOnly<T>; | ||
export declare function collectionListToCollectionOf<T extends { | ||
id: string; | ||
}>(list: FullReadOnly<LCollectionList<T>>): LCollectionOf<T>; | ||
export declare function collectionOfToCollectionList<T extends { | ||
id: string; | ||
}>(collection: FullReadOnly<LCollectionOf<T>>): LCollectionList<T>; | ||
export {}; |
@@ -1,1 +0,1 @@ | ||
var c=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var E=e=>c(e,"__esModule",{value:!0});var G=(e,t)=>{for(var l in t)c(e,l,{get:t[l],enumerable:!0})},v=(e,t,l,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of R(t))!x.call(e,n)&&(l||n!=="default")&&c(e,n,{get:()=>t[n],enumerable:!(o=p(t,n))||o.enumerable});return e};var m=(e=>(t,l)=>e&&e.get(t)||(l=v(E({}),t,1),e&&e.set(t,l),l))(typeof WeakMap!="undefined"?new WeakMap:0);var F={};G(F,{createGlobalState:()=>D,isGlobalState:()=>T,useGlobalState:()=>O});var s=require("react");function D(e){let t=e.compare||((a,r)=>a===r),l=e.initial,o;S();let n=new Set,u={$:{get:b,set:i,subscribe(a){return n.add(a),()=>n&&n.delete(a)},destroy(){u&&(o&&(o.forEach(a=>setTimeout(a,1)),o=void 0),Object.keys(u.$).forEach(a=>{delete u.$[a]}),Object.keys(u).forEach(a=>{delete u[a]}),l=null,n.clear(),n=null,u=null,t=null)}},...e.reducers(i)};return u;function b(){return l}function i(a){let r=a(l);t(l,r)||(l=r,f())}function f(){n.forEach(a=>{setTimeout(()=>a(l),1)})}function S(){if(!e.dependencies)return;o=[];let a,r;e.dependencies.forEach(d=>{T(d)?o.push(d.$.subscribe(y)):a=d}),y();function y(){r&&clearTimeout(r),r=setTimeout(()=>{o&&a(i)},100)}}}function O(e){let[t,l]=(0,s.useState)(()=>e.$.get());return(0,s.useEffect)(()=>e.$.subscribe(l),[]),t}function T(e){return e&&e.$&&e.$.get&&e.$.set}module.exports=m(F);0&&(module.exports={createGlobalState,isGlobalState,useGlobalState}); | ||
import{useEffect as v,useState as C}from"react";function T(e,n,s){if(e===n)return 0;let l=e===null?"null":typeof e,u=n===null?"null":typeof n,o;if(e instanceof Date)return n instanceof Date?e.getTime()-n.getTime():u==="string"?e.getTime()-new Date(n).getTime():-1;if(n instanceof Date)return l==="string"?new Date(e).getTime()-n.getTime():1;if(l!==u)return l==="undefined"?-1:u==="undefined"?1:l<u?-1:1;if(Array.isArray(e))return o=e.length-n.length,o||e.some((d,i)=>(o=T(d,n[i],s),!!o)),o;if(l==="object"){let d=Object.getOwnPropertyNames(e);return d.sort(),o=0,d.some(i=>(o=T(e[i],n[i],s),!!o)),o===0&&s&&(o=d.length-Object.getOwnPropertyNames(n).length),o}else return l==="function"?0:e<n?-1:1}function D(e){let n=e.compare||((t,r)=>t===r),s=e.initial||e.default,l=e.dependencies,u=e.compute,o=new Set,d;l&&O();let i={$:{get:S,subscribe(t){return o.add(t),()=>o&&o.delete(t)},destroy(){i&&(d&&(d.forEach(t=>setTimeout(t,1)),d=void 0),Object.keys(i.$).forEach(t=>{delete i.$[t]}),Object.keys(i).forEach(t=>{delete i[t]}),s=null,o.clear(),o=null,i=null,n=null)}}};return e.compute||(i.$.setter=c,e.actions&&(i={...i,...e.actions(e.default?{setter:c,upsert:f,remove:p}:c)})),e.default&&(i.$.load=t=>{c(()=>b(t))},i.$.subscribeList=t=>i.$.subscribe(r=>t(m(r))),i.$.subscribeQuery=(t,r,a)=>i.$.subscribe(y=>a(m(y).reduce(r,t))),i.$.subscribeItem=(t,r)=>i.$.subscribe(a=>r(a[t])),i.$.upsert=f,i.$.remove=p),i;function S(){return s}function c(t){let r=t(s);n(s,r)||(s=r,x())}function x(){o.forEach(t=>{setTimeout(()=>t(s),1)})}function O(){d=[];let t;d=l.map(a=>a.$.subscribe(r)),r();function r(){t&&clearTimeout(t),t=setTimeout(()=>{d&&u(c,...l.map(a=>a.$.get()))},100)}}function f(t,r){c(a=>{let y=a[t],L=r(y);return T(y,L,!0)===0?a:{...a,[t]:L}})}function p(t){c(r=>{let a={...r};return a[t]?(delete a[t],a):r})}}function $(e){let[n,s]=C(()=>e.$.get());return v(()=>e.$.subscribe(s),[]),n}function b(e){let n={};return e.forEach(s=>{let{id:l,...u}=s;n[l]=u}),n}function m(e){return Object.keys(e).map(n=>({id:n,...e[n]}))}export{b as collectionListToCollectionOf,m as collectionOfToCollectionList,D as createLState,$ as useLState}; |
{ | ||
"name": "lstate", | ||
"description": "A simple, super-efficient and small (just 1.5kb) global state for React/Typescript applications", | ||
"version": "1.1.1", | ||
"description": "A simple, super-efficient and small (just 2.1kb) global state for React/Typescript applications", | ||
"version": "1.2.1", | ||
"private": false, | ||
@@ -38,3 +38,3 @@ "main": "dist/index.js", | ||
"buildDTS": "dts-bundle-generator --project ./tsconfig.build.json src/index.ts -o dist/index.d.ts", | ||
"buildWithEsbuild": "esbuild --bundle src/index.ts --external:react --minify --platform=node --format=cjs --outfile=dist/index.js", | ||
"buildWithEsbuild": "esbuild --bundle src/index.ts --external:react --minify --platform=node --format=esm --outfile=dist/index.js", | ||
"buildWithTSC": "tsc -p ./tsconfig.build.json", | ||
@@ -41,0 +41,0 @@ "test": "jest", |
@@ -5,3 +5,3 @@ [![Node.js CI](https://github.com/teintinu/lstate/actions/workflows/test.yml/badge.svg)](https://github.com/teintinu/lstate/actions/workflows/test.yml) | ||
# lstate | ||
A simple, super-efficient and small (just 1.5kb) global state for React/Typescript applications | ||
A simple, super-efficient and small (just 2.1kb) global state for React/Typescript applications | ||
@@ -19,5 +19,5 @@ # install | ||
import React from 'react'; | ||
import { createGlobalState, useGlobalState } from "lstate"; | ||
import { createLState, useLState } from "lstate"; | ||
const sample = createGlobalState({ | ||
const sample = createLState({ | ||
initial: {count: 0}, | ||
@@ -32,6 +32,6 @@ reducers: (setter) => ({ | ||
export function App() { | ||
const { count } = useGlobalState(sample) | ||
const { count } = useLState(sample) | ||
return <div className="App"> | ||
<h1>Testing lstate</h1> | ||
<h2>A simple, super-efficient and small (just 1.5kb) global state for React/Typescript applications</h2> | ||
<h2>A simple, super-efficient and small (just 2.1kb) global state for React/Typescript applications</h2> | ||
<p>count: {count}</p> | ||
@@ -38,0 +38,0 @@ <button onClick={sample.inc}>+</button> |
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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
43863
96
1