Skip to content

Commit

Permalink
follow new API
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Feb 15, 2020
1 parent bdd17af commit b88b623
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"compile": "microbundle build --format cjs,es,umd",
"test": "run-s eslint tsc-test jest",
"eslint": "eslint --ext .js,.ts,.tsx --ignore-pattern dist .",
"jest": "jest --config '{\"preset\":\"ts-jest/presets/js-with-ts\"}' __tests__/*.ts?",
"jest": "jest --config '{\"preset\":\"ts-jest/presets/js-with-ts\"}' __tests__/*.tsx",
"tsc-test": "tsc --project . --noEmit",
"apidoc": "documentation readme --section API --markdown-toc false src/index.ts",
"examples:minimal": "DIR=01_minimal EXT=js webpack-dev-server",
Expand Down
46 changes: 21 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ const createProvider = <Value>(OrigProvider: React.Provider<ContextValue<Value>>
ref.current.value = value;
ref.current.listeners.forEach((listener) => listener());
const contextValue = React.useMemo(() => {
const source = createMutableSource(ref, {
getVersion: () => ref.current.value,
});
const source = createMutableSource(ref, () => ref.current.value);
return { [SOURCE_SYMBOL]: source };
}, []);
return React.createElement(OrigProvider, { value: contextValue }, children);
Expand Down Expand Up @@ -76,28 +74,26 @@ export const useContextSelector = <Value, Selected>(
if (!source) {
throw new Error('useContextSelector requires special context');
}
const config = React.useMemo(() => ({
getSnapshot: (
ref: React.MutableRefObject<{ value: Value }>,
) => selector(ref.current.value),
subscribe: (
ref: React.MutableRefObject<{ value: Value; listeners: Set<() => void> }>,
callback: () => void,
) => {
let selected = selector(ref.current.value);
const listener = () => {
const nextSelected = selector(ref.current.value);
if (!Object.is(selected, nextSelected)) {
callback();
selected = nextSelected;
}
};
const { listeners } = ref.current;
listeners.add(listener);
return () => listeners.delete(callback);
},
}), [selector]);
return useMutableSource(source, config);
const getSnapshot = React.useCallback((
ref: React.MutableRefObject<{ value: Value }>,
) => selector(ref.current.value), [selector]);
const subscribe = React.useCallback((
ref: React.MutableRefObject<{ value: Value; listeners: Set<() => void> }>,
callback: () => void,
) => {
let selected = selector(ref.current.value);
const listener = () => {
const nextSelected = selector(ref.current.value);
if (!Object.is(selected, nextSelected)) {
callback();
selected = nextSelected;
}
};
const { listeners } = ref.current;
listeners.add(listener);
return () => listeners.delete(callback);
}, [selector]);
return useMutableSource(source, getSnapshot, subscribe);
};

const identity = <T>(x: T) => x;
Expand Down

0 comments on commit b88b623

Please sign in to comment.