Skip to content

Commit e707d73

Browse files
committed
Migrate Performance to component framework (#2325)
* Migrate Performance to component framework * [AUTOMATED]: Prettier Code Styling * removed unused import
1 parent a724edd commit e707d73

File tree

5 files changed

+39
-25
lines changed

5 files changed

+39
-25
lines changed

packages/performance/index.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,23 @@
1616
*/
1717

1818
import firebase from '@firebase/app';
19+
import '@firebase/installations';
1920
import { FirebaseApp, FirebaseNamespace } from '@firebase/app-types';
20-
import {
21-
_FirebaseNamespace,
22-
FirebaseServiceFactory
23-
} from '@firebase/app-types/private';
21+
import { _FirebaseNamespace } from '@firebase/app-types/private';
2422
import { PerformanceController } from './src/controllers/perf';
2523
import { setupApi } from './src/services/api_service';
2624
import { SettingsService } from './src/services/settings_service';
2725
import { ERROR_FACTORY, ErrorCode } from './src/utils/errors';
2826
import { FirebasePerformance } from '@firebase/performance-types';
27+
import { Component, ComponentType } from '@firebase/component';
28+
import { FirebaseInstallations } from '@firebase/installations-types';
2929

3030
const DEFAULT_ENTRY_NAME = '[DEFAULT]';
3131

3232
export function registerPerformance(instance: FirebaseNamespace): void {
33-
const factoryMethod: FirebaseServiceFactory = (
34-
app: FirebaseApp
33+
const factoryMethod = (
34+
app: FirebaseApp,
35+
installations: FirebaseInstallations
3536
): PerformanceController => {
3637
if (app.name !== DEFAULT_ENTRY_NAME) {
3738
throw ERROR_FACTORY.create(ErrorCode.FB_NOT_DEFAULT);
@@ -41,15 +42,27 @@ export function registerPerformance(instance: FirebaseNamespace): void {
4142
}
4243
setupApi(window);
4344
SettingsService.getInstance().firebaseAppInstance = app;
45+
SettingsService.getInstance().installationsService = installations;
4446
return new PerformanceController(app);
4547
};
4648

4749
// Register performance with firebase-app.
48-
const namespaceExports = {};
49-
(instance as _FirebaseNamespace).INTERNAL.registerService(
50-
'performance',
51-
factoryMethod,
52-
namespaceExports
50+
(instance as _FirebaseNamespace).INTERNAL.registerComponent(
51+
new Component(
52+
'performance',
53+
container => {
54+
/* Dependencies */
55+
// getImmediate for FirebaseApp will always succeed
56+
const app = container.getProvider('app').getImmediate();
57+
// The following call will always succeed because perf has `import '@firebase/installations'`
58+
const installations = container
59+
.getProvider('installations')
60+
.getImmediate();
61+
62+
return factoryMethod(app, installations);
63+
},
64+
ComponentType.PUBLIC
65+
)
5366
);
5467
}
5568

packages/performance/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"@firebase/installations": "0.3.3",
3131
"@firebase/util": "0.2.31",
3232
"@firebase/performance-types": "0.0.5",
33+
"@firebase/component": "0.1.0",
3334
"tslib": "1.10.0"
3435
},
3536
"license": "Apache-2.0",

packages/performance/src/services/iid_service.test.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,21 @@ import {
2424
getAuthenticationToken,
2525
getAuthTokenPromise
2626
} from './iid_service';
27-
import { FirebaseApp } from '@firebase/app-types';
2827
import '../../test/setup';
28+
import { FirebaseInstallations } from '@firebase/installations-types';
2929

3030
describe('Firebase Perofmrance > iid_service', () => {
3131
const IID = 'fid';
3232
const AUTH_TOKEN = 'authToken';
33-
const getId = stub().resolves(IID);
34-
const getToken = stub().resolves(AUTH_TOKEN);
3533

36-
SettingsService.prototype.firebaseAppInstance = ({
37-
installations: () => ({ getId, getToken })
38-
} as unknown) as FirebaseApp;
34+
before(() => {
35+
const getId = stub().resolves(IID);
36+
const getToken = stub().resolves(AUTH_TOKEN);
37+
SettingsService.prototype.installationsService = ({
38+
getId,
39+
getToken
40+
} as unknown) as FirebaseInstallations;
41+
});
3942

4043
describe('getIidPromise', () => {
4144
it('provides iid', async () => {

packages/performance/src/services/iid_service.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,13 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
18-
import '@firebase/installations';
1917
import { SettingsService } from './settings_service';
2018

2119
let iid: string | undefined;
2220
let authToken: string | undefined;
2321

2422
export function getIidPromise(): Promise<string> {
25-
const iidPromise = SettingsService.getInstance()
26-
.firebaseAppInstance.installations()
27-
.getId();
23+
const iidPromise = SettingsService.getInstance().installationsService.getId();
2824
// eslint-disable-next-line @typescript-eslint/no-floating-promises
2925
iidPromise.then((iidVal: string) => {
3026
iid = iidVal;
@@ -38,9 +34,7 @@ export function getIid(): string | undefined {
3834
}
3935

4036
export function getAuthTokenPromise(): Promise<string> {
41-
const authTokenPromise = SettingsService.getInstance()
42-
.firebaseAppInstance.installations()
43-
.getToken();
37+
const authTokenPromise = SettingsService.getInstance().installationsService.getToken();
4438
// eslint-disable-next-line @typescript-eslint/no-floating-promises
4539
authTokenPromise.then((authTokenVal: string) => {
4640
authToken = authTokenVal;

packages/performance/src/services/settings_service.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import { FirebaseApp } from '@firebase/app-types';
1919
import { ERROR_FACTORY, ErrorCode } from '../utils/errors';
20+
import { FirebaseInstallations } from '@firebase/installations-types';
2021

2122
let settingsServiceInstance: SettingsService | undefined;
2223

@@ -46,6 +47,8 @@ export class SettingsService {
4647

4748
firebaseAppInstance!: FirebaseApp;
4849

50+
installationsService!: FirebaseInstallations;
51+
4952
getAppId(): string {
5053
const appId =
5154
this.firebaseAppInstance &&

0 commit comments

Comments
 (0)