@pumped-fn/lite 0.1.0
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/CHANGELOG.md +25 -0
- package/LICENSE +21 -0
- package/README.md +250 -0
- package/dist/index.cjs +647 -0
- package/dist/index.d.cts +411 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +411 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +625 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +60 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# @pumped-fn/lite
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- bc2ce40: Add @pumped-fn/lite - lightweight DI with minimal reactivity
|
|
8
|
+
|
|
9
|
+
Lightweight dependency injection for TypeScript with:
|
|
10
|
+
|
|
11
|
+
- `atom()` - long-lived dependencies with lifecycle
|
|
12
|
+
- `flow()` - short-lived execution with input
|
|
13
|
+
- `tag()` - metadata attachment/extraction
|
|
14
|
+
- `controller()` - deferred resolution with reactivity
|
|
15
|
+
- `createScope()` - container with resolution caching
|
|
16
|
+
- Extension system for cross-cutting concerns
|
|
17
|
+
|
|
18
|
+
Reactivity features (ADR-003):
|
|
19
|
+
|
|
20
|
+
- `AtomState`: idle | resolving | resolved | failed
|
|
21
|
+
- `ctx.invalidate()` - self-invalidation from factory
|
|
22
|
+
- `Controller.invalidate()` / `Controller.on()` - external control
|
|
23
|
+
- `scope.on()` - event listening for state transitions
|
|
24
|
+
|
|
25
|
+
Zero external dependencies, <10KB bundle target.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License Copyright (c) 2025 Duke
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of
|
|
4
|
+
charge, to any person obtaining a copy of this software and associated
|
|
5
|
+
documentation files (the "Software"), to deal in the Software without
|
|
6
|
+
restriction, including without limitation the rights to use, copy, modify, merge,
|
|
7
|
+
publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to the
|
|
9
|
+
following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice
|
|
12
|
+
(including the next paragraph) shall be included in all copies or substantial
|
|
13
|
+
portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
|
16
|
+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
|
|
18
|
+
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
19
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
20
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
# @pumped-fn/lite
|
|
2
|
+
|
|
3
|
+
A lightweight effect system for TypeScript with managed lifecycles and minimal reactivity.
|
|
4
|
+
|
|
5
|
+
## What is an Effect System?
|
|
6
|
+
|
|
7
|
+
An effect system manages **how** and **when** computations run, handling:
|
|
8
|
+
- **Resource lifecycle** - acquire, use, release
|
|
9
|
+
- **Computation ordering** - what depends on what
|
|
10
|
+
- **Side effect isolation** - controlled execution boundaries
|
|
11
|
+
- **State transitions** - idle → resolving → resolved → failed
|
|
12
|
+
|
|
13
|
+
## Core Concepts
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
17
|
+
│ Scope │
|
|
18
|
+
│ (long-lived execution boundary) │
|
|
19
|
+
│ │
|
|
20
|
+
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
|
|
21
|
+
│ │ Atom │ ──── │ Atom │ ──── │ Atom │ │
|
|
22
|
+
│ │ (effect)│ │ (effect)│ │ (effect)│ │
|
|
23
|
+
│ └─────────┘ └─────────┘ └─────────┘ │
|
|
24
|
+
│ │ │ │
|
|
25
|
+
│ └──────────────┬───────────────────┘ │
|
|
26
|
+
│ ▼ │
|
|
27
|
+
│ ┌─────────────────────────────────────────────────────┐ │
|
|
28
|
+
│ │ ExecutionContext │ │
|
|
29
|
+
│ │ (short-lived operation with input, tags, cleanup) │ │
|
|
30
|
+
│ └─────────────────────────────────────────────────────┘ │
|
|
31
|
+
└─────────────────────────────────────────────────────────────┘
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
| Concept | Purpose |
|
|
35
|
+
|---------|---------|
|
|
36
|
+
| **Scope** | Long-lived boundary that manages atom lifecycles |
|
|
37
|
+
| **Atom** | A managed effect with lifecycle (create, cache, cleanup, recreate) |
|
|
38
|
+
| **ExecutionContext** | Short-lived context for running operations with input and tags |
|
|
39
|
+
| **Controller** | Handle for observing and controlling an atom's state |
|
|
40
|
+
| **Tag** | Contextual value passed through execution |
|
|
41
|
+
|
|
42
|
+
## Install
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
npm install @pumped-fn/lite
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Quick Example
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import { atom, flow, createScope } from '@pumped-fn/lite'
|
|
52
|
+
|
|
53
|
+
// Define effects (atoms) - long-lived, cached
|
|
54
|
+
const dbAtom = atom({
|
|
55
|
+
factory: async (ctx) => {
|
|
56
|
+
const conn = await createConnection()
|
|
57
|
+
ctx.cleanup(() => conn.close())
|
|
58
|
+
return conn
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
const repoAtom = atom({
|
|
63
|
+
deps: { db: dbAtom },
|
|
64
|
+
factory: (ctx, { db }) => new UserRepository(db)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
// Define operation template
|
|
68
|
+
const getUser = flow({
|
|
69
|
+
deps: { repo: repoAtom },
|
|
70
|
+
factory: async (ctx, { repo }) => {
|
|
71
|
+
return repo.findById(ctx.input as string)
|
|
72
|
+
}
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
// Create scope (long-lived boundary)
|
|
76
|
+
const scope = await createScope()
|
|
77
|
+
|
|
78
|
+
// Create ExecutionContext (short-lived, per-operation)
|
|
79
|
+
const ctx = scope.createContext()
|
|
80
|
+
|
|
81
|
+
// Execute - resolves atoms, runs operation, provides input
|
|
82
|
+
const user = await ctx.exec({ flow: getUser, input: 'user-123' })
|
|
83
|
+
|
|
84
|
+
// ExecutionContext cleanup (operation complete)
|
|
85
|
+
await ctx.close()
|
|
86
|
+
|
|
87
|
+
// Scope cleanup (application shutdown)
|
|
88
|
+
await scope.dispose()
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Effect Lifecycle
|
|
92
|
+
|
|
93
|
+
```mermaid
|
|
94
|
+
stateDiagram-v2
|
|
95
|
+
[*] --> idle
|
|
96
|
+
idle --> resolving: resolve()
|
|
97
|
+
resolving --> resolved: success
|
|
98
|
+
resolving --> failed: error
|
|
99
|
+
resolved --> resolving: invalidate()
|
|
100
|
+
failed --> resolving: invalidate()
|
|
101
|
+
resolved --> idle: release()
|
|
102
|
+
failed --> idle: release()
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Resolution Flow
|
|
106
|
+
|
|
107
|
+
```mermaid
|
|
108
|
+
sequenceDiagram
|
|
109
|
+
participant App
|
|
110
|
+
participant Scope
|
|
111
|
+
participant Atom
|
|
112
|
+
participant Factory
|
|
113
|
+
|
|
114
|
+
App->>Scope: resolve(atom)
|
|
115
|
+
Scope->>Atom: check state
|
|
116
|
+
|
|
117
|
+
alt idle
|
|
118
|
+
Scope->>Atom: state = resolving
|
|
119
|
+
Scope->>Factory: factory(ctx, deps)
|
|
120
|
+
Factory-->>Scope: value
|
|
121
|
+
Scope->>Atom: state = resolved
|
|
122
|
+
else resolved
|
|
123
|
+
Atom-->>App: cached value
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
Atom-->>App: value
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Invalidation (Re-execution)
|
|
130
|
+
|
|
131
|
+
Atoms can be invalidated to re-run their effect:
|
|
132
|
+
|
|
133
|
+
```mermaid
|
|
134
|
+
sequenceDiagram
|
|
135
|
+
participant App
|
|
136
|
+
participant Controller
|
|
137
|
+
participant Scope
|
|
138
|
+
participant Queue
|
|
139
|
+
participant Factory
|
|
140
|
+
|
|
141
|
+
App->>Controller: invalidate()
|
|
142
|
+
Controller->>Scope: queue invalidation
|
|
143
|
+
Scope->>Queue: add atom
|
|
144
|
+
|
|
145
|
+
Note over Queue: queueMicrotask (batched)
|
|
146
|
+
|
|
147
|
+
Queue->>Scope: flush
|
|
148
|
+
Scope->>Scope: run cleanups
|
|
149
|
+
Scope->>Scope: state = resolving
|
|
150
|
+
Scope->>Factory: factory(ctx, deps)
|
|
151
|
+
Factory-->>Scope: new value
|
|
152
|
+
Scope->>Scope: state = resolved
|
|
153
|
+
Scope->>Scope: notify listeners
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Invalidations are **batched** via microtask queue - multiple invalidations in one tick become a single re-execution.
|
|
157
|
+
|
|
158
|
+
## Controller Pattern
|
|
159
|
+
|
|
160
|
+
Controllers provide a handle to observe and control atom state:
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
const configAtom = atom({
|
|
164
|
+
factory: async (ctx) => {
|
|
165
|
+
const config = await fetchConfig()
|
|
166
|
+
setTimeout(() => ctx.invalidate(), 60000) // refresh every minute
|
|
167
|
+
return config
|
|
168
|
+
}
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
const appAtom = atom({
|
|
172
|
+
deps: { config: controller(configAtom) }, // controller, not direct
|
|
173
|
+
factory: (ctx, { config }) => {
|
|
174
|
+
// Subscribe to config changes
|
|
175
|
+
config.on(() => {
|
|
176
|
+
console.log('config updated:', config.state)
|
|
177
|
+
ctx.invalidate() // re-run this atom too
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
return new App(config.get())
|
|
181
|
+
}
|
|
182
|
+
})
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Tags (Contextual Values)
|
|
186
|
+
|
|
187
|
+
Tags pass contextual values through execution without explicit wiring:
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
const requestIdTag = tag<string>({ label: 'requestId' })
|
|
191
|
+
|
|
192
|
+
const loggingAtom = atom({
|
|
193
|
+
deps: { requestId: tags.required(requestIdTag) },
|
|
194
|
+
factory: (ctx, { requestId }) => new Logger(requestId)
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
// Pass tags when creating ExecutionContext or at exec time
|
|
198
|
+
const ctx = scope.createContext({ tags: [requestIdTag('req-abc-123')] })
|
|
199
|
+
await ctx.exec({ flow: myFlow, input: data })
|
|
200
|
+
|
|
201
|
+
// Or per-execution
|
|
202
|
+
await ctx.exec({
|
|
203
|
+
flow: myFlow,
|
|
204
|
+
input: data,
|
|
205
|
+
tags: [requestIdTag('req-xyz-456')]
|
|
206
|
+
})
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
## Extensions (Cross-cutting Effects)
|
|
210
|
+
|
|
211
|
+
Wrap resolution and execution with cross-cutting behavior:
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
const timingExtension: Lite.Extension = {
|
|
215
|
+
name: 'timing',
|
|
216
|
+
wrapResolve: async (next, atom, scope) => {
|
|
217
|
+
const start = performance.now()
|
|
218
|
+
const result = await next()
|
|
219
|
+
console.log(`resolved in ${performance.now() - start}ms`)
|
|
220
|
+
return result
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const scope = await createScope({ extensions: [timingExtension] })
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## API Reference
|
|
228
|
+
|
|
229
|
+
| Function | Description |
|
|
230
|
+
|----------|-------------|
|
|
231
|
+
| `createScope(options?)` | Create execution boundary |
|
|
232
|
+
| `atom(config)` | Define managed effect (long-lived) |
|
|
233
|
+
| `flow(config)` | Define operation template (used by ExecutionContext) |
|
|
234
|
+
| `tag(config)` | Define contextual value |
|
|
235
|
+
| `controller(atom)` | Wrap atom for deferred resolution |
|
|
236
|
+
| `preset(atom, value)` | Override atom value in scope |
|
|
237
|
+
| `scope.createContext(options?)` | Create ExecutionContext for operations |
|
|
238
|
+
| `ctx.exec(options)` | Execute operation with input and tags |
|
|
239
|
+
| `ctx.close()` | Cleanup ExecutionContext resources |
|
|
240
|
+
|
|
241
|
+
## Design Principles
|
|
242
|
+
|
|
243
|
+
1. **Minimal API** - Every export is expensive to learn
|
|
244
|
+
2. **Zero dependencies** - No runtime dependencies
|
|
245
|
+
3. **Explicit lifecycle** - No magic, clear state transitions
|
|
246
|
+
4. **Composable** - Effects compose through deps
|
|
247
|
+
|
|
248
|
+
## License
|
|
249
|
+
|
|
250
|
+
MIT
|