-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding a draft of Subject class #108
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package rx.subjects; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import junit.framework.Assert; | ||
|
||
import org.junit.Test; | ||
|
||
import rx.observables.Notification; | ||
import rx.observables.Observable; | ||
import rx.observables.Observer; | ||
import rx.observables.Subscription; | ||
import rx.util.AtomicObservableSubscription; | ||
import rx.util.AtomicObserver; | ||
import rx.util.functions.Action1; | ||
import rx.util.functions.Func1; | ||
|
||
public class Subject<T> extends Observable<T> implements Observer<T> { | ||
public static <T> Subject<T> create() { | ||
final ConcurrentHashMap<Subscription, Observer<T>> observers = new ConcurrentHashMap<Subscription, Observer<T>>(); | ||
|
||
Func1<Observer<T>, Subscription> onSubscribe = new Func1<Observer<T>, Subscription>() { | ||
@Override | ||
public Subscription call(Observer<T> observer) { | ||
final AtomicObservableSubscription subscription = new AtomicObservableSubscription(); | ||
|
||
subscription.wrap(new Subscription() { | ||
@Override | ||
public void unsubscribe() { | ||
// on unsubscribe remove it from the map of outbound observers to notify | ||
observers.remove(subscription); | ||
} | ||
}); | ||
|
||
// on subscribe add it to the map of outbound observers to notify | ||
observers.put(subscription, new AtomicObserver<T>(observer, subscription)); | ||
return subscription; | ||
} | ||
}; | ||
|
||
return new Subject<T>(onSubscribe, observers); | ||
} | ||
|
||
private final ConcurrentHashMap<Subscription, Observer<T>> observers; | ||
|
||
protected Subject(Func1<Observer<T>, Subscription> onSubscribe, ConcurrentHashMap<Subscription, Observer<T>> observers) { | ||
super(onSubscribe); | ||
this.observers = observers; | ||
} | ||
|
||
@Override | ||
public void onCompleted() { | ||
for (Observer<T> observer : observers.values()) { | ||
observer.onCompleted(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Exception e) { | ||
for (Observer<T> observer : observers.values()) { | ||
observer.onError(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void onNext(T args) { | ||
for (Observer<T> observer : observers.values()) { | ||
observer.onNext(args); | ||
} | ||
} | ||
|
||
public static class UnitTest { | ||
@Test | ||
public void test() { | ||
Subject<Integer> subject = Subject.<Integer> create(); | ||
final AtomicReference<List<Notification<String>>> actualRef = new AtomicReference<List<Notification<String>>>(); | ||
|
||
Observable<List<Notification<Integer>>> wNotificationsList = subject.materialize().toList(); | ||
wNotificationsList.subscribe(new Action1<List<Notification<String>>>() { | ||
@Override | ||
public void call(List<Notification<String>> actual) { | ||
actualRef.set(actual); | ||
} | ||
}); | ||
|
||
Subscription sub = Observable.create(new Func1<Observer<Integer>, Subscription>() { | ||
@Override | ||
public Subscription call(final Observer<Integer> observer) { | ||
final AtomicBoolean stop = new AtomicBoolean(false); | ||
new Thread() { | ||
@Override | ||
public void run() { | ||
int i = 1; | ||
while (!stop.get()) { | ||
observer.onNext(i++); | ||
} | ||
observer.onCompleted(); | ||
} | ||
}.start(); | ||
return new Subscription() { | ||
@Override | ||
public void unsubscribe() { | ||
stop.set(true); | ||
} | ||
}; | ||
} | ||
}).subscribe(subject); | ||
// the subject has received an onComplete from the first subscribe because | ||
// it is synchronous and the next subscribe won't do anything. | ||
Observable.toObservable(-1, -2, -3).subscribe(subject); | ||
|
||
List<Notification<Integer>> expected = new ArrayList<Notification<Integer>>(); | ||
expected.add(new Notification<Integer>(-1)); | ||
expected.add(new Notification<Integer>(-2)); | ||
expected.add(new Notification<Integer>(-3)); | ||
expected.add(new Notification<Integer>()); | ||
Assert.assertTrue(actualRef.get().containsAll(expected)); | ||
|
||
sub.unsubscribe(); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -65,13 +65,13 @@ public final class AtomicObserverSingleThreaded<T> implements Observer<T> { | |
* compositional by its very nature. | ||
*/ | ||
|
||
private final Observer<T> Observer; | ||
private final Observer<T> observer; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for fixing this stuff ... no idea how I ended up with capitalized variable names in so many places. I've been cleaning them up whenever I see them! |
||
private final AtomicObservableSubscription subscription; | ||
private volatile boolean finishRequested = false; | ||
private volatile boolean finished = false; | ||
|
||
public AtomicObserverSingleThreaded(Observer<T> Observer, AtomicObservableSubscription subscription) { | ||
this.Observer = Observer; | ||
this.observer = Observer; | ||
this.subscription = subscription; | ||
} | ||
|
||
|
@@ -86,7 +86,7 @@ public void onNext(T arg) { | |
// if we're already stopped, or a finish request has been received, we won't allow further onNext requests | ||
return; | ||
} | ||
Observer.onNext(arg); | ||
observer.onNext(arg); | ||
} | ||
} | ||
|
||
|
@@ -101,7 +101,7 @@ public void onError(Exception e) { | |
if (finished || subscription.isUnsubscribed()) { | ||
return; | ||
} | ||
Observer.onError(e); | ||
observer.onError(e); | ||
finished = true; | ||
} | ||
} | ||
|
@@ -117,7 +117,7 @@ public void onCompleted() { | |
if (finished || subscription.isUnsubscribed()) { | ||
return; | ||
} | ||
Observer.onCompleted(); | ||
observer.onCompleted(); | ||
finished = true; | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The MSDN documentation suggests it should accept Observer and Observable as arguments:
http://msdn.microsoft.com/en-us/library/hh211824(v=vs.103).aspx