@okikio/observables 1.0.2
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/LICENSE +21 -0
- package/README.md +578 -0
- package/esm/_dnt.polyfills.d.ts +20 -0
- package/esm/_dnt.polyfills.d.ts.map +1 -0
- package/esm/_dnt.polyfills.js +12 -0
- package/esm/_spec.d.ts +260 -0
- package/esm/_spec.d.ts.map +1 -0
- package/esm/_spec.js +1 -0
- package/esm/_types.d.ts +141 -0
- package/esm/_types.d.ts.map +1 -0
- package/esm/_types.js +20 -0
- package/esm/error.d.ts +331 -0
- package/esm/error.d.ts.map +1 -0
- package/esm/error.js +408 -0
- package/esm/events.d.ts +320 -0
- package/esm/events.d.ts.map +1 -0
- package/esm/events.js +451 -0
- package/esm/helpers/_types.d.ts +188 -0
- package/esm/helpers/_types.d.ts.map +1 -0
- package/esm/helpers/_types.js +1 -0
- package/esm/helpers/mod.d.ts +90 -0
- package/esm/helpers/mod.d.ts.map +1 -0
- package/esm/helpers/mod.js +90 -0
- package/esm/helpers/operations/batch.d.ts +109 -0
- package/esm/helpers/operations/batch.d.ts.map +1 -0
- package/esm/helpers/operations/batch.js +140 -0
- package/esm/helpers/operations/combination.d.ts +162 -0
- package/esm/helpers/operations/combination.d.ts.map +1 -0
- package/esm/helpers/operations/combination.js +350 -0
- package/esm/helpers/operations/conditional.d.ts +211 -0
- package/esm/helpers/operations/conditional.d.ts.map +1 -0
- package/esm/helpers/operations/conditional.js +280 -0
- package/esm/helpers/operations/core.d.ts +198 -0
- package/esm/helpers/operations/core.d.ts.map +1 -0
- package/esm/helpers/operations/core.js +264 -0
- package/esm/helpers/operations/errors.d.ts +277 -0
- package/esm/helpers/operations/errors.d.ts.map +1 -0
- package/esm/helpers/operations/errors.js +378 -0
- package/esm/helpers/operations/mod.d.ts +26 -0
- package/esm/helpers/operations/mod.d.ts.map +1 -0
- package/esm/helpers/operations/mod.js +25 -0
- package/esm/helpers/operations/timing.d.ts +206 -0
- package/esm/helpers/operations/timing.d.ts.map +1 -0
- package/esm/helpers/operations/timing.js +457 -0
- package/esm/helpers/operators.d.ts +520 -0
- package/esm/helpers/operators.d.ts.map +1 -0
- package/esm/helpers/operators.js +563 -0
- package/esm/helpers/pipe.d.ts +118 -0
- package/esm/helpers/pipe.d.ts.map +1 -0
- package/esm/helpers/pipe.js +129 -0
- package/esm/helpers/utils.d.ts +142 -0
- package/esm/helpers/utils.d.ts.map +1 -0
- package/esm/helpers/utils.js +193 -0
- package/esm/mod.d.ts +863 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +861 -0
- package/esm/observable.d.ts +1610 -0
- package/esm/observable.d.ts.map +1 -0
- package/esm/observable.js +1970 -0
- package/esm/package.json +3 -0
- package/esm/queue.d.ts +201 -0
- package/esm/queue.d.ts.map +1 -0
- package/esm/queue.js +273 -0
- package/esm/symbol.d.ts +60 -0
- package/esm/symbol.d.ts.map +1 -0
- package/esm/symbol.js +132 -0
- package/package.json +96 -0
- package/script/_dnt.polyfills.d.ts +20 -0
- package/script/_dnt.polyfills.d.ts.map +1 -0
- package/script/_dnt.polyfills.js +13 -0
- package/script/_spec.d.ts +260 -0
- package/script/_spec.d.ts.map +1 -0
- package/script/_spec.js +2 -0
- package/script/_types.d.ts +141 -0
- package/script/_types.d.ts.map +1 -0
- package/script/_types.js +22 -0
- package/script/error.d.ts +331 -0
- package/script/error.d.ts.map +1 -0
- package/script/error.js +414 -0
- package/script/events.d.ts +320 -0
- package/script/events.d.ts.map +1 -0
- package/script/events.js +458 -0
- package/script/helpers/_types.d.ts +188 -0
- package/script/helpers/_types.d.ts.map +1 -0
- package/script/helpers/_types.js +2 -0
- package/script/helpers/mod.d.ts +90 -0
- package/script/helpers/mod.d.ts.map +1 -0
- package/script/helpers/mod.js +106 -0
- package/script/helpers/operations/batch.d.ts +109 -0
- package/script/helpers/operations/batch.d.ts.map +1 -0
- package/script/helpers/operations/batch.js +144 -0
- package/script/helpers/operations/combination.d.ts +162 -0
- package/script/helpers/operations/combination.d.ts.map +1 -0
- package/script/helpers/operations/combination.js +355 -0
- package/script/helpers/operations/conditional.d.ts +211 -0
- package/script/helpers/operations/conditional.d.ts.map +1 -0
- package/script/helpers/operations/conditional.js +286 -0
- package/script/helpers/operations/core.d.ts +198 -0
- package/script/helpers/operations/core.d.ts.map +1 -0
- package/script/helpers/operations/core.js +272 -0
- package/script/helpers/operations/errors.d.ts +277 -0
- package/script/helpers/operations/errors.d.ts.map +1 -0
- package/script/helpers/operations/errors.js +387 -0
- package/script/helpers/operations/mod.d.ts +26 -0
- package/script/helpers/operations/mod.d.ts.map +1 -0
- package/script/helpers/operations/mod.js +41 -0
- package/script/helpers/operations/timing.d.ts +206 -0
- package/script/helpers/operations/timing.d.ts.map +1 -0
- package/script/helpers/operations/timing.js +464 -0
- package/script/helpers/operators.d.ts +520 -0
- package/script/helpers/operators.d.ts.map +1 -0
- package/script/helpers/operators.js +570 -0
- package/script/helpers/pipe.d.ts +118 -0
- package/script/helpers/pipe.d.ts.map +1 -0
- package/script/helpers/pipe.js +132 -0
- package/script/helpers/utils.d.ts +142 -0
- package/script/helpers/utils.d.ts.map +1 -0
- package/script/helpers/utils.js +200 -0
- package/script/mod.d.ts +863 -0
- package/script/mod.d.ts.map +1 -0
- package/script/mod.js +877 -0
- package/script/observable.d.ts +1610 -0
- package/script/observable.d.ts.map +1 -0
- package/script/observable.js +1984 -0
- package/script/package.json +3 -0
- package/script/queue.d.ts +201 -0
- package/script/queue.d.ts.map +1 -0
- package/script/queue.js +286 -0
- package/script/symbol.d.ts +60 -0
- package/script/symbol.d.ts.map +1 -0
- package/script/symbol.js +135 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.map = map;
|
|
4
|
+
exports.filter = filter;
|
|
5
|
+
exports.take = take;
|
|
6
|
+
exports.drop = drop;
|
|
7
|
+
exports.tap = tap;
|
|
8
|
+
exports.scan = scan;
|
|
9
|
+
require("../../_dnt.polyfills.js");
|
|
10
|
+
const operators_js_1 = require("../operators.js");
|
|
11
|
+
/**
|
|
12
|
+
* Core transformation and terminal operators for everyday stream work.
|
|
13
|
+
*
|
|
14
|
+
* This module is the closest match to familiar array helpers. It exports the
|
|
15
|
+
* operators you reach for first when you want to transform values, filter them,
|
|
16
|
+
* accumulate state, or stop after a condition has been met. In practice, this
|
|
17
|
+
* is where most pipelines start before you add timing or concurrency behavior.
|
|
18
|
+
*
|
|
19
|
+
* The important difference from arrays is error handling. These operators are
|
|
20
|
+
* designed to work with the library's pass-through model, so your callbacks see
|
|
21
|
+
* clean data values while `ObservableError` instances continue downstream
|
|
22
|
+
* unchanged until a dedicated error-handling step decides what to do with them.
|
|
23
|
+
*
|
|
24
|
+
* @module
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* Transforms each data item, automatically skipping errors.
|
|
28
|
+
*
|
|
29
|
+
* Like `Array.map()`, but errors flow through unchanged:
|
|
30
|
+
*
|
|
31
|
+
* ```ts
|
|
32
|
+
* // Array.map() transforms everything
|
|
33
|
+
* [1, 2, 3].map(n => n * 2) // [2, 4, 6]
|
|
34
|
+
*
|
|
35
|
+
* // Stream map() transforms only data
|
|
36
|
+
* pipe([1, Error, 3], map(n => n * 2)) // [2, Error, 6]
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* Your function only receives clean data, never errors:
|
|
40
|
+
*
|
|
41
|
+
* ```ts
|
|
42
|
+
* pipe(
|
|
43
|
+
* userIds,
|
|
44
|
+
* map(id => fetchUser(id)), // Some API calls fail
|
|
45
|
+
* map(user => user.name), // Only runs on real users
|
|
46
|
+
* map(name => name.toUpperCase()) // Only runs on real names
|
|
47
|
+
* );
|
|
48
|
+
* // Result: [Name, Error, Name] - clean transformations, errors preserved
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @param project - Function that transforms each data item
|
|
52
|
+
*/
|
|
53
|
+
function map(project) {
|
|
54
|
+
return (0, operators_js_1.createStatefulOperator)({
|
|
55
|
+
name: "map",
|
|
56
|
+
createState: () => ({ index: 0 }),
|
|
57
|
+
transform(chunk, state, controller) {
|
|
58
|
+
const result = project(chunk, state.index++);
|
|
59
|
+
controller.enqueue(result);
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Keeps data items that pass your test, always preserves errors.
|
|
65
|
+
*
|
|
66
|
+
* Like `Array.filter()`, but errors automatically pass through:
|
|
67
|
+
*
|
|
68
|
+
* ```ts
|
|
69
|
+
* // Array.filter() tests everything
|
|
70
|
+
* [1, 2, 3, 4].filter(n => n > 2) // [3, 4]
|
|
71
|
+
*
|
|
72
|
+
* // Stream filter() tests only data
|
|
73
|
+
* pipe([1, Error, 3, 4], filter(n => n > 2)) // [Error, 3, 4]
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* Your test function only receives clean data:
|
|
77
|
+
*
|
|
78
|
+
* ```ts
|
|
79
|
+
* pipe(
|
|
80
|
+
* users,
|
|
81
|
+
* filter(user => user.isActive), // Only tests real users
|
|
82
|
+
* filter(user => user.age >= 18) // Chain multiple filters
|
|
83
|
+
* );
|
|
84
|
+
* // Result: filtered users + all errors preserved
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* @param predicate - Test function that decides which data items to keep
|
|
88
|
+
*/
|
|
89
|
+
function filter(predicate) {
|
|
90
|
+
return (0, operators_js_1.createStatefulOperator)({
|
|
91
|
+
name: "filter",
|
|
92
|
+
createState: () => ({ index: 0 }),
|
|
93
|
+
transform(chunk, state, controller) {
|
|
94
|
+
if (predicate(chunk, state.index++)) {
|
|
95
|
+
controller.enqueue(chunk);
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Takes the first N data items, errors don't count toward the limit.
|
|
102
|
+
*
|
|
103
|
+
* Like `Array.slice(0, N)`, but counts only successful data:
|
|
104
|
+
*
|
|
105
|
+
* ```ts
|
|
106
|
+
* // Array.slice() counts everything
|
|
107
|
+
* [1, "error", 2, 3].slice(0, 2) // [1, "error"] - 2 items total
|
|
108
|
+
*
|
|
109
|
+
* // Stream take() counts only data
|
|
110
|
+
* pipe([1, Error, 2, 3], take(2)) // [1, Error, 2] - 2 data items
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* Perfect for pagination and "top N" scenarios:
|
|
114
|
+
*
|
|
115
|
+
* ```ts
|
|
116
|
+
* pipe(
|
|
117
|
+
* productIds,
|
|
118
|
+
* map(id => fetchProduct(id)), // Some API calls fail
|
|
119
|
+
* filter(p => p.rating > 4), // Only high-rated products
|
|
120
|
+
* take(10) // Exactly 10 products + any errors
|
|
121
|
+
* );
|
|
122
|
+
* // Guarantees 10 actual products for your UI
|
|
123
|
+
* ```
|
|
124
|
+
*
|
|
125
|
+
* Stream stops immediately after collecting N data items.
|
|
126
|
+
*
|
|
127
|
+
* @param count - How many data items to take
|
|
128
|
+
*/
|
|
129
|
+
function take(count) {
|
|
130
|
+
return (0, operators_js_1.createStatefulOperator)({
|
|
131
|
+
name: "take",
|
|
132
|
+
createState: () => ({ taken: 0 }),
|
|
133
|
+
transform(chunk, state, controller) {
|
|
134
|
+
if (state.taken < count) {
|
|
135
|
+
controller.enqueue(chunk);
|
|
136
|
+
state.taken++;
|
|
137
|
+
// If we've taken enough values, terminate the stream
|
|
138
|
+
if (state.taken >= count) {
|
|
139
|
+
controller.terminate();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Skips the first N data items, errors don't count toward the skip count.
|
|
147
|
+
*
|
|
148
|
+
* Like `Array.slice(N)`, but counts only successful data:
|
|
149
|
+
*
|
|
150
|
+
* ```ts
|
|
151
|
+
* // Array.slice() counts everything
|
|
152
|
+
* [1, "error", 2, 3].slice(2) // [2, 3] - skipped first 2 items
|
|
153
|
+
*
|
|
154
|
+
* // Stream drop() counts only data
|
|
155
|
+
* pipe([1, Error, 2, 3], drop(2)) // [Error, 3] - skipped first 2 data items
|
|
156
|
+
* ```
|
|
157
|
+
*
|
|
158
|
+
* Perfect for pagination - skip to the right page regardless of errors:
|
|
159
|
+
*
|
|
160
|
+
* ```ts
|
|
161
|
+
* // Page 2: skip first 20 successful products
|
|
162
|
+
* pipe(
|
|
163
|
+
* productIds,
|
|
164
|
+
* map(id => fetchProduct(id)), // Some API calls fail
|
|
165
|
+
* drop(20), // Skip first 20 real products
|
|
166
|
+
* take(20) // Next 20 products
|
|
167
|
+
* );
|
|
168
|
+
* // Errors flow through immediately, don't affect page positioning
|
|
169
|
+
* ```
|
|
170
|
+
*
|
|
171
|
+
* @param count - How many data items to skip
|
|
172
|
+
*/
|
|
173
|
+
function drop(count) {
|
|
174
|
+
return (0, operators_js_1.createStatefulOperator)({
|
|
175
|
+
name: "drop",
|
|
176
|
+
createState: () => ({ dropped: 0 }),
|
|
177
|
+
transform(chunk, state, controller) {
|
|
178
|
+
if (state.dropped < count) {
|
|
179
|
+
state.dropped++;
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
controller.enqueue(chunk);
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Runs side effects on data items, automatically skipping errors.
|
|
189
|
+
*
|
|
190
|
+
* Like adding `console.log()` to debug, but only logs clean data:
|
|
191
|
+
*
|
|
192
|
+
* ```ts
|
|
193
|
+
* // Regular debugging sees everything (messy)
|
|
194
|
+
* data.map(x => { console.log('Item:', x); return transform(x); })
|
|
195
|
+
*
|
|
196
|
+
* // Stream tap() sees only data (clean)
|
|
197
|
+
* pipe(
|
|
198
|
+
* data,
|
|
199
|
+
* map(x => transform(x)),
|
|
200
|
+
* tap(x => console.log('Processed:', x)) // Only logs real data
|
|
201
|
+
* )
|
|
202
|
+
* ```
|
|
203
|
+
*
|
|
204
|
+
* Perfect for logging, analytics, caching, progress tracking:
|
|
205
|
+
*
|
|
206
|
+
* ```ts
|
|
207
|
+
* pipe(
|
|
208
|
+
* userIds,
|
|
209
|
+
* map(id => fetchUser(id)), // Some API calls fail
|
|
210
|
+
* tap(user => console.log('Loaded:', user.name)), // Clean logs
|
|
211
|
+
* tap(user => analytics.track('user_loaded')), // Business events only
|
|
212
|
+
* filter(user => user.isActive)
|
|
213
|
+
* );
|
|
214
|
+
* // Logs show only successful operations, errors flow silently
|
|
215
|
+
* ```
|
|
216
|
+
*
|
|
217
|
+
* Data flows through unchanged - `tap` just observes.
|
|
218
|
+
*
|
|
219
|
+
* @param fn - Function to run on each data item (for side effects)
|
|
220
|
+
*/
|
|
221
|
+
function tap(fn) {
|
|
222
|
+
return (0, operators_js_1.createOperator)({
|
|
223
|
+
name: "tap",
|
|
224
|
+
transform(chunk, controller) {
|
|
225
|
+
fn(chunk);
|
|
226
|
+
controller.enqueue(chunk);
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Builds running totals from data, automatically skipping errors.
|
|
232
|
+
*
|
|
233
|
+
* Like `Array.reduce()`, but shows each step and handles errors:
|
|
234
|
+
*
|
|
235
|
+
* ```ts
|
|
236
|
+
* // Array.reduce() gives final result only
|
|
237
|
+
* [1, 2, 3].reduce((sum, n) => sum + n, 0) // 6
|
|
238
|
+
*
|
|
239
|
+
* // Stream scan() shows progressive results
|
|
240
|
+
* pipe([1, Error, 3], scan((sum, n) => sum + n, 0)) // [0, 1, Error, 4]
|
|
241
|
+
* ```
|
|
242
|
+
*
|
|
243
|
+
* Perfect for running totals, counters, building objects:
|
|
244
|
+
*
|
|
245
|
+
* ```ts
|
|
246
|
+
* pipe(
|
|
247
|
+
* productIds,
|
|
248
|
+
* map(id => fetchPrice(id)), // Some API calls fail
|
|
249
|
+
* scan((total, price) => total + price, 0) // Running cart total
|
|
250
|
+
* );
|
|
251
|
+
* // Result: [0, 10, Error, 35, 40] - total ignores errors
|
|
252
|
+
* ```
|
|
253
|
+
*
|
|
254
|
+
* Always starts by emitting your initial value immediately.
|
|
255
|
+
*
|
|
256
|
+
* @param accumulator - Function that combines current total with new data
|
|
257
|
+
* @param seed - Starting value (emitted first)
|
|
258
|
+
*/
|
|
259
|
+
function scan(accumulator, seed) {
|
|
260
|
+
return (0, operators_js_1.createStatefulOperator)({
|
|
261
|
+
name: "scan",
|
|
262
|
+
createState: () => ({ acc: seed, index: 0 }),
|
|
263
|
+
start(state, controller) {
|
|
264
|
+
// Emit the seed value immediately
|
|
265
|
+
controller.enqueue(state.acc);
|
|
266
|
+
},
|
|
267
|
+
transform(chunk, state, controller) {
|
|
268
|
+
state.acc = accumulator(state.acc, chunk, state.index++);
|
|
269
|
+
controller.enqueue(state.acc);
|
|
270
|
+
},
|
|
271
|
+
});
|
|
272
|
+
}
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error-focused operators for recovering from or reshaping stream failures.
|
|
3
|
+
*
|
|
4
|
+
* This entrypoint is for pipelines that expect some work to fail and want to
|
|
5
|
+
* keep going. It exports helpers for dropping wrapped errors, mapping them to
|
|
6
|
+
* fallback values, logging them, or converting them into a shape that fits the
|
|
7
|
+
* rest of the pipeline.
|
|
8
|
+
*
|
|
9
|
+
* These operators are most useful with the library's pass-through error mode,
|
|
10
|
+
* where failures travel as `ObservableError` values instead of immediately
|
|
11
|
+
* terminating the whole stream.
|
|
12
|
+
*
|
|
13
|
+
* @module
|
|
14
|
+
*/
|
|
15
|
+
import "../../_dnt.polyfills.js";
|
|
16
|
+
import type { ExcludeError, Operator } from "../_types.js";
|
|
17
|
+
import { ObservableError } from "../../error.js";
|
|
18
|
+
/**
|
|
19
|
+
* Removes all errors from the stream, keeping only successful data.
|
|
20
|
+
*
|
|
21
|
+
* Like a filter that only allows non-error values to pass. It's a simple way
|
|
22
|
+
* to clean up a stream when you don't need to handle failures.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* import { pipe, ignoreErrors, from } from "./helpers/mod.ts";
|
|
27
|
+
*
|
|
28
|
+
* // Array behavior (conceptual)
|
|
29
|
+
* const mixedData = [1, new Error("fail"), 2, 3];
|
|
30
|
+
* const goodData = mixedData.filter(item => !(item instanceof Error)); // [1, 2, 3]
|
|
31
|
+
*
|
|
32
|
+
* // Stream behavior
|
|
33
|
+
* const sourceStream = from([1, new ObservableError("fail"), 2, 3]);
|
|
34
|
+
* const cleanStream = pipe(sourceStream, ignoreErrors()); // Emits 1, 2, 3
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* ## Practical Use Case
|
|
38
|
+
*
|
|
39
|
+
* Use `ignoreErrors` when processing a batch of items where some failures are
|
|
40
|
+
* expected and acceptable. For example, fetching a list of URLs where some
|
|
41
|
+
* might be broken. You only want to process the ones that work.
|
|
42
|
+
*
|
|
43
|
+
* ## Key Insight
|
|
44
|
+
*
|
|
45
|
+
* `ignoreErrors` provides a "fire-and-forget" approach to error handling. It's
|
|
46
|
+
* useful for non-critical tasks or when partial success is sufficient.
|
|
47
|
+
*
|
|
48
|
+
* @template T The type of good data in your stream
|
|
49
|
+
* @returns A stream operator that silently removes all errors
|
|
50
|
+
*/
|
|
51
|
+
export declare function ignoreErrors<T>(): Operator<T | ObservableError, ExcludeError<T>>;
|
|
52
|
+
/**
|
|
53
|
+
* Replaces any error in the stream with a fallback value.
|
|
54
|
+
*
|
|
55
|
+
* Like a `try...catch` block for each item in a stream, but instead of just
|
|
56
|
+
* handling the error, you provide a default value to take its place.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* import { pipe, catchErrors, from } from "./helpers/mod.ts";
|
|
61
|
+
*
|
|
62
|
+
* // Conceptual equivalent
|
|
63
|
+
* function process(item) {
|
|
64
|
+
* try {
|
|
65
|
+
* if (item instanceof Error) throw item;
|
|
66
|
+
* return item;
|
|
67
|
+
* } catch {
|
|
68
|
+
* return "default";
|
|
69
|
+
* }
|
|
70
|
+
* }
|
|
71
|
+
* const results = [1, new Error("fail")].map(process); // [1, "default"]
|
|
72
|
+
*
|
|
73
|
+
* // Stream behavior
|
|
74
|
+
* const sourceStream = from([1, new ObservableError("fail")]);
|
|
75
|
+
* const safeStream = pipe(sourceStream, catchErrors("default")); // Emits 1, "default"
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* ## Practical Use Case
|
|
79
|
+
*
|
|
80
|
+
* Use `catchErrors` to provide a default state or value when an operation
|
|
81
|
+
* fails. For example, if fetching a user's profile fails, you could return a
|
|
82
|
+
* default "Guest" profile instead of letting the error propagate.
|
|
83
|
+
*
|
|
84
|
+
* ## Key Insight
|
|
85
|
+
*
|
|
86
|
+
* `catchErrors` ensures your stream continues with a predictable structure,
|
|
87
|
+
* even when individual operations fail. It maintains the flow of data by
|
|
88
|
+
* substituting errors with safe, default values.
|
|
89
|
+
*
|
|
90
|
+
* @template T The type of data in your stream.
|
|
91
|
+
* @template R The type of the fallback value.
|
|
92
|
+
* @param fallback The value to use whenever an error occurs.
|
|
93
|
+
* @returns A stream operator that replaces errors with a fallback value.
|
|
94
|
+
*/
|
|
95
|
+
export declare function catchErrors<T, R>(fallback: R): Operator<T | ObservableError, ExcludeError<T> | R>;
|
|
96
|
+
/**
|
|
97
|
+
* Transforms errors into custom values using a mapping function.
|
|
98
|
+
*
|
|
99
|
+
* This is a more powerful version of `catchErrors`. Instead of a single
|
|
100
|
+
* fallback, you can inspect each error and decide what to replace it with.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* import { pipe, mapErrors, from } from "./helpers/mod.ts";
|
|
105
|
+
*
|
|
106
|
+
* // Stream behavior
|
|
107
|
+
* const sourceStream = from([
|
|
108
|
+
* 1,
|
|
109
|
+
* new ObservableError("Not Found"),
|
|
110
|
+
* new ObservableError("Server Error")
|
|
111
|
+
* ]);
|
|
112
|
+
*
|
|
113
|
+
* const handledStream = pipe(
|
|
114
|
+
* sourceStream,
|
|
115
|
+
* mapErrors(err => {
|
|
116
|
+
* if (err.message === "Not Found") return { status: 404 };
|
|
117
|
+
* return { status: 500 };
|
|
118
|
+
* })
|
|
119
|
+
* );
|
|
120
|
+
* // Emits 1, { status: 404 }, { status: 500 }
|
|
121
|
+
* ```
|
|
122
|
+
*
|
|
123
|
+
* ## Practical Use Case
|
|
124
|
+
*
|
|
125
|
+
* Use `mapErrors` to convert different types of errors into meaningful data.
|
|
126
|
+
* For example, a "Not Found" error could be mapped to `null`, while a "Server
|
|
127
|
+
* Error" could be mapped to an object that triggers a retry mechanism.
|
|
128
|
+
*
|
|
129
|
+
* ## Key Insight
|
|
130
|
+
*
|
|
131
|
+
* `mapErrors` treats errors as data. It allows you to create a resilient
|
|
132
|
+
* stream that can intelligently respond to different failure modes without
|
|
133
|
+
* stopping.
|
|
134
|
+
*
|
|
135
|
+
* @template T The type of successful data in your stream.
|
|
136
|
+
* @template E The type your errors will be mapped to.
|
|
137
|
+
* @param errorMapper A function that receives an error and returns a new value.
|
|
138
|
+
* @returns A stream operator that transforms errors.
|
|
139
|
+
*/
|
|
140
|
+
export declare function mapErrors<T, E>(errorMapper: (error: ObservableError) => E): Operator<T | ObservableError, ExcludeError<T> | E>;
|
|
141
|
+
/**
|
|
142
|
+
* Keeps only the errors from the stream, discarding successful data.
|
|
143
|
+
*
|
|
144
|
+
* This is the inverse of `ignoreErrors`. It's useful for creating a dedicated
|
|
145
|
+
* error-handling pipeline.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* import { pipe, onlyErrors, from } from "./helpers/mod.ts";
|
|
150
|
+
*
|
|
151
|
+
* // Stream behavior
|
|
152
|
+
* const sourceStream = from([1, new ObservableError("fail"), 2]);
|
|
153
|
+
* const errorStream = pipe(sourceStream, onlyErrors()); // Emits ObservableError("fail")
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* ## Practical Use Case
|
|
157
|
+
*
|
|
158
|
+
* Use `onlyErrors` to separate the error stream from the data stream. You can
|
|
159
|
+
* then log these errors, send them to a monitoring service, or trigger alerts
|
|
160
|
+
* without interfering with the main data processing flow.
|
|
161
|
+
*
|
|
162
|
+
* ## Key Insight
|
|
163
|
+
*
|
|
164
|
+
* `onlyErrors` allows you to create a side-channel for failures, enabling
|
|
165
|
+
* robust monitoring and debugging.
|
|
166
|
+
*
|
|
167
|
+
* @template T The type of data in the source stream.
|
|
168
|
+
* @returns An operator that filters for errors.
|
|
169
|
+
*/
|
|
170
|
+
export declare function onlyErrors<T>(): Operator<T | ObservableError, ObservableError>;
|
|
171
|
+
/**
|
|
172
|
+
* Summarizes the stream into a final count of successes and errors.
|
|
173
|
+
*
|
|
174
|
+
* This operator waits for the stream to complete and then emits a single
|
|
175
|
+
* object with the total counts.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* import { pipe, summarizeErrors, from } from "./helpers/mod.ts";
|
|
180
|
+
*
|
|
181
|
+
* // Stream behavior
|
|
182
|
+
* const sourceStream = from([1, new ObservableError("fail"), 2, 3]);
|
|
183
|
+
* const summary = await pipe(sourceStream, summarizeErrors()).toPromise();
|
|
184
|
+
* // { successCount: 3, errorCount: 1, totalProcessed: 4, successRate: 0.75 }
|
|
185
|
+
* ```
|
|
186
|
+
*
|
|
187
|
+
* ## Practical Use Case
|
|
188
|
+
*
|
|
189
|
+
* Use `summarizeErrors` at the end of a batch processing job to get a report
|
|
190
|
+
* on how many items were processed successfully and how many failed. This is
|
|
191
|
+
|
|
192
|
+
* useful for logging, monitoring, and generating reports.
|
|
193
|
+
*
|
|
194
|
+
* ## Key Insight
|
|
195
|
+
*
|
|
196
|
+
* `summarizeErrors` is a terminal operator that provides a high-level overview
|
|
197
|
+
* of a stream's health and completeness.
|
|
198
|
+
*
|
|
199
|
+
* @template T The type of data in the source stream.
|
|
200
|
+
* @returns An operator that emits a summary of successes and errors.
|
|
201
|
+
*/
|
|
202
|
+
export declare function summarizeErrors<T>(): Operator<T | ObservableError, {
|
|
203
|
+
successCount: number;
|
|
204
|
+
errorCount: number;
|
|
205
|
+
totalProcessed: number;
|
|
206
|
+
successRate: number;
|
|
207
|
+
}>;
|
|
208
|
+
/**
|
|
209
|
+
* Performs a side effect for each error without modifying the stream.
|
|
210
|
+
*
|
|
211
|
+
* Like `Array.prototype.forEach` but only for errors. It's useful for logging
|
|
212
|
+
* or debugging without altering the data flow.
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```ts
|
|
216
|
+
* import { pipe, tapError, from } from "./helpers/mod.ts";
|
|
217
|
+
*
|
|
218
|
+
* // Stream behavior
|
|
219
|
+
* const sourceStream = from([1, new ObservableError("fail")]);
|
|
220
|
+
* const tappedStream = pipe(
|
|
221
|
+
* sourceStream,
|
|
222
|
+
* tapError(err => console.error("Found an error:", err))
|
|
223
|
+
* );
|
|
224
|
+
* // Logs the error, then emits 1 and the error object.
|
|
225
|
+
* ```
|
|
226
|
+
*
|
|
227
|
+
* ## Practical Use Case
|
|
228
|
+
*
|
|
229
|
+
* Use `tapError` to log errors to the console or a monitoring service as they
|
|
230
|
+
* happen, without stopping or changing the stream. This is invaluable for
|
|
231
|
+
* real-time debugging.
|
|
232
|
+
*
|
|
233
|
+
* ## Key Insight
|
|
234
|
+
*
|
|
235
|
+
* `tapError` gives you a window into the stream's errors without affecting the
|
|
236
|
+
* stream itself.
|
|
237
|
+
*
|
|
238
|
+
* @template T The type of data in the stream.
|
|
239
|
+
* @param sideEffect A function to call for each error.
|
|
240
|
+
* @returns An operator that performs a side effect on errors.
|
|
241
|
+
*/
|
|
242
|
+
export declare function tapError<T>(sideEffect: (error: ObservableError) => void): Operator<T | ObservableError, T | ObservableError>;
|
|
243
|
+
/**
|
|
244
|
+
* Throws an error if it encounters one in the stream.
|
|
245
|
+
*
|
|
246
|
+
* This operator is useful for enforcing strict error handling. If an error is
|
|
247
|
+
* encountered, it will throw immediately, stopping the iteration.
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* ```ts
|
|
251
|
+
* import { pipe, throwErrors, from } from "./helpers/mod.ts";
|
|
252
|
+
*
|
|
253
|
+
* // Stream behavior
|
|
254
|
+
* const sourceStream = from([1, new ObservableError("fail"), 2]);
|
|
255
|
+
* const throwingStream = pipe(sourceStream, throwErrors());
|
|
256
|
+
*
|
|
257
|
+
* for await (const item of throwingStream) {
|
|
258
|
+
* console.log(item); // Will log 1, then throw on "fail"
|
|
259
|
+
* }
|
|
260
|
+
* ```
|
|
261
|
+
*
|
|
262
|
+
* ## Practical Use Case
|
|
263
|
+
*
|
|
264
|
+
* Use `throwErrors` when you want to ensure that any error in the stream is
|
|
265
|
+
* treated as a critical failure. This is useful in scenarios where errors must
|
|
266
|
+
* be handled immediately and cannot be ignored.
|
|
267
|
+
*
|
|
268
|
+
* ## Key Insight
|
|
269
|
+
*
|
|
270
|
+
* `throwErrors` provides a way to enforce strict error handling in streams,
|
|
271
|
+
* ensuring that no errors go unnoticed.
|
|
272
|
+
*
|
|
273
|
+
* @template T The type of data in the stream.
|
|
274
|
+
* @returns An operator that throws on errors.
|
|
275
|
+
*/
|
|
276
|
+
export declare function throwErrors<T>(): Operator<T | ObservableError, T>;
|
|
277
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../../src/helpers/operations/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,yBAAyB,CAAC;AAEjC,OAAO,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAC3D,OAAO,EAAqB,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAGpE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAgB,YAAY,CAAC,CAAC,KAAK,QAAQ,CACzC,CAAC,GAAG,eAAe,EACnB,YAAY,CAAC,CAAC,CAAC,CAChB,CASA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,CAAC,EAC9B,QAAQ,EAAE,CAAC,GACV,QAAQ,CAAC,CAAC,GAAG,eAAe,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAYpD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,EAC5B,WAAW,EAAE,CAAC,KAAK,EAAE,eAAe,KAAK,CAAC,GACzC,QAAQ,CAAC,CAAC,GAAG,eAAe,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CA6BpD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,UAAU,CAAC,CAAC,KAAK,QAAQ,CACvC,CAAC,GAAG,eAAe,EACnB,eAAe,CAChB,CAWA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,eAAe,CAAC,CAAC,KAAK,QAAQ,CAAC,CAAC,GAAG,eAAe,EAAE;IAClE,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,cAAc,EAAE,MAAM,CAAC;IACvB,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC,CA2BD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EACxB,UAAU,EAAE,CAAC,KAAK,EAAE,eAAe,KAAK,IAAI,GAC3C,QAAQ,CAAC,CAAC,GAAG,eAAe,EAAE,CAAC,GAAG,eAAe,CAAC,CAgBpD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAgB,WAAW,CAAC,CAAC,KAAK,QAAQ,CAAC,CAAC,GAAG,eAAe,EAAE,CAAC,CAAC,CASjE"}
|