| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.internal.operators.observable; |
| 15 |
|
| 16 |
import java.util.*; |
| 17 |
import java.util.concurrent.TimeUnit; |
| 18 |
import java.util.concurrent.atomic.*; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.Observable; |
| 21 |
import io.reactivex.rxjava3.core.Observer; |
| 22 |
import io.reactivex.rxjava3.core.Scheduler; |
| 23 |
import io.reactivex.rxjava3.core.Scheduler.Worker; |
| 24 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 25 |
import io.reactivex.rxjava3.internal.disposables.*; |
| 26 |
import io.reactivex.rxjava3.internal.queue.MpscLinkedQueue; |
| 27 |
import io.reactivex.rxjava3.operators.SimplePlainQueue; |
| 28 |
import io.reactivex.rxjava3.subjects.UnicastSubject; |
| 29 |
|
| 30 |
public final class ObservableWindowTimed<T> extends AbstractObservableWithUpstream<T, Observable<T>> { |
| 31 |
final long timespan; |
| 32 |
final long timeskip; |
| 33 |
final TimeUnit unit; |
| 34 |
final Scheduler scheduler; |
| 35 |
final long maxSize; |
| 36 |
final int bufferSize; |
| 37 |
final boolean restartTimerOnMaxSize; |
| 38 |
|
| 39 |
public ObservableWindowTimed(Observable<T> source, |
| 40 |
long timespan, long timeskip, TimeUnit unit, Scheduler scheduler, long maxSize, |
| 41 |
int bufferSize, boolean restartTimerOnMaxSize) { |
| 42 |
super(source); |
| 43 |
this.timespan = timespan; |
| 44 |
this.timeskip = timeskip; |
| 45 |
this.unit = unit; |
| 46 |
this.scheduler = scheduler; |
| 47 |
this.maxSize = maxSize; |
| 48 |
this.bufferSize = bufferSize; |
| 49 |
this.restartTimerOnMaxSize = restartTimerOnMaxSize; |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
protected void subscribeActual(Observer<? super Observable<T>> downstream) { |
| 54 |
if (timespan == timeskip) { |
| 55 |
if (maxSize == Long.MAX_VALUE) { |
| 56 |
source.subscribe(new WindowExactUnboundedObserver<>( |
| 57 |
downstream, |
| 58 |
timespan, unit, scheduler, bufferSize)); |
| 59 |
return; |
| 60 |
} |
| 61 |
source.subscribe(new WindowExactBoundedObserver<>( |
| 62 |
downstream, |
| 63 |
timespan, unit, scheduler, |
| 64 |
bufferSize, maxSize, restartTimerOnMaxSize)); |
| 65 |
return; |
| 66 |
} |
| 67 |
source.subscribe(new WindowSkipObserver<>(downstream, |
| 68 |
timespan, timeskip, unit, scheduler.createWorker(), bufferSize)); |
| 69 |
} |
| 70 |
|
| 71 |
abstract static class AbstractWindowObserver<T> |
| 72 |
extends AtomicInteger |
| 73 |
implements Observer<T>, Disposable { |
| 74 |
private static final long serialVersionUID = 5724293814035355511L; |
| 75 |
|
| 76 |
final Observer<? super Observable<T>> downstream; |
| 77 |
|
| 78 |
final SimplePlainQueue<Object> queue; |
| 79 |
|
| 80 |
final long timespan; |
| 81 |
final TimeUnit unit; |
| 82 |
final int bufferSize; |
| 83 |
|
| 84 |
long emitted; |
| 85 |
|
| 86 |
volatile boolean done; |
| 87 |
Throwable error; |
| 88 |
|
| 89 |
Disposable upstream; |
| 90 |
|
| 91 |
final AtomicBoolean downstreamCancelled; |
| 92 |
|
| 93 |
volatile boolean upstreamCancelled; |
| 94 |
|
| 95 |
final AtomicInteger windowCount; |
| 96 |
|
| 97 |
AbstractWindowObserver(Observer<? super Observable<T>> downstream, long timespan, TimeUnit unit, int bufferSize) { |
| 98 |
this.downstream = downstream; |
| 99 |
this.queue = new MpscLinkedQueue<>(); |
| 100 |
this.timespan = timespan; |
| 101 |
this.unit = unit; |
| 102 |
this.bufferSize = bufferSize; |
| 103 |
this.downstreamCancelled = new AtomicBoolean(); |
| 104 |
this.windowCount = new AtomicInteger(1); |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public final void onSubscribe(Disposable d) { |
| 109 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 110 |
this.upstream = d; |
| 111 |
|
| 112 |
downstream.onSubscribe(this); |
| 113 |
|
| 114 |
createFirstWindow(); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
abstract void createFirstWindow(); |
| 119 |
|
| 120 |
@Override |
| 121 |
public final void onNext(T t) { |
| 122 |
queue.offer(t); |
| 123 |
drain(); |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public final void onError(Throwable t) { |
| 128 |
error = t; |
| 129 |
done = true; |
| 130 |
drain(); |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public final void onComplete() { |
| 135 |
done = true; |
| 136 |
drain(); |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public final void dispose() { |
| 141 |
if (downstreamCancelled.compareAndSet(false, true)) { |
| 142 |
windowDone(); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public final boolean isDisposed() { |
| 148 |
return downstreamCancelled.get(); |
| 149 |
} |
| 150 |
|
| 151 |
final void windowDone() { |
| 152 |
if (windowCount.decrementAndGet() == 0) { |
| 153 |
cleanupResources(); |
| 154 |
upstream.dispose(); |
| 155 |
upstreamCancelled = true; |
| 156 |
drain(); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
abstract void cleanupResources(); |
| 161 |
|
| 162 |
abstract void drain(); |
| 163 |
} |
| 164 |
|
| 165 |
static final class WindowExactUnboundedObserver<T> |
| 166 |
extends AbstractWindowObserver<T> |
| 167 |
implements Runnable { |
| 168 |
|
| 169 |
private static final long serialVersionUID = 1155822639622580836L; |
| 170 |
|
| 171 |
final Scheduler scheduler; |
| 172 |
|
| 173 |
UnicastSubject<T> window; |
| 174 |
|
| 175 |
final SequentialDisposable timer; |
| 176 |
|
| 177 |
static final Object NEXT_WINDOW = new Object(); |
| 178 |
|
| 179 |
final Runnable windowRunnable; |
| 180 |
|
| 181 |
WindowExactUnboundedObserver(Observer<? super Observable<T>> actual, long timespan, TimeUnit unit, |
| 182 |
Scheduler scheduler, int bufferSize) { |
| 183 |
super(actual, timespan, unit, bufferSize); |
| 184 |
this.scheduler = scheduler; |
| 185 |
this.timer = new SequentialDisposable(); |
| 186 |
this.windowRunnable = new WindowRunnable(); |
| 187 |
} |
| 188 |
|
| 189 |
@Override |
| 190 |
void createFirstWindow() { |
| 191 |
if (!downstreamCancelled.get()) { |
| 192 |
windowCount.getAndIncrement(); |
| 193 |
window = UnicastSubject.create(bufferSize, windowRunnable); |
| 194 |
|
| 195 |
emitted = 1; |
| 196 |
|
| 197 |
ObservableWindowSubscribeIntercept<T> intercept = new ObservableWindowSubscribeIntercept<>(window); |
| 198 |
downstream.onNext(intercept); |
| 199 |
|
| 200 |
timer.replace(scheduler.schedulePeriodicallyDirect(this, timespan, timespan, unit)); |
| 201 |
|
| 202 |
if (intercept.tryAbandon()) { |
| 203 |
window.onComplete(); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
@Override |
| 209 |
public void run() { |
| 210 |
queue.offer(NEXT_WINDOW); |
| 211 |
drain(); |
| 212 |
} |
| 213 |
|
| 214 |
@Override |
| 215 |
void drain() { |
| 216 |
if (getAndIncrement() != 0) { |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
final SimplePlainQueue<Object> queue = this.queue; |
| 221 |
final Observer<? super Observable<T>> downstream = this.downstream; |
| 222 |
UnicastSubject<T> window = this.window; |
| 223 |
|
| 224 |
int missed = 1; |
| 225 |
for (;;) { |
| 226 |
|
| 227 |
if (upstreamCancelled) { |
| 228 |
queue.clear(); |
| 229 |
window = null; |
| 230 |
this.window = null; |
| 231 |
} else { |
| 232 |
boolean isDone = done; |
| 233 |
Object o = queue.poll(); |
| 234 |
boolean isEmpty = o == null; |
| 235 |
|
| 236 |
if (isDone && isEmpty) { |
| 237 |
Throwable ex = error; |
| 238 |
if (ex != null) { |
| 239 |
if (window != null) { |
| 240 |
window.onError(ex); |
| 241 |
} |
| 242 |
downstream.onError(ex); |
| 243 |
} else { |
| 244 |
if (window != null) { |
| 245 |
window.onComplete(); |
| 246 |
} |
| 247 |
downstream.onComplete(); |
| 248 |
} |
| 249 |
cleanupResources(); |
| 250 |
upstreamCancelled = true; |
| 251 |
continue; |
| 252 |
} |
| 253 |
else if (!isEmpty) { |
| 254 |
|
| 255 |
if (o == NEXT_WINDOW) { |
| 256 |
if (window != null) { |
| 257 |
window.onComplete(); |
| 258 |
window = null; |
| 259 |
this.window = null; |
| 260 |
} |
| 261 |
if (downstreamCancelled.get()) { |
| 262 |
timer.dispose(); |
| 263 |
} else { |
| 264 |
emitted++; |
| 265 |
|
| 266 |
windowCount.getAndIncrement(); |
| 267 |
window = UnicastSubject.create(bufferSize, windowRunnable); |
| 268 |
this.window = window; |
| 269 |
|
| 270 |
ObservableWindowSubscribeIntercept<T> intercept = new ObservableWindowSubscribeIntercept<>(window); |
| 271 |
downstream.onNext(intercept); |
| 272 |
|
| 273 |
if (intercept.tryAbandon()) { |
| 274 |
window.onComplete(); |
| 275 |
} |
| 276 |
} |
| 277 |
} else if (window != null) { |
| 278 |
@SuppressWarnings("unchecked") |
| 279 |
T item = (T)o; |
| 280 |
window.onNext(item); |
| 281 |
} |
| 282 |
|
| 283 |
continue; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
missed = addAndGet(-missed); |
| 288 |
if (missed == 0) { |
| 289 |
break; |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
@Override |
| 295 |
void cleanupResources() { |
| 296 |
timer.dispose(); |
| 297 |
} |
| 298 |
|
| 299 |
final class WindowRunnable implements Runnable { |
| 300 |
@Override |
| 301 |
public void run() { |
| 302 |
windowDone(); |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
static final class WindowExactBoundedObserver<T> |
| 308 |
extends AbstractWindowObserver<T> |
| 309 |
implements Runnable { |
| 310 |
private static final long serialVersionUID = -6130475889925953722L; |
| 311 |
|
| 312 |
final Scheduler scheduler; |
| 313 |
final boolean restartTimerOnMaxSize; |
| 314 |
final long maxSize; |
| 315 |
final Scheduler.Worker worker; |
| 316 |
|
| 317 |
long count; |
| 318 |
|
| 319 |
UnicastSubject<T> window; |
| 320 |
|
| 321 |
final SequentialDisposable timer; |
| 322 |
|
| 323 |
WindowExactBoundedObserver( |
| 324 |
Observer<? super Observable<T>> actual, |
| 325 |
long timespan, TimeUnit unit, Scheduler scheduler, |
| 326 |
int bufferSize, long maxSize, boolean restartTimerOnMaxSize) { |
| 327 |
super(actual, timespan, unit, bufferSize); |
| 328 |
this.scheduler = scheduler; |
| 329 |
this.maxSize = maxSize; |
| 330 |
this.restartTimerOnMaxSize = restartTimerOnMaxSize; |
| 331 |
if (restartTimerOnMaxSize) { |
| 332 |
worker = scheduler.createWorker(); |
| 333 |
} else { |
| 334 |
worker = null; |
| 335 |
} |
| 336 |
this.timer = new SequentialDisposable(); |
| 337 |
} |
| 338 |
|
| 339 |
@Override |
| 340 |
void createFirstWindow() { |
| 341 |
if (!downstreamCancelled.get()) { |
| 342 |
emitted = 1; |
| 343 |
|
| 344 |
windowCount.getAndIncrement(); |
| 345 |
window = UnicastSubject.create(bufferSize, this); |
| 346 |
|
| 347 |
ObservableWindowSubscribeIntercept<T> intercept = new ObservableWindowSubscribeIntercept<>(window); |
| 348 |
downstream.onNext(intercept); |
| 349 |
|
| 350 |
Runnable boundaryTask = new WindowBoundaryRunnable(this, 1L); |
| 351 |
if (restartTimerOnMaxSize) { |
| 352 |
timer.replace(worker.schedulePeriodically(boundaryTask, timespan, timespan, unit)); |
| 353 |
} else { |
| 354 |
timer.replace(scheduler.schedulePeriodicallyDirect(boundaryTask, timespan, timespan, unit)); |
| 355 |
} |
| 356 |
|
| 357 |
if (intercept.tryAbandon()) { |
| 358 |
window.onComplete(); |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
@Override |
| 364 |
public void run() { |
| 365 |
windowDone(); |
| 366 |
} |
| 367 |
|
| 368 |
@Override |
| 369 |
void cleanupResources() { |
| 370 |
timer.dispose(); |
| 371 |
Worker w = worker; |
| 372 |
if (w != null) { |
| 373 |
w.dispose(); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
void boundary(WindowBoundaryRunnable sender) { |
| 378 |
queue.offer(sender); |
| 379 |
drain(); |
| 380 |
} |
| 381 |
|
| 382 |
@Override |
| 383 |
void drain() { |
| 384 |
if (getAndIncrement() != 0) { |
| 385 |
return; |
| 386 |
} |
| 387 |
|
| 388 |
int missed = 1; |
| 389 |
final SimplePlainQueue<Object> queue = this.queue; |
| 390 |
final Observer<? super Observable<T>> downstream = this.downstream; |
| 391 |
UnicastSubject<T> window = this.window; |
| 392 |
|
| 393 |
for (;;) { |
| 394 |
|
| 395 |
if (upstreamCancelled) { |
| 396 |
queue.clear(); |
| 397 |
window = null; |
| 398 |
this.window = null; |
| 399 |
} else { |
| 400 |
|
| 401 |
boolean isDone = done; |
| 402 |
Object o = queue.poll(); |
| 403 |
boolean isEmpty = o == null; |
| 404 |
|
| 405 |
if (isDone && isEmpty) { |
| 406 |
Throwable ex = error; |
| 407 |
if (ex != null) { |
| 408 |
if (window != null) { |
| 409 |
window.onError(ex); |
| 410 |
} |
| 411 |
downstream.onError(ex); |
| 412 |
} else { |
| 413 |
if (window != null) { |
| 414 |
window.onComplete(); |
| 415 |
} |
| 416 |
downstream.onComplete(); |
| 417 |
} |
| 418 |
cleanupResources(); |
| 419 |
upstreamCancelled = true; |
| 420 |
continue; |
| 421 |
} else if (!isEmpty) { |
| 422 |
if (o instanceof WindowBoundaryRunnable) { |
| 423 |
WindowBoundaryRunnable boundary = (WindowBoundaryRunnable) o; |
| 424 |
if (boundary.index == emitted || !restartTimerOnMaxSize) { |
| 425 |
this.count = 0; |
| 426 |
window = createNewWindow(window); |
| 427 |
} |
| 428 |
} else if (window != null) { |
| 429 |
@SuppressWarnings("unchecked") |
| 430 |
T item = (T)o; |
| 431 |
window.onNext(item); |
| 432 |
|
| 433 |
long count = this.count + 1; |
| 434 |
if (count == maxSize) { |
| 435 |
this.count = 0; |
| 436 |
window = createNewWindow(window); |
| 437 |
} else { |
| 438 |
this.count = count; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
continue; |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
missed = addAndGet(-missed); |
| 447 |
if (missed == 0) { |
| 448 |
break; |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
UnicastSubject<T> createNewWindow(UnicastSubject<T> window) { |
| 454 |
if (window != null) { |
| 455 |
window.onComplete(); |
| 456 |
window = null; |
| 457 |
} |
| 458 |
|
| 459 |
if (downstreamCancelled.get()) { |
| 460 |
cleanupResources(); |
| 461 |
} else { |
| 462 |
long emitted = this.emitted; |
| 463 |
this.emitted = ++emitted; |
| 464 |
|
| 465 |
windowCount.getAndIncrement(); |
| 466 |
window = UnicastSubject.create(bufferSize, this); |
| 467 |
this.window = window; |
| 468 |
|
| 469 |
ObservableWindowSubscribeIntercept<T> intercept = new ObservableWindowSubscribeIntercept<>(window); |
| 470 |
downstream.onNext(intercept); |
| 471 |
|
| 472 |
if (restartTimerOnMaxSize) { |
| 473 |
timer.update(worker.schedulePeriodically(new WindowBoundaryRunnable(this, emitted), timespan, timespan, unit)); |
| 474 |
} |
| 475 |
|
| 476 |
if (intercept.tryAbandon()) { |
| 477 |
window.onComplete(); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
return window; |
| 482 |
} |
| 483 |
|
| 484 |
static final class WindowBoundaryRunnable implements Runnable { |
| 485 |
|
| 486 |
final WindowExactBoundedObserver<?> parent; |
| 487 |
|
| 488 |
final long index; |
| 489 |
|
| 490 |
WindowBoundaryRunnable(WindowExactBoundedObserver<?> parent, long index) { |
| 491 |
this.parent = parent; |
| 492 |
this.index = index; |
| 493 |
} |
| 494 |
|
| 495 |
@Override |
| 496 |
public void run() { |
| 497 |
parent.boundary(this); |
| 498 |
} |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
static final class WindowSkipObserver<T> |
| 503 |
extends AbstractWindowObserver<T> |
| 504 |
implements Runnable { |
| 505 |
private static final long serialVersionUID = -7852870764194095894L; |
| 506 |
|
| 507 |
final long timeskip; |
| 508 |
final Scheduler.Worker worker; |
| 509 |
|
| 510 |
final List<UnicastSubject<T>> windows; |
| 511 |
|
| 512 |
WindowSkipObserver(Observer<? super Observable<T>> actual, |
| 513 |
long timespan, long timeskip, TimeUnit unit, |
| 514 |
Worker worker, int bufferSize) { |
| 515 |
super(actual, timespan, unit, bufferSize); |
| 516 |
this.timeskip = timeskip; |
| 517 |
this.worker = worker; |
| 518 |
this.windows = new LinkedList<>(); |
| 519 |
} |
| 520 |
|
| 521 |
@Override |
| 522 |
void createFirstWindow() { |
| 523 |
if (!downstreamCancelled.get()) { |
| 524 |
emitted = 1; |
| 525 |
|
| 526 |
windowCount.getAndIncrement(); |
| 527 |
UnicastSubject<T> window = UnicastSubject.create(bufferSize, this); |
| 528 |
windows.add(window); |
| 529 |
|
| 530 |
ObservableWindowSubscribeIntercept<T> intercept = new ObservableWindowSubscribeIntercept<>(window); |
| 531 |
downstream.onNext(intercept); |
| 532 |
|
| 533 |
worker.schedule(new WindowBoundaryRunnable(this, false), timespan, unit); |
| 534 |
worker.schedulePeriodically(new WindowBoundaryRunnable(this, true), timeskip, timeskip, unit); |
| 535 |
|
| 536 |
if (intercept.tryAbandon()) { |
| 537 |
window.onComplete(); |
| 538 |
windows.remove(window); |
| 539 |
} |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
@Override |
| 544 |
void cleanupResources() { |
| 545 |
worker.dispose(); |
| 546 |
} |
| 547 |
|
| 548 |
@Override |
| 549 |
void drain() { |
| 550 |
if (getAndIncrement() != 0) { |
| 551 |
return; |
| 552 |
} |
| 553 |
|
| 554 |
int missed = 1; |
| 555 |
final SimplePlainQueue<Object> queue = this.queue; |
| 556 |
final Observer<? super Observable<T>> downstream = this.downstream; |
| 557 |
final List<UnicastSubject<T>> windows = this.windows; |
| 558 |
|
| 559 |
for (;;) { |
| 560 |
if (upstreamCancelled) { |
| 561 |
queue.clear(); |
| 562 |
windows.clear(); |
| 563 |
} else { |
| 564 |
boolean isDone = done; |
| 565 |
Object o = queue.poll(); |
| 566 |
boolean isEmpty = o == null; |
| 567 |
|
| 568 |
if (isDone && isEmpty) { |
| 569 |
Throwable ex = error; |
| 570 |
if (ex != null) { |
| 571 |
for (UnicastSubject<T> window : windows) { |
| 572 |
window.onError(ex); |
| 573 |
} |
| 574 |
downstream.onError(ex); |
| 575 |
} else { |
| 576 |
for (UnicastSubject<T> window : windows) { |
| 577 |
window.onComplete(); |
| 578 |
} |
| 579 |
downstream.onComplete(); |
| 580 |
} |
| 581 |
cleanupResources(); |
| 582 |
upstreamCancelled = true; |
| 583 |
continue; |
| 584 |
} else if (!isEmpty) { |
| 585 |
if (o == WINDOW_OPEN) { |
| 586 |
if (!downstreamCancelled.get()) { |
| 587 |
long emitted = this.emitted; |
| 588 |
this.emitted = ++emitted; |
| 589 |
|
| 590 |
windowCount.getAndIncrement(); |
| 591 |
UnicastSubject<T> window = UnicastSubject.create(bufferSize, this); |
| 592 |
windows.add(window); |
| 593 |
|
| 594 |
ObservableWindowSubscribeIntercept<T> intercept = new ObservableWindowSubscribeIntercept<>(window); |
| 595 |
downstream.onNext(intercept); |
| 596 |
|
| 597 |
worker.schedule(new WindowBoundaryRunnable(this, false), timespan, unit); |
| 598 |
|
| 599 |
if (intercept.tryAbandon()) { |
| 600 |
window.onComplete(); |
| 601 |
} |
| 602 |
} |
| 603 |
} else if (o == WINDOW_CLOSE) { |
| 604 |
if (!windows.isEmpty()) { |
| 605 |
windows.remove(0).onComplete(); |
| 606 |
} |
| 607 |
} else { |
| 608 |
@SuppressWarnings("unchecked") |
| 609 |
T item = (T)o; |
| 610 |
for (UnicastSubject<T> window : windows) { |
| 611 |
window.onNext(item); |
| 612 |
} |
| 613 |
} |
| 614 |
continue; |
| 615 |
} |
| 616 |
} |
| 617 |
missed = addAndGet(-missed); |
| 618 |
if (missed == 0) { |
| 619 |
break; |
| 620 |
} |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
@Override |
| 625 |
public void run() { |
| 626 |
windowDone(); |
| 627 |
} |
| 628 |
|
| 629 |
void boundary(boolean isOpen) { |
| 630 |
queue.offer(isOpen ? WINDOW_OPEN : WINDOW_CLOSE); |
| 631 |
drain(); |
| 632 |
} |
| 633 |
|
| 634 |
static final Object WINDOW_OPEN = new Object(); |
| 635 |
static final Object WINDOW_CLOSE = new Object(); |
| 636 |
|
| 637 |
static final class WindowBoundaryRunnable implements Runnable { |
| 638 |
|
| 639 |
final WindowSkipObserver<?> parent; |
| 640 |
|
| 641 |
final boolean isOpen; |
| 642 |
|
| 643 |
WindowBoundaryRunnable(WindowSkipObserver<?> parent, boolean isOpen) { |
| 644 |
this.parent = parent; |
| 645 |
this.isOpen = isOpen; |
| 646 |
} |
| 647 |
|
| 648 |
@Override |
| 649 |
public void run() { |
| 650 |
parent.boundary(isOpen); |
| 651 |
} |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
} |