Comparing version 1.2.3 to 1.3.0
@@ -1,1 +0,1 @@ | ||
import{useState as t,useRef as e,useEffect as r,memo as n}from"react";function s(t,e,r,n){if("a"===r&&!n)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!n:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?n:"a"===r?n.call(t):n?n.value:e.get(t)}var i,o,a,c,f,u,h,l;class d{constructor(t){i.add(this),o.set(this,{}),a.set(this,new Map),s(this,i,"m",u).call(this,t)}subscribe(t,e){if("function"!=typeof e)return r=["listener must be a function"],console.warn(r.join("\n")),()=>!1;var r;Array.isArray(t)||(t=[t]),s(this,i,"m",f).call(this,e);const n=t.map((t=>t.toString().toLowerCase()));return n.forEach((t=>{s(this,i,"m",c).call(this,t),s(this,a,"f").get(e).add(t),s(this,o,"f")[t].listeners.add(e)})),()=>{s(this,a,"f").delete(e),n.forEach((t=>s(this,o,"f")[t.toLowerCase()].listeners.delete(e)))}}set(t,{silent:e}={silent:!1}){if("object"!=typeof t)return;const r=s(this,i,"m",u).call(this,t);!e&&r&&s(this,i,"m",h).call(this,r)}get(t){var e,r;return"string"==typeof t?null===(r=null===(e=s(this,o,"f"))||void 0===e?void 0:e[t.toLowerCase()])||void 0===r?void 0:r.value:Array.isArray(t)?Object.fromEntries(t.map((t=>[t,m.get(t)]))):void 0}isValid(t){return void 0!==s(this,o,"f")[t]}}o=new WeakMap,a=new WeakMap,i=new WeakSet,c=function(t){s(this,o,"f")[t]||(s(this,o,"f")[t]={listeners:new Set})},f=function(t){s(this,a,"f").has(t)||s(this,a,"f").set(t,new Set)},u=function(t){if("object"!=typeof t)return;const e=Object.keys(t);return e.forEach((e=>{s(this,i,"m",c).call(this,e),s(this,o,"f")[e].value=t[e]})),e},h=function(t){const e=new Set;t.forEach((t=>{s(this,o,"f")[t].listeners.forEach((t=>e.add(t)))})),e.forEach((t=>t(s(this,i,"m",l).call(this,t))))},l=function(t){const e=Array.from(s(this,a,"f").get(t));return 1===e.length?s(this,o,"f")[e[0]].value:Object.fromEntries(e.map((t=>[t,s(this,o,"f")[t].value])))};const m=new d;function w(n=undefined){const s=n instanceof d,i=s?void 0:n,o=s?n:m,a=t({}),c=e({keys:new Set});return r((()=>{const{keys:t}=c.current;if(0===t.size)return;const e=o.subscribe(Array.from(t),(function(t){a[1]({value:t})}));return()=>e()}),[]),new Proxy({},{get(t,e){const{keys:r}=c.current;if((e=e.toLowerCase()).startsWith("set")){const t=e.replace("set","");return void 0!==i&&f(t),e=>{o.set({[t]:u(e,t)})}}return t[e]?t[e]:(r.add(e),f(e))}});function f(t){let e=o.get(t);return o.isValid(t)||(e=u(i,t),o.set({[t]:e})),e}function u(t,e){return"function"==typeof t?t(o.get(e)):t}}function p(t,e){function r(r){const n=w(e);return Object.defineProperties(n,Object.fromEntries(Object.entries(r).map((([t,e])=>[t,{value:e}])))),t(n)}return n(r)}export{d as Observer,m as observer,w as useObserver,p as withState}; | ||
import{useState as t,useRef as e,useEffect as r,memo as s}from"react";function n(t,e,r,s){if("a"===r&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!s:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?s:"a"===r?s.call(t):s?s.value:e.get(t)}var i,o,a,c,f,h,u,l,d;class m{constructor(t){i.add(this),o.set(this,{}),a.set(this,new Map),c.set(this,{}),n(this,i,"m",f).call(this,"*"),n(this,i,"m",u).call(this,t)}subscribe(t,e){if("function"!=typeof e)return r=["listener must be a function"],console.warn(r.join("\n")),()=>!1;var r;Array.isArray(t)||(t=[t]),n(this,i,"m",h).call(this,e);const s=t.map((t=>t.toString().toLowerCase()));return s.forEach((t=>{n(this,i,"m",f).call(this,t),n(this,a,"f").get(e).add(t),n(this,o,"f")[t].listeners.add(e)})),()=>{n(this,a,"f").delete(e),s.forEach((t=>n(this,o,"f")[t.toLowerCase()].listeners.delete(e)))}}set(t,{silent:e}={silent:!1}){if("object"!=typeof t)return;const r=n(this,i,"m",u).call(this,t);!e&&r&&n(this,i,"m",l).call(this,r)}get(t){var e;return"string"==typeof t?null===(e=n(this,c,"f"))||void 0===e?void 0:e[t.toLowerCase()]:Array.isArray(t)?Object.fromEntries(t.map((t=>[t,w.get(t)]))):void 0}get state(){return n(this,c,"f")}isValid(t){return void 0!==n(this,o,"f")[t]}}o=new WeakMap,a=new WeakMap,c=new WeakMap,i=new WeakSet,f=function(t){n(this,o,"f")[t]||(n(this,o,"f")[t]={listeners:new Set})},h=function(t){n(this,a,"f").has(t)||n(this,a,"f").set(t,new Set)},u=function(t){if("object"!=typeof t)return;const e=Object.keys(t);return e.forEach((e=>{n(this,i,"m",f).call(this,e),n(this,c,"f")[e.toLowerCase()]=t[e]})),e},l=function(t){const e=new Set;n(this,o,"f")["*"].listeners.forEach((t=>e.add(t))),t.forEach((t=>{n(this,o,"f")[t].listeners.forEach((t=>e.add(t)))})),e.forEach((t=>t(n(this,i,"m",d).call(this,t))))},d=function(t){if(n(this,o,"f")["*"].listeners.has(t))return this.state;const e=Array.from(n(this,a,"f").get(t));return 1===e.length?n(this,c,"f")[e[0]]:Object.fromEntries(e.map((t=>[t,n(this,c,"f")[t]])))};const w=new m;function p(s=undefined){const n=s instanceof m,i=n?void 0:s,o=n?s:w,a=t({}),c=e({keys:new Set});return r((()=>{const{keys:t}=c.current;if(0===t.size)return;const e=o.subscribe(Array.from(t),(function(t){a[1]({value:t})}));return()=>e()}),[]),new Proxy({},{get(t,e){const{keys:r}=c.current;if((e=e.toLowerCase()).startsWith("set")){const t=e.replace("set","");return void 0!==i&&f(t),e=>{o.set({[t]:h(e,t)})}}return t[e]?t[e]:(r.add(e),f(e))}});function f(t){let e=o.get(t);return o.isValid(t)||(e=h(i,t),o.set({[t]:e})),e}function h(t,e){return"function"==typeof t?t(o.get(e)):t}}function y(t,e){function r(r){const s=p(e);return Object.defineProperties(s,Object.fromEntries(Object.entries(r).map((([t,e])=>[t,{value:e}])))),t(s)}return s(r)}export{m as Observer,w as observer,p as useObserver,y as withState}; |
{ | ||
"name": "rosma", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"repository": { | ||
@@ -9,4 +9,4 @@ "type": "git", | ||
"author": "Shahab Yazdi <sh.yazdi@gmail.com>", | ||
"homepage": "https://github.com/shahabyazdi/rosma/", | ||
"description": "combined javascript proxy and observer pattern to optimize your react component.", | ||
"homepage": "https://shahabyazdi.github.io/rosma/", | ||
"description": "Simple and easy-to-use global state management for React.", | ||
"keywords": [ | ||
@@ -13,0 +13,0 @@ "rosma", |
@@ -1,6 +0,6 @@ | ||
import { Listener } from '../types'; | ||
import { Listener, StarOrKey } from '../types'; | ||
declare class Observer<T = Record<string, any>> { | ||
#private; | ||
constructor(initialValues?: T); | ||
subscribe<K extends keyof T>(key: K | K[], listener: Listener): () => void; | ||
subscribe<K extends keyof T>(key: StarOrKey<K> | Array<StarOrKey<K>>, listener: Listener): () => void; | ||
set<StateType>(object: Partial<StateType | T>, { silent }?: { | ||
@@ -10,2 +10,3 @@ silent?: boolean; | ||
get<K extends keyof T>(key: K | Array<K>): any; | ||
get state(): T; | ||
isValid(key: string): boolean; | ||
@@ -12,0 +13,0 @@ } |
@@ -9,4 +9,5 @@ export declare type Listener = (value: any) => void; | ||
export declare type Setter<T> = T | ((prev: T) => T); | ||
export declare type StarOrKey<T> = '*' | T; | ||
export declare type WithSetters<T> = T extends object ? { | ||
[K in keyof T as `set${Capitalize<string & K>}`]: (value: Setter<T[K]>) => void; | ||
} & T : T; |
Sorry, the diff of this file is not supported yet
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
13564
61
2