New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

r18gs

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

r18gs - npm Package Compare versions

Comparing version 0.2.0 to 1.0.0

dist/chunk-336R5BEM.mjs

19

dist/index.d.ts

@@ -1,3 +0,18 @@

import useRGS from "./use-rgs";
import type { SetStateAction } from "./utils";
export type { SetterArgType, SetStateAction, Plugin } from "./utils";
export default useRGS;
/**
* Use this hook similar to `useState` hook.
* The difference is that you need to pass a
* unique key - unique across the app to make
* this state accessible to all client components.
*
* @example
* ```tsx
* const [state, setState] = useRGS<number>("counter", 1);
* ```
*
* @param key - Unique key to identify the store.
* @param value - Initial value of the store.
* @returns - A tuple (Ordered sequance of values) containing the state and a function to set the state.
*/
export default function useRGS<T>(key: string, value?: T): [T, SetStateAction<T>];

2

dist/index.js

@@ -1,1 +0,1 @@

"use strict";var s=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var p=Object.getOwnPropertyNames;var b=Object.prototype.hasOwnProperty;var d=(e,t)=>{for(var n in t)s(e,n,{get:t[n],enumerable:!0})},v=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of p(t))!b.call(e,r)&&r!==n&&s(e,r,{get:()=>t[r],enumerable:!(o=f(t,r))||o.enumerable});return e};var R=e=>v(s({},"__esModule",{value:!0}),e);var A={};d(A,{default:()=>G});module.exports=R(A);var u=require("react");var S=globalThis;S.rgs={};var i=S.rgs;function x(e){e[2].forEach(t=>t())}function T(e){return t=>{let n=i[e],o=n[2];return o.push(t),()=>{n[2]=o.filter(r=>r!==t)}}}function g(e){return t=>{let n=i[e];n[0]=t instanceof Function?t(n[0]):t,x(n)}}function l(e){let t=i[e],n=t[3],o=()=>t[0],r=()=>{var c;return(c=t[1])!=null?c:t[0]};return[(0,u.useSyncExternalStore)(t[4],o,r),n]}function a(e,t,n){return i[e]||(i[e]=[t,n,[],g(e),T(e)]),l(e)}var G=a;
"use strict";var s=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var l=Object.getOwnPropertyNames;var p=Object.prototype.hasOwnProperty;var b=(e,t)=>{for(var n in t)s(e,n,{get:t[n],enumerable:!0})},d=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of l(t))!p.call(e,o)&&o!==n&&s(e,o,{get:()=>t[o],enumerable:!(r=f(t,o))||r.enumerable});return e};var x=e=>d(s({},"__esModule",{value:!0}),e);var A={};b(A,{default:()=>g});module.exports=x(A);var a=require("react");var c=globalThis;c.rgs={};var i=c.rgs;function u(e){return t=>{let n=i[e];return n[1].push(t),()=>{n[1]=n[1].filter(r=>r!==t)}}}function S(e){return t=>{let n=i[e];n[0]=t instanceof Function?t(n[0]):t,n[1].forEach(r=>r())}}function T(e){let t=i[e];return[(0,a.useSyncExternalStore)(t[3],()=>t[0]),t[2]]}function g(e,t){return i[e]||(i[e]=[t,[],S(e),u(e)]),T(e)}

@@ -5,3 +5,3 @@ type Listener = () => void;

export type SetStateAction<T> = (value: SetterArgType<T>) => void;
type RGS = [unknown, unknown, Listener[], SetStateAction<unknown>, Subscriber];
type RGS = [unknown, Listener[], SetStateAction<unknown> | null, Subscriber];
declare global {

@@ -17,9 +17,27 @@ var rgs: Record<string, RGS | undefined>;

export declare function createHook<T>(key: string): [T, SetStateAction<T>];
type Mutate<T> = (value?: T, serverValue?: T) => void;
type Mutate<T> = (value?: T) => void;
export type Plugin<T> = {
init?: (key: string, value: T | undefined, serverValue: T | undefined, mutate: Mutate<T>) => void;
onChange?: (key: string, value?: T, serverValue?: T) => void;
init?: (key: string, value: T | undefined, mutate: Mutate<T>) => void;
onChange?: (key: string, value?: T) => void;
};
/** Initialize the named store when invoked for the first time. */
export declare function initWithPlugins<T>(key: string, value?: T, serverValue?: T, plugins?: Plugin<T>[]): void;
export declare function initWithPlugins<T>(key: string, value?: T, plugins?: Plugin<T>[], doNotInit?: boolean): void;
/**
* Use this hook similar to `useState` hook.
* The difference is that you need to pass a
* unique key - unique across the app to make
* this state accessible to all client components.
*
* @example
* ```tsx
* const [state, setState] = useRGS<number>("counter", 1);
* ```
*
* @param key - Unique key to identify the store.
* @param value - Initial value of the store.
* @param plugins - Plugins to be applied to the store.
* @param doNotInit - Do not initialize the store. Useful when you want to initialize the store later. Note that the setter function is not available until the store is initialized.
* @returns - A tuple (Ordered sequance of values) containing the state and a function to set the state.
*/
export declare function useRGSWithPlugins<T>(key: string, value?: T, plugins?: Plugin<T>[], doNotInit?: boolean): [T, SetStateAction<T>];
export {};

@@ -1,1 +0,1 @@

"use strict";var S=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var G=(e,t)=>{for(var n in t)S(e,n,{get:t[n],enumerable:!0})},h=(e,t,n,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of R(t))!x.call(e,r)&&r!==n&&S(e,r,{get:()=>t[r],enumerable:!(s=v(t,r))||s.enumerable});return e};var y=e=>h(S({},"__esModule",{value:!0}),e);var l=(e,t,n)=>new Promise((s,r)=>{var i=a=>{try{c(n.next(a))}catch(T){r(T)}},o=a=>{try{c(n.throw(a))}catch(T){r(T)}},c=a=>a.done?s(a.value):Promise.resolve(a.value).then(i,o);c((n=n.apply(e,t)).next())});var m={};G(m,{createHook:()=>w,createSetter:()=>A,createSubcriber:()=>d,globalRGS:()=>u,initWithPlugins:()=>P});module.exports=y(m);var f=require("react");var p=globalThis;p.rgs={};var u=p.rgs;function g(e){e[2].forEach(t=>t())}function d(e){return t=>{let n=u[e],s=n[2];return s.push(t),()=>{n[2]=s.filter(r=>r!==t)}}}function A(e){return t=>{let n=u[e];n[0]=t instanceof Function?t(n[0]):t,g(n)}}function w(e){let t=u[e],n=t[3],s=()=>t[0],r=()=>{var o;return(o=t[1])!=null?o:t[0]};return[(0,f.useSyncExternalStore)(t[4],s,r),n]}var b=!1;function L(e,t){return l(this,null,function*(){var r;let n=u[e],s=(i,o)=>{n[0]=i,n[1]=o,g(n)};for(let i of t)yield(r=i.init)==null?void 0:r.call(i,e,n[0],n[1],s);b=!0})}function P(e,t,n,s=[]){let r=i=>{if(!b)return;let o=u[e];o[0]=i instanceof Function?i(o[0]):i,g(o),s.forEach(c=>{var a;return(a=c.onChange)==null?void 0:a.call(c,e,o[0],o[1])})};u[e]=[t,n,[],r,d(e)],L(e,s)}0&&(module.exports={createHook,createSetter,createSubcriber,globalRGS,initWithPlugins});
"use strict";var S=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames;var A=Object.prototype.hasOwnProperty;var v=(t,e)=>{for(var n in e)S(t,n,{get:e[n],enumerable:!0})},L=(t,e,n,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of G(e))!A.call(t,r)&&r!==n&&S(t,r,{get:()=>e[r],enumerable:!(i=R(e,r))||i.enumerable});return t};var P=t=>L(S({},"__esModule",{value:!0}),t);var f=(t,e,n)=>new Promise((i,r)=>{var s=o=>{try{c(n.next(o))}catch(u){r(u)}},T=o=>{try{c(n.throw(o))}catch(u){r(u)}},c=o=>o.done?i(o.value):Promise.resolve(o.value).then(s,T);c((n=n.apply(t,e)).next())});var M={};v(M,{createHook:()=>d,createSetter:()=>w,createSubcriber:()=>g,globalRGS:()=>a,initWithPlugins:()=>x,useRGSWithPlugins:()=>m});module.exports=P(M);var l=require("react");var p=globalThis;p.rgs={};var a=p.rgs;function b(t){t[1].forEach(e=>e())}function g(t){return e=>{let n=a[t];return n[1].push(e),()=>{n[1]=n[1].filter(i=>i!==e)}}}function w(t){return e=>{let n=a[t];n[0]=e instanceof Function?e(n[0]):e,n[1].forEach(i=>i())}}function d(t){let e=a[t];return[(0,l.useSyncExternalStore)(e[3],()=>e[0]),e[2]]}var h=!1;function E(t,e){return f(this,null,function*(){var r;let n=a[t],i=s=>{n[0]=s,b(n)};for(let s of e)yield(r=s.init)==null?void 0:r.call(s,t,n[0],i);h=!0})}function x(t,e,n=[],i=!1){if(i){a[t]=[e,[],null,g(t)];return}let r=T=>{if(!h)return;let c=a[t];c[0]=T instanceof Function?T(c[0]):T,b(c),n.forEach(o=>{var u;return(u=o.onChange)==null?void 0:u.call(o,t,c[0])})},s=a[t];s?(s[0]=e,s[2]=r):a[t]=[e,[],r,g(t)],E(t,n)}function m(t,e,n,i=!1){var r;return(r=a[t])!=null&&r[2]||x(t,e,n,i),d(t)}0&&(module.exports={createHook,createSetter,createSubcriber,globalRGS,initWithPlugins,useRGSWithPlugins});

@@ -0,11 +1,13 @@

import { useRGSWithPlugins } from "./utils";
import type { Plugin, SetStateAction } from "./utils";
/**
* Use this hook similar to `useState` hook.
* The difference is that you need to pass a
* unique key - unique across the app to make
* this state accessible to all client components.
* Creates a store with plugins.
* @example
*
* @example
* ```tsx
* const [state, setState] = useRGS<number>("counter", 1);
* // in hook file, e.g., store.ts
* export const useMyRGS = create<type>(key, value, plugins);
*
* // in component file
* const [state, setState] = useMyRGS();
* ```

@@ -15,28 +17,13 @@ *

* @param value - Initial value of the store.
* @param serverValue - Server value of the store.
* @param plugins - Plugins to be applied to the store.
* @returns - A tuple (Ordered sequance of values) containing the state and a function to set the state.
* @returns - A hook function that returns a tuple (Ordered sequence of values) containing the state and a function to set the state.
*/
export declare function useRGSWithPlugins<T>(key: string, value?: T, serverValue?: T, plugins?: Plugin<T>[]): [T, SetStateAction<T>];
export declare function create<T>(key: string, value?: T, plugins?: Plugin<T>[]): () => [T, SetStateAction<T>];
/**
* Use this hook similar to `useState` hook.
* The difference is that you need to pass a
* unique key - unique across the app to make
* this state accessible to all client components.
* Creates a hook similar to useRGS, but with plugins to be applied on first invocation.
*
* @example
* ```tsx
* // in hook file
* export const useRGS = create<type>(key, value, serverValue, plugins);
*
* // in component file
* const [state, setState] = useRGS();
* ```
*
* @param key - Unique key to identify the store.
* @param value - Initial value of the store.
* @param serverValue - Server value of the store.
* @param plugins - Plugins to be applied to the store.
* @returns - A hook funciton that returns a tuple (Ordered sequance of values) containing the state and a function to set the state.
* @returns A hook that automatically initializes the store (if not already initialized) with the given plugins.
*/
export declare function create<T>(key: string, value?: T, serverValue?: T, plugins?: Plugin<T>[]): () => [T, SetStateAction<T>];
export declare function withPlugins<T>(plugins: Plugin<T>[]): <U = T>(key: string, value?: U, doNotInit?: boolean) => [U, SetStateAction<U>];
export { useRGSWithPlugins };

@@ -1,1 +0,1 @@

"use strict";var S=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames;var h=Object.prototype.hasOwnProperty;var A=(t,e)=>{for(var n in e)S(t,n,{get:e[n],enumerable:!0})},P=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of G(e))!h.call(t,i)&&i!==n&&S(t,i,{get:()=>e[i],enumerable:!(r=x(e,i))||r.enumerable});return t};var m=t=>P(S({},"__esModule",{value:!0}),t);var g=(t,e,n)=>new Promise((r,i)=>{var o=a=>{try{c(n.next(a))}catch(u){i(u)}},s=a=>{try{c(n.throw(a))}catch(u){i(u)}},c=a=>a.done?r(a.value):Promise.resolve(a.value).then(o,s);c((n=n.apply(t,e)).next())});var E={};A(E,{create:()=>L,useRGSWithPlugins:()=>R});module.exports=m(E);var l=require("react");var f=globalThis;f.rgs={};var T=f.rgs;function p(t){t[2].forEach(e=>e())}function y(t){return e=>{let n=T[t],r=n[2];return r.push(e),()=>{n[2]=r.filter(i=>i!==e)}}}function b(t){let e=T[t],n=e[3],r=()=>e[0],i=()=>{var s;return(s=e[1])!=null?s:e[0]};return[(0,l.useSyncExternalStore)(e[4],r,i),n]}var d=!1;function w(t,e){return g(this,null,function*(){var i;let n=T[t],r=(o,s)=>{n[0]=o,n[1]=s,p(n)};for(let o of e)yield(i=o.init)==null?void 0:i.call(o,t,n[0],n[1],r);d=!0})}function v(t,e,n,r=[]){let i=o=>{if(!d)return;let s=T[t];s[0]=o instanceof Function?o(s[0]):o,p(s),r.forEach(c=>{var a;return(a=c.onChange)==null?void 0:a.call(c,t,s[0],s[1])})};T[t]=[e,n,[],i,y(t)],w(t,r)}function R(t,e,n,r){return T[t]||v(t,e,n,r),b(t)}function L(t,e,n,r){return()=>R(t,e,n,r)}0&&(module.exports={create,useRGSWithPlugins});
"use strict";var g=Object.defineProperty;var d=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var A=Object.prototype.hasOwnProperty;var R=(t,n)=>{for(var e in n)g(t,e,{get:n[e],enumerable:!0})},G=(t,n,e,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let i of x(n))!A.call(t,i)&&i!==e&&g(t,i,{get:()=>n[i],enumerable:!(r=d(n,i))||r.enumerable});return t};var v=t=>G(g({},"__esModule",{value:!0}),t);var l=(t,n,e)=>new Promise((r,i)=>{var o=s=>{try{a(e.next(s))}catch(c){i(c)}},T=s=>{try{a(e.throw(s))}catch(c){i(c)}},a=s=>s.done?r(s.value):Promise.resolve(s.value).then(o,T);a((e=e.apply(t,n)).next())});var y={};R(y,{create:()=>w,useRGSWithPlugins:()=>S,withPlugins:()=>W});module.exports=v(y);var p=require("react");var h=globalThis;h.rgs={};var u=h.rgs;function P(t){t[1].forEach(n=>n())}function f(t){return n=>{let e=u[t];return e[1].push(n),()=>{e[1]=e[1].filter(r=>r!==n)}}}function U(t){let n=u[t];return[(0,p.useSyncExternalStore)(n[3],()=>n[0]),n[2]]}var b=!1;function m(t,n){return l(this,null,function*(){var i;let e=u[t],r=o=>{e[0]=o,P(e)};for(let o of n)yield(i=o.init)==null?void 0:i.call(o,t,e[0],r);b=!0})}function L(t,n,e=[],r=!1){if(r){u[t]=[n,[],null,f(t)];return}let i=T=>{if(!b)return;let a=u[t];a[0]=T instanceof Function?T(a[0]):T,P(a),e.forEach(s=>{var c;return(c=s.onChange)==null?void 0:c.call(s,t,a[0])})},o=u[t];o?(o[0]=n,o[2]=i):u[t]=[n,[],i,f(t)],m(t,e)}function S(t,n,e,r=!1){var i;return(i=u[t])!=null&&i[2]||L(t,n,e,r),U(t)}function w(t,n,e){return()=>S(t,n,e)}function W(t){return(e,r,i=!1)=>S(e,r,t,i)}0&&(module.exports={create,useRGSWithPlugins,withPlugins});

@@ -5,3 +5,3 @@ {

"private": false,
"version": "0.2.0",
"version": "1.0.0",
"description": "A simple yet elegant, light weight, react18 global store to replace Zustand for better tree shaking.",

@@ -23,3 +23,3 @@ "main": "./dist/index.js",

"sideEffects": false,
"license": "MIT",
"license": "MPL-2.0",
"scripts": {

@@ -47,2 +47,6 @@ "test": "vitest run --coverage",

"typedoc": "^0.25.13",
"typedoc-plugin-mdn-links": "^3.1.23",
"typedoc-plugin-missing-exports": "^2.2.0",
"typedoc-plugin-rename-defaults": "^0.7.0",
"typedoc-plugin-zod": "^1.1.2",
"typescript": "^5.4.5",

@@ -56,6 +60,9 @@ "vite-tsconfig-paths": "^4.3.2",

},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/mayank1513"
},
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/mayank1513"
},
"https://pages.razorpay.com/mayank1513"
],
"keywords": [

@@ -62,0 +69,0 @@ "react18-global-store",

@@ -7,5 +7,5 @@ # React18GlobalStore

I have built wonderful libraries utilizing React18 features using Zustand. They worked awesome. However, when I try importing from specific folder for better tree-shaking, the libraries fail. This is because, for each import a separate zustand store is created. This actually increases the package size also.
I've developed fantastic libraries leveraging React18 features using Zustand, and they performed admirably. However, when attempting to import from specific folders for better tree-shaking, the libraries encountered issues. Each import resulted in a separate Zustand store being created, leading to increased package size.
Thus, I decided to create a bare minimum, ultra-light store that creates shared state even while importing components from separate files for better treeshaking.
As a solution, I set out to create a lightweight, bare minimum store that facilitates shared state even when importing components from separate files, optimizing tree-shaking.

@@ -18,35 +18,12 @@ ## Features

✅ Works with all build systems/tools/frameworks for React18
✅ Compatible with all build systems/tools/frameworks for React18
✅ Doccumented with [Typedoc](https://react18-tools.github.io/react18-global-store) ([Docs](https://react18-tools.github.io/react18-global-store))
✅ Documented with [Typedoc](https://react18-tools.github.io/react18-global-store) ([Docs](https://react18-tools.github.io/react18-global-store))
✅ Next.js, Vite and Remix examples
✅ Examples for Next.js, Vite, and Remix
## Install
## Simple Global State Shared Across Multiple Components
> A canonical package with longer name is also published `react18-global-store`
Utilize this hook similarly to the `useState` hook. However, ensure to pass a unique key, unique across the app, to identify and make this state accessible to all client components.
```bash
$ pnpm add r18gs
```
or
```bash
$ npm install r18gs
```
or
```bash
$ yarn add r18gs
```
## Usage
Use this hook similar to `useState` hook.
The difference is that you need to pass an unique key - unique across the app to identify
and make this state accessible to all client components.
```tsx

@@ -56,55 +33,24 @@ const [state, setState] = useRGS<number>("counter", 1);

You can access the same state across all client side components using unique key.
> For detailed instructions, see [Getting Started](./md-docs/1.getting-started.md)
> It is recommended to store your keys in separate file to avoid typos and unnecessary conflicts.
## Using Plugins
### Example
Enhance the functionality of the store by leveraging either the `create` function, `withPlugins` function, or the `useRGSWithPlugins` hook from `r18gs/dist/with-plugins`, enabling features such as storing to local storage, among others.
```tsx
// constants/global-states.ts
export const COUNTER = "counter";
// store.ts
import { create } from "r18gs/dist/with-plugins";
import { persist } from "r18gs/dist/plugins"; /** You can create your own plugin or import third-party plugins */
export const useMyPersistentCounterStore = create<number>("persistent-counter", 0, [persist()]);
```
Now, you can utilize `useMyPersistentCounterStore` similarly to `useState` without specifying an initial value.
```tsx
// components/display.tsx
"use client";
import useRGS from "r18gs";
import { COUNTER } from "../constants/global-states";
export default function Display() {
const [count] = useRGS<number>(COUNTER);
return (
<div>
<h2>Client component 2</h2>
<b>{count}</b>
</div>
);
}
const [persistedCount, setPersistedCount] = useMyPersistentCounterStore();
```
```tsx
// components/counter.tsx
"use client";
> For detailed instructions, see [Leveraging Plugins](./md-docs/2.leveraging-plugins.md)
import useRGS from "r18gs";
import { COUNTER } from "../constants/global-states";
export default function Counter() {
const [count, setCount] = useRGS(COUNTER, 0);
return (
<div>
<h2>Clinet component 1</h2>
<input
onChange={e => {
setCount(parseInt(e.target.value.trim()));
}}
type="number"
value={count}
/>
</div>
);
}
```
## Contributing

@@ -114,5 +60,5 @@

### 🤩 Don't forger to star [this repo](https://github.com/mayank1513/react18-global-store)!
### 🤩 Don't forget to star [this repo](https://github.com/mayank1513/react18-global-store)!
Want hands-on course for getting started with Turborepo? Check out [React and Next.js with TypeScript](https://mayank-chaudhari.vercel.app/courses/react-and-next-js-with-typescript) and [The Game of Chess with Next.js, React and TypeScrypt](https://www.udemy.com/course/game-of-chess-with-nextjs-react-and-typescrypt/?referralCode=851A28F10B254A8523FE)
Interested in hands-on courses for getting started with Turborepo? Check out [React and Next.js with TypeScript](https://mayank-chaudhari.vercel.app/courses/react-and-next-js-with-typescript) and [The Game of Chess with Next.js, React and TypeScript](https://www.udemy.com/course/game-of-chess-with-nextjs-react-and-typescript/?referralCode=851A28F10B254A8523FE)

@@ -123,3 +69,3 @@ ![Repo Stats](https://repobeats.axiom.co/api/embed/ec3e74d795ed805a0fce67c0b64c3f08872e7945.svg "Repobeats analytics image")

Licensed as MIT open source.
Licensed under the MPL 2.0 open source license.

@@ -126,0 +72,0 @@ <hr />

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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