Skip to content

Commit

Permalink
feat: new ObservableOptions type export
Browse files Browse the repository at this point in the history
  • Loading branch information
mihar-22 committed Nov 25, 2022
1 parent a3db078 commit e6aa68c
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/observables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export type Observable<T> = {
(): T;
};

export type ObservableOptions<T> = {
id?: string;
dirty?: (prev: T, next: T) => boolean;
};

export type ObservableValue<T> = T extends Observable<infer R> ? R : T;

export type ObservableSubject<T> = Observable<T> & {
Expand Down Expand Up @@ -105,11 +110,11 @@ export function peek<T>(fn: () => T): T {
*/
export function observable<T>(
initialValue: T,
opts?: { id?: string; dirty?: (prev: T, next: T) => boolean },
options?: ObservableOptions<T>,
): ObservableSubject<T> {
let currentValue = initialValue;

const isDirty = opts?.dirty ?? notEqual;
const isDirty = options?.dirty ?? notEqual;

const $observable: ObservableSubject<T> = () => {
if (__DEV__) callStack.push($observable);
Expand All @@ -128,7 +133,7 @@ export function observable<T>(
$observable.set(next(currentValue));
};

if (__DEV__) $observable.id = opts?.id ?? 'observable';
if (__DEV__) $observable.id = options?.id ?? 'observable';

$observable[OBSERVABLE] = true;
adopt($observable);
Expand All @@ -151,14 +156,11 @@ export function isObservable<T>(fn: MaybeObservable<T>): fn is Observable<T> {
*
* @see {@link https://github.com/maverick-js/observables#computed}
*/
export function computed<T>(
fn: () => T,
opts?: { id?: string; dirty?: (prev: T, next: T) => boolean },
): Observable<T> {
export function computed<T>(fn: () => T, options?: ObservableOptions<T>): Observable<T> {
let currentValue,
init = false;

const isDirty = opts?.dirty ?? notEqual;
const isDirty = options?.dirty ?? notEqual;

const $computed: Observable<T> = () => {
if (__DEV__ && computeStack.includes($computed)) {
Expand Down Expand Up @@ -203,7 +205,7 @@ export function computed<T>(
return currentValue;
};

if (__DEV__) $computed.id = opts?.id ?? `computed`;
if (__DEV__) $computed.id = options?.id ?? `computed`;

// Starts off dirty because it hasn't run yet.
$computed[DIRTY] = true;
Expand All @@ -227,10 +229,11 @@ export function isObserved(): boolean {
*
* @see {@link https://github.com/maverick-js/observables#effect}
*/
export function effect(fn: Effect, opts?: { id?: string }): StopEffect {
const $effect = computed(() => onDispose(fn()), {
id: __DEV__ ? opts?.id ?? 'effect' : opts?.id,
});
export function effect(fn: Effect, options?: { id?: string }): StopEffect {
const $effect = computed(
() => onDispose(fn()),
__DEV__ ? { id: options?.id ?? 'effect' } : undefined,
);

$effect();
return () => dispose($effect);
Expand Down

0 comments on commit e6aa68c

Please sign in to comment.