Comparing version 1.1.1 to 1.2.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,c,a;o=new WeakMap,i=new WeakSet,c=function(t){s(this,o,"f")[t]||(s(this,o,"f")[t]={listeners:new Set})},a=function(t){const e=s(this,o,"f")[t].listeners,r=s(this,o,"f")[t].value;e.forEach((t=>t(r)))};const u=new class{constructor(){i.add(this),o.set(this,{})}subscribe(t,e){if("function"!=typeof e)return r=["listener must be a function"],console.warn(r.join("\n")),()=>!1;var r;s(this,i,"m",c).call(this,t);const n=s(this,o,"f")[t].listeners;return n.add(e),()=>n.delete(e)}set(t,{silent:e}={silent:!1}){"object"==typeof t&&Object.entries(t).forEach((([t,r])=>{s(this,i,"m",c).call(this,t),s(this,o,"f")[t].value=r,e||s(this,i,"m",a).call(this,t)}))}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,u.get(t)]))):void 0}isValid(t){return void 0!==s(this,o,"f")[t]}};function f(n){const s=t({}),i=e({keys:new Set});return r((()=>{const{keys:t}=i.current;if(0===t.size)return;const e=[];function r(t){s[1]({value:t})}return t.forEach((t=>e.push(u.subscribe(t,r)))),()=>{e.forEach((t=>t()))}}),[]),new Proxy({},{get(t,e){const{keys:r}=i.current;if((e=e.toLowerCase()).startsWith("set")){const t=e.replace("set","");return void 0!==n&&o(t),e=>{u.set({[t]:c(e,t)})}}return t[e]?t[e]:(r.add(e),o(e))}});function o(t){let e=u.get(t);return u.isValid(t)||(e=c(n,t),u.set({[t]:e})),e}function c(t,e){return"function"==typeof t?t(u.get(e)):t}}function l(t){function e(e){const r=f();return Object.defineProperties(r,Object.fromEntries(Object.entries(e).map((([t,e])=>[t,{value:e}])))),t(r)}return n(e)}export{u as observer,f as useObserver,l as withState}; | ||
import{useState as t,useRef as e,useEffect as n,memo as r}from"react";function s(t,e,n,r){if("a"===n&&!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"===n?r:"a"===n?r.call(t):r?r.value:e.get(t)}var i,o,a,c,f,u,h;class l{constructor(){i.add(this),o.set(this,{}),a.set(this,new Map)}subscribe(t,e){return"function"!=typeof e?(n=["listener must be a function"],console.warn(n.join("\n")),()=>!1):("string"==typeof t&&(t=[t]),s(this,i,"m",f).call(this,e),t.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),t.forEach((t=>s(this,o,"f")[t].listeners.delete(e)))});var n}set(t,{silent:e}={silent:!1}){if("object"!=typeof t)return;const n=Object.keys(t);n.forEach((e=>{s(this,i,"m",c).call(this,e),s(this,o,"f")[e].value=t[e]})),e||s(this,i,"m",u).call(this,n)}get(t){var e,n;return"string"==typeof t?null===(n=null===(e=s(this,o,"f"))||void 0===e?void 0:e[t.toLowerCase()])||void 0===n?void 0:n.value:Array.isArray(t)?Object.fromEntries(t.map((t=>[t,d.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){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",h).call(this,t))))},h=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 d=new l;function m(r=undefined){const s=r instanceof l,i=s?void 0:r,o=s?r:d,a=t({}),c=e({keys:new Set});return n((()=>{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:n}=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]:(n.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 v(t,e){function n(n){const r=m(e);return Object.defineProperties(r,Object.fromEntries(Object.entries(n).map((([t,e])=>[t,{value:e}])))),t(r)}return r(n)}export{l as Observer,d as observer,m as useObserver,v as withState}; |
{ | ||
"name": "rosma", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"repository": { | ||
@@ -5,0 +5,0 @@ "type": "git", |
155
README.md
@@ -149,30 +149,30 @@ # Rosma | ||
export default function Note() { | ||
const Input = withState(({ note = '', setNote }) => ( | ||
<input | ||
value={note} | ||
onChange={(e) => setNote(e.target.value)} | ||
placeholder="write something" | ||
/> | ||
)); | ||
const Input = withState(({ note = '', setNote }) => ( | ||
<input | ||
value={note} | ||
onChange={(e) => setNote(e.target.value)} | ||
placeholder="write something" | ||
/> | ||
)); | ||
const Button = withState(({ note, setNotes, setNote }) => ( | ||
<button | ||
onClick={() => { | ||
setNotes((notes = []) => notes.concat(note)); | ||
setNote(''); | ||
}} | ||
> | ||
Add | ||
</button> | ||
)); | ||
const Button = withState(({ note, setNotes, setNote }) => ( | ||
<button | ||
onClick={() => { | ||
setNotes((notes = []) => notes.concat(note)); | ||
setNote(''); | ||
}} | ||
> | ||
Add | ||
</button> | ||
)); | ||
const List = withState(({ notes = [] }) => ( | ||
<ul> | ||
{notes.map((note, index) => ( | ||
<li key={index}>{note}</li> | ||
))} | ||
</ul> | ||
)); | ||
const List = withState(({ notes = [] }) => ( | ||
<ul> | ||
{notes.map((note, index) => ( | ||
<li key={index}>{note}</li> | ||
))} | ||
</ul> | ||
)); | ||
export default function Note() { | ||
return ( | ||
@@ -195,47 +195,29 @@ <> | ||
export default function Note() { | ||
const Input = withState(({ note = '', setNote }) => ( | ||
<input | ||
value={note} | ||
onChange={(e) => setNote(e.target.value)} | ||
placeholder="write something" | ||
/> | ||
)); | ||
const Input = withState(({ note = '', setNote }) => ( | ||
<input | ||
value={note} | ||
onChange={(e) => setNote(e.target.value)} | ||
placeholder="write something" | ||
/> | ||
)); | ||
const Button = withState(({ setNotes, setNote }) => ( | ||
<button | ||
onClick={() => { | ||
setNotes((notes = []) => notes.concat(observer.get('note'))); | ||
setNote(''); | ||
}} | ||
> | ||
Add | ||
</button> | ||
)); | ||
const Button = withState(({ setNotes, setNote }) => ( | ||
<button | ||
onClick={() => { | ||
setNotes((notes = []) => notes.concat(observer.get('note'))); | ||
setNote(''); | ||
}} | ||
> | ||
Add | ||
</button> | ||
)); | ||
const List = withState(({ notes = [] }) => ( | ||
<ul> | ||
{notes.map((note, index) => ( | ||
<li key={index}>{note}</li> | ||
))} | ||
</ul> | ||
)); | ||
const List = withState(({ notes = [] }) => ( | ||
<ul> | ||
{notes.map((note, index) => ( | ||
<li key={index}>{note}</li> | ||
))} | ||
</ul> | ||
)); | ||
return ( | ||
<> | ||
<Input /> | ||
<Button /> | ||
<List /> | ||
</> | ||
); | ||
} | ||
``` | ||
### Separate everything | ||
Given that all state variables can be used globally across all components in your application, you can define the parent component's parts outside of it. | ||
```javascript | ||
import { observer, useObserver } from 'rosma'; | ||
export default function Note() { | ||
@@ -250,41 +232,2 @@ return ( | ||
} | ||
function Input() { | ||
const { note, setNote } = useObserver(''); | ||
return ( | ||
<input | ||
value={note} | ||
onChange={(e) => setNote(e.target.value)} | ||
placeholder="write something" | ||
/> | ||
); | ||
} | ||
function Button() { | ||
const { setNotes, setNote } = useObserver(); | ||
return ( | ||
<button | ||
onClick={() => { | ||
setNotes((notes) => notes.concat(observer.get('note'))); | ||
setNote(''); | ||
}} | ||
> | ||
Add | ||
</button> | ||
); | ||
} | ||
function List() { | ||
const { notes } = useObserver([]); | ||
return ( | ||
<ul> | ||
{notes.map((note, index) => ( | ||
<li key={index}>{note}</li> | ||
))} | ||
</ul> | ||
); | ||
} | ||
``` |
@@ -1,2 +0,1 @@ | ||
import { State } from '../types'; | ||
export declare function useObserver<T>(initialValue?: any): State<T>; | ||
export declare function useObserver<T extends Record<string, any>>(input?: any): T; |
/// <reference types="react" /> | ||
import { State } from '../types'; | ||
export declare function withState<T>(callback: any): import("react").NamedExoticComponent<State<T>>; | ||
import { Observer } from '../observer'; | ||
export declare function withState<T extends Record<string, any>>(callback: any, observer?: Observer): import("react").NamedExoticComponent<T>; |
import { Listener } from '../types'; | ||
declare class Observer { | ||
#private; | ||
subscribe(key: string, listener: Listener): () => boolean; | ||
subscribe(key: string | string[], listener: Listener): () => void; | ||
set<T>(object: T | Record<string, any>, { silent }?: { | ||
@@ -11,3 +11,3 @@ silent?: boolean; | ||
} | ||
export declare const observer: Observer; | ||
export {}; | ||
declare const observer: Observer; | ||
export { Observer, observer }; |
@@ -1,2 +0,1 @@ | ||
export declare type State<T> = T & Record<string, any>; | ||
export declare type Listener = (value: any) => void; | ||
@@ -3,0 +2,0 @@ export declare type CacheData = { |
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
47
12209
231