1
+ import 'dart:async' ;
2
+
1
3
import '../notifications/receive.dart' ;
2
4
import 'store.dart' ;
3
5
@@ -6,10 +8,7 @@ import 'store.dart';
6
8
// TODO(#1764) do that tracking of responses
7
9
class PushDeviceManager extends PerAccountStoreBase {
8
10
PushDeviceManager ({required super .core}) {
9
- if (! debugAutoRegisterToken) {
10
- return ;
11
- }
12
- registerToken ();
11
+ _registerTokenAndSubscribe ();
13
12
}
14
13
15
14
bool _disposed = false ;
@@ -25,15 +24,18 @@ class PushDeviceManager extends PerAccountStoreBase {
25
24
}
26
25
27
26
/// Send this client's notification token to the server, now and if it changes.
28
- ///
29
- /// TODO The returned future isn't especially meaningful (it may or may not
30
- /// mean we actually sent the token). Make it just `void` once we fix the
31
- /// one test that relies on the future.
32
27
// TODO(#322) save acked token, to dedupe updating it on the server
33
28
// TODO(#323) track the addFcmToken/etc request, warn if not succeeding
34
- Future <void > registerToken () async {
29
+ void _registerTokenAndSubscribe () async {
30
+ _debugMaybePause ();
31
+ if (_debugRegisterTokenProceed != null ) {
32
+ await _debugRegisterTokenProceed! .future;
33
+ }
34
+
35
35
NotificationService .instance.token.addListener (_registerToken);
36
36
await _registerToken ();
37
+
38
+ _debugRegisterTokenCompleted? .complete ();
37
39
}
38
40
39
41
Future <void > _registerToken () async {
@@ -42,22 +44,49 @@ class PushDeviceManager extends PerAccountStoreBase {
42
44
await NotificationService .instance.registerToken (connection);
43
45
}
44
46
45
- /// In debug mode, controls whether [registerToken] should be called
46
- /// immediately in the constructor.
47
+ Completer <void >? _debugRegisterTokenProceed;
48
+ Completer <void >? _debugRegisterTokenCompleted;
49
+
50
+ void _debugMaybePause () {
51
+ assert (() {
52
+ if (debugAutoPause) {
53
+ _debugRegisterTokenProceed = Completer ();
54
+ _debugRegisterTokenCompleted = Completer ();
55
+ }
56
+ return true ;
57
+ }());
58
+ }
59
+
60
+ /// Unpause registering the token (after [debugAutoPause] ),
61
+ /// returning a future that completes when any immediate request is completed.
62
+ ///
63
+ /// This has no effect if [debugAutoPause] was false
64
+ /// when this instance was constructed,
65
+ /// and therefore no effect outside of debug mode.
66
+ Future <void > debugUnpauseRegisterToken () async {
67
+ _debugRegisterTokenProceed! .complete ();
68
+ await _debugRegisterTokenCompleted! .future;
69
+ }
70
+
71
+ /// In debug mode, controls whether new instances should pause
72
+ /// before registering the token with the server.
73
+ ///
74
+ /// When paused, token registration can be unpaused
75
+ /// with [debugUnpauseRegisterToken] .
47
76
///
48
- /// Outside of debug mode, this is always true and the setter has no effect.
49
- static bool get debugAutoRegisterToken {
50
- bool result = true ;
77
+ /// Outside of debug mode, this is always false and the setter has no effect.
78
+ static bool get debugAutoPause {
79
+ bool result = false ;
51
80
assert (() {
52
- result = _debugAutoRegisterToken ;
81
+ result = _debugAutoPause ;
53
82
return true ;
54
83
}());
55
84
return result;
56
85
}
57
- static bool _debugAutoRegisterToken = true ;
58
- static set debugAutoRegisterToken (bool value) {
86
+ static bool _debugAutoPause = false ;
87
+ static set debugAutoPause (bool value) {
59
88
assert (() {
60
- _debugAutoRegisterToken = value;
89
+ _debugAutoPause = value;
61
90
return true ;
62
91
}());
63
92
}
0 commit comments