Skip to content

Commit e3dcfc5

Browse files
committed
Merge branch 'fei-component-integration2' of https://github.com/firebase/firebase-js-sdk
2 parents ba2ff35 + fd3aae4 commit e3dcfc5

File tree

135 files changed

+3278
-1552
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+3278
-1552
lines changed

integration/browserify/src/namespace.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ firebase.initializeApp({
2525
databaseURL: 'https://test-project-name.firebaseio.com',
2626
projectId: 'test-project-name',
2727
storageBucket: 'test-project-name.appspot.com',
28-
messagingSenderId: '012345678910'
28+
messagingSenderId: '012345678910',
29+
appId: 'myAppId'
2930
});
3031

3132
describe('Firebase Namespace Validation', function() {

integration/shared/namespaceDefinition.json

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
},
4141
"INTERNAL": {
4242
"__type": "object",
43-
"registerService": {
43+
"registerComponent": {
4444
"__type": "function"
4545
},
4646
"extendNamespace": {
@@ -55,20 +55,8 @@
5555
"removeApp": {
5656
"__type": "function"
5757
},
58-
"factories": {
59-
"__type": "object",
60-
"storage": {
61-
"__type": "function"
62-
},
63-
"auth": {
64-
"__type": "function"
65-
},
66-
"database": {
67-
"__type": "function"
68-
},
69-
"messaging": {
70-
"__type": "function"
71-
}
58+
"components": {
59+
"__type": "Map"
7260
},
7361
"ErrorFactory": {
7462
"__type": "function"

integration/typescript/test/namespace.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ firebase.initializeApp({
2525
databaseURL: 'https://test-project-name.firebaseio.com',
2626
projectId: 'test-project-name',
2727
storageBucket: 'test-project-name.appspot.com',
28-
messagingSenderId: '012345678910'
28+
messagingSenderId: '012345678910',
29+
appId: 'myAppId'
2930
});
3031

3132
describe('Firebase Namespace Validation', function() {

integration/webpack/src/namespace.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ firebase.initializeApp({
2525
databaseURL: 'https://test-project-name.firebaseio.com',
2626
projectId: 'test-project-name',
2727
storageBucket: 'test-project-name.appspot.com',
28-
messagingSenderId: '012345678910'
28+
messagingSenderId: '012345678910',
29+
appId: 'myAppId'
2930
});
3031

3132
describe('Firebase Namespace Validation', function() {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# @firebase/analytics-interop-types
2+
3+
**This package is not intended for direct usage, and should only be used via the officially supported [firebase](https://www.npmjs.com/package/firebase) package.**
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @license
3+
* Copyright 2019 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
export interface FirebaseAnalyticsInternal {
19+
/**
20+
* Sends analytics event with given `eventParams`. This method
21+
* automatically associates this logged event with this Firebase web
22+
* app instance on this device.
23+
* List of official event parameters can be found in
24+
* {@link https://developers.google.com/gtagjs/reference/event
25+
* the gtag.js reference documentation}.
26+
*/
27+
logEvent(
28+
eventName: string,
29+
eventParams?: { [key: string]: unknown },
30+
options?: AnalyticsCallOptions
31+
): void;
32+
}
33+
34+
export interface AnalyticsCallOptions {
35+
/**
36+
* If true, this config or event call applies globally to all
37+
* analytics properties on the page.
38+
*/
39+
global: boolean;
40+
}
41+
42+
export type FirebaseAnalyticsInternalName = 'analytics-internal';
43+
44+
declare module '@firebase/component' {
45+
interface NameServiceMapping {
46+
'analytics-internal': FirebaseAnalyticsInternal;
47+
}
48+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "@firebase/analytics-interop-types",
3+
"version": "0.1.0",
4+
"description": "@firebase/analytics Types",
5+
"author": "Firebase <[email protected]> (https://firebase.google.com/)",
6+
"license": "Apache-2.0",
7+
"scripts": {
8+
"test": "tsc"
9+
},
10+
"files": [
11+
"index.d.ts"
12+
],
13+
"repository": {
14+
"directory": "packages/analytics-interop-types",
15+
"type": "git",
16+
"url": "https://github.com/firebase/firebase-js-sdk.git"
17+
},
18+
"bugs": {
19+
"url": "https://github.com/firebase/firebase-js-sdk/issues"
20+
},
21+
"devDependencies": {
22+
"typescript": "3.7.2"
23+
}
24+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../config/tsconfig.base.json",
3+
"compilerOptions": {
4+
"noEmit": true
5+
},
6+
"exclude": [
7+
"dist/**/*"
8+
]
9+
}

packages/analytics-types/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,9 @@ export interface Promotion {
201201
id?: string;
202202
name?: string;
203203
}
204+
205+
declare module '@firebase/component' {
206+
interface NameServiceMapping {
207+
'analytics': FirebaseAnalytics;
208+
}
209+
}

packages/analytics/index.test.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import {
2424
factory as analyticsFactory,
2525
resetGlobalVars
2626
} from './index';
27-
import { getFakeApp } from './testing/get-fake-app';
27+
import {
28+
getFakeApp,
29+
getFakeInstallations
30+
} from './testing/get-fake-firebase-services';
2831
import { FirebaseApp } from '@firebase/app-types';
2932
import { GtagCommand, EventName } from './src/constants';
3033
import { findGtagScriptOnPage } from './src/helpers';
@@ -39,21 +42,29 @@ const customDataLayerName = 'customDataLayer';
3942
describe('FirebaseAnalytics instance tests', () => {
4043
it('Throws if no analyticsId in config', () => {
4144
const app = getFakeApp();
42-
expect(() => analyticsFactory(app, () => {})).to.throw('field is empty');
45+
const installations = getFakeInstallations();
46+
expect(() => analyticsFactory(app, installations)).to.throw(
47+
'field is empty'
48+
);
4349
});
4450
it('Throws if creating an instance with already-used analytics ID', () => {
4551
const app = getFakeApp(analyticsId);
52+
const installations = getFakeInstallations();
4653
resetGlobalVars(false, { [analyticsId]: Promise.resolve() });
47-
expect(() => analyticsFactory(app, () => {})).to.throw('already exists');
54+
expect(() => analyticsFactory(app, installations)).to.throw(
55+
'already exists'
56+
);
4857
});
4958
describe('Standard app, page already has user gtag script', () => {
5059
let app: FirebaseApp = {} as FirebaseApp;
5160
before(() => {
5261
resetGlobalVars();
5362
app = getFakeApp(analyticsId);
63+
const installations = getFakeInstallations();
64+
5465
window['gtag'] = gtagStub;
5566
window['dataLayer'] = [];
56-
analyticsInstance = analyticsFactory(app, () => {});
67+
analyticsInstance = analyticsFactory(app, installations);
5768
});
5869
after(() => {
5970
delete window['gtag'];
@@ -113,13 +124,14 @@ describe('FirebaseAnalytics instance tests', () => {
113124
before(() => {
114125
resetGlobalVars();
115126
const app = getFakeApp(analyticsId);
127+
const installations = getFakeInstallations();
116128
window[customGtagName] = gtagStub;
117129
window[customDataLayerName] = [];
118130
analyticsSettings({
119131
dataLayerName: customDataLayerName,
120132
gtagName: customGtagName
121133
});
122-
analyticsInstance = analyticsFactory(app, () => {});
134+
analyticsInstance = analyticsFactory(app, installations);
123135
});
124136
after(() => {
125137
delete window[customGtagName];
@@ -162,7 +174,8 @@ describe('FirebaseAnalytics instance tests', () => {
162174
before(() => {
163175
resetGlobalVars();
164176
const app = getFakeApp(analyticsId);
165-
analyticsInstance = analyticsFactory(app, () => {});
177+
const installations = getFakeInstallations();
178+
analyticsInstance = analyticsFactory(app, installations);
166179
});
167180
after(() => {
168181
delete window['gtag'];

packages/analytics/index.ts

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515
* limitations under the License.
1616
*/
1717
import firebase from '@firebase/app';
18+
import '@firebase/installations';
1819
import { FirebaseAnalytics } from '@firebase/analytics-types';
19-
import {
20-
FirebaseServiceFactory,
21-
_FirebaseNamespace
22-
} from '@firebase/app-types/private';
20+
import { FirebaseAnalyticsInternal } from '@firebase/analytics-interop-types';
21+
import { _FirebaseNamespace } from '@firebase/app-types/private';
2322
import { factory, settings, resetGlobalVars } from './src/factory';
2423
import { EventName } from './src/constants';
24+
import {
25+
Component,
26+
ComponentType,
27+
ComponentContainer
28+
} from '@firebase/component';
29+
import { ERROR_FACTORY, AnalyticsError } from './src/errors';
2530

2631
declare global {
2732
interface Window {
@@ -34,18 +39,43 @@ declare global {
3439
*/
3540
const ANALYTICS_TYPE = 'analytics';
3641
export function registerAnalytics(instance: _FirebaseNamespace): void {
37-
instance.INTERNAL.registerService(
38-
ANALYTICS_TYPE,
39-
factory as FirebaseServiceFactory,
40-
{
42+
instance.INTERNAL.registerComponent(
43+
new Component(
44+
ANALYTICS_TYPE,
45+
container => {
46+
// getImmediate for FirebaseApp will always succeed
47+
const app = container.getProvider('app').getImmediate();
48+
const installations = container
49+
.getProvider('installations')
50+
.getImmediate();
51+
52+
return factory(app, installations);
53+
},
54+
ComponentType.PUBLIC
55+
).setServiceProps({
4156
settings,
4257
EventName
43-
},
44-
// We don't need to wait on any AppHooks.
45-
undefined,
46-
// Allow multiple analytics instances per app.
47-
false
58+
})
4859
);
60+
61+
instance.INTERNAL.registerComponent(
62+
new Component('analytics-internal', internalFactory, ComponentType.PRIVATE)
63+
);
64+
65+
function internalFactory(
66+
container: ComponentContainer
67+
): FirebaseAnalyticsInternal {
68+
try {
69+
const analytics = container.getProvider(ANALYTICS_TYPE).getImmediate();
70+
return {
71+
logEvent: analytics.logEvent
72+
};
73+
} catch (e) {
74+
throw ERROR_FACTORY.create(AnalyticsError.INTEROP_COMPONENT_REG_FAILED, {
75+
reason: e
76+
});
77+
}
78+
}
4979
}
5080

5181
export { factory, settings, resetGlobalVars };

packages/analytics/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"@firebase/analytics-types": "0.2.3",
2727
"@firebase/installations": "0.3.6",
2828
"@firebase/util": "0.2.34",
29+
"@firebase/component": "0.1.0",
2930
"tslib": "1.10.0"
3031
},
3132
"license": "Apache-2.0",

packages/analytics/src/errors.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import { ANALYTICS_ID_FIELD } from './constants';
2121
export const enum AnalyticsError {
2222
NO_GA_ID = 'no-ga-id',
2323
ALREADY_EXISTS = 'already-exists',
24-
ALREADY_INITIALIZED = 'already-initialized'
24+
ALREADY_INITIALIZED = 'already-initialized',
25+
INTEROP_COMPONENT_REG_FAILED = 'interop-component-reg-failed'
2526
}
2627

2728
const ERRORS: ErrorMap<AnalyticsError> = {
@@ -36,11 +37,14 @@ const ERRORS: ErrorMap<AnalyticsError> = {
3637
[AnalyticsError.ALREADY_INITIALIZED]:
3738
'Firebase Analytics has already been initialized.' +
3839
'settings() must be called before initializing any Analytics instance' +
39-
'or it will have no effect.'
40+
'or it will have no effect.',
41+
[AnalyticsError.INTEROP_COMPONENT_REG_FAILED]:
42+
'Firebase Analytics Interop Component failed to instantiate'
4043
};
4144

4245
interface ErrorParams {
4346
[AnalyticsError.ALREADY_EXISTS]: { id: string };
47+
[AnalyticsError.INTEROP_COMPONENT_REG_FAILED]: { reason: Error };
4448
}
4549

4650
export const ERROR_FACTORY = new ErrorFactory<AnalyticsError, ErrorParams>(

0 commit comments

Comments
 (0)