This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
/
Observer.cs
524 lines (439 loc) · 18.8 KB
/
Observer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
using System;
using System.Threading;
using UniRx.InternalUtil;
namespace UniRx
{
public static class Observer
{
internal static IObserver<T> CreateSubscribeObserver<T>(Action<T> onNext, Action<Exception> onError, Action onCompleted)
{
// need compare for avoid iOS AOT
if (onNext == Stubs<T>.Ignore)
{
return new Subscribe_<T>(onError, onCompleted);
}
else
{
return new Subscribe<T>(onNext, onError, onCompleted);
}
}
internal static IObserver<T> CreateSubscribeWithStateObserver<T, TState>(TState state, Action<T, TState> onNext, Action<Exception, TState> onError, Action<TState> onCompleted)
{
return new Subscribe<T, TState>(state, onNext, onError, onCompleted);
}
internal static IObserver<T> CreateSubscribeWithState2Observer<T, TState1, TState2>(TState1 state1, TState2 state2, Action<T, TState1, TState2> onNext, Action<Exception, TState1, TState2> onError, Action<TState1, TState2> onCompleted)
{
return new Subscribe<T, TState1, TState2>(state1, state2, onNext, onError, onCompleted);
}
internal static IObserver<T> CreateSubscribeWithState3Observer<T, TState1, TState2, TState3>(TState1 state1, TState2 state2, TState3 state3, Action<T, TState1, TState2, TState3> onNext, Action<Exception, TState1, TState2, TState3> onError, Action<TState1, TState2, TState3> onCompleted)
{
return new Subscribe<T, TState1, TState2, TState3>(state1, state2, state3, onNext, onError, onCompleted);
}
public static IObserver<T> Create<T>(Action<T> onNext)
{
return Create<T>(onNext, UniRx.Stubs.Throw, UniRx.Stubs.Nop);
}
public static IObserver<T> Create<T>(Action<T> onNext, Action<Exception> onError)
{
return Create<T>(onNext, onError, UniRx.Stubs.Nop);
}
public static IObserver<T> Create<T>(Action<T> onNext, Action onCompleted)
{
return Create<T>(onNext, UniRx.Stubs.Throw, onCompleted);
}
public static IObserver<T> Create<T>(Action<T> onNext, Action<Exception> onError, Action onCompleted)
{
// need compare for avoid iOS AOT
if (onNext == Stubs<T>.Ignore)
{
return new EmptyOnNextAnonymousObserver<T>(onError, onCompleted);
}
else
{
return new AnonymousObserver<T>(onNext, onError, onCompleted);
}
}
public static IObserver<T> CreateAutoDetachObserver<T>(IObserver<T> observer, IDisposable disposable)
{
return new AutoDetachObserver<T>(observer, disposable);
}
class AnonymousObserver<T> : IObserver<T>
{
readonly Action<T> onNext;
readonly Action<Exception> onError;
readonly Action onCompleted;
int isStopped = 0;
public AnonymousObserver(Action<T> onNext, Action<Exception> onError, Action onCompleted)
{
this.onNext = onNext;
this.onError = onError;
this.onCompleted = onCompleted;
}
public void OnNext(T value)
{
if (isStopped == 0)
{
onNext(value);
}
}
public void OnError(Exception error)
{
if (Interlocked.Increment(ref isStopped) == 1)
{
onError(error);
}
}
public void OnCompleted()
{
if (Interlocked.Increment(ref isStopped) == 1)
{
onCompleted();
}
}
}
class EmptyOnNextAnonymousObserver<T> : IObserver<T>
{
readonly Action<Exception> onError;
readonly Action onCompleted;
int isStopped = 0;
public EmptyOnNextAnonymousObserver(Action<Exception> onError, Action onCompleted)
{
this.onError = onError;
this.onCompleted = onCompleted;
}
public void OnNext(T value)
{
}
public void OnError(Exception error)
{
if (Interlocked.Increment(ref isStopped) == 1)
{
onError(error);
}
}
public void OnCompleted()
{
if (Interlocked.Increment(ref isStopped) == 1)
{
onCompleted();
}
}
}
// same as AnonymousObserver...
class Subscribe<T> : IObserver<T>
{
readonly Action<T> onNext;
readonly Action<Exception> onError;
readonly Action onCompleted;
int isStopped = 0;
public Subscribe(Action<T> onNext, Action<Exception> onError, Action onCompleted)
{
this.onNext = onNext;
this.onError = onError;
this.onCompleted = onCompleted;
}
public void OnNext(T value)
{
if (isStopped == 0)
{
onNext(value);
}
}
public void OnError(Exception error)
{
if (Interlocked.Increment(ref isStopped) == 1)
{
onError(error);
}
}
public void OnCompleted()
{
if (Interlocked.Increment(ref isStopped) == 1)
{
onCompleted();
}
}
}
// same as EmptyOnNextAnonymousObserver...
class Subscribe_<T> : IObserver<T>
{
readonly Action<Exception> onError;
readonly Action onCompleted;
int isStopped = 0;
public Subscribe_(Action<Exception> onError, Action onCompleted)
{
this.onError = onError;
this.onCompleted = onCompleted;
}
public void OnNext(T value)
{
}
public void OnError(Exception error)
{
if (Interlocked.Increment(ref isStopped) == 1)
{
onError(error);
}
}
public void OnCompleted()
{
if (Interlocked.Increment(ref isStopped) == 1)
{
onCompleted();
}
}
}
// with state
class Subscribe<T, TState> : IObserver<T>
{
readonly TState state;
readonly Action<T, TState> onNext;
readonly Action<Exception, TState> onError;
readonly Action<TState> onCompleted;
int isStopped = 0;
public Subscribe(TState state, Action<T, TState> onNext, Action<Exception, TState> onError, Action<TState> onCompleted)
{
this.state = state;
this.onNext = onNext;
this.onError = onError;
this.onCompleted = onCompleted;
}
public void OnNext(T value)
{
if (isStopped == 0)
{
onNext(value, state);
}
}
public void OnError(Exception error)
{
if (Interlocked.Increment(ref isStopped) == 1)
{
onError(error, state);
}
}
public void OnCompleted()
{
if (Interlocked.Increment(ref isStopped) == 1)
{
onCompleted(state);
}
}
}
class Subscribe<T, TState1, TState2> : IObserver<T>
{
readonly TState1 state1;
readonly TState2 state2;
readonly Action<T, TState1, TState2> onNext;
readonly Action<Exception, TState1, TState2> onError;
readonly Action<TState1, TState2> onCompleted;
int isStopped = 0;
public Subscribe(TState1 state1, TState2 state2, Action<T, TState1, TState2> onNext, Action<Exception, TState1, TState2> onError, Action<TState1, TState2> onCompleted)
{
this.state1 = state1;
this.state2 = state2;
this.onNext = onNext;
this.onError = onError;
this.onCompleted = onCompleted;
}
public void OnNext(T value)
{
if (isStopped == 0)
{
onNext(value, state1, state2);
}
}
public void OnError(Exception error)
{
if (Interlocked.Increment(ref isStopped) == 1)
{
onError(error, state1, state2);
}
}
public void OnCompleted()
{
if (Interlocked.Increment(ref isStopped) == 1)
{
onCompleted(state1, state2);
}
}
}
class Subscribe<T, TState1, TState2, TState3> : IObserver<T>
{
readonly TState1 state1;
readonly TState2 state2;
readonly TState3 state3;
readonly Action<T, TState1, TState2, TState3> onNext;
readonly Action<Exception, TState1, TState2, TState3> onError;
readonly Action<TState1, TState2, TState3> onCompleted;
int isStopped = 0;
public Subscribe(TState1 state1, TState2 state2, TState3 state3, Action<T, TState1, TState2, TState3> onNext, Action<Exception, TState1, TState2, TState3> onError, Action<TState1, TState2, TState3> onCompleted)
{
this.state1 = state1;
this.state2 = state2;
this.state3 = state3;
this.onNext = onNext;
this.onError = onError;
this.onCompleted = onCompleted;
}
public void OnNext(T value)
{
if (isStopped == 0)
{
onNext(value, state1, state2, state3);
}
}
public void OnError(Exception error)
{
if (Interlocked.Increment(ref isStopped) == 1)
{
onError(error, state1, state2, state3);
}
}
public void OnCompleted()
{
if (Interlocked.Increment(ref isStopped) == 1)
{
onCompleted(state1, state2, state3);
}
}
}
class AutoDetachObserver<T> : UniRx.Operators.OperatorObserverBase<T, T>
{
public AutoDetachObserver(IObserver<T> observer, IDisposable cancel)
: base(observer, cancel)
{
}
public override void OnNext(T value)
{
try
{
base.observer.OnNext(value);
}
catch
{
Dispose();
throw;
}
}
public override void OnError(Exception error)
{
try { observer.OnError(error); }
finally { Dispose(); }
}
public override void OnCompleted()
{
try { observer.OnCompleted(); }
finally { Dispose(); }
}
}
}
public static partial class ObserverExtensions
{
public static IObserver<T> Synchronize<T>(this IObserver<T> observer)
{
return new UniRx.Operators.SynchronizedObserver<T>(observer, new object());
}
public static IObserver<T> Synchronize<T>(this IObserver<T> observer, object gate)
{
return new UniRx.Operators.SynchronizedObserver<T>(observer, gate);
}
}
public static partial class ObservableExtensions
{
public static IDisposable Subscribe<T>(this IObservable<T> source)
{
return source.Subscribe(UniRx.InternalUtil.ThrowObserver<T>.Instance);
}
public static IDisposable Subscribe<T>(this IObservable<T> source, Action<T> onNext)
{
return source.Subscribe(Observer.CreateSubscribeObserver(onNext, Stubs.Throw, Stubs.Nop));
}
public static IDisposable Subscribe<T>(this IObservable<T> source, Action<T> onNext, Action<Exception> onError)
{
return source.Subscribe(Observer.CreateSubscribeObserver(onNext, onError, Stubs.Nop));
}
public static IDisposable Subscribe<T>(this IObservable<T> source, Action<T> onNext, Action onCompleted)
{
return source.Subscribe(Observer.CreateSubscribeObserver(onNext, Stubs.Throw, onCompleted));
}
public static IDisposable Subscribe<T>(this IObservable<T> source, Action<T> onNext, Action<Exception> onError, Action onCompleted)
{
return source.Subscribe(Observer.CreateSubscribeObserver(onNext, onError, onCompleted));
}
public static IDisposable SubscribeWithState<T, TState>(this IObservable<T> source, TState state, Action<T, TState> onNext)
{
return source.Subscribe(Observer.CreateSubscribeWithStateObserver(state, onNext, Stubs<TState>.Throw, Stubs<TState>.Ignore));
}
public static IDisposable SubscribeWithState<T, TState>(this IObservable<T> source, TState state, Action<T, TState> onNext, Action<Exception, TState> onError)
{
return source.Subscribe(Observer.CreateSubscribeWithStateObserver(state, onNext, onError, Stubs<TState>.Ignore));
}
public static IDisposable SubscribeWithState<T, TState>(this IObservable<T> source, TState state, Action<T, TState> onNext, Action<TState> onCompleted)
{
return source.Subscribe(Observer.CreateSubscribeWithStateObserver(state, onNext, Stubs<TState>.Throw, onCompleted));
}
public static IDisposable SubscribeWithState<T, TState>(this IObservable<T> source, TState state, Action<T, TState> onNext, Action<Exception, TState> onError, Action<TState> onCompleted)
{
return source.Subscribe(Observer.CreateSubscribeWithStateObserver(state, onNext, onError, onCompleted));
}
public static IDisposable SubscribeWithState2<T, TState1, TState2>(this IObservable<T> source, TState1 state1, TState2 state2, Action<T, TState1, TState2> onNext)
{
return source.Subscribe(Observer.CreateSubscribeWithState2Observer(state1, state2, onNext, Stubs<TState1, TState2>.Throw, Stubs<TState1, TState2>.Ignore));
}
public static IDisposable SubscribeWithState2<T, TState1, TState2>(this IObservable<T> source, TState1 state1, TState2 state2, Action<T, TState1, TState2> onNext, Action<Exception, TState1, TState2> onError)
{
return source.Subscribe(Observer.CreateSubscribeWithState2Observer(state1, state2, onNext, onError, Stubs<TState1, TState2>.Ignore));
}
public static IDisposable SubscribeWithState2<T, TState1, TState2>(this IObservable<T> source, TState1 state1, TState2 state2, Action<T, TState1, TState2> onNext, Action<TState1, TState2> onCompleted)
{
return source.Subscribe(Observer.CreateSubscribeWithState2Observer(state1, state2, onNext, Stubs<TState1, TState2>.Throw, onCompleted));
}
public static IDisposable SubscribeWithState2<T, TState1, TState2>(this IObservable<T> source, TState1 state1, TState2 state2, Action<T, TState1, TState2> onNext, Action<Exception, TState1, TState2> onError, Action<TState1, TState2> onCompleted)
{
return source.Subscribe(Observer.CreateSubscribeWithState2Observer(state1, state2, onNext, onError, onCompleted));
}
public static IDisposable SubscribeWithState3<T, TState1, TState2, TState3>(this IObservable<T> source, TState1 state1, TState2 state2, TState3 state3, Action<T, TState1, TState2, TState3> onNext)
{
return source.Subscribe(Observer.CreateSubscribeWithState3Observer(state1, state2, state3, onNext, Stubs<TState1, TState2, TState3>.Throw, Stubs<TState1, TState2, TState3>.Ignore));
}
public static IDisposable SubscribeWithState3<T, TState1, TState2, TState3>(this IObservable<T> source, TState1 state1, TState2 state2, TState3 state3, Action<T, TState1, TState2, TState3> onNext, Action<Exception, TState1, TState2, TState3> onError)
{
return source.Subscribe(Observer.CreateSubscribeWithState3Observer(state1, state2, state3, onNext, onError, Stubs<TState1, TState2, TState3>.Ignore));
}
public static IDisposable SubscribeWithState3<T, TState1, TState2, TState3>(this IObservable<T> source, TState1 state1, TState2 state2, TState3 state3, Action<T, TState1, TState2, TState3> onNext, Action<TState1, TState2, TState3> onCompleted)
{
return source.Subscribe(Observer.CreateSubscribeWithState3Observer(state1, state2, state3, onNext, Stubs<TState1, TState2, TState3>.Throw, onCompleted));
}
public static IDisposable SubscribeWithState3<T, TState1, TState2, TState3>(this IObservable<T> source, TState1 state1, TState2 state2, TState3 state3, Action<T, TState1, TState2, TState3> onNext, Action<Exception, TState1, TState2, TState3> onError, Action<TState1, TState2, TState3> onCompleted)
{
return source.Subscribe(Observer.CreateSubscribeWithState3Observer(state1, state2, state3, onNext, onError, onCompleted));
}
}
internal static class Stubs
{
public static readonly Action Nop = () => { };
public static readonly Action<Exception> Throw = ex => { ex.Throw(); };
// marker for CatchIgnore and Catch avoid iOS AOT problem.
public static IObservable<TSource> CatchIgnore<TSource>(Exception ex)
{
return Observable.Empty<TSource>();
}
}
internal static class Stubs<T>
{
public static readonly Action<T> Ignore = (T t) => { };
public static readonly Func<T, T> Identity = (T t) => t;
public static readonly Action<Exception, T> Throw = (ex, _) => { ex.Throw(); };
}
internal static class Stubs<T1, T2>
{
public static readonly Action<T1, T2> Ignore = (x, y) => { };
public static readonly Action<Exception, T1, T2> Throw = (ex, _, __) => { ex.Throw(); };
}
internal static class Stubs<T1, T2, T3>
{
public static readonly Action<T1, T2, T3> Ignore = (x, y, z) => { };
public static readonly Action<Exception, T1, T2, T3> Throw = (ex, _, __, ___) => { ex.Throw(); };
}
}