Skip to content

Commit 8101042

Browse files
committed
Support signals without importing preact-hooks
1 parent c3b6075 commit 8101042

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

packages/preact/src/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function SignalValue(this: AugmentedComponent, { data }: { data: Signal }) {
9292
const currentSignal = useSignal(data);
9393
currentSignal.value = data;
9494

95-
const [isText, s] = useStoreOnce(() => {
95+
const [isText, s] = useStoreValueOnce(() => {
9696
let self = this;
9797
// mark the parent component as having computeds so it gets optimized
9898
let v = this.__v;
@@ -404,7 +404,7 @@ Component.prototype.shouldComponentUpdate = function (
404404
export function useSignal<T>(value: T, options?: SignalOptions<T>): Signal<T>;
405405
export function useSignal<T = undefined>(): Signal<T | undefined>;
406406
export function useSignal<T>(value?: T, options?: SignalOptions<T>) {
407-
return useStoreOnce(() =>
407+
return useStoreValueOnce(() =>
408408
signal<T | undefined>(value, options as SignalOptions)
409409
);
410410
}
@@ -413,7 +413,9 @@ export function useComputed<T>(compute: () => T, options?: SignalOptions<T>) {
413413
const $compute = useRef(compute);
414414
$compute.current = compute;
415415
(currentComponent as AugmentedComponent)._updateFlags |= HAS_COMPUTEDS;
416-
return useStoreOnce(() => computed<T>(() => $compute.current(), options));
416+
return useStoreValueOnce(() =>
417+
computed<T>(() => $compute.current(), options)
418+
);
417419
}
418420

419421
function safeRaf(callback: () => void) {
@@ -523,7 +525,7 @@ function getState(index: number): HookState {
523525
return hooks._list[index];
524526
}
525527

526-
function useStoreOnce<T>(factory: () => T): T {
528+
export function useStoreValueOnce<T>(factory: () => T): T {
527529
const state = getState(currentHookIndex++);
528530
if (!state._stored) {
529531
state._stored = true;
@@ -532,8 +534,8 @@ function useStoreOnce<T>(factory: () => T): T {
532534
return state._value;
533535
}
534536

535-
function useRef<T>(initialValue: T): { current: T } {
536-
return useStoreOnce(() => ({ current: initialValue }));
537+
export function useRef<T>(initialValue: T): { current: T } {
538+
return useStoreValueOnce(() => ({ current: initialValue }));
537539
}
538540

539541
function useOnce(callback: () => void | (() => void)): void {

packages/preact/utils/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { ReadonlySignal, Signal } from "@preact/signals-core";
2-
import { useSignal } from "@preact/signals";
2+
import { useSignal, useStoreValueOnce } from "@preact/signals";
33
import { Fragment, createElement, JSX } from "preact";
4-
import { useMemo } from "preact/hooks";
54

65
interface ShowProps<T = boolean> {
76
when: Signal<T> | ReadonlySignal<T>;
@@ -27,7 +26,7 @@ interface ForProps<T> {
2726
}
2827

2928
export function For<T>(props: ForProps<T>): JSX.Element | null {
30-
const cache = useMemo(() => new Map(), []);
29+
const cache = useStoreValueOnce(() => new Map<T, JSX.Element>());
3130
let list = (
3231
(typeof props.each === "function" ? props.each() : props.each) as Signal<
3332
Array<T>
@@ -60,6 +59,7 @@ export function useSignalRef<T>(value: T): Signal<T> & { current: T } {
6059
Object.defineProperty(ref, "current", refSignalProto);
6160
return ref;
6261
}
62+
6363
const refSignalProto = {
6464
configurable: true,
6565
get(this: Signal) {

0 commit comments

Comments
 (0)