Comparing version 1.4.2 to 1.5.0
@@ -1,1 +0,1 @@ | ||
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,f,c,h,u,l,d;class m{constructor(t){i.add(this),o.set(this,{}),a.set(this,new Map),f.set(this,{}),n(this,i,"m",c).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;const s=Array.isArray(t)?Array.from(new Set(t)).filter((t=>"*"!==t)):[t];n(this,i,"m",h).call(this,e);const f=s.map(y);return f.forEach((t=>{n(this,i,"m",c).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),f.forEach((t=>n(this,o,"f")[t].listeners.delete(e)))}}set(t,{silent:e}={silent:!1}){if("function"==typeof t&&(t=t(p(n(this,f,"f")))),"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){return"string"==typeof t?n(this,f,"f")[t.toLowerCase()]:Array.isArray(t)?Object.fromEntries(t.map((t=>[t,n(this,f,"f")[y(t)]]))):void 0}get state(){return p(n(this,f,"f"))}isValid(t){return void 0!==n(this,o,"f")[t.toLowerCase()]}}o=new WeakMap,a=new WeakMap,f=new WeakMap,i=new WeakSet,c=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),r=e.map(y);return r.forEach(((r,s)=>{n(this,i,"m",c).call(this,r),n(this,f,"f")[r]=t[e[s]]})),r},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.toLowerCase()].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,f,"f")[e[0]]:Object.fromEntries(e.map((t=>[t,n(this,f,"f")[t]])))};const w=new m;function p(t){return new Proxy(t,{get:(t,e)=>t[y(e)],set:(t,e,r)=>(t[y(e)]=r,!0)})}function y(t){return t.toString().toLowerCase()}function b(s=undefined){const n=s instanceof m,i=n?void 0:s,o=n?s:w,a=t({}),f=e({keys:new Set});return r((()=>{const{keys:t}=f.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}=f.current,s=e.toLowerCase();if("set"===s)return(...t)=>{o.set(...t)};if(s.startsWith("set")){const t=s.replace("set","");return void 0!==i&&c(t),e=>{o.set({[t]:h(e,t)})}}return t[e]?t[e]:(r.add(s),c(s))},defineProperty:(t,e,r)=>(t[e]=r.value,!0)});function c(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 g(t,e){function r(r){const s=b(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,b as useObserver,g as withState}; | ||
import{useState as t,useRef as e,useEffect as s,memo as r}from"react";function i(t,e,s,r){if("a"===s&&!r)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!r:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===s?r:"a"===s?r.call(t):r?r.value:e.get(t)}var n,o,a,c,f,h,u,l,d;class m{constructor(t,e){n.add(this),o.set(this,{}),a.set(this,new Map),c.set(this,{}),this.statics={},i(this,n,"m",f).call(this,"*"),i(this,n,"m",u).call(this,t),this.setStatics(e)}setStatics(t){"object"==typeof t&&Object.entries(t).forEach((([t,e])=>{this.statics[t]="function"==typeof e?e.bind(this):e}))}subscribe(t,e){if("function"!=typeof e)return s=["listener must be a function"],console.warn(s.join("\n")),()=>!1;var s;const r=Array.isArray(t)?Array.from(new Set(t)).filter((t=>"*"!==t)):[t];i(this,n,"m",h).call(this,e);const c=r.map(y);return c.forEach((t=>{i(this,n,"m",f).call(this,t),i(this,a,"f").get(e).add(t),i(this,o,"f")[t].listeners.add(e)})),()=>{i(this,a,"f").delete(e),c.forEach((t=>i(this,o,"f")[t].listeners.delete(e)))}}set(t,{silent:e}={silent:!1}){if("function"==typeof t&&(t=t(w(i(this,c,"f")))),"object"!=typeof t)return;const s=i(this,n,"m",u).call(this,t);!e&&s&&i(this,n,"m",l).call(this,s)}get(t){const e=t=>(t=t.toString(),this.statics[t]||i(this,c,"f")[t.toLowerCase()]);return"string"==typeof t?e(t):Array.isArray(t)?Object.fromEntries(t.map((t=>[t,e(t)]))):void 0}get state(){return w(i(this,c,"f"))}isValid(t){return void 0!==i(this,o,"f")[t.toLowerCase()]}}o=new WeakMap,a=new WeakMap,c=new WeakMap,n=new WeakSet,f=function(t){i(this,o,"f")[t]||(i(this,o,"f")[t]={listeners:new Set})},h=function(t){i(this,a,"f").has(t)||i(this,a,"f").set(t,new Set)},u=function(t){if("object"!=typeof t)return;const e=Object.keys(t).filter((t=>!this.statics[t])),s=e.map(y);return s.forEach(((s,r)=>{i(this,n,"m",f).call(this,s),i(this,c,"f")[s]=t[e[r]]})),s},l=function(t){const e=new Set;i(this,o,"f")["*"].listeners.forEach((t=>e.add(t))),t.forEach((t=>{i(this,o,"f")[t.toLowerCase()].listeners.forEach((t=>e.add(t)))})),e.forEach((t=>t(i(this,n,"m",d).call(this,t))))},d=function(t){if(i(this,o,"f")["*"].listeners.has(t))return this.state;const e=Array.from(i(this,a,"f").get(t));return 1===e.length?i(this,c,"f")[e[0]]:Object.fromEntries(e.map((t=>[t,i(this,c,"f")[t]])))};const p=new m;function w(t){return new Proxy(t,{get:(t,e)=>t[y(e)],set:(t,e,s)=>(t[y(e)]=s,!0)})}function y(t){return t.toString().toLowerCase()}function b(r=undefined){const i=r instanceof m,n=i?void 0:r,o=i?r:p,a=t({}),c=e({keys:new Set});return s((()=>{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:s}=c.current,r=e.toLowerCase();if("set"===r)return(...t)=>{o.set(...t)};if(r.startsWith("set")){const t=r.replace("set","");return void 0!==n&&f(t),e=>{o.set({[t]:h(e,t)})}}return t[e]?t[e]:o.statics[e]?o.statics[e]:(s.add(r),f(r))},defineProperty:(t,e,s)=>(t[e]=s.value,!0)});function f(t){let e=o.get(t);return o.isValid(t)||(e=h(n,t),o.set({[t]:e})),e}function h(t,e){return"function"==typeof t?t(o.get(e)):t}}function g(t,e){function s(s){const r=b(e);return Object.defineProperties(r,Object.fromEntries(Object.entries(s).map((([t,e])=>[t,{value:e}])))),t(r)}return r(s)}export{m as Observer,p as observer,b as useObserver,g as withState}; |
{ | ||
"name": "rosma", | ||
"version": "1.4.2", | ||
"version": "1.5.0", | ||
"repository": { | ||
@@ -27,6 +27,6 @@ "type": "git", | ||
"type": "module", | ||
"types": "./src\\index.d.ts", | ||
"types": "./src/index.d.ts", | ||
"exports": { | ||
".": { | ||
"types": "./src\\index.d.ts", | ||
"types": "./src/index.d.ts", | ||
"import": "./index.js", | ||
@@ -33,0 +33,0 @@ "require": "./index.cjs" |
@@ -0,0 +0,0 @@ # Rosma |
@@ -1,2 +0,2 @@ | ||
import { WithSetters } from '../types'; | ||
export declare function useObserver<T = Record<string, any>>(input?: any): WithSetters<T>; | ||
import { WithSetters, GlobalState, GlobalStatics } from '../types'; | ||
export declare function useObserver<T = GlobalState, Statics = GlobalStatics>(input?: any): WithSetters<T> & Statics; |
import { ReactElement } from 'react'; | ||
import { Observer } from '../observer'; | ||
import { WithSetters } from '../types'; | ||
export declare function withState<StateProps extends Record<string, any>, ComponentProps extends Record<string, any> = Record<string, any>>(callback: (state?: WithSetters<StateProps> & ComponentProps) => ReactElement, observer?: Observer): import("react").NamedExoticComponent<ComponentProps>; | ||
import { WithSetters, GlobalState, GlobalStatics } from '../types'; | ||
export declare function withState<StateProps = GlobalState, ComponentProps extends Record<string, any> = Record<string, any>, StaticProps = GlobalStatics>(callback: (state?: WithSetters<StateProps> & ComponentProps & StaticProps) => ReactElement, observer?: Observer<StateProps, StaticProps>): import("react").NamedExoticComponent<ComponentProps>; |
@@ -1,12 +0,14 @@ | ||
import { Listener, ObserverValues, SetOptions, StarOrKey } from '../types'; | ||
declare class Observer<T = Record<string, any>> { | ||
import { Listener, ObserverValues, SetOptions, StarOrKey, BindObserver, GlobalState, GlobalStatics } from '../types'; | ||
declare class Observer<T = Record<string, any>, S = Record<string, any>> { | ||
#private; | ||
constructor(initialValues?: T); | ||
statics: S; | ||
constructor(initialValues?: T, statics?: BindObserver<S, T>); | ||
setStatics(statics?: BindObserver<S, T>): void; | ||
subscribe<StateType extends T>(key: StarOrKey<keyof StateType> | Array<keyof StateType>, listener: Listener): () => void; | ||
set<StateType>(values: ObserverValues<StateType, T>, { silent }?: SetOptions): void; | ||
get<StateType extends T>(key: keyof StateType | Array<keyof StateType>): any; | ||
get<StateType extends T & S>(key: keyof StateType | Array<keyof StateType>): any; | ||
get state(): T; | ||
isValid(key: string): boolean; | ||
} | ||
declare const observer: Observer<Record<string, any>>; | ||
declare const observer: Observer<GlobalState, GlobalStatics>; | ||
export { Observer, observer }; |
@@ -0,1 +1,2 @@ | ||
import { Observer } from '../observer'; | ||
export declare type Listener = (value: any) => void; | ||
@@ -21,1 +22,8 @@ export declare type PartialValues<S, T> = Partial<S extends object ? S : T>; | ||
} : T; | ||
export declare type BindObserver<T, O> = { | ||
[K in keyof T]: T[K] extends (...args: any[]) => any ? (this: Observer<O>, ...args: Parameters<T[K]>) => ReturnType<T[K]> : T[K]; | ||
}; | ||
export interface GlobalState extends Record<string, any> { | ||
} | ||
export interface GlobalStatics { | ||
} |
Sorry, the diff of this file is not supported yet
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
15648
81