Skip to content

avoids duplicate deref calculations in useStoreDependency #96

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

Merged
merged 1 commit into from
Mar 15, 2021
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
36 changes: 36 additions & 0 deletions src/dependencies/__tests__/useStoreDependency-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,16 @@ describe('useStoreDependency', () => {
describe('with props', () => {
let store;
let multiplyDependency;
let derefCallCount;

beforeEach(() => {
store = BaseStoreFactory.register(dispatcher);
derefCallCount = 0;
multiplyDependency = {
stores: [store],
deref: ({ factor }) => {
if (!factor) throw new Error('no factor provided');
derefCallCount += 1;
return store.get() * factor;
},
};
Expand Down Expand Up @@ -287,5 +290,38 @@ describe('useStoreDependency', () => {
rendered.update();
expect(rendered.find('div').prop('data-factor')).toBe(16);
});

describe('deref call count', () => {
it('called once on initial render', () => {
const Component = ({ factor }) => {
const value = useStoreDependency(multiplyDependency, { factor });
return <div data-value={value} />;
};
mount(<Component factor={4} />);
expect(derefCallCount).toBe(1);
});

it('called once on re-render', () => {
const Component = ({ factor }) => {
const value = useStoreDependency(multiplyDependency, { factor });
return <div data-value={value} />;
};
const rendered = mount(<Component factor={4} />);
rendered.setProps({});
rendered.update();
expect(derefCallCount).toBe(2);
});

it('called twice on prop change (once from the initial re-render, again in the second re-render after the internal setState call)', () => {
const Component = ({ factor }) => {
const value = useStoreDependency(multiplyDependency, { factor });
return <div data-value={value} />;
};
const rendered = mount(<Component factor={4} />);
rendered.setProps({ factor: 8 });
rendered.update();
expect(derefCallCount).toBe(3);
});
});
});
});
6 changes: 2 additions & 4 deletions src/dependencies/useStoreDependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ function useStoreDependency<Props, DepType>(
): DepType {
enforceDispatcher(dispatcher);

const [dependencyValue, setDependencyValue] = useState({
dependency: calculate(dependency, props),
});
const newValue = { dependency: calculate(dependency, props) };
const [dependencyValue, setDependencyValue] = useState(newValue);

const currProps = useCurrent(props);

Expand All @@ -99,7 +98,6 @@ function useStoreDependency<Props, DepType>(
setDependencyValue
);

const newValue = { dependency: calculate(dependency, props) };
if (!shallowEqual(newValue.dependency, dependencyValue.dependency)) {
setDependencyValue(newValue);
return newValue.dependency;
Expand Down