-
Notifications
You must be signed in to change notification settings - Fork 17
/
nested_realtime_events.dart
74 lines (65 loc) · 2.77 KB
/
nested_realtime_events.dart
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
// NOTE: this file is not being used anywhere, yet
// this could serve as a reference for writing nested listeners
import 'dart:async';
import 'package:ably_flutter/ably_flutter.dart' as ably;
/// This method is a demonstration on how to use multiple listeners
/// with realtime connection state change listeners as an example
///
/// It aims to demonstrate the working of nested listeners
/// and cancelling listeners on specific events
void listenRealtimeConnection(ably.Realtime realtime) {
//One can listen from multiple listeners on the same event,
// and must cancel each subscription one by one
//RETAINING LISTENER - α
realtime.connection.on().listen((stateChange) {
print('RETAINING LISTENER α :: Change event arrived!: ${stateChange.event}'
'\nReason: ${stateChange.reason}');
});
//DISPOSE ON CONNECTED
final stream = realtime.connection.on();
late StreamSubscription omegaSubscription;
omegaSubscription = stream.listen((stateChange) async {
print('DISPOSABLE LISTENER ω :: Change event arrived!:'
' ${stateChange.event}');
if (stateChange.event == ably.ConnectionEvent.connected) {
await omegaSubscription.cancel();
}
});
//RETAINING LISTENER - β
realtime.connection.on().listen((stateChange) {
print('RETAINING LISTENER β :: Change event arrived!:'
' ${stateChange.event}');
// NESTED LISTENER - ξ
// will be registered only when connected event is received by β listener
realtime.connection.on().listen((stateChange) {
// k ξ listeners will be registered
// and each listener will be called `n-k` times respectively
// if listener β is called `n` times
print('NESTED LISTENER ξ: ${stateChange.event}');
});
});
StreamSubscription preZetaSubscription;
late StreamSubscription postZetaSubscription;
preZetaSubscription = realtime.connection.on().listen((stateChange) {
//This listener "pre ζ" will be cancelled from γ
print('NESTED LISTENER "pre ζ": ${stateChange.event}');
});
//RETAINING LISTENER - γ
realtime.connection.on().listen((stateChange) async {
print(
'RETAINING LISTENER γ :: Change event arrived!: ${stateChange.event}');
if (stateChange.event == ably.ConnectionEvent.connected) {
//by the time this cancel is triggered,
// preZeta will already have received current event.
await preZetaSubscription.cancel();
//by the time this cancel is triggered,
// postZeta hasn't received the event yet.
// And will never receive as it is cancelled.
await postZetaSubscription.cancel();
}
});
postZetaSubscription = realtime.connection.on().listen((stateChange) {
//This listener "post ζ" will be cancelled from γ
print('NESTED LISTENER "post ζ": ${stateChange.event}');
});
}