@nlozgachev/pipelined 0.34.0 → 0.36.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/README.md +77 -95
- package/dist/{chunk-AHEZFTMT.mjs → chunk-3Q5UBRYB.mjs} +92 -0
- package/dist/{chunk-5AWUAG7G.mjs → chunk-4QMYKCWE.mjs} +5 -5
- package/dist/composition.d.mts +189 -1
- package/dist/composition.d.ts +189 -1
- package/dist/composition.js +93 -0
- package/dist/composition.mjs +3 -1
- package/dist/core.d.mts +5 -5
- package/dist/core.d.ts +5 -5
- package/dist/core.js +5 -5
- package/dist/core.mjs +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +98 -5
- package/dist/index.mjs +4 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,11 +14,11 @@ npm add @nlozgachev/pipelined
|
|
|
14
14
|
|
|
15
15
|
## Possibly maybe
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
In mainstream TypeScript, code is often burdened by implicit control flow: unchecked exceptions,
|
|
18
|
+
manual null propagation, and unhandled asynchronous failures. `pipelined` turns these complex
|
|
19
|
+
runtime states into simple, transparent data structures that compose. By representing optionality as
|
|
20
|
+
`Maybe`, failures as `Result`, lazy asynchronous pipelines as `TaskResult`, and repeated stateful
|
|
21
|
+
interactions as `Op`, the library helps disentangle business logic from control mechanics.
|
|
22
22
|
|
|
23
23
|
## Documentation
|
|
24
24
|
|
|
@@ -54,8 +54,10 @@ Every step that sees `None` is skipped. The fallback runs once, at the end.
|
|
|
54
54
|
|
|
55
55
|
## Example: typed async errors
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
In JavaScript, asynchronous exceptions bypass the static type system, leaving unhandled rejections
|
|
58
|
+
as invisible runtime risks. `TaskResult<E, A>` represents fallible asynchronous computations as
|
|
59
|
+
lazy, infallible tasks that resolve to a typed `Result`. The error type is explicitly tracked in the
|
|
60
|
+
function signature, ensuring that failures are handled before compile time:
|
|
59
61
|
|
|
60
62
|
```ts
|
|
61
63
|
import { pipe } from "@nlozgachev/pipelined/composition";
|
|
@@ -110,8 +112,10 @@ if (Result.isOk(result)) {
|
|
|
110
112
|
|
|
111
113
|
## Example: transforming data
|
|
112
114
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
+
Standard JavaScript arrays and records routinely return `undefined` on out-of-bounds access or
|
|
116
|
+
missing keys. The utility modules in `pipelined` wrap these operations with data-last, curried
|
|
117
|
+
helper functions that return `Maybe` when a value might be missing, allowing data transformation
|
|
118
|
+
steps to compose naturally with the core types:
|
|
115
119
|
|
|
116
120
|
```ts
|
|
117
121
|
import { pipe } from "@nlozgachev/pipelined/composition";
|
|
@@ -148,7 +152,9 @@ the same way.
|
|
|
148
152
|
|
|
149
153
|
## Example: retry, timeout, and cancellation
|
|
150
154
|
|
|
151
|
-
|
|
155
|
+
Handling robust network interactions — including retry attempts, backoff timing, timeouts, and
|
|
156
|
+
signal-driven cancellation — typically requires complex, stateful code that is highly prone to
|
|
157
|
+
subtle race conditions:
|
|
152
158
|
|
|
153
159
|
```ts
|
|
154
160
|
type UserResult =
|
|
@@ -184,14 +190,11 @@ async function fetchUser(
|
|
|
184
190
|
}
|
|
185
191
|
```
|
|
186
192
|
|
|
187
|
-
The signal is forwarded by hand. The timeout needs its own controller. Timed-out aborts are
|
|
188
|
-
distinguished from external cancellation by checking `signal?.aborted`. The retry is recursive to
|
|
189
|
-
thread the attempt count.
|
|
190
|
-
|
|
191
193
|
With **pipelined**:
|
|
192
194
|
|
|
193
195
|
```ts
|
|
194
196
|
import { Op } from "@nlozgachev/pipelined/core";
|
|
197
|
+
import { Duration } from "@nlozgachev/pipelined/types";
|
|
195
198
|
|
|
196
199
|
const fetchUser = Op.interpret(
|
|
197
200
|
Op.create(
|
|
@@ -201,8 +204,11 @@ const fetchUser = Op.interpret(
|
|
|
201
204
|
),
|
|
202
205
|
{
|
|
203
206
|
strategy: "restartable",
|
|
204
|
-
retry: { attempts: 3, backoff: (n) => n
|
|
205
|
-
timeout: {
|
|
207
|
+
retry: { attempts: 3, backoff: (n) => Duration.seconds(n) },
|
|
208
|
+
timeout: {
|
|
209
|
+
duration: Duration.seconds(5),
|
|
210
|
+
onTimeout: () => new ApiError("request timed out"),
|
|
211
|
+
},
|
|
206
212
|
},
|
|
207
213
|
);
|
|
208
214
|
```
|
|
@@ -224,18 +230,19 @@ if (Op.isOk(outcome)) {
|
|
|
224
230
|
fetchUser.abort();
|
|
225
231
|
```
|
|
226
232
|
|
|
227
|
-
## Example: repeated interactions
|
|
228
|
-
|
|
229
|
-
Real UIs make the same call many times — a search input fires on every keystroke, a submit button
|
|
230
|
-
gets clicked twice, a polling loop needs to stop when something newer starts. Each scenario has a
|
|
231
|
-
different answer to the same question: *what happens to the previous call when a new one arrives?*
|
|
233
|
+
## Example: repeated UI interactions
|
|
232
234
|
|
|
233
|
-
|
|
235
|
+
User interfaces frequently trigger repeated asynchronous events: a search input firing on every
|
|
236
|
+
keystroke, a submit button clicked multiple times, or a polling loop that must terminate when a
|
|
237
|
+
newer request starts. Managing these concurrency scenarios traditionally requires complex, ad-hoc
|
|
238
|
+
state machines. `Op` simplifies this by allowing developers to declare the concurrency strategy as a
|
|
239
|
+
simple configuration choice:
|
|
234
240
|
|
|
235
241
|
**Search — cancel the previous call when the user types:**
|
|
236
242
|
|
|
237
243
|
```ts
|
|
238
244
|
import { Op } from "@nlozgachev/pipelined/core";
|
|
245
|
+
import { Duration } from "@nlozgachev/pipelined/types";
|
|
239
246
|
|
|
240
247
|
const searchOp = Op.create(
|
|
241
248
|
(signal) => (query: string) =>
|
|
@@ -247,7 +254,7 @@ const searchOp = Op.create(
|
|
|
247
254
|
|
|
248
255
|
const search = Op.interpret(searchOp, {
|
|
249
256
|
strategy: "restartable", // new call cancels the previous one
|
|
250
|
-
retry: { attempts: 2, backoff: 300 },
|
|
257
|
+
retry: { attempts: 2, backoff: Duration.milliseconds(300) },
|
|
251
258
|
});
|
|
252
259
|
|
|
253
260
|
search.subscribe((state) => {
|
|
@@ -287,78 +294,53 @@ form.addEventListener("submit", (e) => {
|
|
|
287
294
|
});
|
|
288
295
|
```
|
|
289
296
|
|
|
290
|
-
|
|
291
|
-
`
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
## What
|
|
295
|
-
|
|
296
|
-
The library covers the
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
Everyday utilities for built-in JS types.
|
|
338
|
-
|
|
339
|
-
- **`Arr`** — array utilities, data-last, returning `Maybe` instead of `undefined`.
|
|
340
|
-
- **`Rec`** — record/object utilities, data-last, with `Maybe`-returning key lookup.
|
|
341
|
-
- **`Dict`** — `ReadonlyMap<K, V>` utilities: `lookup`, `groupBy`, `upsert`, set operations.
|
|
342
|
-
- **`Uniq`** — `ReadonlySet<A>` utilities: `insert`, `remove`, `union`, `intersection`,
|
|
343
|
-
`difference`.
|
|
344
|
-
- **`Num`** — number utilities: `range`, `clamp`, `between`, safe `parse`, and curried arithmetic.
|
|
345
|
-
- **`Str`** — string utilities: `split`, `trim`, `words`, `lines`, and safe `parse.int` /
|
|
346
|
-
`parse.float`.
|
|
347
|
-
|
|
348
|
-
Every utility is benchmarked against its native equivalent. The data-last currying adds a function
|
|
349
|
-
call; that is the expected cost of composability. Operations that exceeded a reasonable overhead
|
|
350
|
-
have custom implementations that in several cases run faster than the native method they replace.
|
|
351
|
-
See the [benchmarks page](https://pipelined.lozgachev.dev/appendix/benchmarks) for the methodology.
|
|
352
|
-
|
|
353
|
-
### pipelined/types
|
|
354
|
-
|
|
355
|
-
- **`Brand<K, T>`** — nominal typing at compile time, zero runtime cost.
|
|
356
|
-
- **`NonEmptyList<A>`** — an array guaranteed to have at least one element.
|
|
357
|
-
|
|
358
|
-
### pipelined/composition
|
|
359
|
-
|
|
360
|
-
- **`pipe`**, **`flow`**, **`compose`** — function composition.
|
|
361
|
-
- **`curry`** / **`uncurry`**, **`tap`**, **`memoize`**, and other function utilities.
|
|
297
|
+
The system supports a variety of built-in strategies — `restartable`, `exclusive`, `debounced`,
|
|
298
|
+
`throttled`, `queue`, `buffered`, `concurrent`, `keyed`, and `once` — making the integration of
|
|
299
|
+
complex async scenarios highly predictable.
|
|
300
|
+
|
|
301
|
+
## What is included
|
|
302
|
+
|
|
303
|
+
The library covers the full spectrum of state and control flow scenarios encountered in production
|
|
304
|
+
applications.
|
|
305
|
+
|
|
306
|
+
### Core context containers
|
|
307
|
+
|
|
308
|
+
`Maybe` represents explicit optionality without null checks. `Result` handles synchronous, typed
|
|
309
|
+
success and failure, while `Validation` accumulates multiple errors. `RemoteData` tracks the four
|
|
310
|
+
states of an asynchronous data fetch (`NotAsked`, `Loading`, `Failure`, `Success`), and `These`
|
|
311
|
+
handles inclusive-OR scenarios containing a first value, a second, or both simultaneously.
|
|
312
|
+
|
|
313
|
+
### Asynchronous operations
|
|
314
|
+
|
|
315
|
+
`Task` represents a lazy, infallible asynchronous computation. Fallible asynchronous workflows are
|
|
316
|
+
handled by `TaskResult`, `TaskMaybe`, and `TaskValidation`. For managing stateful, recurring
|
|
317
|
+
asynchronous operations with complex scheduling, `Op` implements named concurrency strategies such
|
|
318
|
+
as `restartable`, `exclusive`, `debounced`, `throttled`, and `queue`, handling retries, timeouts,
|
|
319
|
+
and signal propagation automatically.
|
|
320
|
+
|
|
321
|
+
### Optics and environment state
|
|
322
|
+
|
|
323
|
+
`Lens` and `Optional` provide a simple concrete interface for safe, nested immutable data updates.
|
|
324
|
+
Environment-dependent calculations and explicit state threading are supported by the `Reader` and
|
|
325
|
+
`State` abstractions, while `Logged` enables side-effect-free data logging.
|
|
326
|
+
|
|
327
|
+
### Optimized utilities
|
|
328
|
+
|
|
329
|
+
Custom, performance-optimized utility modules (`Arr`, `Rec`, `Dict`, `Uniq`, `Num`, `Str`) wrap
|
|
330
|
+
standard JavaScript types to return explicit types like `Maybe` and support data-last currying.
|
|
331
|
+
Functions are composed using `pipe` and `flow`, which are enriched with high-level composition
|
|
332
|
+
helpers like `when`, `unless`, `either`, `safe`, and `async` to support robust, expressive
|
|
333
|
+
pipelines.
|
|
334
|
+
|
|
335
|
+
### Nominal branding and non-empty lists
|
|
336
|
+
|
|
337
|
+
Compile-time nominal typing with zero runtime overhead is provided by `Brand`, and `NonEmptyList`
|
|
338
|
+
guarantees that a list is never empty, eliminating defensive array length checks.
|
|
339
|
+
|
|
340
|
+
Every utility in the library is benchmarked against its native equivalent. The data-last currying
|
|
341
|
+
adds a small function call overhead, which is the expected cost of composability. For operations
|
|
342
|
+
where native overhead is significant, custom implementations are used that often run faster than
|
|
343
|
+
their native counterparts.
|
|
362
344
|
|
|
363
345
|
## License
|
|
364
346
|
|
|
@@ -125,6 +125,53 @@ function flow(ab, bc, cd, de, ef, fg, gh, hi, ij, jk) {
|
|
|
125
125
|
}
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
|
+
var when = (predicate, onTrue) => (a) => predicate(a) ? onTrue(a) : a;
|
|
129
|
+
var unless = (predicate, onFalse) => (a) => predicate(a) ? a : onFalse(a);
|
|
130
|
+
var either = (predicate, onTrue, onFalse) => (a) => predicate(a) ? onTrue(a) : onFalse(a);
|
|
131
|
+
var struct = (fields) => (a) => {
|
|
132
|
+
const result = {};
|
|
133
|
+
for (const key of Object.keys(fields)) {
|
|
134
|
+
result[key] = fields[key](a);
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
};
|
|
138
|
+
function safe(...fns) {
|
|
139
|
+
return (a) => {
|
|
140
|
+
let result = a;
|
|
141
|
+
if (result === null || result === void 0) {
|
|
142
|
+
return result;
|
|
143
|
+
}
|
|
144
|
+
for (const fn of fns) {
|
|
145
|
+
result = fn(result);
|
|
146
|
+
if (result === null || result === void 0) {
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return result;
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function async(...fns) {
|
|
154
|
+
return async (a) => {
|
|
155
|
+
let result = await a;
|
|
156
|
+
for (const fn of fns) {
|
|
157
|
+
result = await fn(result);
|
|
158
|
+
}
|
|
159
|
+
return result;
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
flow.when = when;
|
|
163
|
+
flow.unless = unless;
|
|
164
|
+
flow.either = either;
|
|
165
|
+
flow.struct = struct;
|
|
166
|
+
flow.safe = safe;
|
|
167
|
+
flow.async = async;
|
|
168
|
+
flow.try = (f, onError) => (a) => {
|
|
169
|
+
try {
|
|
170
|
+
return f(a);
|
|
171
|
+
} catch (error) {
|
|
172
|
+
return onError(error, a);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
128
175
|
|
|
129
176
|
// src/Composition/fn.ts
|
|
130
177
|
var identity = (a) => a;
|
|
@@ -148,6 +195,7 @@ var once = (f) => {
|
|
|
148
195
|
return result;
|
|
149
196
|
};
|
|
150
197
|
};
|
|
198
|
+
var defaultTo = (fallback) => (a) => a === null || a === void 0 ? fallback : a;
|
|
151
199
|
|
|
152
200
|
// src/Composition/juxt.ts
|
|
153
201
|
function juxt(fns) {
|
|
@@ -223,6 +271,49 @@ function pipe(a, ab, bc, cd, de, ef, fg, gh, hi, ij, jk) {
|
|
|
223
271
|
}
|
|
224
272
|
}
|
|
225
273
|
}
|
|
274
|
+
var when2 = (predicate, onTrue) => (a) => predicate(a) ? onTrue(a) : a;
|
|
275
|
+
var unless2 = (predicate, onFalse) => (a) => predicate(a) ? a : onFalse(a);
|
|
276
|
+
var either2 = (predicate, onTrue, onFalse) => (a) => predicate(a) ? onTrue(a) : onFalse(a);
|
|
277
|
+
var struct2 = (fields) => (a) => {
|
|
278
|
+
const result = {};
|
|
279
|
+
for (const key of Object.keys(fields)) {
|
|
280
|
+
result[key] = fields[key](a);
|
|
281
|
+
}
|
|
282
|
+
return result;
|
|
283
|
+
};
|
|
284
|
+
function safe2(a, ...fns) {
|
|
285
|
+
let result = a;
|
|
286
|
+
if (result === null || result === void 0) {
|
|
287
|
+
return result;
|
|
288
|
+
}
|
|
289
|
+
for (const fn of fns) {
|
|
290
|
+
result = fn(result);
|
|
291
|
+
if (result === null || result === void 0) {
|
|
292
|
+
return result;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return result;
|
|
296
|
+
}
|
|
297
|
+
async function async2(a, ...fns) {
|
|
298
|
+
let result = await a;
|
|
299
|
+
for (const fn of fns) {
|
|
300
|
+
result = await fn(result);
|
|
301
|
+
}
|
|
302
|
+
return result;
|
|
303
|
+
}
|
|
304
|
+
pipe.when = when2;
|
|
305
|
+
pipe.unless = unless2;
|
|
306
|
+
pipe.either = either2;
|
|
307
|
+
pipe.struct = struct2;
|
|
308
|
+
pipe.safe = safe2;
|
|
309
|
+
pipe.async = async2;
|
|
310
|
+
pipe.try = (f, onError) => (a) => {
|
|
311
|
+
try {
|
|
312
|
+
return f(a);
|
|
313
|
+
} catch (error) {
|
|
314
|
+
return onError(error, a);
|
|
315
|
+
}
|
|
316
|
+
};
|
|
226
317
|
|
|
227
318
|
// src/Composition/tap.ts
|
|
228
319
|
var tap = (f) => (a) => {
|
|
@@ -258,6 +349,7 @@ export {
|
|
|
258
349
|
and,
|
|
259
350
|
or,
|
|
260
351
|
once,
|
|
352
|
+
defaultTo,
|
|
261
353
|
juxt,
|
|
262
354
|
memoize,
|
|
263
355
|
memoizeWeak,
|
|
@@ -1092,7 +1092,7 @@ var Op;
|
|
|
1092
1092
|
}
|
|
1093
1093
|
return cases.nil();
|
|
1094
1094
|
};
|
|
1095
|
-
Op2.fold = (onErr,
|
|
1095
|
+
Op2.fold = (onErr, onNil, onOk) => (outcome) => {
|
|
1096
1096
|
if (outcome.kind === "OpOk") {
|
|
1097
1097
|
return onOk(outcome.value);
|
|
1098
1098
|
}
|
|
@@ -1321,17 +1321,17 @@ var RemoteData;
|
|
|
1321
1321
|
}
|
|
1322
1322
|
return (0, RemoteData2.notAsked)();
|
|
1323
1323
|
};
|
|
1324
|
-
RemoteData2.fold = (onNotAsked, onLoading,
|
|
1324
|
+
RemoteData2.fold = (onFailure, onNotAsked, onLoading, onSuccess) => (data) => {
|
|
1325
1325
|
switch (data.kind) {
|
|
1326
|
+
case "Failure": {
|
|
1327
|
+
return onFailure(data.error);
|
|
1328
|
+
}
|
|
1326
1329
|
case "NotAsked": {
|
|
1327
1330
|
return onNotAsked();
|
|
1328
1331
|
}
|
|
1329
1332
|
case "Loading": {
|
|
1330
1333
|
return onLoading();
|
|
1331
1334
|
}
|
|
1332
|
-
case "Failure": {
|
|
1333
|
-
return onFailure(data.error);
|
|
1334
|
-
}
|
|
1335
1335
|
case "Success": {
|
|
1336
1336
|
return onSuccess(data.value);
|
|
1337
1337
|
}
|