You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Part 3 - Taming the sequence/5. Time-shifted sequences.md
+55Lines changed: 55 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -181,6 +181,61 @@ Output
181
181
182
182
We've created an `Observable.interval` that signals the opening of a new buffer every 250ms. Because observables created with `interval` do not immediately emit a value, the first values were lost. For the closing of a buffer, we provided a lambda function that took every value emitted by our buffer-opening observable. The values generated by `interval` are the natural progression 1,2,3... but that doesn't matter, because we discarded that value. Such an example would be too complicated. Instead, we just created an observable that waits 200ms and then emits a single value. That means that each buffer last exactly 200ms, similarily to when buffering by time.
183
183
184
+
### takeLastBuffer
185
+
186
+
We have already seen the [takeLast](/Part%202%20-%20Sequence%20Basics/2.%20Reducing%20a%20sequence.md#skiplast-and-takelast) operator, which returns the last N number of items. Internally, `takeLast` needs to buffer items and re-emits them when the source sequence ends. The `takeLastBuffer` operator returns the last elements as one buffer.
`takeLastBuffer` by time will emit, as a buffer, the items that were received during the specified timespan. The timespan has the specified length and ends at the end of the source sequence.
209
+
210
+
```java
211
+
Observable.interval(100, TimeUnit.MILLISECONDS)
212
+
.take(5)
213
+
.takeLastBuffer(200, TimeUnit.MILLISECONDS)
214
+
.subscribe(System.out::println);
215
+
```
216
+
Output
217
+
```
218
+
[2, 3, 4]
219
+
```
220
+
221
+
#### By count and time
222
+
223
+
The buffer emitted by this overload of `takeLastBuffer` will contain items that were emitted over the specified timespan before the end. If this window contains more than the specified number of items, the buffer will contain the last `N` items.
224
+
225
+
```java
226
+
Observable.interval(100, TimeUnit.MILLISECONDS)
227
+
.take(5)
228
+
.takeLastBuffer(2, 200, TimeUnit.MILLISECONDS)
229
+
.subscribe(System.out::println);
230
+
```
231
+
Output
232
+
```
233
+
[3, 4]
234
+
```
235
+
236
+
As we saw in the previous example, the last 200ms include three values. With `.takeLastBuffer(2, 200, TimeUnit.MILLISECONDS)` we specified that we want values from the last 200ms, but no more than 2 values. For that reason, we only get the last two values.
237
+
238
+
184
239
## Delay
185
240
186
241
`delay`, as the name suggests, will postpone the emission of values for a specified amount of time. The are two ways to do that. One is by storing values until you are ready to emit them. The other is to delay the subscription to observable.
0 commit comments