-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make sure when taking just the first row, that we unsubscribe immedia…
…tely so that we don't hold ono to a connection
- Loading branch information
Showing
7 changed files
with
90 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
core/src/main/java/foundation/stack/datamill/db/impl/UnsubscribeOnCompletedOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package foundation.stack.datamill.db.impl; | ||
|
||
import rx.Observable; | ||
import rx.Subscriber; | ||
import rx.exceptions.Exceptions; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** | ||
* @author Ravi Chodavarapu (rchodava@gmail.com) | ||
*/ | ||
public class UnsubscribeOnCompletedOperator<T> implements Observable.Operator<T, T> { | ||
private final AtomicBoolean completed = new AtomicBoolean(); | ||
|
||
@Override | ||
public Subscriber<? super T> call(Subscriber<? super T> subscriber) { | ||
return new Subscriber<T>() { | ||
@Override | ||
public void onCompleted() { | ||
this.unsubscribe(); | ||
|
||
if (!subscriber.isUnsubscribed()) { | ||
if (completed.compareAndSet(false, true)) { | ||
subscriber.onCompleted(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
this.unsubscribe(); | ||
|
||
if (!subscriber.isUnsubscribed()) { | ||
if (completed.compareAndSet(false, true)) { | ||
subscriber.onError(e); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void onNext(T t) { | ||
this.unsubscribe(); | ||
|
||
if (!subscriber.isUnsubscribed()) { | ||
if (completed.compareAndSet(false, true)) { | ||
try { | ||
subscriber.onNext(t); | ||
subscriber.onCompleted(); | ||
} catch (Throwable e) { | ||
Exceptions.throwIfFatal(e); | ||
subscriber.onError(e); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
core/src/test/java/foundation/stack/datamill/db/impl/UnsubscribeOnCompletedOperatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package foundation.stack.datamill.db.impl; | ||
|
||
import org.junit.Test; | ||
import rx.Observable; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* @author Ravi Chodavarapu (rchodava@gmail.com) | ||
*/ | ||
public class UnsubscribeOnCompletedOperatorTest { | ||
@Test | ||
public void operator() { | ||
int[] unsubscribed = new int[] { 1 }; | ||
int result = Observable.just(1, 2, 3) | ||
.doOnUnsubscribe(() -> unsubscribed[0] = unsubscribed[0] + 10) | ||
.lift(new UnsubscribeOnCompletedOperator<>()) | ||
.doOnUnsubscribe(() -> unsubscribed[0] = unsubscribed[0] * 2) | ||
.toBlocking() | ||
.lastOrDefault(0); | ||
assertEquals(1, result); | ||
assertEquals(22, unsubscribed[0]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters