@@ -48,6 +48,8 @@ static Listener* g_listener = nullptr;
4848
4949// Keep track of the most recent token received.
5050static std::string* g_prev_token_received = nullptr ;
51+ // Keep track if that token was receieved without a listener set.
52+ static bool g_has_pending_token = false ;
5153
5254namespace internal {
5355
@@ -91,11 +93,18 @@ Listener* SetListener(Listener* listener) {
9193 g_prev_token_received = new std::string;
9294 }
9395 g_listener = listener;
96+ // If we have a pending token, send it before notifying other systems about
97+ // the listener.
98+ if (g_listener && g_has_pending_token && g_prev_token_received) {
99+ g_listener->OnTokenReceived (g_prev_token_received->c_str ());
100+ g_has_pending_token = false ;
101+ }
94102 NotifyListenerSet (listener);
95103 if (!listener && g_prev_token_received) {
96104 std::string* ptr = g_prev_token_received;
97105 g_prev_token_received = nullptr ;
98106 delete ptr;
107+ g_has_pending_token = false ;
99108 }
100109 return previous_listener;
101110}
@@ -119,13 +128,20 @@ void NotifyListenerOnTokenReceived(const char* token) {
119128 MutexLock lock (g_listener_lock);
120129 // If we have a previous token that we've notified any listener about, check
121130 // to ensure no repeat.
122- if (g_prev_token_received) {
123- if (*g_prev_token_received == token) {
124- return ;
125- }
126- *g_prev_token_received = token;
131+ if (g_prev_token_received && *g_prev_token_received == token) {
132+ return ;
133+ }
134+
135+ if (!g_prev_token_received) g_prev_token_received = new std::string;
136+ *g_prev_token_received = token;
137+
138+ if (g_listener) {
139+ g_listener->OnTokenReceived (token);
140+ } else {
141+ // Set so that if a listener is set in the future, it will be sent
142+ // the token, since the underlying platform won't send it again.
143+ g_has_pending_token = true ;
127144 }
128- if (g_listener) g_listener->OnTokenReceived (token);
129145}
130146
131147class PollableListenerImpl {
0 commit comments