From 79e5893f766e43e9ecc3a6d681b52c14249a0538 Mon Sep 17 00:00:00 2001 From: e7h4n Date: Thu, 21 Nov 2024 20:20:41 +0800 Subject: [PATCH] rename ripple/drip to compute/effect --- README.md | 12 ++++++------ src/__tests__/rippling.test.ts | 30 +++++++++++++++--------------- src/index.ts | 4 ++-- src/rippling.ts | 20 ++++++++++---------- src/typing.ts | 8 ++++---- 5 files changed, 37 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 5f31af1..87c90d5 100644 --- a/README.md +++ b/README.md @@ -23,22 +23,22 @@ 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: @@ -46,7 +46,7 @@ For Example: 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); }); diff --git a/src/__tests__/rippling.test.ts b/src/__tests__/rippling.test.ts index 6f9ad46..fdb57b8 100644 --- a/src/__tests__/rippling.test.ts +++ b/src/__tests__/rippling.test.ts @@ -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(); @@ -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 }); @@ -28,43 +28,43 @@ 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, 3) - expect(store.get(doubleRip)).toBe(2) + store.set(doubleCmpt as unknown as Atom, 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> = ripple(async (get) => { + const asyncCmpt: Compute> = 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) @@ -72,7 +72,7 @@ test('read & write drip as an action', async () => { 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) }) diff --git a/src/index.ts b/src/index.ts index d1a8848..610675b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,2 @@ -export { atom, ripple, drip, createStore } from './rippling' -export type { Atom, Ripple, Drip, Store } from './typing' \ No newline at end of file +export { atom, compute, effect, createStore } from './rippling' +export type { Atom, Compute, Effect, Store } from './typing' diff --git a/src/rippling.ts b/src/rippling.ts index 899f686..ec3be58 100644 --- a/src/rippling.ts +++ b/src/rippling.ts @@ -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(initialValue: Value): Atom { return { _initialValue: initialValue } } -export function ripple(read: Read): Ripple { +export function compute(read: Read): Compute { return { _read: read } } -export function drip(write: Write): Drip; -export function drip(read: Read, write: Write): Drip; -export function drip(prop1: Read | Write, prop2?: Write) { +export function effect(write: Write): Effect; +export function effect(read: Read, write: Write): Effect; +export function effect(prop1: Read | Write, prop2?: Write) { if (prop2 === undefined) { return { _write: prop1 as Write @@ -19,16 +19,16 @@ export function drip(prop1: Read | return { _read: prop1 as Read, _write: prop2 } } -function isRipple(readable: Readable): readable is Ripple { +function isEffect(readable: Readable): readable is Compute { return '_read' in readable && readable._read !== undefined } -type StoreKey = Atom | Ripple | Drip +type StoreKey = Atom | Compute | Effect export function createStore(): Store { const data = new WeakMap(); const set: Setter = function set( - atom: Atom | Drip, + atom: Atom | Effect, ...args: [Value] | Args ): undefined | ReturnValue { if ('_write' in atom) { @@ -40,12 +40,12 @@ export function createStore(): Store { } const get: Getter = function get(readable: Readable): 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)) { diff --git a/src/typing.ts b/src/typing.ts index 77b6ffa..69fceba 100644 --- a/src/typing.ts +++ b/src/typing.ts @@ -3,20 +3,20 @@ export interface Atom { _initialValue: Value; } -export interface Ripple { +export interface Compute { _read: Read; } -export type Readable = Atom | Ripple | Drip +export type Readable = Atom | Compute | Effect export type Getter = (readable: Readable) => Value; export interface Setter { (atom: Atom, value: Value): void; - (drip: Drip, ...args: Args): ReturnValue; + (drip: Effect, ...args: Args): ReturnValue; } -export interface Drip { +export interface Effect { _read?: Read; _write: Write; }