1+ import 'dart:async' ;
2+
13import '../notifications/receive.dart' ;
24import 'store.dart' ;
35
@@ -6,10 +8,7 @@ import 'store.dart';
68// TODO(#1764) do that tracking of responses
79class PushDeviceManager extends PerAccountStoreBase {
810 PushDeviceManager ({required super .core}) {
9- if (! debugAutoRegisterToken) {
10- return ;
11- }
12- registerToken ();
11+ _registerTokenAndSubscribe ();
1312 }
1413
1514 bool _disposed = false ;
@@ -25,15 +24,18 @@ class PushDeviceManager extends PerAccountStoreBase {
2524 }
2625
2726 /// 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.
3227 // TODO(#322) save acked token, to dedupe updating it on the server
3328 // 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+
3535 NotificationService .instance.token.addListener (_registerToken);
3636 await _registerToken ();
37+
38+ _debugRegisterTokenCompleted? .complete ();
3739 }
3840
3941 Future <void > _registerToken () async {
@@ -42,22 +44,49 @@ class PushDeviceManager extends PerAccountStoreBase {
4244 await NotificationService .instance.registerToken (connection);
4345 }
4446
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] .
4776 ///
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 ;
5180 assert (() {
52- result = _debugAutoRegisterToken ;
81+ result = _debugAutoPause ;
5382 return true ;
5483 }());
5584 return result;
5685 }
57- static bool _debugAutoRegisterToken = true ;
58- static set debugAutoRegisterToken (bool value) {
86+ static bool _debugAutoPause = false ;
87+ static set debugAutoPause (bool value) {
5988 assert (() {
60- _debugAutoRegisterToken = value;
89+ _debugAutoPause = value;
6190 return true ;
6291 }());
6392 }
0 commit comments