Skip to content

Commit

Permalink
rename ripple/drip to compute/effect
Browse files Browse the repository at this point in the history
  • Loading branch information
e7h4n committed Nov 21, 2024
1 parent 8d221be commit 79e5893
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 37 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@ store.set(count, 1);
console.log(store.get(count)); // 1
```

### Ripple
### Compute

Ripple are the basic compute units in Rippling. The can read other Atom / Ripple / Drip.
Compute are the basic compute units in Rippling. The can read other Atom / Compute / Effect.

For Example:

```typescript
const store = createStore();
const count = atom(0);
const doubleCount = ripple((get) => get(count) * 2);
const doubleCount = compute((get) => get(count) * 2);
console.log(store.get(doubleCount)); // 0
```

### Drip
### Effect

Drip are the basic action units in Rippling. The can read other Atom / Ripple / Drip and write to Atom / Drip.
Effect are the basic action units in Rippling. The can read other Atom / Compute / Effect and write to Atom / Effect.

For Example:

```typescript
const store = createStore();
const count = atom(0);
const doubleCount = atom(0);
const updateCount = drip((get, set, value) => {
const updateCount = effect((get, set, value) => {
set(count, value);
set(doubleCount, get(count) * 2);
});
Expand Down
30 changes: 15 additions & 15 deletions src/__tests__/rippling.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from 'vitest';
import { atom, createStore, drip, ripple, Atom, Ripple } from '../';
import { atom, createStore, Atom, compute, Compute, effect } from '../';

test('should work', () => {
const store = createStore();
Expand All @@ -17,7 +17,7 @@ test('should work', () => {
test('compute atom should work', () => {
const store = createStore();
const anAtom = atom(1);
const computedAtom = ripple((get) => {
const computedAtom = compute((get) => {
const num = get(anAtom);
return num * 2
});
Expand All @@ -28,51 +28,51 @@ test('compute atom should work', () => {
test('compute atom should net set', () => {
const store = createStore()
const anAtom = atom(1)
const doubleRip = ripple((get) => {
const doubleCmpt = compute((get) => {
return get(anAtom) * 2
})

store.set(doubleRip as unknown as Atom<number>, 3)
expect(store.get(doubleRip)).toBe(2)
store.set(doubleCmpt as unknown as Atom<number>, 3)
expect(store.get(doubleCmpt)).toBe(2)
})

test('async atom should works like sync atom', async () => {
const store = createStore()
const anAtom = atom(1)
const asyncRip: Ripple<Promise<number>> = ripple(async (get) => {
const asyncCmpt: Compute<Promise<number>> = compute(async (get) => {
await Promise.resolve()
return get(anAtom) * 2
})

expect(await store.get(asyncRip)).toBe(2)
expect(await store.get(asyncCmpt)).toBe(2)
})

test('drip can set other atom', () => {
test('effect can set other atom', () => {
const store = createStore()
const anAtom = atom(1)
const doubleAtom = atom(0)
const doubleDrip = drip((get, set, num) => {
const doubleEffect = effect((get, set, num) => {
set(anAtom, num)
set(doubleAtom, get(anAtom) * 2)
})
store.set(doubleDrip, 2)
store.set(doubleEffect, 2)
expect(store.get(anAtom)).toBe(2)
expect(store.get(doubleAtom)).toBe(4)
})

test('read & write drip as an action', async () => {
test('read & write effect as an action', async () => {
const store = createStore()
const promiseAtom = atom(Promise.resolve(1))

const actionDrip = drip(get => {
const actionEffect = effect(get => {
return get(promiseAtom)
}, (_, set) => {
const promise = Promise.resolve(2)
set(promiseAtom, promise)
return promise
})

expect(await store.get(actionDrip)).toBe(1)
void store.set(actionDrip)
expect(await store.get(actionDrip)).toBe(2)
expect(await store.get(actionEffect)).toBe(1)
void store.set(actionEffect)
expect(await store.get(actionEffect)).toBe(2)
})
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { atom, ripple, drip, createStore } from './rippling'
export type { Atom, Ripple, Drip, Store } from './typing'
export { atom, compute, effect, createStore } from './rippling'
export type { Atom, Compute, Effect, Store } from './typing'
20 changes: 10 additions & 10 deletions src/rippling.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Atom, Drip, Getter, Read, Readable, Ripple, Setter, Store, Write } from "./typing";
import { Atom, Effect, Getter, Read, Readable, Compute, Setter, Store, Write } from "./typing";

export function atom<Value>(initialValue: Value): Atom<Value> {
return { _initialValue: initialValue }
}

export function ripple<Value>(read: Read<Value>): Ripple<Value> {
export function compute<Value>(read: Read<Value>): Compute<Value> {
return { _read: read }
}

export function drip<Args extends unknown[], Return>(write: Write<Args, Return>): Drip<null, Args, Return>;
export function drip<Value, Args extends unknown[], Return>(read: Read<Value>, write: Write<Args, Return>): Drip<Value, Args, Return>;
export function drip<Value, Args extends unknown[], Return>(prop1: Read<Value> | Write<Args, Return>, prop2?: Write<Args, Return>) {
export function effect<Args extends unknown[], Return>(write: Write<Args, Return>): Effect<null, Args, Return>;
export function effect<Value, Args extends unknown[], Return>(read: Read<Value>, write: Write<Args, Return>): Effect<Value, Args, Return>;
export function effect<Value, Args extends unknown[], Return>(prop1: Read<Value> | Write<Args, Return>, prop2?: Write<Args, Return>) {
if (prop2 === undefined) {
return {
_write: prop1 as Write<Args, Return>
Expand All @@ -19,16 +19,16 @@ export function drip<Value, Args extends unknown[], Return>(prop1: Read<Value> |
return { _read: prop1 as Read<Value>, _write: prop2 }
}

function isRipple<Value>(readable: Readable<Value>): readable is Ripple<Value> {
function isEffect<Value>(readable: Readable<Value>): readable is Compute<Value> {
return '_read' in readable && readable._read !== undefined
}

type StoreKey = Atom<unknown> | Ripple<unknown> | Drip<unknown, unknown[], unknown>
type StoreKey = Atom<unknown> | Compute<unknown> | Effect<unknown, unknown[], unknown>
export function createStore(): Store {
const data = new WeakMap<StoreKey, unknown>();

const set: Setter = function set<Value, Args extends unknown[], ReturnValue>(
atom: Atom<Value> | Drip<unknown, Args, ReturnValue>,
atom: Atom<Value> | Effect<unknown, Args, ReturnValue>,
...args: [Value] | Args
): undefined | ReturnValue {
if ('_write' in atom) {
Expand All @@ -40,12 +40,12 @@ export function createStore(): Store {
}

const get: Getter = function get<Value>(readable: Readable<Value>): Value {
if (isRipple(readable)) {
if (isEffect(readable)) {
return readable._read(get);
}

if ('_write' in readable) {
throw new Error('Cannot get value of a drip without read function')
throw new Error('Cannot get value of an effect without read function')
}

if (data.has(readable)) {
Expand Down
8 changes: 4 additions & 4 deletions src/typing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ export interface Atom<Value> {
_initialValue: Value;
}

export interface Ripple<Value> {
export interface Compute<Value> {
_read: Read<Value>;
}

export type Readable<Value> = Atom<Value> | Ripple<Value> | Drip<Value, unknown[], unknown>
export type Readable<Value> = Atom<Value> | Compute<Value> | Effect<Value, unknown[], unknown>

export type Getter = <Value>(readable: Readable<Value>) => Value;

export interface Setter {
<Value>(atom: Atom<Value>, value: Value): void;
<Args extends unknown[], ReturnValue>(drip: Drip<unknown, Args, ReturnValue>, ...args: Args): ReturnValue;
<Args extends unknown[], ReturnValue>(drip: Effect<unknown, Args, ReturnValue>, ...args: Args): ReturnValue;
}

export interface Drip<Value, Args extends unknown[], Return> {
export interface Effect<Value, Args extends unknown[], Return> {
_read?: Read<Value>;
_write: Write<Args, Return>;
}
Expand Down

0 comments on commit 79e5893

Please sign in to comment.