Skip to content

Commit

Permalink
breaking: drop react < 18
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Apr 26, 2024
1 parent 73ac3ec commit 5f697d5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 43 deletions.
14 changes: 2 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"compile": "run-s compile:*",
"compile:dom": "microbundle build -f modern,umd --globals react=React,react-dom=ReactDOM --no-compress",
"postcompile:dom": "cp dist/index.modern.mjs dist/index.modern.js && cp dist/index.modern.mjs.map dist/index.modern.js.map",
"precompile:native": "mkdir src/native && cp src/index.ts src/native/index.ts && cp src/batchedUpdates.native.ts src/native/batchedUpdates.ts",
"precompile:native": "mkdir src/native && cp src/index.ts src/native/index.ts",
"compile:native": "microbundle build -f modern,umd -i ./src/native/index.ts -o ./dist/index.native.js --no-compress",
"postcompile:native": "rm -r src/native",
"test": "run-s eslint tsc-test jest",
Expand Down Expand Up @@ -84,17 +84,7 @@
"webpack-dev-server": "^5.0.2"
},
"peerDependencies": {
"react": ">=16.8.0",
"react-dom": "*",
"react-native": "*",
"react": ">=18.0.0",
"scheduler": ">=0.19.0"
},
"peerDependenciesMeta": {
"react-dom": {
"optional": true
},
"react-native": {
"optional": true
}
}
}
3 changes: 0 additions & 3 deletions src/batchedUpdates.native.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/batchedUpdates.ts

This file was deleted.

48 changes: 22 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import {
unstable_runWithPriority as runWithPriority,
} from 'scheduler';

import { batchedUpdates } from './batchedUpdates';

const CONTEXT_VALUE = Symbol();
const ORIGINAL_PROVIDER = Symbol();

Expand All @@ -30,17 +28,17 @@ const useIsomorphicLayoutEffect = isSSR ? useEffect : useLayoutEffect;

// for preact that doesn't have runWithPriority
const runWithNormalPriority = runWithPriority
? (thunk: () => void) => {
? (fn: () => void) => {
try {
runWithPriority(NormalPriority, thunk);
runWithPriority(NormalPriority, fn);
} catch (e: any) {
if (e.message === 'Not implemented.') {
thunk();
fn();
} else {
throw e;
}
}
} : (thunk: () => void) => thunk();
} : (fn: () => void) => fn();

type Version = number;
type Listener<Value> = (
Expand All @@ -52,7 +50,7 @@ type ContextValue<Value> = {
/* "v"alue */ v: MutableRefObject<Value>;
/* versio"n" */ n: MutableRefObject<Version>;
/* "l"isteners */ l: Set<Listener<Value>>;
/* "u"pdate */ u: (thunk: () => void, options?: { suspense: boolean }) => void;
/* "u"pdate */ u: (fn: () => void, options?: { suspense: boolean }) => void;
};
};

Expand All @@ -75,25 +73,23 @@ const createProvider = <Value>(
const contextValue = useRef<ContextValue<Value>>();
if (!contextValue.current) {
const listeners = new Set<Listener<Value>>();
const update = (thunk: () => void, options?: { suspense: boolean }) => {
batchedUpdates(() => {
versionRef.current += 1;
const action: Parameters<Listener<Value>>[0] = {
n: versionRef.current,
};
if (options?.suspense) {
action.n *= -1; // this is intentional to make it temporary version
action.p = new Promise<Value>((r) => {
setResolve(() => (v: Value) => {
action.v = v;
delete action.p;
r(v);
});
const update = (fn: () => void, options?: { suspense: boolean }) => {
versionRef.current += 1;
const action: Parameters<Listener<Value>>[0] = {
n: versionRef.current,
};
if (options?.suspense) {
action.n *= -1; // this is intentional to make it temporary version
action.p = new Promise<Value>((r) => {
setResolve(() => (v: Value) => {
action.v = v;
delete action.p;
r(v);
});
}
listeners.forEach((listener) => listener(action));
thunk();
});
});
}
listeners.forEach((listener) => listener(action));
fn();
};
contextValue.current = {
[CONTEXT_VALUE]: {
Expand Down Expand Up @@ -237,7 +233,7 @@ export function useContext<Value>(context: Context<Value>) {
}

/**
* This hook returns an update function that accepts a thunk function
* This hook returns an update function that accepts a fn function
*
* Use this for a function that will change a value in
* concurrent rendering in React 18.
Expand Down

0 comments on commit 5f697d5

Please sign in to comment.