|
1 | 1 | package rx.plugins;
|
2 | 2 |
|
3 |
| -import rx.Notification; |
| 3 | +import rx.Observable; |
4 | 4 | import rx.Observable.OnSubscribe;
|
5 | 5 | import rx.Observable.Operator;
|
6 | 6 | import rx.Observer;
|
7 | 7 | import rx.observers.SafeSubscriber;
|
8 | 8 | import rx.operators.DebugSubscriber;
|
9 | 9 |
|
10 |
| -public class DebugNotification<T> { |
| 10 | +public class DebugNotification<T, C> { |
11 | 11 | public static enum Kind {
|
12 | 12 | OnNext, OnError, OnCompleted, Subscribe, Unsubscribe
|
13 | 13 | }
|
14 | 14 |
|
15 |
| - private final OnSubscribe<T> source; |
| 15 | + private final Observable<? extends T> source; |
| 16 | + private final OnSubscribe<T> sourceFunc; |
16 | 17 | private final Operator<? extends T, ?> from;
|
17 | 18 | private final Kind kind;
|
18 |
| - private final Notification<T> notification; |
19 | 19 | private final Operator<?, ? super T> to;
|
20 |
| - private final long nanoTime; |
21 |
| - private final long threadId; |
22 |
| - private Observer o; |
| 20 | + private final Throwable throwable; |
| 21 | + private final T value; |
| 22 | + private final Observer observer; |
23 | 23 |
|
24 |
| - public static <T> DebugNotification<T> createSubscribe(Observer<? super T> o, OnSubscribe<T> source) { |
| 24 | + public static <T, C> DebugNotification<T, C> createSubscribe(Observer<? super T> o, Observable<? extends T> source, OnSubscribe<T> sourceFunc) { |
25 | 25 | Operator<?, ? super T> to = null;
|
26 | 26 | Operator<? extends T, ?> from = null;
|
| 27 | + if (o instanceof SafeSubscriber) { |
| 28 | + o = ((SafeSubscriber) o).getActual(); |
| 29 | + } |
27 | 30 | if (o instanceof DebugSubscriber) {
|
28 |
| - to = ((DebugSubscriber<T>) o).getTo(); |
29 |
| - from = ((DebugSubscriber<T>) o).getFrom(); |
| 31 | + to = ((DebugSubscriber<T, C>) o).getTo(); |
| 32 | + from = ((DebugSubscriber<T, C>) o).getFrom(); |
30 | 33 | o = ((DebugSubscriber) o).getActual();
|
31 | 34 | }
|
32 |
| - return new DebugNotification<T>(o, from, Kind.Subscribe, null, to, source); |
| 35 | + if (sourceFunc instanceof DebugHook.OnCreateWrapper) { |
| 36 | + sourceFunc = ((DebugHook.OnCreateWrapper) sourceFunc).getActual(); |
| 37 | + } |
| 38 | + return new DebugNotification<T, C>(o, from, Kind.Subscribe, null, null, to, source, sourceFunc); |
33 | 39 | }
|
34 | 40 |
|
35 |
| - public static <T> DebugNotification<T> createOnNext(Observer<? super T> o, Operator<? extends T, ?> from, T t, Operator<?, ? super T> to) { |
36 |
| - return new DebugNotification<T>(o, from, Kind.OnNext, Notification.createOnNext(t), to, null); |
| 41 | + public static <T, C> DebugNotification<T, C> createOnNext(Observer<? super T> o, Operator<? extends T, ?> from, T t, Operator<?, ? super T> to) { |
| 42 | + return new DebugNotification<T, C>(o, from, Kind.OnNext, t, null, to, null, null); |
37 | 43 | }
|
38 | 44 |
|
39 |
| - public static <T> DebugNotification<T> createOnError(Observer<? super T> o, Operator<? extends T, ?> from, Throwable e, Operator<?, ? super T> to) { |
40 |
| - return new DebugNotification<T>(o, from, Kind.OnError, Notification.<T> createOnError(e), to, null); |
| 45 | + public static <T, C> DebugNotification<T, C> createOnError(Observer<? super T> o, Operator<? extends T, ?> from, Throwable e, Operator<?, ? super T> to) { |
| 46 | + return new DebugNotification<T, C>(o, from, Kind.OnError, null, e, to, null, null); |
41 | 47 | }
|
42 | 48 |
|
43 |
| - public static <T> DebugNotification<T> createOnCompleted(Observer<? super T> o, Operator<? extends T, ?> from, Operator<?, ? super T> to) { |
44 |
| - return new DebugNotification<T>(o, from, Kind.OnCompleted, Notification.<T> createOnCompleted(), to, null); |
| 49 | + public static <T, C> DebugNotification<T, C> createOnCompleted(Observer<? super T> o, Operator<? extends T, ?> from, Operator<?, ? super T> to) { |
| 50 | + return new DebugNotification<T, C>(o, from, Kind.OnCompleted, null, null, to, null, null); |
45 | 51 | }
|
46 | 52 |
|
47 |
| - public static <T> DebugNotification<T> createUnsubscribe(Observer<? super T> o, Operator<? extends T, ?> from, Operator<?, ? super T> to) { |
48 |
| - return new DebugNotification<T>(o, from, Kind.Unsubscribe, null, to, null); |
| 53 | + public static <T, C> DebugNotification<T, C> createUnsubscribe(Observer<? super T> o, Operator<? extends T, ?> from, Operator<?, ? super T> to) { |
| 54 | + return new DebugNotification<T, C>(o, from, Kind.Unsubscribe, null, null, to, null, null); |
49 | 55 | }
|
50 | 56 |
|
51 |
| - private DebugNotification(Observer o, Operator<? extends T, ?> from, Kind kind, Notification<T> notification, Operator<?, ? super T> to, OnSubscribe<T> source) { |
52 |
| - this.o = (o instanceof SafeSubscriber) ? ((SafeSubscriber) o).getActual() : o; |
| 57 | + private DebugNotification(Observer o, Operator<? extends T, ?> from, Kind kind, T value, Throwable throwable, Operator<?, ? super T> to, Observable<? extends T> source, OnSubscribe<T> sourceFunc) { |
| 58 | + this.observer = (o instanceof SafeSubscriber) ? ((SafeSubscriber) o).getActual() : o; |
53 | 59 | this.from = from;
|
54 | 60 | this.kind = kind;
|
55 |
| - this.notification = notification; |
| 61 | + this.value = value; |
| 62 | + this.throwable = throwable; |
56 | 63 | this.to = to;
|
57 | 64 | this.source = source;
|
58 |
| - this.nanoTime = System.nanoTime(); |
59 |
| - this.threadId = Thread.currentThread().getId(); |
| 65 | + this.sourceFunc = sourceFunc; |
| 66 | + } |
| 67 | + |
| 68 | + public Observer getObserver() { |
| 69 | + return observer; |
60 | 70 | }
|
61 | 71 |
|
62 | 72 | public Operator<? extends T, ?> getFrom() {
|
63 | 73 | return from;
|
64 | 74 | }
|
65 | 75 |
|
66 |
| - public Notification<T> getNotification() { |
67 |
| - return notification; |
| 76 | + public T getValue() { |
| 77 | + return value; |
68 | 78 | }
|
69 | 79 |
|
70 |
| - public Operator<?, ? super T> getTo() { |
71 |
| - return to; |
| 80 | + public Throwable getThrowable() { |
| 81 | + return throwable; |
72 | 82 | }
|
73 | 83 |
|
74 |
| - public long getNanoTime() { |
75 |
| - return nanoTime; |
76 |
| - } |
77 |
| - |
78 |
| - public long getThreadId() { |
79 |
| - return threadId; |
| 84 | + public Operator<?, ? super T> getTo() { |
| 85 | + return to; |
80 | 86 | }
|
81 | 87 |
|
82 | 88 | public Kind getKind() {
|
83 | 89 | return kind;
|
84 | 90 | }
|
| 91 | + |
| 92 | + public Observable<? extends T> getSource() { |
| 93 | + return source; |
| 94 | + } |
| 95 | + |
| 96 | + public OnSubscribe<T> getSourceFunc() { |
| 97 | + return sourceFunc; |
| 98 | + } |
85 | 99 |
|
86 | 100 | @Override
|
| 101 | + /** |
| 102 | + * Does a very bad job of making JSON like string. |
| 103 | + */ |
87 | 104 | public String toString() {
|
88 | 105 | final StringBuilder s = new StringBuilder("{");
|
89 |
| - s.append(" \"nano\": ").append(nanoTime); |
90 |
| - s.append(", \"thread\": ").append(threadId); |
91 |
| - s.append(", \"observer\": \"").append(o.getClass().getName()).append("@").append(Integer.toHexString(o.hashCode())).append("\""); |
| 106 | + s.append("\"observer\": \"").append(observer.getClass().getName()).append("@").append(Integer.toHexString(observer.hashCode())).append("\""); |
92 | 107 | s.append(", \"type\": \"").append(kind).append("\"");
|
93 |
| - if (notification != null) { |
94 |
| - if (notification.hasValue()) |
95 |
| - s.append(", \"value\": \"").append(notification.getValue()).append("\""); |
96 |
| - if (notification.hasThrowable()) |
97 |
| - s.append(", \"exception\": \"").append(notification.getThrowable().getMessage().replace("\\", "\\\\").replace("\"", "\\\"")).append("\""); |
98 |
| - } |
| 108 | + if (kind == Kind.OnNext) |
| 109 | + // not json safe |
| 110 | + s.append(", \"value\": \"").append(value).append("\""); |
| 111 | + if (kind == Kind.OnError) |
| 112 | + s.append(", \"exception\": \"").append(throwable.getMessage().replace("\\", "\\\\").replace("\"", "\\\"")).append("\""); |
99 | 113 | if (source != null)
|
100 | 114 | s.append(", \"source\": \"").append(source.getClass().getName()).append("@").append(Integer.toHexString(source.hashCode())).append("\"");
|
| 115 | + if (sourceFunc != null) |
| 116 | + s.append(", \"sourceFunc\": \"").append(sourceFunc.getClass().getName()).append("@").append(Integer.toHexString(sourceFunc.hashCode())).append("\""); |
101 | 117 | if (from != null)
|
102 | 118 | s.append(", \"from\": \"").append(from.getClass().getName()).append("@").append(Integer.toHexString(from.hashCode())).append("\"");
|
103 | 119 | if (to != null)
|
|
0 commit comments