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

Add watched and unwatched #634

Open
wants to merge 7 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add in integrations
  • Loading branch information
JoviDeCroock committed Dec 27, 2024
commit 30a196b9a43791f9b385e43e51dd19b7c8b78704
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ declare class Signal<T = any> {
set value(value: T);
}

interface SignalOptions<T = any> {
export interface SignalOptions<T = any> {
watched: (this: Signal<T>) => void;
unwatched: (this: Signal<T>) => void;
}
Expand Down
10 changes: 7 additions & 3 deletions packages/preact/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Signal,
type ReadonlySignal,
untracked,
SignalOptions,
} from "@preact/signals-core";
import {
VNode,
Expand Down Expand Up @@ -362,10 +363,13 @@ Component.prototype.shouldComponentUpdate = function (
return false;
};

export function useSignal<T>(value: T): Signal<T>;
export function useSignal<T>(value: T, options?: SignalOptions<T>): Signal<T>;
export function useSignal<T = undefined>(): Signal<T | undefined>;
export function useSignal<T>(value?: T) {
return useMemo(() => signal<T | undefined>(value), []);
export function useSignal<T>(value?: T, options?: SignalOptions<T>) {
return useMemo(
() => signal<T | undefined>(value, options as SignalOptions),
[]
);
}

export function useComputed<T>(compute: () => T) {
Expand Down
10 changes: 7 additions & 3 deletions packages/react/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
effect,
Signal,
ReadonlySignal,
SignalOptions,
} from "@preact/signals-core";
import {
useRef,
Expand Down Expand Up @@ -384,10 +385,13 @@ export function useSignals(usage?: EffectStoreUsage): EffectStore {
return _useSignalsImplementation(usage);
}

export function useSignal<T>(value: T): Signal<T>;
export function useSignal<T>(value: T, options?: SignalOptions<T>): Signal<T>;
export function useSignal<T = undefined>(): Signal<T | undefined>;
export function useSignal<T>(value?: T) {
return useMemo(() => signal<T | undefined>(value), Empty);
export function useSignal<T>(value?: T, options?: SignalOptions<T>) {
return useMemo(
() => signal<T | undefined>(value, options as SignalOptions),
Empty
);
}

export function useComputed<T>(compute: () => T): ReadonlySignal<T> {
Expand Down
Loading