Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable server side #7

Merged
merged 5 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ jobs:
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}

- name: Mark scoped package as deprecated
run: npm deprecate @mayank1513/r18gs "Please use r18gs instead. We initially created scoped packages to have similarities with the GitHub Public Repository (which requires packages to be scoped). We are no longer using GPR and thus deprecating all scoped packages for which corresponding un-scoped packages exist."
run: npm deprecate @mayank1513/r18gs "Please use https://www.npmjs.com/package/r18gs instead. We initially created scoped packages to have similarities with the GitHub Public Repository (which requires packages to be scoped). We are no longer using GPR and thus deprecating all scoped packages for which corresponding un-scoped packages exist."
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ I have built wonderful libraries utilizing React18 features using Zustand. They

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.

> Will be using this library soon to fix issues with `nextjs-themes`.

## Features

✅ Full TypeScript Support
Expand All @@ -18,6 +16,8 @@ Thus, I decided to create a bare minimum, ultra-light store that creates shared

✅ Works with all build systems/tools/frameworks for React18

✅ Works for both client side and server side components (be careful, separate states are created for server side and client side. Any changes done on client side will not affect the server components.)

✅ Doccumented 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
Expand Down
8 changes: 8 additions & 0 deletions examples/nextjs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# nextjs-example

## 0.0.6

### Patch Changes

- Updated dependencies
- [email protected]
- [email protected]

## 0.0.5

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nextjs-example",
"version": "0.0.5",
"version": "0.0.6",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
8 changes: 8 additions & 0 deletions examples/remix/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# remix-example

## 0.0.6

### Patch Changes

- Updated dependencies
- [email protected]
- [email protected]

## 0.0.5

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion examples/remix/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "remix-example",
"version": "0.0.5",
"version": "0.0.6",
"private": true,
"sideEffects": false,
"type": "module",
Expand Down
8 changes: 8 additions & 0 deletions examples/vite/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# vite-example

## 0.0.6

### Patch Changes

- Updated dependencies
- [email protected]
- [email protected]

## 0.0.5

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion examples/vite/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vite-example",
"private": true,
"version": "0.0.5",
"version": "0.0.6",
"type": "module",
"scripts": {
"dev": "vite --port 3001",
Expand Down
6 changes: 6 additions & 0 deletions lib/r18gs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# r18gs

## 0.1.0

### Minor Changes

- Remove client side hooks. Now this store works well with server components as well. Though, server side use is tricky and not recommended yet.

## 0.0.5

### Patch Changes
Expand Down
6 changes: 3 additions & 3 deletions lib/r18gs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "r18gs",
"author": "Mayank Kumar Chaudhari <https://mayank-chaudhari.vercel.app>",
"private": false,
"version": "0.0.5",
"version": "0.1.0",
"description": "A simple yet elegant, light weight, react18 global store to replace Zustand for better tree shaking.",
"main": "./index.ts",
"types": "./index.ts",
Expand All @@ -13,7 +13,7 @@
"bugs": {
"url": "https://github.com/react18-tools/react18-global-store/issues"
},
"homepage": "https://github.com/react18-tools/react18-global-store#readme",
"homepage": "https://react18-global-store.vercel.app/",
"sideEffects": false,
"license": "MIT",
"scripts": {
Expand Down Expand Up @@ -67,4 +67,4 @@
"mayank1513",
"turborepo-trmplate"
]
}
}
47 changes: 29 additions & 18 deletions lib/r18gs/src/use-rgs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useCallback, useSyncExternalStore } from "react";
/* eslint-disable @typescript-eslint/non-nullable-type-assertion-style -- as ! operator is forbidden by eslint*/
import { useSyncExternalStore } from "react";

interface React18GlobalStore {
listeners: (() => void)[];
Expand All @@ -11,10 +12,16 @@ export type SetStateAction<T> = (val: SetterArgType<T>) => void;

declare global {
// eslint-disable-next-line no-var -- var required for global declaration.
var rgs: Record<string, React18GlobalStore>;
var rgs: Record<string, React18GlobalStore | undefined>;
// eslint-disable-next-line no-var -- var required for global declaration.
var setters: Record<string, SetStateAction<unknown> | undefined>;
// eslint-disable-next-line no-var -- var required for global declaration.
var subscribers: Record<string, ((listener: () => void) => () => void) | undefined>;
}

globalThis.rgs = {};
globalThis.setters = {};
globalThis.subscribers = {};

/**
* Use this hook similar to `useState` hook.
Expand All @@ -28,29 +35,33 @@ globalThis.rgs = {};
* ```
*/
export default function useRGS<T>(key: string, value?: T): [T, (val: SetterArgType<T>) => void] {
const setRGState = useCallback(
(val: SetterArgType<T>) => {
const rgs = globalThis.rgs[key] as React18GlobalStore;
rgs.value = val instanceof Function ? val(rgs.value as T) : val;
for (const listener of rgs.listeners) listener();
},
[key],
);
const subscribe = useCallback(
(listener: () => void) => {
if (!globalThis.rgs[key]) globalThis.rgs[key] = { listeners: [], value };
if (!globalThis.subscribers[key]) {
globalThis.subscribers[key] = (listener: () => void) => {
if (!globalThis.rgs[key]) {
/** opportunity to add initializer */
globalThis.rgs[key] = { listeners: [], value };
}
const rgs = globalThis.rgs[key] as React18GlobalStore;
rgs.listeners.push(listener);
return () => {
rgs.listeners = rgs.listeners.filter(l => l !== listener);
};
},
[key, value],
);
};
}
const subscribe = globalThis.subscribers[key] as (listener: () => void) => () => void;

if (!globalThis.setters[key]) {
globalThis.setters[key] = val => {
const rgs = globalThis.rgs[key] as React18GlobalStore;
rgs.value = val instanceof Function ? val(rgs.value as T) : val;
/** opportunity to add custom listener */
for (const listener of rgs.listeners) listener();
};
}
const setRGState = globalThis.setters[key] as SetStateAction<T>;

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- temp fix
const getSnapshot = () => (globalThis.rgs[key]?.value ?? value) as T;

const val = useSyncExternalStore<T>(subscribe, getSnapshot, () => value as T);
const val = useSyncExternalStore<T>(subscribe, getSnapshot, getSnapshot);
return [val, setRGState];
}
Loading