@ersbeth/picoflow 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -170
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,187 +1,25 @@
|
|
|
1
1
|
# Picoflow
|
|
2
2
|
|
|
3
|
-
**Picoflow** is a lightweight reactive dataflow library that provides fine-grained reactive primitives. It gives you an intuitive API for signals, state, asynchronous
|
|
3
|
+
**Picoflow** is a lightweight reactive dataflow library that provides fine-grained reactive primitives. It gives you an intuitive API for signals, state, (asynchronous) derivations, effects, and reactive maps/arrays. Picoflow uses an explicit tracking context to automatically track reactive dependencies.
|
|
4
4
|
|
|
5
|
-
> **Upgrading from v0.x?** See the [Upgrade Guide](
|
|
6
|
-
|
|
7
|
-
## Features
|
|
8
|
-
|
|
9
|
-
- **Reactive Signals:** Manually trigger updates using `$signal.trigger()`.
|
|
10
|
-
- **Reactive State:** Manage state with observables that expose getter/setter APIs.
|
|
11
|
-
- **Asynchronous Resources:** Fetch and update asynchronous data reactively.
|
|
12
|
-
- **Reactive Streams:** Create streams that update over time using an updater function.
|
|
13
|
-
- **Derivations:** Compute derived values from other reactive primitives.
|
|
14
|
-
- **Automatic Caching:** Derived values are cached and are re-evaluated only when one or more of their dependencies change.
|
|
15
|
-
- **Lazy Evaluation:** Derivations are evaluated lazily—that is, the computation only runs if and when an effect actually uses the derived value.
|
|
16
|
-
- **Reactive Maps:** Manage collections reactively with granular update notifications.
|
|
17
|
-
- **Effects:** Automatically run side-effect functions when dependencies change.
|
|
18
|
-
- **Explicit Control:** Choose between reactive (`.get(t)`) and non-reactive (`.pick()`) reads for fine-grained control.
|
|
5
|
+
> **Upgrading from v0.x?** See the [Upgrade Guide](https://ersbeth-web.gitlab.io/picoflow/guide/advanced/upgrading.html) for migration instructions.
|
|
19
6
|
|
|
20
7
|
## Installation
|
|
21
8
|
|
|
22
|
-
Install via npm:
|
|
23
|
-
|
|
24
9
|
```bash
|
|
10
|
+
# npm
|
|
25
11
|
npm install @ersbeth/picoflow
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
## Usage
|
|
29
|
-
|
|
30
|
-
In Picoflow, we advise to prefix all reactive primitives and effects with `$` for improved readability. Rather than using a subscribe method, you always create an effect that tracks dependencies using a tracking context. Below are some examples:
|
|
31
|
-
|
|
32
|
-
### Creating a Signal
|
|
33
|
-
|
|
34
|
-
```ts
|
|
35
|
-
import { signal, effect } from '@ersbeth/picoflow';
|
|
36
|
-
|
|
37
|
-
const $signal = signal();
|
|
38
|
-
|
|
39
|
-
const $effect = effect((t) => {
|
|
40
|
-
// Automatically tracks $signal
|
|
41
|
-
$signal.watch(t);
|
|
42
|
-
console.log('$signal has been triggered');
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
// Trigger the signal
|
|
46
|
-
$signal.trigger();
|
|
47
|
-
|
|
48
|
-
// LOG -> $signal has been triggered
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
### Creating Reactive State
|
|
52
|
-
|
|
53
|
-
```ts
|
|
54
|
-
import { state, effect } from '@ersbeth/picoflow';
|
|
55
|
-
|
|
56
|
-
const $count = state(0);
|
|
57
|
-
|
|
58
|
-
const $effect = effect((t) => {
|
|
59
|
-
// Automatically tracks $count
|
|
60
|
-
console.log('Count changed:', $count.get(t));
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
// Update the state
|
|
64
|
-
$count.set(42);
|
|
65
|
-
|
|
66
|
-
// LOG -> Count changed: 42
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
### Creating an Asynchronous Resource
|
|
70
|
-
|
|
71
|
-
```ts
|
|
72
|
-
import { resource, effect } from '@ersbeth/picoflow';
|
|
73
|
-
|
|
74
|
-
async function fetchData() {
|
|
75
|
-
const response = await fetch('https://api.example.com/data');
|
|
76
|
-
return response.json();
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const $data = resource(fetchData, {});
|
|
80
|
-
|
|
81
|
-
// Create an effect to watch for updates
|
|
82
|
-
const $effect = effect((t) => {
|
|
83
|
-
console.log('Resource updated:', $data.get(t));
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
// Later, you can trigger a new fetch
|
|
87
|
-
$data.fetch();
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
### Creating a Reactive Stream
|
|
91
12
|
|
|
92
|
-
|
|
93
|
-
|
|
13
|
+
# pnpm
|
|
14
|
+
pnpm add @ersbeth/picoflow
|
|
94
15
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
const interval = setInterval(() => set(Date.now()), 1000);
|
|
98
|
-
// Return a disposer to clear the interval when the stream is disposed.
|
|
99
|
-
return () => clearInterval(interval);
|
|
100
|
-
},
|
|
101
|
-
Date.now()
|
|
102
|
-
);
|
|
103
|
-
|
|
104
|
-
const $effect = effect((t) => {
|
|
105
|
-
console.log('Current time:', $ticker.get(t));
|
|
106
|
-
});
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
### Creating a Derived Value
|
|
110
|
-
|
|
111
|
-
```ts
|
|
112
|
-
import { state, derivation, effect } from '@ersbeth/picoflow';
|
|
113
|
-
|
|
114
|
-
const $a = state(10);
|
|
115
|
-
const $b = state(20);
|
|
116
|
-
|
|
117
|
-
// Create a derived value that sums $a and $b.
|
|
118
|
-
const $sum = derivation((t) => {
|
|
119
|
-
return $a.get(t) + $b.get(t);
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
const $effect = effect((t) => {
|
|
123
|
-
console.log('Sum:', $sum.get(t));
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
// Update a dependency to see the derivation update.
|
|
127
|
-
$a.set(15);
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
### Creating a Reactive Map
|
|
131
|
-
|
|
132
|
-
Reactive maps allow fine-grained tracking of collection updates.
|
|
133
|
-
|
|
134
|
-
```ts
|
|
135
|
-
import { map, effect } from '@ersbeth/picoflow';
|
|
136
|
-
|
|
137
|
-
const $map = map({ foo: 'bar' });
|
|
138
|
-
|
|
139
|
-
const $effect = effect((t) => {
|
|
140
|
-
console.log('Map updated:', $map.get(t));
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
// Update the map to trigger updates
|
|
144
|
-
$map.setAt('baz', 'qux');
|
|
145
|
-
$map.delete('foo');
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
### Non-Reactive Reads
|
|
149
|
-
|
|
150
|
-
Sometimes you need to read a value without creating a dependency. Use `pick()` for this:
|
|
151
|
-
|
|
152
|
-
```ts
|
|
153
|
-
import { state, signal, effect } from '@ersbeth/picoflow';
|
|
154
|
-
|
|
155
|
-
const $data = state({ count: 0 });
|
|
156
|
-
const $trigger = signal();
|
|
157
|
-
|
|
158
|
-
const $effect = effect((t) => {
|
|
159
|
-
// React to $trigger changes only
|
|
160
|
-
$trigger.watch(t);
|
|
161
|
-
|
|
162
|
-
// Read $data without creating a dependency
|
|
163
|
-
const snapshot = $data.pick();
|
|
164
|
-
console.log('Triggered! Current data:', snapshot);
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
$data.set({ count: 1 }); // Does NOT trigger the effect
|
|
168
|
-
$trigger.trigger(); // Triggers the effect, logs: { count: 1 }
|
|
16
|
+
# yarn
|
|
17
|
+
yarn add @ersbeth/picoflow
|
|
169
18
|
```
|
|
170
19
|
|
|
171
20
|
## Documentation
|
|
172
21
|
|
|
173
|
-
For comprehensive guides and API documentation, visit
|
|
174
|
-
|
|
175
|
-
- **[Getting Started](docs/guide/getting-started.md)** - Quick introduction to PicoFlow
|
|
176
|
-
- **[API Reference](docs/api/index.md)** - Complete API documentation
|
|
177
|
-
|
|
178
|
-
Or run the documentation locally:
|
|
179
|
-
|
|
180
|
-
```bash
|
|
181
|
-
pnpm docs:dev
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
Then open [http://localhost:5173](http://localhost:5173) in your browser.
|
|
22
|
+
For comprehensive guides and API documentation, visit the [official website](https://ersbeth-web.gitlab.io/picoflow/)
|
|
185
23
|
|
|
186
24
|
## License
|
|
187
25
|
|