Skip to content

Commit b88b623

Browse files
committed
follow new API
1 parent bdd17af commit b88b623

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"compile": "microbundle build --format cjs,es,umd",
2222
"test": "run-s eslint tsc-test jest",
2323
"eslint": "eslint --ext .js,.ts,.tsx --ignore-pattern dist .",
24-
"jest": "jest --config '{\"preset\":\"ts-jest/presets/js-with-ts\"}' __tests__/*.ts?",
24+
"jest": "jest --config '{\"preset\":\"ts-jest/presets/js-with-ts\"}' __tests__/*.tsx",
2525
"tsc-test": "tsc --project . --noEmit",
2626
"apidoc": "documentation readme --section API --markdown-toc false src/index.ts",
2727
"examples:minimal": "DIR=01_minimal EXT=js webpack-dev-server",

src/index.ts

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ const createProvider = <Value>(OrigProvider: React.Provider<ContextValue<Value>>
1818
ref.current.value = value;
1919
ref.current.listeners.forEach((listener) => listener());
2020
const contextValue = React.useMemo(() => {
21-
const source = createMutableSource(ref, {
22-
getVersion: () => ref.current.value,
23-
});
21+
const source = createMutableSource(ref, () => ref.current.value);
2422
return { [SOURCE_SYMBOL]: source };
2523
}, []);
2624
return React.createElement(OrigProvider, { value: contextValue }, children);
@@ -76,28 +74,26 @@ export const useContextSelector = <Value, Selected>(
7674
if (!source) {
7775
throw new Error('useContextSelector requires special context');
7876
}
79-
const config = React.useMemo(() => ({
80-
getSnapshot: (
81-
ref: React.MutableRefObject<{ value: Value }>,
82-
) => selector(ref.current.value),
83-
subscribe: (
84-
ref: React.MutableRefObject<{ value: Value; listeners: Set<() => void> }>,
85-
callback: () => void,
86-
) => {
87-
let selected = selector(ref.current.value);
88-
const listener = () => {
89-
const nextSelected = selector(ref.current.value);
90-
if (!Object.is(selected, nextSelected)) {
91-
callback();
92-
selected = nextSelected;
93-
}
94-
};
95-
const { listeners } = ref.current;
96-
listeners.add(listener);
97-
return () => listeners.delete(callback);
98-
},
99-
}), [selector]);
100-
return useMutableSource(source, config);
77+
const getSnapshot = React.useCallback((
78+
ref: React.MutableRefObject<{ value: Value }>,
79+
) => selector(ref.current.value), [selector]);
80+
const subscribe = React.useCallback((
81+
ref: React.MutableRefObject<{ value: Value; listeners: Set<() => void> }>,
82+
callback: () => void,
83+
) => {
84+
let selected = selector(ref.current.value);
85+
const listener = () => {
86+
const nextSelected = selector(ref.current.value);
87+
if (!Object.is(selected, nextSelected)) {
88+
callback();
89+
selected = nextSelected;
90+
}
91+
};
92+
const { listeners } = ref.current;
93+
listeners.add(listener);
94+
return () => listeners.delete(callback);
95+
}, [selector]);
96+
return useMutableSource(source, getSnapshot, subscribe);
10197
};
10298

10399
const identity = <T>(x: T) => x;

0 commit comments

Comments
 (0)