Skip to content

Commit

Permalink
fix: rename update() to next()
Browse files Browse the repository at this point in the history
  • Loading branch information
mihar-22 committed Jun 27, 2022
1 parent 0e0bea4 commit 323f20e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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).

Expand All @@ -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**
Expand Down Expand Up @@ -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';
Expand Down
10 changes: 5 additions & 5 deletions src/observables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type Observable<T> = {
$id?: string;
(): T;
set: (nextValue: T) => void;
update: (next: (prevValue: T) => T) => void;
next: (next: (prevValue: T) => T) => void;
};

export type Computation<T> = {
Expand Down Expand Up @@ -84,7 +84,7 @@ export function $peek<T>(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
Expand All @@ -93,7 +93,7 @@ export function $peek<T>(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<T>(initialValue: T, $id?: string): Observable<T> {
Expand All @@ -117,7 +117,7 @@ export function $observable<T>(initialValue: T, $id?: string): Observable<T> {
}
};

$observable.update = (next: (prevValue: T) => T) => {
$observable.next = (next: (prevValue: T) => T) => {
$observable.set(next(currentValue));
};

Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/observables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand Down Expand Up @@ -385,7 +385,7 @@ describe('$readonly', () => {

expect(() => {
// @ts-expect-error
$b.update((n) => n + 10);
$b.next((n) => n + 10);
}).toThrow();

await $tick();
Expand Down

0 comments on commit 323f20e

Please sign in to comment.