Skip to content

Commit 62cb609

Browse files
Merge pull request #233 from tresmo/master
fix: run tokensetup outside ngzone
2 parents 5bdc5b1 + 90327fa commit 62cb609

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

angular-oauth2-oidc/src/oauth-service.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Injectable, Optional } from '@angular/core';
1+
import { Injectable, NgZone, Optional } from '@angular/core'
22
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
33
import { Observable, Subject, Subscription, of, race } from 'rxjs';
44
import { filter, take, delay, first, tap, map } from 'rxjs/operators';
@@ -67,6 +67,7 @@ export class OAuthService
6767
private inImplicitFlow = false;
6868

6969
constructor(
70+
private ngZone: NgZone,
7071
private http: HttpClient,
7172
@Optional() storage: OAuthStorage,
7273
@Optional() tokenValidationHandler: ValidationHandler,
@@ -263,10 +264,16 @@ export class OAuthService
263264
let storedAt = this.getAccessTokenStoredAt();
264265
let timeout = this.calcTimeout(storedAt, expiration);
265266

266-
this.accessTokenTimeoutSubscription =
267-
of(new OAuthInfoEvent('token_expires', 'access_token'))
268-
.pipe(delay(timeout))
269-
.subscribe(e => this.eventsSubject.next(e));
267+
this.ngZone.runOutsideAngular(() => {
268+
this.accessTokenTimeoutSubscription =
269+
of(new OAuthInfoEvent('token_expires', 'access_token'))
270+
.pipe(delay(timeout))
271+
.subscribe(e => {
272+
this.ngZone.run(() => {
273+
this.eventsSubject.next(e);
274+
})
275+
});
276+
})
270277
}
271278

272279

@@ -275,10 +282,16 @@ export class OAuthService
275282
let storedAt = this.getIdTokenStoredAt();
276283
let timeout = this.calcTimeout(storedAt, expiration);
277284

278-
this.idTokenTimeoutSubscription =
279-
of(new OAuthInfoEvent('token_expires', 'id_token'))
280-
.pipe(delay(timeout))
281-
.subscribe(e => this.eventsSubject.next(e));
285+
this.ngZone.runOutsideAngular(() => {
286+
this.idTokenTimeoutSubscription =
287+
of(new OAuthInfoEvent('token_expires', 'id_token'))
288+
.pipe(delay(timeout))
289+
.subscribe(e => {
290+
this.ngZone.run(() => {
291+
this.eventsSubject.next(e);
292+
})
293+
});
294+
})
282295
}
283296

284297
private clearAccessTokenTimer(): void {

0 commit comments

Comments
 (0)