diff --git a/README.md b/README.md index 1856ae7..1509317 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ $root((dispose) => { // Wait a tick so update is applied and effect is run. await $tick(); - $b.update((prev) => prev + 5); // logs `15` inside effect + $b.next((prev) => prev + 5); // logs `15` inside effect // Wait a tick so effect runs last update. await $tick(); @@ -143,7 +143,7 @@ console.log(result); // logs `10` ### `$observable` Wraps the given value into an observable function. The observable function will return the -current value when invoked `fn()`, and provide a simple write API via `set()` and `update()`. The +current value when invoked `fn()`, and provide a simple write API via `set()` and `next()`. The value can now be observed when used inside other computations created with [`$computed`](#computed) and [`$effect`](#effect). @@ -154,7 +154,7 @@ const $a = $observable(10); $a(); // read $a.set(20); // write (1) -$a.update((prev) => prev + 10); // write (2) +$a.next((prev) => prev + 10); // write (2) ``` > **Warning** @@ -247,7 +247,7 @@ $computed(() => { ### `$readonly` Takes in the given observable and makes it read only by removing access to write -operations (i.e., `set()` and `update()`). +operations (i.e., `set()` and `next()`). ```js import { $observable, $readonly } from '@maverick-js/observables'; diff --git a/src/observables.ts b/src/observables.ts index 81b2c20..4963db0 100644 --- a/src/observables.ts +++ b/src/observables.ts @@ -4,7 +4,7 @@ export type Observable = { $id?: string; (): T; set: (nextValue: T) => void; - update: (next: (prevValue: T) => T) => void; + next: (next: (prevValue: T) => T) => void; }; export type Computation = { @@ -84,7 +84,7 @@ export function $peek(fn: () => T): T { /** * Wraps the given value into an observable function. The observable function will return the - * current value when invoked `fn()`, and provide a simple write API via `set()` and `update()`. The + * current value when invoked `fn()`, and provide a simple write API via `set()` and `next()`. The * value can now be observed when used inside other computations created with `$computed` and `$effect`. * * @example @@ -93,7 +93,7 @@ export function $peek(fn: () => T): T { * * $a(); // read * $a.set(20); // write (1) - * $a.update(prev => prev + 10); // write (2) + * $a.next(prev => prev + 10); // write (2) * ``` */ export function $observable(initialValue: T, $id?: string): Observable { @@ -117,7 +117,7 @@ export function $observable(initialValue: T, $id?: string): Observable { } }; - $observable.update = (next: (prevValue: T) => T) => { + $observable.next = (next: (prevValue: T) => T) => { $observable.set(next(currentValue)); }; @@ -251,7 +251,7 @@ export function $effect(fn: () => void, $id?: string): StopEffect { /** * Takes in the given observable and makes it read only by removing access to write - * operations (i.e., `set()` and `update()`). + * operations (i.e., `set()` and `next()`). * * @example * ```js diff --git a/tests/observables.test.ts b/tests/observables.test.ts index 93c1275..ba016e7 100644 --- a/tests/observables.test.ts +++ b/tests/observables.test.ts @@ -68,9 +68,9 @@ describe('$observable', () => { expect($a()).toEqual(20); }); - it('should update observable via `update()`', () => { + it('should update observable via `next()`', () => { const $a = $observable(10); - $a.update((n) => n + 10); + $a.next((n) => n + 10); expect($a()).toEqual(20); }); }); @@ -385,7 +385,7 @@ describe('$readonly', () => { expect(() => { // @ts-expect-error - $b.update((n) => n + 10); + $b.next((n) => n + 10); }).toThrow(); await $tick();