Skip to content
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

Prevent initializers from being tracked #22

Merged
merged 2 commits into from
Jun 4, 2023
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
30 changes: 30 additions & 0 deletions src/__tests__/component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
reactive,
ref,
useComputed,
useReactive,
useReference,
useWatch,
useWatchEffect,
Expand Down Expand Up @@ -191,6 +192,35 @@ describe('makeReactive', () => {
expect(content).toBeTruthy();
});
});
it('does not re-render when state changes in initializer', async () => {
const count = ref(0);
const mockInitializer = jest.fn(() => ({ a: count.value, b: 2 }));

const Tester = makeReactive(function Tester() {
const obj = useReactive(mockInitializer);
return <p>{obj.a}</p>;
});

const { renderCount } = perf(React);

const { findByText } = render(<Tester />);

expect(mockInitializer).toBeCalledTimes(1);
const content = await findByText('0');
expect(content).toBeTruthy();

act(() => {
count.value++;
});

await wait(async () => {
// should not re-render, but should trigger effect again
expect(renderCount.current.Tester).toBeRenderedTimes(1);
expect(mockInitializer).toBeCalledTimes(1);
const content = await findByText('0');
expect(content).toBeTruthy();
});
});
it('does not re-render when state changes in nested watcher', async () => {
const obj = reactive({ a: 1, b: 2 });
const mockEffect = jest.fn();
Expand Down
10 changes: 6 additions & 4 deletions src/advanced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
shallowReadonly,
shallowRef,
} from '@vue/reactivity';
import { isFunction } from './helper';
import { invokeUntracked, isFunction } from './helper';
import { useRef } from 'react';

export {
Expand Down Expand Up @@ -49,7 +49,9 @@ export {
export const useShallowRef = <T>(value: T | (() => T)): ShallowRef<T> => {
const reactiveRef = useRef<ShallowRef<T> | null>(null);
if (reactiveRef.current === null) {
reactiveRef.current = shallowRef(isFunction(value) ? value() : value);
reactiveRef.current = shallowRef(
isFunction(value) ? invokeUntracked(value) : value
);
}
return reactiveRef.current;
};
Expand Down Expand Up @@ -121,7 +123,7 @@ export const useShallowReactive = <T extends object>(
const reactiveRef = useRef<T | null>(null);
if (reactiveRef.current === null) {
reactiveRef.current = shallowReactive(
isFunction(target) ? target() : target
isFunction(target) ? invokeUntracked(target) : target
);
}
return reactiveRef.current;
Expand Down Expand Up @@ -173,7 +175,7 @@ export const useShallowReadonly = <T extends object>(
const reactiveRef = useRef<Readonly<T> | null>(null);
if (reactiveRef.current === null) {
reactiveRef.current = shallowReadonly(
isFunction(target) ? target() : target
isFunction(target) ? invokeUntracked(target) : target
);
}
return reactiveRef.current;
Expand Down
20 changes: 16 additions & 4 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ import {
readonly,
ref,
} from '@vue/reactivity';
import { getFiberInDev, hasChanged, isFunction, traverse } from './helper';
import {
getFiberInDev,
hasChanged,
invokeUntracked,
isFunction,
traverse,
} from './helper';
import { useEffect, useRef } from 'react';
import messages from './messages';

Expand Down Expand Up @@ -51,7 +57,9 @@ export { ref, computed, reactive, readonly } from '@vue/reactivity';
export const useReference = <T>(value: T | (() => T)): Ref<UnwrapRef<T>> => {
const reactiveRef = useRef<Ref<UnwrapRef<T>> | null>(null);
if (reactiveRef.current === null) {
reactiveRef.current = ref(isFunction(value) ? value() : value);
reactiveRef.current = ref(
isFunction(value) ? invokeUntracked(value) : value
);
}
return reactiveRef.current;
};
Expand Down Expand Up @@ -182,7 +190,9 @@ export const useReactive = <T extends object>(
): UnwrapNestedRefs<T> => {
const reactiveRef = useRef<UnwrapNestedRefs<T> | null>(null);
if (reactiveRef.current === null) {
reactiveRef.current = reactive(isFunction(target) ? target() : target);
reactiveRef.current = reactive(
isFunction(target) ? invokeUntracked(target) : target
);
}
return reactiveRef.current;
};
Expand Down Expand Up @@ -231,7 +241,9 @@ export const useReadonly = <T extends object>(
): DeepReadonly<UnwrapNestedRefs<T>> => {
const reactiveRef = useRef<DeepReadonly<UnwrapNestedRefs<T>> | null>(null);
if (reactiveRef.current === null) {
reactiveRef.current = readonly(isFunction(target) ? target() : target);
reactiveRef.current = readonly(
isFunction(target) ? invokeUntracked(target) : target
);
}
return reactiveRef.current;
};
Expand Down
16 changes: 15 additions & 1 deletion src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ReactiveFlags, isRef } from '@vue/reactivity';
import {
ReactiveFlags,
isRef,
pauseTracking,
resetTracking,
} from '@vue/reactivity';
import { __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED as SecretInternals } from 'react';

declare module 'react' {
Expand Down Expand Up @@ -64,3 +69,12 @@ export function traverse(value: unknown, seen?: Set<unknown>) {
}
return value;
}

export function invokeUntracked<T extends (...args: any[]) => any>(
initializer: T
): ReturnType<T> {
pauseTracking();
const ret = initializer();
resetTracking();
return ret;
}