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

chore(hooks): removed usage of useMemo instead using useConst implemented with useRef #543

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 19 additions & 4 deletions packages/preact/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ const HAS_PENDING_UPDATE = 1 << 0;
const HAS_HOOK_STATE = 1 << 1;
const HAS_COMPUTEDS = 1 << 2;


function useConst<T>(fn: () => T): T {
interface RefManagement {
value: T
status: 'uninitialized' | 'initialized'
}
const ref = useRef<RefManagement>({status: 'uninitialized'} as any)
if (ref.current.status === 'uninitialized') {
ref.current = {status: 'initialized', value: fn()}
}

return ref.current.value
}
danilocastro-shopify marked this conversation as resolved.
Show resolved Hide resolved


// Install a Preact options hook
function hook<T extends OptionsTypes>(hookName: T, hookFn: HookFn<T>) {
// @ts-ignore-next-line private options hooks usage
Expand Down Expand Up @@ -79,7 +94,7 @@ function SignalValue(this: AugmentedComponent, { data }: { data: Signal }) {
const currentSignal = useSignal(data);
currentSignal.value = data;

const s = useMemo(() => {
const s = useConst(() => {
// mark the parent component as having computeds so it gets optimized
let v = this.__v;
while ((v = v.__!)) {
Expand All @@ -104,7 +119,7 @@ function SignalValue(this: AugmentedComponent, { data }: { data: Signal }) {
let s = data.value;
return s === 0 ? 0 : s === true ? "" : s || "";
});
}, []);
});

return s.value;
}
Expand Down Expand Up @@ -346,14 +361,14 @@ Component.prototype.shouldComponentUpdate = function (
};

export function useSignal<T>(value: T) {
return useMemo(() => signal<T>(value), []);
return useConst(() => signal<T>(value));
}

export function useComputed<T>(compute: () => T) {
const $compute = useRef(compute);
$compute.current = compute;
(currentComponent as AugmentedComponent)._updateFlags |= HAS_COMPUTEDS;
return useMemo(() => computed<T>(() => $compute.current()), []);
return useConst(() => computed<T>(() => $compute.current()));
}

export function useSignalEffect(cb: () => void | (() => void)) {
Expand Down
17 changes: 15 additions & 2 deletions packages/react/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,19 +360,32 @@ Object.defineProperties(Signal.prototype, {
ref: { configurable: true, value: null },
});

function useConst<T>(fn: () => T): T {
interface RefManagement {
value: T
status: 'uninitialized' | 'initialized'
}
const ref = useRef<RefManagement>({status: 'uninitialized'} as any)
if (ref.current.status === 'uninitialized') {
ref.current = {status: 'initialized', value: fn()}
}

return ref.current.value
}
danilocastro-shopify marked this conversation as resolved.
Show resolved Hide resolved

export function useSignals(usage?: EffectStoreUsage): EffectStore {
if (isAutoSignalTrackingInstalled) return emptyEffectStore;
return _useSignalsImplementation(usage);
}

export function useSignal<T>(value: T): Signal<T> {
return useMemo(() => signal<T>(value), Empty);
return useConst(() => signal<T>(value));
}

export function useComputed<T>(compute: () => T): ReadonlySignal<T> {
const $compute = useRef(compute);
$compute.current = compute;
return useMemo(() => computed<T>(() => $compute.current()), Empty);
return useConst(() => computed<T>(() => $compute.current()));
}

export function useSignalEffect(cb: () => void | (() => void)): void {
Expand Down