1 | import { Desc } from "./describe";
|
2 | import { EventSink, EventStreamDelay, Sink, Subscribe, Unsub, VoidSink, Function1, Function2, Function0 } from "./types";
|
3 | import { StateF } from "./withstatemachine";
|
4 | import { Equals } from "./skipduplicates";
|
5 | import { Accumulator } from "./scan";
|
6 | import { SpawnerOrObservable, EventSpawner, EventOrValue } from "./flatmap_";
|
7 | import { DelayFunction } from "./buffer";
|
8 | import { Transformer } from "./transform";
|
9 | import { Predicate, PredicateOrProperty } from "./predicate";
|
10 | import { GroupTransformer } from "./groupby";
|
11 | import { Differ } from "./diff";
|
12 | import { DecodedValueOf } from "./decode";
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | export declare abstract class Observable<V> {
|
19 | |
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | desc: Desc;
|
27 | |
28 |
|
29 |
|
30 | id: number;
|
31 |
|
32 | initialDesc: Desc;
|
33 |
|
34 | _name?: string;
|
35 |
|
36 | _isObservable: boolean;
|
37 | constructor(desc: Desc);
|
38 | /**
|
39 | Creates a Property that indicates whether
|
40 | `observable` is awaiting `otherObservable`, i.e. has produced a value after the latest
|
41 | value from `otherObservable`. This is handy for keeping track whether we are
|
42 | currently awaiting an AJAX response:
|
43 |
|
44 | ```js
|
45 | var showAjaxIndicator = ajaxRequest.awaiting(ajaxResponse)
|
46 | ```
|
47 |
|
48 | */
|
49 | awaiting(other: Observable<any>): Property<boolean>;
|
50 | /**
|
51 | Throttles the observable using a buffer so that at most one value event in minimumInterval is issued.
|
52 | Unlike [`throttle`](#observable-throttle), it doesn't discard the excessive events but buffers them instead, outputting
|
53 | them with a rate of at most one value per minimumInterval.
|
54 |
|
55 | Example:
|
56 |
|
57 | ```js
|
58 | var throttled = source.bufferingThrottle(2)
|
59 | ```
|
60 |
|
61 | ```
|
62 | source: asdf----asdf----
|
63 | throttled: a-s-d-f-a-s-d-f-
|
64 | ```
|
65 | */
|
66 | bufferingThrottle(minimumInterval: number): this;
|
67 | /**
|
68 | * Creates a stream of changes to the Property. The stream *does not* include
|
69 | an event for the current value of the Property at the time this method was called.
|
70 | For EventStreams, this method returns the stream itself.
|
71 | */
|
72 | abstract changes(): EventStream<V>;
|
73 | /**
|
74 | Combines the latest values of the two
|
75 | streams or properties using a two-arg function. Similarly to [`scan`](#scan), you can use a
|
76 | method name instead, so you could do `a.combine(b, ".concat")` for two
|
77 | properties with array value. The result is a [Property](property.html).
|
78 | */
|
79 | combine<V2, R>(right: Observable<V2>, f: Function2<V, V2, R>): Property<R>;
|
80 | /**
|
81 | Concatenates two streams/properties into one stream/property so that
|
82 | it will deliver events from this observable until it ends and then deliver
|
83 | events from `other`. This means too that events from `other`,
|
84 | occurring before the end of this observable will not be included in the result
|
85 | stream/property.
|
86 | */
|
87 | abstract concat(other: Observable<V>): Observable<V>;
|
88 | abstract concat<V2>(other: Observable<V2>): Observable<V | V2>;
|
89 | /**
|
90 | Throttles stream/property by given amount
|
91 | of milliseconds, but so that event is only emitted after the given
|
92 | "quiet period". Does not affect emitting the initial value of a [Property](property.html).
|
93 | The difference of [`throttle`](#throttle) and [`debounce`](#debounce) is the same as it is in the
|
94 | same methods in jQuery.
|
95 |
|
96 | Example:
|
97 |
|
98 | ```
|
99 | source: asdf----asdf----
|
100 | source.debounce(2): -----f-------f--
|
101 | ```
|
102 |
|
103 | */
|
104 | debounce(minimumInterval: number): this;
|
105 | /**
|
106 | Passes the first event in the
|
107 | stream through, but after that, only passes events after a given number
|
108 | of milliseconds have passed since previous output.
|
109 |
|
110 | Example:
|
111 |
|
112 | ```
|
113 | source: asdf----asdf----
|
114 | source.debounceImmediate(2): a-d-----a-d-----
|
115 | ```
|
116 | */
|
117 | debounceImmediate(minimumInterval: number): this;
|
118 | /**
|
119 | Decodes input using the given mapping. Is a
|
120 | bit like a switch-case or the decode function in Oracle SQL. For
|
121 | example, the following would map the value 1 into the string "mike"
|
122 | and the value 2 into the value of the `who` property.
|
123 |
|
124 | ```js
|
125 | property.decode({1 : "mike", 2 : who})
|
126 | ```
|
127 |
|
128 | This is actually based on [`combineTemplate`](#combinetemplate) so you can compose static
|
129 | and dynamic data quite freely, as in
|
130 |
|
131 | ```js
|
132 | property.decode({1 : { type: "mike" }, 2 : { type: "other", whoThen : who }})
|
133 | ```
|
134 |
|
135 | The return value of [`decode`](#decode) is always a [`Property`](property.html).
|
136 |
|
137 | */
|
138 | decode<T extends Record<any, any>>(cases: T): Property<DecodedValueOf<T>>;
|
139 | /**
|
140 | Delays the stream/property by given amount of milliseconds. Does not delay the initial value of a [`Property`](property.html).
|
141 |
|
142 | ```js
|
143 | var delayed = source.delay(2)
|
144 | ```
|
145 |
|
146 | ```
|
147 | source: asdf----asdf----
|
148 | delayed: --asdf----asdf--
|
149 | ```
|
150 |
|
151 | */
|
152 | delay(delayMs: number): this;
|
153 | /** @hidden */
|
154 | abstract transformChanges(desc: Desc, f: EventStreamDelay<V>): this;
|
155 | /**
|
156 | * Returns the an array of dependencies that the Observable has. For instance, for `a.map(function() {}).deps()`, would return `[a]`.
|
157 | This method returns the "visible" dependencies only, skipping internal details. This method is thus suitable for visualization tools.
|
158 | Internally, many combinator functions depend on other combinators to create intermediate Observables that the result will actually depend on.
|
159 | The `deps` method will skip these internal dependencies. See also: [internalDeps](#internaldeps)
|
160 | */
|
161 | deps(): Observable<any>[];
|
162 | /**
|
163 | Returns a Property that represents the result of a comparison
|
164 | between the previous and current value of the Observable. For the initial value of the Observable,
|
165 | the previous value will be the given start.
|
166 |
|
167 | Example:
|
168 |
|
169 | ```js
|
170 | var distance = function (a,b) { return Math.abs(b - a) }
|
171 | Bacon.sequentially(1, [1,2,3]).diff(0, distance)
|
172 | ```
|
173 |
|
174 | This would result to following elements in the result stream:
|
175 |
|
176 | 1 - 0 = 1
|
177 | 2 - 1 = 1
|
178 | 3 - 2 = 1
|
179 |
|
180 | */
|
181 | diff<V2>(start: V, f: Differ<V, V2>): Property<V2>;
|
182 | /**
|
183 | Returns a stream/property where the function f
|
184 | is executed for each value, before dispatching to subscribers. This is
|
185 | useful for debugging, but also for stuff like calling the
|
186 | `preventDefault()` method for events. In fact, you can
|
187 | also use a property-extractor string instead of a function, as in
|
188 | `".preventDefault"`.
|
189 |
|
190 | Please note that for Properties, it's not guaranteed that the function will be called exactly once
|
191 | per event; when a Property loses all of its subscribers it will re-emit its current value when a
|
192 | new subscriber is added.
|
193 | */
|
194 | doAction(f: Function1<V, any>): this;
|
195 | doEnd(f: Function0<any>): this;
|
196 | /**
|
197 | Returns a stream/property where the function f
|
198 | is executed for each error, before dispatching to subscribers.
|
199 | That is, same as [`doAction`](#observable-doaction) but for errors.
|
200 | */
|
201 | doError(f: Function1<any, any>): this;
|
202 | /**
|
203 | Logs each value of the Observable to the console. doLog() behaves like [`log`](#log)
|
204 | but does not subscribe to the event stream. You can think of doLog() as a
|
205 | logger function that – unlike log() – is safe to use in production. doLog() is
|
206 | safe, because it does not cause the same surprising side-effects as log()
|
207 | does.
|
208 | */
|
209 | doLog(...args: any[]): this;
|
210 | endAsValue(): Observable<{}>;
|
211 | /**
|
212 | Returns a stream/property that ends the on first [`Error`](error.html) event. The
|
213 | error is included in the output of the returned Observable.
|
214 |
|
215 | @param predicate optional predicate function to determine whether to end on a given error
|
216 | */
|
217 | endOnError(predicate?: Predicate<any>): this;
|
218 | /**
|
219 | Returns a stream containing [`Error`](error.html) events only.
|
220 | Same as filtering with a function that always returns false.
|
221 | */
|
222 | errors(): this;
|
223 | /**
|
224 | Filters values using given predicate function.
|
225 | Instead of a function, you can use a constant value (`true` to include all, `false` to exclude all).
|
226 |
|
227 | You can also filter values based on the value of a
|
228 | property. Event will be included in output [if and only if](http://en.wikipedia.org/wiki/If_and_only_if) the property holds `true`
|
229 | at the time of the event.
|
230 | */
|
231 | filter(f: Predicate<V> | boolean | Property<boolean>): this;
|
232 | /**
|
233 | Takes the first element from the stream. Essentially `observable.take(1)`.
|
234 | */
|
235 | first(): this;
|
236 | /**
|
237 | Returns a Promise which will be resolved with the first event coming from an Observable.
|
238 | Like [`toPromise`](#topromise), the global ES6 promise implementation will be used unless a promise
|
239 | constructor is given.
|
240 | */
|
241 | firstToPromise(PromiseCtr?: Function): Promise<V>;
|
242 | /**
|
243 | For each element in the source stream, spawn a new
|
244 | stream/property using the function `f`. Collect events from each of the spawned
|
245 | streams into the result stream/property. Note that instead of a function, you can provide a
|
246 | stream/property too. Also, the return value of function `f` can be either an
|
247 | `Observable` (stream/property) or a constant value.
|
248 |
|
249 | `stream.flatMap()` can be used conveniently with [`Bacon.once()`](../globals.html#once) and [`Bacon.never()`](../globals.html#never)
|
250 | for converting and filtering at the same time, including only some of the results.
|
251 |
|
252 | Example - converting strings to integers, skipping empty values:
|
253 |
|
254 | ```js
|
255 | stream.flatMap(function(text) {
|
256 | return (text != "") ? parseInt(text) : Bacon.never()
|
257 | })
|
258 | ```
|
259 | */
|
260 | abstract flatMap<V2>(f: SpawnerOrObservable<V, V2>): Observable<V2>;
|
261 | /**
|
262 | A [`flatMapWithConcurrencyLimit`](#flatmapwithconcurrencylimit) with limit of 1.
|
263 | */
|
264 | abstract flatMapConcat<V2>(f: SpawnerOrObservable<V, V2>): Observable<V2>;
|
265 | /**
|
266 | Like [`flatMap`](#flatmap), but is applied only on [`Error`](error.html) events. Returned values go into the
|
267 | value stream, unless an error event is returned. As an example, one type of error could result in a retry and another just
|
268 | passed through, which can be implemented using flatMapError.
|
269 | */
|
270 | abstract flatMapError<V2>(f: Function1<any, Observable<V2> | EventOrValue<V2>>): Observable<V | V2>;
|
271 | abstract flatMapEvent<V2>(f: EventSpawner<V, V2>): Observable<V2>;
|
272 | /**
|
273 | Like [`flatMap`](#observable-flatmap), but only spawns a new
|
274 | stream if the previously spawned stream has ended.
|
275 | */
|
276 | abstract flatMapFirst<V2>(f: SpawnerOrObservable<V, V2>): Observable<V2>;
|
277 | /**
|
278 | Like [`flatMap`](#flatmap), but instead of including events from
|
279 | all spawned streams, only includes them from the latest spawned stream.
|
280 | You can think this as switching from stream to stream.
|
281 | Note that instead of a function, you can provide a stream/property too.
|
282 | */
|
283 | abstract flatMapLatest<V2>(f: SpawnerOrObservable<V, V2>): Observable<V2>;
|
284 | /**
|
285 | A super method of *flatMap* family. It limits the number of open spawned streams and buffers incoming events.
|
286 | [`flatMapConcat`](#flatmapconcat) is `flatMapWithConcurrencyLimit(1)` (only one input active),
|
287 | and [`flatMap`](#flatmap) is `flatMapWithConcurrencyLimit ∞` (all inputs are piped to output).
|
288 | */
|
289 | abstract flatMapWithConcurrencyLimit<V2>(limit: number, f: SpawnerOrObservable<V, V2>): Observable<V2>;
|
290 | /**
|
291 | Works like [`scan`](#scan) but only emits the final
|
292 | value, i.e. the value just before the observable ends. Returns a
|
293 | [`Property`](property.html).
|
294 | */
|
295 | fold<V2>(seed: V2, f: Accumulator<V, V2>): Property<V2>;
|
296 | /**
|
297 | An alias for [onValue](#onvalue).
|
298 |
|
299 | Subscribes a given handler function to the observable. Function will be called for each new value (not for errors or stream end).
|
300 | */
|
301 | forEach(f?: Sink<V>): Unsub;
|
302 | /**
|
303 | Groups stream events to new streams by `keyF`. Optional `limitF` can be provided to limit grouped
|
304 | stream life. Stream transformed by `limitF` is passed on if provided. `limitF` gets grouped stream
|
305 | and the original event causing the stream to start as parameters.
|
306 |
|
307 | Calculator for grouped consecutive values until group is cancelled:
|
308 |
|
309 | ```
|
310 | var events = [
|
311 | {id: 1, type: "add", val: 3 },
|
312 | {id: 2, type: "add", val: -1 },
|
313 | {id: 1, type: "add", val: 2 },
|
314 | {id: 2, type: "cancel"},
|
315 | {id: 3, type: "add", val: 2 },
|
316 | {id: 3, type: "cancel"},
|
317 | {id: 1, type: "add", val: 1 },
|
318 | {id: 1, type: "add", val: 2 },
|
319 | {id: 1, type: "cancel"}
|
320 | ]
|
321 |
|
322 | function keyF(event) {
|
323 | return event.id
|
324 | }
|
325 |
|
326 | function limitF(groupedStream, groupStartingEvent) {
|
327 | var cancel = groupedStream.filter(function(x) { return x.type === "cancel"}).take(1)
|
328 | var adds = groupedStream.filter(function(x) { return x.type === "add" })
|
329 | return adds.takeUntil(cancel).map(".val")
|
330 | }
|
331 |
|
332 | Bacon.sequentially(2, events)
|
333 | .groupBy(keyF, limitF)
|
334 | .flatMap(function(groupedStream) {
|
335 | return groupedStream.fold(0, function(acc, x) { return acc + x })
|
336 | })
|
337 | .onValue(function(sum) {
|
338 | console.log(sum)
|
339 |
|
340 | })
|
341 | ```
|
342 |
|
343 | */
|
344 | abstract groupBy<V2 = V>(keyF: Function1<V, string>, limitF?: GroupTransformer<V, V2>): Observable<EventStream<V2>>;
|
345 | /**
|
346 | Pauses and buffers the event stream if last event in valve is truthy.
|
347 | All buffered events are released when valve becomes falsy.
|
348 | */
|
349 | holdWhen(valve: Property<boolean>): EventStream<V>;
|
350 | inspect(): string;
|
351 | /**
|
352 | * Returns the true dependencies of the observable, including the intermediate "hidden" Observables.
|
353 | This method is for Bacon.js internal purposes but could be useful for debugging/analysis tools as well.
|
354 | See also: [deps](#deps)
|
355 | */
|
356 | internalDeps(): any[];
|
357 | /**
|
358 | Takes the last element from the stream. None, if stream is empty.
|
359 |
|
360 |
|
361 | *Note:* `neverEndingStream.last()` creates the stream which doesn't produce any events and never ends.
|
362 | */
|
363 | last(): this;
|
364 | /**
|
365 | Logs each value of the Observable to the console.
|
366 | It optionally takes arguments to pass to console.log() alongside each
|
367 | value. To assist with chaining, it returns the original Observable. Note
|
368 | that as a side-effect, the observable will have a constant listener and
|
369 | will not be garbage-collected. So, use this for debugging only and
|
370 | remove from production code. For example:
|
371 |
|
372 | ```js
|
373 | myStream.log("New event in myStream")
|
374 | ```
|
375 |
|
376 | or just
|
377 |
|
378 | ```js
|
379 | myStream.log()
|
380 | ```
|
381 |
|
382 | */
|
383 | log(...args: any[]): this;
|
384 | /**
|
385 | Maps values using given function, returning a new
|
386 | stream/property. Instead of a function, you can also provide a [Property](property.html),
|
387 | in which case each element in the source stream will be mapped to the current value of
|
388 | the given property.
|
389 | */
|
390 | abstract map<V2>(f: (Function1<V, V2> | Property<V2> | V2)): Observable<V2>;
|
391 | /**
|
392 | Adds an extra [`Next`](next.html) event just before End. The value is created
|
393 | by calling the given function when the source stream ends. Instead of a
|
394 | function, a static value can be used.
|
395 | */
|
396 | mapEnd(f: Function0<V> | V): this;
|
397 | /**
|
398 | Maps errors using given function. More
|
399 | specifically, feeds the "error" field of the error event to the function
|
400 | and produces a [`Next`](next.html) event based on the return value.
|
401 | */
|
402 | mapError(f: Function1<any, V> | V): this;
|
403 | /**
|
404 | Sets the name of the observable. Overrides the default
|
405 | implementation of [`toString`](#tostring) and `inspect`.
|
406 | Returns the same observable, with mutated name.
|
407 | */
|
408 | name(name: string): this;
|
409 | /**
|
410 | Returns a stream/property that inverts boolean values (using `!`)
|
411 | */
|
412 | abstract not(): Observable<boolean>;
|
413 | /**
|
414 | Subscribes a callback to stream end. The function will be called when the stream ends.
|
415 | Just like `subscribe`, this method returns a function for unsubscribing.
|
416 | */
|
417 | onEnd(f?: VoidSink): Unsub;
|
418 | /**
|
419 | Subscribes a handler to error events. The function will be called for each error in the stream.
|
420 | Just like `subscribe`, this method returns a function for unsubscribing.
|
421 | */
|
422 | onError(f?: Sink<any>): Unsub;
|
423 | /**
|
424 | Subscribes a given handler function to the observable. Function will be called for each new value.
|
425 | This is the simplest way to assign a side-effect to an observable. The difference
|
426 | to the `subscribe` method is that the actual stream values are
|
427 | received, instead of [`Event`](event) objects.
|
428 | Just like `subscribe`, this method returns a function for unsubscribing.
|
429 | `stream.onValue` and `property.onValue` behave similarly, except that the latter also
|
430 | pushes the initial value of the property, in case there is one.
|
431 | */
|
432 | onValue(f?: Sink<V>): Unsub;
|
433 | /**
|
434 | Like [`onValue`](#onvalue), but splits the value (assuming its an array) as function arguments to `f`.
|
435 | Only applicable for observables with arrays as values.
|
436 | */
|
437 | onValues(f: Function): Unsub;
|
438 | /** A synonym for [scan](#scan).
|
439 | */
|
440 | reduce<V2>(seed: V2, f: Accumulator<V, V2>): Property<V2>;
|
441 | /**
|
442 | Creates an EventStream by sampling this
|
443 | stream/property value at each event from the `sampler` stream. The result
|
444 | `EventStream` will contain the sampled value at each event in the source
|
445 | stream.
|
446 |
|
447 | @param {Observable<V2>} sampler
|
448 | */
|
449 | /**
|
450 | Creates an EventStream/Property by sampling this
|
451 | stream/property value at each event from the `sampler` stream. The result
|
452 | will contain the sampled value at each event in the source stream.
|
453 |
|
454 | @param {Observable<V2>} sampler
|
455 | */
|
456 | sampledBy(sampler: EventStream<any>): EventStream<V>;
|
457 | sampledBy(sampler: Property<any>): Property<V>;
|
458 | sampledBy(sampler: Observable<any>): Observable<V>;
|
459 | /**
|
460 | Scans stream/property with given seed value and
|
461 | accumulator function, resulting to a Property. For example, you might
|
462 | use zero as seed and a "plus" function as the accumulator to create
|
463 | an "integral" property. Instead of a function, you can also supply a
|
464 | method name such as ".concat", in which case this method is called on
|
465 | the accumulator value and the new stream value is used as argument.
|
466 |
|
467 | Example:
|
468 |
|
469 | ```js
|
470 | var plus = function (a,b) { return a + b }
|
471 | Bacon.sequentially(1, [1,2,3]).scan(0, plus)
|
472 | ```
|
473 |
|
474 | This would result to following elements in the result stream:
|
475 |
|
476 | seed value = 0
|
477 | 0 + 1 = 1
|
478 | 1 + 2 = 3
|
479 | 3 + 3 = 6
|
480 |
|
481 | When applied to a Property as in `r = p.scan(seed, f)`, there's a (hopefully insignificant) catch:
|
482 | The starting value for `r` depends on whether `p` has an
|
483 | initial value when scan is applied. If there's no initial value, this works
|
484 | identically to EventStream.scan: the `seed` will be the initial value of
|
485 | `r`. However, if `r` already has a current/initial value `x`, the
|
486 | seed won't be output as is. Instead, the initial value of `r` will be `f(seed, x)`. This makes sense,
|
487 | because there can only be 1 initial value for a Property at a time.
|
488 | */
|
489 | scan<V2>(seed: V2, f: Accumulator<V, V2>): Property<V2>;
|
490 | /**
|
491 | Skips the first n elements from the stream
|
492 | */
|
493 | skip(count: number): this;
|
494 | /**
|
495 | Drops consecutive equal elements. So,
|
496 | from `[1, 2, 2, 1]` you'd get `[1, 2, 1]`. Uses the `===` operator for equality
|
497 | checking by default. If the isEqual argument is supplied, checks by calling
|
498 | isEqual(oldValue, newValue). For instance, to do a deep comparison,you can
|
499 | use the isEqual function from [underscore.js](http://underscorejs.org/)
|
500 | like `stream.skipDuplicates(_.isEqual)`.
|
501 | */
|
502 | skipDuplicates(isEqual?: Equals<V>): this;
|
503 | /**
|
504 | * Returns a new stream/property which excludes all [Error](error.html) events in the source
|
505 | */
|
506 | skipErrors(): this;
|
507 | /**
|
508 | Skips elements from the source, until a value event
|
509 | appears in the given `starter` stream/property. In other words, starts delivering values
|
510 | from the source after first value appears in `starter`.
|
511 | */
|
512 | skipUntil(starter: Observable<any>): this;
|
513 | /**
|
514 | Skips elements until the given predicate function returns falsy once, and then
|
515 | lets all events pass through. Instead of a predicate you can also pass in a `Property<boolean>` to skip elements
|
516 | while the Property holds a truthy value.
|
517 | */
|
518 | skipWhile(f: PredicateOrProperty<V>): this;
|
519 | /**
|
520 | Returns a Property that represents a
|
521 | "sliding window" into the history of the values of the Observable. The
|
522 | result Property will have a value that is an array containing the last `n`
|
523 | values of the original observable, where `n` is at most the value of the
|
524 | `max` argument, and at least the value of the `min` argument. If the
|
525 | `min` argument is omitted, there's no lower limit of values.
|
526 |
|
527 | For example, if you have a stream `s` with value a sequence 1 - 2 - 3 - 4 - 5, the
|
528 | respective values in `s.slidingWindow(2)` would be [] - [1] - [1,2] -
|
529 | [2,3] - [3,4] - [4,5]. The values of `s.slidingWindow(2,2)`would be
|
530 | [1,2] - [2,3] - [3,4] - [4,5].
|
531 |
|
532 | */
|
533 | slidingWindow(maxValues: number, minValues?: number): Property<V[]>;
|
534 | /**
|
535 | Adds a starting value to the stream/property, i.e. concats a
|
536 | single-element stream containing the single seed value with this stream.
|
537 | */
|
538 | abstract startWith(seed: V): Observable<V>;
|
539 | /**
|
540 | * subscribes given handler function to event stream. Function will receive [event](event.html) objects
|
541 | for all new value, end and error events in the stream.
|
542 | The subscribe() call returns a `unsubscribe` function that you can call to unsubscribe.
|
543 | You can also unsubscribe by returning [`Bacon.noMore`](../globals.html#nomore) from the handler function as a reply
|
544 | to an Event.
|
545 | `stream.subscribe` and `property.subscribe` behave similarly, except that the latter also
|
546 | pushes the initial value of the property, in case there is one.
|
547 |
|
548 | * @param {EventSink<V>} sink the handler function
|
549 | * @returns {Unsub}
|
550 | */
|
551 | subscribe(sink?: EventSink<V>): Unsub;
|
552 | /** @hidden */
|
553 | abstract subscribeInternal(sink: EventSink<V>): Unsub;
|
554 | /**
|
555 | Takes at most n values from the stream and then ends the stream. If the stream has
|
556 | fewer than n values then it is unaffected.
|
557 | Equal to [`Bacon.never()`](../globals.html#never) if `n <= 0`.
|
558 | */
|
559 | take(count: number): this;
|
560 | /**
|
561 | Takes elements from source until a value event appears in the other stream.
|
562 | If other stream ends without value, it is ignored.
|
563 | */
|
564 | takeUntil(stopper: Observable<any>): this;
|
565 | /**
|
566 | Takes while given predicate function holds true, and then ends. Alternatively, you can supply a boolean Property to take elements while the Property holds `true`.
|
567 | */
|
568 | takeWhile(f: PredicateOrProperty<V>): this;
|
569 | /**
|
570 | Throttles stream/property by given amount
|
571 | of milliseconds. Events are emitted with the minimum interval of
|
572 | [`delay`](#observable-delay). The implementation is based on [`stream.bufferWithTime`](#stream-bufferwithtime).
|
573 | Does not affect emitting the initial value of a [`Property`](#property).
|
574 |
|
575 | Example:
|
576 |
|
577 | ```js
|
578 | var throttled = source.throttle(2)
|
579 | ```
|
580 |
|
581 | ```
|
582 | source: asdf----asdf----
|
583 | throttled: --s--f----s--f--
|
584 | ```
|
585 | */
|
586 | throttle(minimumInterval: number): this;
|
587 | abstract toEventStream(): EventStream<V>;
|
588 | /**
|
589 | Returns a Promise which will be resolved with the last event coming from an Observable.
|
590 | The global ES6 promise implementation will be used unless a promise constructor is given.
|
591 | Use a shim if you need to support legacy browsers or platforms.
|
592 | [caniuse promises](http://caniuse.com/#feat=promises).
|
593 |
|
594 | See also [firstToPromise](#firsttopromise).
|
595 | */
|
596 | toPromise(PromiseCtr?: Function): Promise<V>;
|
597 | /**
|
598 | In case of EventStream, creates a Property based on the EventStream.
|
599 |
|
600 | In case of Property, returns the Property itself.
|
601 | */
|
602 | abstract toProperty(): Property<V>;
|
603 | /**
|
604 | *Returns a textual description of the Observable. For instance, `Bacon.once(1).map(function() {}).toString()` would return "Bacon.once(1).map(function)".
|
605 | **/
|
606 | toString(): string;
|
607 | /**
|
608 | * TODO: proper documentation missing
|
609 | Lets you do more custom event handling: you
|
610 | get all events to your function and you can output any number of events
|
611 | and end the stream if you choose. For example, to send an error and end
|
612 | the stream in case a value is below zero:
|
613 |
|
614 | ```js
|
615 | if (Bacon.hasValue(event) && event.value < 0) {
|
616 | sink(new Bacon.Error("Value below zero"));
|
617 | return sink(end());
|
618 | } else {
|
619 | return sink(event);
|
620 | }
|
621 | ```
|
622 |
|
623 | Note that it's important to return the value from `sink` so that
|
624 | the connection to the underlying stream will be closed when no more
|
625 | events are needed.
|
626 | */
|
627 | abstract transform<V2>(transformer: Transformer<V, V2>, desc?: Desc): Observable<V2>;
|
628 | withDesc(desc?: Desc): this;
|
629 | /**
|
630 | Sets the structured description of the observable. The [`toString`](#tostring) and `inspect` methods
|
631 | use this data recursively to create a string representation for the observable. This method
|
632 | is probably useful for Bacon core / library / plugin development only.
|
633 |
|
634 | For example:
|
635 |
|
636 | var src = Bacon.once(1)
|
637 | var obs = src.map(function(x) { return -x })
|
638 | console.log(obs.toString())
|
639 | --> Bacon.once(1).map(function)
|
640 | obs.withDescription(src, "times", -1)
|
641 | console.log(obs.toString())
|
642 | --> Bacon.once(1).times(-1)
|
643 |
|
644 | The method returns the same observable with mutated description.
|
645 |
|
646 | */
|
647 | withDescription(context: any, method: string, ...args: any[]): this;
|
648 | /**
|
649 | Creates an EventStream/Property by sampling a given `samplee`
|
650 | stream/property value at each event from the this stream/property.
|
651 |
|
652 | @param {Observable<V2>} samplee
|
653 | @param f function to select/calculate the result value based on the value in the source stream and the samplee
|
654 |
|
655 | @typeparam V2 type of values in the samplee
|
656 | @typeparam R type of values in the result
|
657 | */
|
658 | abstract withLatestFrom<V2, R>(samplee: Observable<V2>, f: Function2<V, V2, R>): Observable<R>;
|
659 | /**
|
660 | Lets you run a state machine
|
661 | on an observable. Give it an initial state object and a state
|
662 | transformation function that processes each incoming event and
|
663 | returns an array containing the next state and an array of output
|
664 | events. Here's an example where we calculate the total sum of all
|
665 | numbers in the stream and output the value on stream end:
|
666 |
|
667 | ```js
|
668 | Bacon.fromArray([1,2,3])
|
669 | .withStateMachine(0, function(sum, event) {
|
670 | if (event.hasValue)
|
671 | return [sum + event.value, []]
|
672 | else if (event.isEnd)
|
673 | return [undefined, [new Bacon.Next(sum), event]]
|
674 | else
|
675 | return [sum, [event]]
|
676 | })
|
677 | ```
|
678 | @param initState initial state for the state machine
|
679 | @param f the function that defines the state machine
|
680 | @typeparam State type of machine state
|
681 | @typeparam Out type of values to be emitted
|
682 | */
|
683 | abstract withStateMachine<State, Out>(initState: State, f: StateF<V, State, Out>): Observable<Out>;
|
684 | /**
|
685 | Returns an EventStream with elements
|
686 | pair-wise lined up with events from this and the other EventStream or Property.
|
687 | A zipped stream will publish only when it has a value from each
|
688 | source and will only produce values up to when any single source ends.
|
689 |
|
690 | The given function `f` is used to create the result value from value in the two
|
691 | sources. If no function is given, the values are zipped into an array.
|
692 |
|
693 | Be careful not to have too much "drift" between streams. If one stream
|
694 | produces many more values than some other excessive buffering will
|
695 | occur inside the zipped observable.
|
696 |
|
697 | Example 1:
|
698 |
|
699 | ```js
|
700 | var x = Bacon.fromArray([1, 2])
|
701 | var y = Bacon.fromArray([3, 4])
|
702 | x.zip(y, function(x, y) { return x + y })
|
703 |
|
704 | # produces values 4, 6
|
705 | ```
|
706 |
|
707 | See also [`zipWith`](../globals.html#zipwith) and [`zipAsArray`](../globals.html/zipasarray) for zipping more than 2 sources.
|
708 |
|
709 | */
|
710 | zip<V2, R>(other: Observable<V2>, f: Function2<V, V2, R>): EventStream<R>;
|
711 | }
|
712 | /** @hidden */
|
713 | export declare type ObservableConstructor = (description: Desc, subscribe: Subscribe<any>) => Observable<any>;
|
714 | /**
|
715 | A reactive property. Has the concept of "current value".
|
716 | You can create a Property from an EventStream by using either [`toProperty`](eventstream.html#toproperty)
|
717 | or [`scan`](eventstream.html#scan) method. Note: depending on how a Property is created, it may or may not
|
718 | have an initial value. The current value stays as its last value after the stream has ended.
|
719 |
|
720 | Here are the most common ways for creating Properties:
|
721 |
|
722 | - Create a constant property with [constant](../globals.html#constant)
|
723 | - Create a property based on an EventStream with [toProperty](eventstream.html#toproperty)
|
724 | - Scan an EventStream with an accumulator function with [scan](eventstream.html#scan)
|
725 | - Create a state property based on multiple sources using [update](../globals.html#update)
|
726 |
|
727 | @typeparam V Type of the elements/values in the stream/property
|
728 | */
|
729 | export declare class Property<V> extends Observable<V> {
|
730 | constructor(desc: Desc, subscribe: Subscribe<V>, handler?: EventSink<V>);
|
731 | /**
|
732 | Combines properties with the `&&` operator. It produces a new value when either of the Properties change,
|
733 | combining the latest values using `&&`.
|
734 | */
|
735 | and(other: Property<any>): Property<boolean>;
|
736 | /**
|
737 | * creates a stream of changes to the Property. The stream *does not* include
|
738 | an event for the current value of the Property at the time this method was called.
|
739 | */
|
740 | changes(): EventStream<V>;
|
741 | /**
|
742 | Concatenates this property with another stream/properties into one property so that
|
743 | it will deliver events from this property it ends and then deliver
|
744 | events from `other`. This means too that events from `other`,
|
745 | occurring before the end of this property will not be included in the result
|
746 | stream/property.
|
747 | */
|
748 | concat(other: Observable<V>): Property<V>;
|
749 | concat<V2>(other: Observable<V2>): Property<V | V2>;
|
750 | /** @hidden */
|
751 | transformChanges(desc: Desc, f: EventStreamDelay<V>): this;
|
752 | /**
|
753 | For each element in the source stream, spawn a new
|
754 | stream/property using the function `f`. Collect events from each of the spawned
|
755 | streams into the result property. Note that instead of a function, you can provide a
|
756 | stream/property too. Also, the return value of function `f` can be either an
|
757 | `Observable` (stream/property) or a constant value.
|
758 |
|
759 | `stream.flatMap()` can be used conveniently with [`Bacon.once()`](../globals.html#once) and [`Bacon.never()`](../globals.html#never)
|
760 | for converting and filtering at the same time, including only some of the results.
|
761 |
|
762 | Example - converting strings to integers, skipping empty values:
|
763 |
|
764 | ```js
|
765 | stream.flatMap(function(text) {
|
766 | return (text != "") ? parseInt(text) : Bacon.never()
|
767 | })
|
768 | ```
|
769 | */
|
770 | flatMap<V2>(f: SpawnerOrObservable<V, V2>): Property<V2>;
|
771 | /**
|
772 | A [`flatMapWithConcurrencyLimit`](#flatmapwithconcurrencylimit) with limit of 1.
|
773 | */
|
774 | flatMapConcat<V2>(f: SpawnerOrObservable<V, V2>): Property<V2>;
|
775 | /**
|
776 | Like [`flatMap`](#flatmap), but is applied only on [`Error`](error.html) events. Returned values go into the
|
777 | value stream, unless an error event is returned. As an example, one type of error could result in a retry and another just
|
778 | passed through, which can be implemented using flatMapError.
|
779 | */
|
780 | flatMapError<V2>(f: Function1<any, Observable<V2> | EventOrValue<V2>>): Property<V | V2>;
|
781 | flatMapEvent<V2>(f: EventSpawner<V, V2>): Property<V2>;
|
782 | /**
|
783 | Like [`flatMap`](#observable-flatmap), but only spawns a new
|
784 | stream if the previously spawned stream has ended.
|
785 | */
|
786 | flatMapFirst<V2>(f: SpawnerOrObservable<V, V2>): Property<V2>;
|
787 | /**
|
788 | Like [`flatMap`](#flatmap), but instead of including events from
|
789 | all spawned streams, only includes them from the latest spawned stream.
|
790 | You can think this as switching from stream to stream.
|
791 | Note that instead of a function, you can provide a stream/property too.
|
792 | */
|
793 | flatMapLatest<V2>(f: SpawnerOrObservable<V, V2>): Property<V2>;
|
794 | /**
|
795 | A super method of *flatMap* family. It limits the number of open spawned streams and buffers incoming events.
|
796 | [`flatMapConcat`](#flatmapconcat) is `flatMapWithConcurrencyLimit(1)` (only one input active),
|
797 | and [`flatMap`](#flatmap) is `flatMapWithConcurrencyLimit ∞` (all inputs are piped to output).
|
798 | */
|
799 | flatMapWithConcurrencyLimit<V2>(limit: number, f: SpawnerOrObservable<V, V2>): Property<V2>;
|
800 | /**
|
801 | Groups stream events to new streams by `keyF`. Optional `limitF` can be provided to limit grouped
|
802 | stream life. Stream transformed by `limitF` is passed on if provided. `limitF` gets grouped stream
|
803 | and the original event causing the stream to start as parameters.
|
804 |
|
805 | Calculator for grouped consecutive values until group is cancelled:
|
806 |
|
807 | ```
|
808 | var events = [
|
809 | {id: 1, type: "add", val: 3 },
|
810 | {id: 2, type: "add", val: -1 },
|
811 | {id: 1, type: "add", val: 2 },
|
812 | {id: 2, type: "cancel"},
|
813 | {id: 3, type: "add", val: 2 },
|
814 | {id: 3, type: "cancel"},
|
815 | {id: 1, type: "add", val: 1 },
|
816 | {id: 1, type: "add", val: 2 },
|
817 | {id: 1, type: "cancel"}
|
818 | ]
|
819 |
|
820 | function keyF(event) {
|
821 | return event.id
|
822 | }
|
823 |
|
824 | function limitF(groupedStream, groupStartingEvent) {
|
825 | var cancel = groupedStream.filter(function(x) { return x.type === "cancel"}).take(1)
|
826 | var adds = groupedStream.filter(function(x) { return x.type === "add" })
|
827 | return adds.takeUntil(cancel).map(".val")
|
828 | }
|
829 |
|
830 | Bacon.sequentially(2, events)
|
831 | .groupBy(keyF, limitF)
|
832 | .flatMap(function(groupedStream) {
|
833 | return groupedStream.fold(0, function(acc, x) { return acc + x })
|
834 | })
|
835 | .onValue(function(sum) {
|
836 | console.log(sum)
|
837 |
|
838 | })
|
839 | ```
|
840 |
|
841 | */
|
842 | groupBy<V2 = V>(keyF: Function1<V, string>, limitF?: GroupTransformer<V, V2>): Property<EventStream<V2>>;
|
843 | map<V2>(f: Function1<V, V2>): Property<V2>;
|
844 | map<V2>(f: Property<V2> | V2): Property<V2>;
|
845 | /** Returns a Property that inverts the value of this one (using the `!` operator). **/
|
846 | not(): Property<boolean>;
|
847 | /**
|
848 | Combines properties with the `||` operator. It produces a new value when either of the Properties change,
|
849 | combining the latest values using `||`.
|
850 | */
|
851 | or(other: Property<any>): Property<boolean>;
|
852 | /**
|
853 | Creates an EventStream by sampling the
|
854 | property value at given interval (in milliseconds)
|
855 | */
|
856 | sample(interval: number): EventStream<V>;
|
857 | /**
|
858 | Adds an initial "default" value for the
|
859 | Property. If the Property doesn't have an initial value of it's own, the
|
860 | given value will be used as the initial value. If the property has an
|
861 | initial value of its own, the given value will be ignored.
|
862 | */
|
863 | startWith(seed: V): Property<V>;
|
864 | /** @hidden */
|
865 | subscribeInternal(sink?: EventSink<V>): Unsub;
|
866 | /**
|
867 | Creates an EventStream based on this Property. The stream contains also an event for the current
|
868 | value of this Property at the time this method was called.
|
869 | */
|
870 | toEventStream(options?: EventStreamOptions): EventStream<V>;
|
871 | /**
|
872 | Returns the Property itself.
|
873 | */
|
874 | toProperty(): Property<V>;
|
875 | transform<V2>(transformer: Transformer<V, V2>, desc?: Desc): Property<V2>;
|
876 | /**
|
877 | Creates an EventStream/Property by sampling a given `samplee`
|
878 | stream/property value at each event from the this stream/property.
|
879 |
|
880 | @param {Observable<V2>} samplee
|
881 | @param f function to select/calculate the result value based on the value in the source stream and the samplee
|
882 |
|
883 | @typeparam V2 type of values in the samplee
|
884 | @typeparam R type of values in the result
|
885 | */
|
886 | withLatestFrom<V2, R>(samplee: Observable<V2>, f: Function2<V, V2, R>): Property<R>;
|
887 | /**
|
888 | Lets you run a state machine
|
889 | on an observable. Give it an initial state object and a state
|
890 | transformation function that processes each incoming event and
|
891 | returns an array containing the next state and an array of output
|
892 | events. Here's an example where we calculate the total sum of all
|
893 | numbers in the stream and output the value on stream end:
|
894 |
|
895 | ```js
|
896 | Bacon.fromArray([1,2,3])
|
897 | .withStateMachine(0, function(sum, event) {
|
898 | if (event.hasValue)
|
899 | return [sum + event.value, []]
|
900 | else if (event.isEnd)
|
901 | return [undefined, [new Bacon.Next(sum), event]]
|
902 | else
|
903 | return [sum, [event]]
|
904 | })
|
905 | ```
|
906 | @param initState initial state for the state machine
|
907 | @param f the function that defines the state machine
|
908 | @typeparam State type of machine state
|
909 | @typeparam Out type of values to be emitted
|
910 | */
|
911 | withStateMachine<State, Out>(initState: State, f: StateF<V, State, Out>): Property<Out>;
|
912 | }
|
913 | /** @hidden */
|
914 | export declare function isProperty<V>(x: any): x is Property<V>;
|
915 | /** @hidden */
|
916 | export declare const allowSync: {
|
917 | forceAsync: boolean;
|
918 | };
|
919 | /** @hidden */
|
920 | export interface EventStreamOptions {
|
921 | forceAsync: boolean;
|
922 | }
|
923 | /**
|
924 | * EventStream represents a stream of events. It is an Observable object, meaning
|
925 | that you can listen to events in the stream using, for instance, the [`onValue`](#onvalue) method
|
926 | with a callback.
|
927 |
|
928 | To create an EventStream, you'll want to use one of the following factory methods:
|
929 |
|
930 | - From DOM EventTarget or Node.JS EventEmitter objects using [fromEvent](../globals.html#fromevent)
|
931 | - From a Promise using [fromPromise](../globals.html#frompromise)
|
932 | - From an unary callback using [fromCallback](../globals.html#fromcallback)
|
933 | - From a Node.js style callback using [fromNodeCallback](../globals.html#fromnodecallback)
|
934 | - From RxJs or Kefir observables using [fromESObservable](../globals.html#fromesobservable)
|
935 | - By polling a synchronous function using [fromPoll](../globals.html#fromPoll)
|
936 | - Emit a single event instantly using [once](../globals.html#once)
|
937 | - Emit a single event with a delay [later](../globals.html#later)
|
938 | - Emit the same event indefinitely using [interval](../globals.html#interval)
|
939 | - Emit an array of events instantly [fromArray](../globals.html#fromarray)
|
940 | - Emit an array of events with a delay [sequentially](../globals.html#sequentially)
|
941 | - Emit an array of events repeatedly with a delay [repeatedly](../globals.html#repeatedly)
|
942 | - Use a generator function to be called repeatedly [repeat](../globals.html#repeat)
|
943 | - Create a stream that never emits an event, ending immediately [never](../globals.html#never)
|
944 | - Create a stream that never emits an event, ending with a delay [silence](../globals.html#silence)
|
945 | - Create stream using a custom binder function [fromBinder](../globals.html#frombinder)
|
946 | - Wrap jQuery events using [asEventStream](../globals.html#_)
|
947 |
|
948 |
|
949 | @typeparam V Type of the elements/values in the stream/property
|
950 |
|
951 | */
|
952 | export declare class EventStream<V> extends Observable<V> {
|
953 | /** @hidden */
|
954 | _isEventStream: boolean;
|
955 | constructor(desc: Desc, subscribe: Subscribe<V>, handler?: EventSink<V>, options?: EventStreamOptions);
|
956 | /**
|
957 | Buffers stream events with given delay.
|
958 | The buffer is flushed at most once in the given interval. So, if your input
|
959 | contains [1,2,3,4,5,6,7], then you might get two events containing [1,2,3,4]
|
960 | and [5,6,7] respectively, given that the flush occurs between numbers 4 and 5.
|
961 |
|
962 | Also works with a given "defer-function" instead
|
963 | of a delay. Here's a simple example, which is equivalent to
|
964 | stream.bufferWithTime(10):
|
965 |
|
966 | ```js
|
967 | stream.bufferWithTime(function(f) { setTimeout(f, 10) })
|
968 | ```
|
969 |
|
970 | * @param delay buffer duration in milliseconds
|
971 | */
|
972 | bufferWithTime(delay: number | DelayFunction): EventStream<V[]>;
|
973 | /**
|
974 | Buffers stream events with given count.
|
975 | The buffer is flushed when it contains the given number of elements or the source stream ends.
|
976 |
|
977 | So, if you buffer a stream of `[1, 2, 3, 4, 5]` with count `2`, you'll get output
|
978 | events with values `[1, 2]`, `[3, 4]` and `[5]`.
|
979 |
|
980 | * @param {number} count
|
981 | */
|
982 | bufferWithCount(count: number): EventStream<V[]>;
|
983 | /**
|
984 | Buffers stream events and
|
985 | flushes when either the buffer contains the given number elements or the
|
986 | given amount of milliseconds has passed since last buffered event.
|
987 |
|
988 | * @param {number | DelayFunction} delay in milliseconds or as a function
|
989 | * @param {number} count maximum buffer size
|
990 | */
|
991 | bufferWithTimeOrCount(delay?: number | DelayFunction, count?: number): EventStream<V[]>;
|
992 | changes(): EventStream<V>;
|
993 | /**
|
994 | Concatenates two streams/properties into one stream/property so that
|
995 | it will deliver events from this observable until it ends and then deliver
|
996 | events from `other`. This means too that events from `other`,
|
997 | occurring before the end of this observable will not be included in the result
|
998 | stream/property.
|
999 | */
|
1000 | concat(other: Observable<V>, options?: EventStreamOptions): EventStream<V>;
|
1001 | concat<V2>(other: Observable<V2>, options?: EventStreamOptions): EventStream<V | V2>;
|
1002 | /** @hidden */
|
1003 | transformChanges(desc: Desc, f: EventStreamDelay<V>): this;
|
1004 | /**
|
1005 | For each element in the source stream, spawn a new
|
1006 | stream/property using the function `f`. Collect events from each of the spawned
|
1007 | streams into the result stream/property. Note that instead of a function, you can provide a
|
1008 | stream/property too. Also, the return value of function `f` can be either an
|
1009 | `Observable` (stream/property) or a constant value.
|
1010 |
|
1011 | `stream.flatMap()` can be used conveniently with [`Bacon.once()`](../globals.html#once) and [`Bacon.never()`](../globals.html#never)
|
1012 | for converting and filtering at the same time, including only some of the results.
|
1013 |
|
1014 | Example - converting strings to integers, skipping empty values:
|
1015 |
|
1016 | ```js
|
1017 | stream.flatMap(function(text) {
|
1018 | return (text != "") ? parseInt(text) : Bacon.never()
|
1019 | })
|
1020 | ```
|
1021 | */
|
1022 | flatMap<V2>(f: SpawnerOrObservable<V, V2>): EventStream<V2>;
|
1023 | /**
|
1024 | A [`flatMapWithConcurrencyLimit`](#flatmapwithconcurrencylimit) with limit of 1.
|
1025 | */
|
1026 | flatMapConcat<V2>(f: SpawnerOrObservable<V, V2>): EventStream<V2>;
|
1027 | /**
|
1028 | Like [`flatMap`](#flatmap), but is applied only on [`Error`](error.html) events. Returned values go into the
|
1029 | value stream, unless an error event is returned. As an example, one type of error could result in a retry and another just
|
1030 | passed through, which can be implemented using flatMapError.
|
1031 | */
|
1032 | flatMapError<V2>(f: Function1<any, Observable<V2> | EventOrValue<V2>>): EventStream<V | V2>;
|
1033 | /**
|
1034 | Like [`flatMap`](#observable-flatmap), but only spawns a new
|
1035 | stream if the previously spawned stream has ended.
|
1036 | */
|
1037 | flatMapFirst<V2>(f: SpawnerOrObservable<V, V2>): EventStream<V2>;
|
1038 | /**
|
1039 | Like [`flatMap`](#flatmap), but instead of including events from
|
1040 | all spawned streams, only includes them from the latest spawned stream.
|
1041 | You can think this as switching from stream to stream.
|
1042 | Note that instead of a function, you can provide a stream/property too.
|
1043 | */
|
1044 | flatMapLatest<V2>(f: SpawnerOrObservable<V, V2>): EventStream<V2>;
|
1045 | /**
|
1046 | A super method of *flatMap* family. It limits the number of open spawned streams and buffers incoming events.
|
1047 | [`flatMapConcat`](#flatmapconcat) is `flatMapWithConcurrencyLimit(1)` (only one input active),
|
1048 | and [`flatMap`](#flatmap) is `flatMapWithConcurrencyLimit ∞` (all inputs are piped to output).
|
1049 | */
|
1050 | flatMapWithConcurrencyLimit<V2>(limit: number, f: SpawnerOrObservable<V, V2>): EventStream<V2>;
|
1051 | flatMapEvent<V2>(f: EventSpawner<V, V2>): EventStream<V2>;
|
1052 | /**
|
1053 | Scans stream with given seed value and accumulator function, resulting to a Property.
|
1054 | Difference to [`scan`](#scan) is that the function `f` can return an [`EventStream`](eventstream.html) or a [`Property`](property.html) instead
|
1055 | of a pure value, meaning that you can use [`flatScan`](#flatscan) for asynchronous updates of state. It serializes
|
1056 | updates so that that the next update will be queued until the previous one has completed.
|
1057 |
|
1058 | * @param seed initial value to start with
|
1059 | * @param f transition function from previous state and new value to next state
|
1060 | * @typeparam V2 state and result type
|
1061 | */
|
1062 | flatScan<V2>(seed: V2, f: Function2<V2, V, Observable<V2>>): Property<V2>;
|
1063 | /**
|
1064 | Groups stream events to new streams by `keyF`. Optional `limitF` can be provided to limit grouped
|
1065 | stream life. Stream transformed by `limitF` is passed on if provided. `limitF` gets grouped stream
|
1066 | and the original event causing the stream to start as parameters.
|
1067 |
|
1068 | Calculator for grouped consecutive values until group is cancelled:
|
1069 |
|
1070 | ```
|
1071 | var events = [
|
1072 | {id: 1, type: "add", val: 3 },
|
1073 | {id: 2, type: "add", val: -1 },
|
1074 | {id: 1, type: "add", val: 2 },
|
1075 | {id: 2, type: "cancel"},
|
1076 | {id: 3, type: "add", val: 2 },
|
1077 | {id: 3, type: "cancel"},
|
1078 | {id: 1, type: "add", val: 1 },
|
1079 | {id: 1, type: "add", val: 2 },
|
1080 | {id: 1, type: "cancel"}
|
1081 | ]
|
1082 |
|
1083 | function keyF(event) {
|
1084 | return event.id
|
1085 | }
|
1086 |
|
1087 | function limitF(groupedStream, groupStartingEvent) {
|
1088 | var cancel = groupedStream.filter(function(x) { return x.type === "cancel"}).take(1)
|
1089 | var adds = groupedStream.filter(function(x) { return x.type === "add" })
|
1090 | return adds.takeUntil(cancel).map(".val")
|
1091 | }
|
1092 |
|
1093 | Bacon.sequentially(2, events)
|
1094 | .groupBy(keyF, limitF)
|
1095 | .flatMap(function(groupedStream) {
|
1096 | return groupedStream.fold(0, function(acc, x) { return acc + x })
|
1097 | })
|
1098 | .onValue(function(sum) {
|
1099 | console.log(sum)
|
1100 |
|
1101 | })
|
1102 | ```
|
1103 |
|
1104 | */
|
1105 | groupBy<V2 = V>(keyF: Function1<V, string>, limitF?: GroupTransformer<V, V2>): EventStream<EventStream<V2>>;
|
1106 | map<V2>(f: Function1<V, V2>): EventStream<V2>;
|
1107 | map<V2>(f: Property<V2> | V2): EventStream<V2>;
|
1108 | /**
|
1109 | Merges two streams into one stream that delivers events from both
|
1110 | */
|
1111 | merge(other: EventStream<V>): EventStream<V>;
|
1112 | merge<V2>(other: EventStream<V2>): EventStream<V | V2>;
|
1113 | /**
|
1114 | Returns a stream/property that inverts boolean values (using `!`)
|
1115 | */
|
1116 | not(): EventStream<boolean>;
|
1117 | /**
|
1118 | Adds a starting value to the stream/property, i.e. concats a
|
1119 | single-element stream containing the single seed value with this stream.
|
1120 | */
|
1121 | startWith(seed: V): EventStream<V>;
|
1122 | /** @hidden */
|
1123 | subscribeInternal(sink?: EventSink<V>): Unsub;
|
1124 | /**
|
1125 | * Returns this stream.
|
1126 | */
|
1127 | toEventStream(): this;
|
1128 | /**
|
1129 | Creates a Property based on the
|
1130 | EventStream.
|
1131 |
|
1132 | Without arguments, you'll get a Property without an initial value.
|
1133 | The Property will get its first actual value from the stream, and after that it'll
|
1134 | always have a current value.
|
1135 |
|
1136 | You can also give an initial value that will be used as the current value until
|
1137 | the first value comes from the stream.
|
1138 | */
|
1139 | toProperty(initValue?: V): Property<V>;
|
1140 | transform<V2>(transformer: Transformer<V, V2>, desc?: Desc): EventStream<V2>;
|
1141 | /**
|
1142 | Creates an EventStream/Property by sampling a given `samplee`
|
1143 | stream/property value at each event from the this stream/property.
|
1144 |
|
1145 | @param {Observable<V2>} samplee
|
1146 | @param f function to select/calculate the result value based on the value in the source stream and the samplee
|
1147 |
|
1148 | @typeparam V2 type of values in the samplee
|
1149 | @typeparam R type of values in the result
|
1150 | */
|
1151 | withLatestFrom<V2, R>(samplee: Observable<V2>, f: Function2<V, V2, R>): EventStream<R>;
|
1152 | /**
|
1153 | Lets you run a state machine
|
1154 | on an observable. Give it an initial state object and a state
|
1155 | transformation function that processes each incoming event and
|
1156 | returns an array containing the next state and an array of output
|
1157 | events. Here's an example where we calculate the total sum of all
|
1158 | numbers in the stream and output the value on stream end:
|
1159 |
|
1160 | ```js
|
1161 | Bacon.fromArray([1,2,3])
|
1162 | .withStateMachine(0, function(sum, event) {
|
1163 | if (event.hasValue)
|
1164 | return [sum + event.value, []]
|
1165 | else if (event.isEnd)
|
1166 | return [undefined, [new Bacon.Next(sum), event]]
|
1167 | else
|
1168 | return [sum, [event]]
|
1169 | })
|
1170 | ```
|
1171 | @param initState initial state for the state machine
|
1172 | @param f the function that defines the state machine
|
1173 | @typeparam State type of machine state
|
1174 | @typeparam Out type of values to be emitted
|
1175 | */
|
1176 | withStateMachine<State, Out>(initState: State, f: StateF<V, State, Out>): EventStream<Out>;
|
1177 | }
|
1178 | /** @hidden */
|
1179 | export declare function newEventStream<V>(description: Desc, subscribe: Subscribe<V>): EventStream<V>;
|
1180 | /** @hidden */
|
1181 | export declare function newEventStreamAllowSync<V>(description: Desc, subscribe: Subscribe<V>): EventStream<V>;
|
1182 | export default Observable;
|
1183 |
|
\ | No newline at end of file |