Skip to content

fix(app): fix issue with FirebaseApp extending INTERNAL #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions src/app/firebase_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,18 @@ class FirebaseAppImpl implements FirebaseApp {
private firebase_: FirebaseNamespace) {
this.name_ = name;
this.options_ = deepCopy<FirebaseOptions>(options);
this.INTERNAL = {
'getUid': () => null,
'getToken': () => LocalPromise.resolve(null),
'addAuthTokenListener': (callback: (token: string|null) => void) => {
tokenListeners.push(callback);
// Make sure callback is called, asynchronously, in the absence of the auth module
setTimeout(() => callback(null), 0);
},
'removeAuthTokenListener': (callback) => {
tokenListeners = tokenListeners.filter(listener => listener !== callback);
},
};
}

get name(): string {
Expand Down Expand Up @@ -321,7 +333,7 @@ class FirebaseAppImpl implements FirebaseApp {
*/
private extendApp(props: {[name: string]: any}): void {
// Copy the object onto the FirebaseAppImpl prototype
deepExtend(FirebaseAppImpl.prototype, props);
deepExtend(this, props);

/**
* If the app has overwritten the addAuthTokenListener stub, forward
Expand Down Expand Up @@ -351,19 +363,6 @@ class FirebaseAppImpl implements FirebaseApp {
}
};

FirebaseAppImpl.prototype.INTERNAL = {
'getUid': () => null,
'getToken': () => LocalPromise.resolve(null),
'addAuthTokenListener': (callback: (token: string|null) => void) => {
tokenListeners.push(callback);
// Make sure callback is called, asynchronously, in the absence of the auth module
setTimeout(() => callback(null), 0);
},
'removeAuthTokenListener': (callback) => {
tokenListeners = tokenListeners.filter(listener => listener !== callback);
},
}

// Prevent dead-code elimination of these methods w/o invalid property
// copying.
FirebaseAppImpl.prototype.name &&
Expand Down Expand Up @@ -603,4 +602,4 @@ let errors: {[code: string]: string} = {
'Firebase App instance.'
};

let appErrors = new ErrorFactory<AppError>('app', 'Firebase', errors);
let appErrors = new ErrorFactory<AppError>('app', 'Firebase', errors);
35 changes: 35 additions & 0 deletions tests/app/unit/firebase_app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,41 @@ describe("Firebase App Class", () => {
const service = (firebase.app() as any).testService();
});

it(`Should extend INTERNAL per app instance`, () => {
let counter: number = 0;
firebase.INTERNAL.registerService(
'test',
(app: FirebaseApp, extendApp: any) => {
const service = new TestService(app);
(service as any).token = 'tokenFor' + counter++;
extendApp({
'INTERNAL': {
getToken: () => {
return Promise.resolve({
accessToken: (service as any).token,
});
},
},
});
return service;
});
// Initialize 2 apps and their corresponding services.
const app = firebase.initializeApp({});
(app as any).test();
const app2 = firebase.initializeApp({}, 'app2');
(app2 as any).test();
// Confirm extended INTERNAL getToken resolve with the corresponding
// service's value.
return app.INTERNAL.getToken()
.then(token => {
assert.equal('tokenFor0', token.accessToken);
return app2.INTERNAL.getToken();
})
.then(token => {
assert.equal('tokenFor1', token.accessToken);
});
});

describe("Check for bad app names", () => {
let tests = ["", 123, false, null];
for (let data of tests) {
Expand Down