Skip to content

Commit

Permalink
Merge pull request #16 from react18-tools/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
mayank1513 committed Apr 20, 2024
2 parents 82fa2cf + d67f13d commit 32dbd7d
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 47 deletions.
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.7

### Patch Changes

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

## 0.0.6

### 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.6",
"version": "0.0.7",
"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.7

### Patch Changes

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

## 0.0.6

### 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.6",
"version": "0.0.7",
"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.7

### Patch Changes

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

## 0.0.6

### 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.6",
"version": "0.0.7",
"type": "module",
"scripts": {
"dev": "vite --port 3001",
Expand Down
5 changes: 4 additions & 1 deletion examples/vite/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import ReactDOM from "react-dom/client";
import App from "./App.tsx";
import { SharedRootLayout } from "shared-ui";

ReactDOM.createRoot(document.getElementById("root")!).render(
const rootElement = document.getElementById("root");
if (!rootElement) throw new Error("Root element not found");

ReactDOM.createRoot(rootElement).render(
<React.StrictMode>
<SharedRootLayout>
<App />
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.1

### Patch Changes

- Refactor without changing the exposed APIs. Some libraries using internal APIs may face temporary issues.

## 0.1.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion 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.1.0",
"version": "0.1.1",
"description": "A simple yet elegant, light weight, react18 global store to replace Zustand for better tree shaking.",
"main": "./index.ts",
"types": "./index.ts",
Expand Down
89 changes: 48 additions & 41 deletions lib/r18gs/src/use-rgs.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
/* eslint-disable @typescript-eslint/non-nullable-type-assertion-style -- as ! operator is forbidden by eslint*/
import { useSyncExternalStore } from "react";

interface React18GlobalStore {
listeners: (() => void)[];
value: unknown;
}

export type SetterArgType<T> = T | ((prevState: T) => T);

type Listener = () => void;
type SetterArgType<T> = T | ((prevState: T) => T);
type Subscriber = (l: Listener) => () => void;
export type SetStateAction<T> = (val: SetterArgType<T>) => void;

/**
* This is a hack to reduce lib size + readability + not encouraging direct access to globalThis
*/
const [VALUE, LISTENERS, SETTER, SUBSCRIBER] = [0, 1, 2, 3];
type RGS = [unknown, Listener[], SetStateAction<unknown>, Subscriber];

declare global {
// eslint-disable-next-line no-var -- var required for global declaration.
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>;
var rgs: Record<string, RGS | undefined>;
}

globalThis.rgs = {};
globalThis.setters = {};
globalThis.subscribers = {};
const globalThisForBetterMinification = globalThis;
globalThisForBetterMinification.rgs = {};
const globalRGS = globalThisForBetterMinification.rgs;

/** Initialize the named store when invoked for the first time. */
function init<T>(key: string, value?: T) {
const listeners: Listener[] = [];
/** setter function to set the state. */
const setter: SetStateAction<T> = val => {
const rgs = globalRGS[key] as RGS;
rgs[VALUE] = val instanceof Function ? val(rgs[VALUE] as T) : val;
(rgs[LISTENERS] as Listener[]).forEach(listener => listener());
};
/** subscriber function to subscribe to the store. */
const subscriber: Subscriber = listener => {
const rgs = globalRGS[key] as RGS;
const listeners = rgs[LISTENERS] as Listener[];
listeners.push(listener);
return () => {
rgs[LISTENERS] = listeners.filter(l => l !== listener);
};
};
globalRGS[key] = [value, listeners, setter as SetStateAction<unknown>, subscriber];
}

/**
* Use this hook similar to `useState` hook.
Expand All @@ -34,34 +53,22 @@ globalThis.subscribers = {};
* const [state, setState] = useRGS<number>("counter", 1);
* ```
*/
export default function useRGS<T>(key: string, value?: T): [T, (val: SetterArgType<T>) => void] {
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);
};
};
}
const subscribe = globalThis.subscribers[key] as (listener: () => void) => () => void;
export default function useRGS<T>(
key: string,
value?: T,
serverValue?: T,
): [T, (val: SetterArgType<T>) => void] {
if (!globalRGS[key]) init(key, value);

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>;
const rgs = globalRGS[key] as RGS;

const getSnapshot = () => (globalThis.rgs[key]?.value ?? value) as T;
/** Function to set the state. */
const setRGState = rgs[SETTER] as SetStateAction<T>;
/** Function to get snapshot of the state. */
const getSnap = () => (rgs[VALUE] ?? value) as T;
/** Function to get server snapshot. Returns server value is provided else the default value. */
const getServerSnap = () => (serverValue ?? rgs[VALUE] ?? value) as T;

const val = useSyncExternalStore<T>(subscribe, getSnapshot, getSnapshot);
const val = useSyncExternalStore<T>(rgs[SUBSCRIBER] as Subscriber, getSnap, getServerSnap);
return [val, setRGState];
}
2 changes: 1 addition & 1 deletion packages/shared-ui/src/root-layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "./globals.css";
import "nthul-lite/styles.css";
import { ThemeSwitcher } from "nthul-lite/client/theme-switcher";
import { ForkMe } from "@mayank1513/fork-me/server"; // todo: import directory not supported in remix
import { ForkMe } from "@mayank1513/fork-me/server";
import type { HTMLProps } from "react";
import styles from "./root-layout.module.css";
import { Cards } from "./cards";
Expand Down

0 comments on commit 32dbd7d

Please sign in to comment.