Skip to content

Commit

Permalink
fix(auth): add scheduler to schedule onAuth events through Angular zone
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffbcross committed Jul 19, 2016
1 parent 9edf33a commit 99e02f4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
31 changes: 24 additions & 7 deletions src/auth/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,17 @@ describe('FirebaseAuth', () => {
});
authSpy['getRedirectResult'].and.returnValue(Promise.resolve(null));

inject([FirebaseApp, AngularFireAuth], (_app: firebase.app.App, _afAuth: AngularFireAuth) => {
app = _app;
afAuth = _afAuth;
authData = null;
authCb = null;
backend = new FirebaseSdkAuthBackend(app);
})();
var mockNgZone = Zone.current.fork({
name: 'mockNgZone'
});
mockNgZone.run(
inject([FirebaseApp, AngularFireAuth], (_app: firebase.app.App, _afAuth: AngularFireAuth) => {
app = _app;
afAuth = _afAuth;
authData = null;
authCb = null;
backend = new FirebaseSdkAuthBackend(app);
}));
});

afterEach(done => {
Expand All @@ -157,6 +161,19 @@ describe('FirebaseAuth', () => {
expect(afAuth instanceof Observable).toBe(true);
});

it('should remain in the Angular zone', (done) => {
afAuth
.take(1)
.do((user: FirebaseAuthState) => {
expect(Zone.current.name).toBe('mockNgZone');
})
.subscribe(done, done.fail);


// Calling with undefined `github` value to mimick actual Firebase value
fbAuthObserver.next(firebaseUser);
}, 10);


it('should emit auth updates', (done: any) => {
let count = 0;
Expand Down
5 changes: 3 additions & 2 deletions src/auth/firebase_sdk_auth_backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, Inject } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Observer } from 'rxjs/Observer';
import { FirebaseApp } from '../tokens';
import { isPresent } from '../utils';
import { isPresent, ZoneScheduler } from '../utils';
import { auth } from 'firebase';
import {
authDataToAuthState,
Expand All @@ -22,6 +22,7 @@ const {

import 'rxjs/add/operator/map';
import 'rxjs/add/observable/fromPromise';
import 'rxjs/add/operator/observeOn';

@Injectable()
export class FirebaseSdkAuthBackend extends AuthBackend {
Expand All @@ -42,10 +43,10 @@ export class FirebaseSdkAuthBackend extends AuthBackend {
}

onAuth(): Observable<FirebaseAuthState> {
// TODO: assumes this will accept an RxJS observer
return Observable.create((observer: Observer<FirebaseAuthState>) => {
return this._fbAuth.onAuthStateChanged(observer);
})
.observeOn(new ZoneScheduler(Zone.current))
.map((user: firebase.User) => {
if (!user) return null;
return authDataToAuthState(user);
Expand Down
12 changes: 11 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Subscription } from 'rxjs/Subscription';
import { QueueScheduler } from 'rxjs/scheduler/QueueScheduler';
import { AFUnwrappedDataSnapshot} from './interfaces';

export function isPresent(obj: any): boolean {
Expand Down Expand Up @@ -75,4 +77,12 @@ export function stripLeadingSlash(value: string): string {
} else {
return value;
}
}
}


export class ZoneScheduler extends QueueScheduler {
constructor(_zone: Zone) {
super();
this.schedule = <(work: any) => Subscription>_zone.wrap(this.schedule, 'ZoneScheduler');
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"src/auth/auth.spec.ts",
"src/auth/auth_backend.spec.ts",
"typings/main.d.ts",
"manual_typings/firebase3/firebase3.d.ts"
"manual_typings/firebase3/firebase3.d.ts",
"node_modules/zone.js/dist/zone.js.d.ts"
]
}

0 comments on commit 99e02f4

Please sign in to comment.