Skip to content

Commit 6e9133e

Browse files
Support lazy-loaded Auth
1 parent efafeec commit 6e9133e

21 files changed

+196
-148
lines changed

common/api-review/database.api.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { EmulatorMockTokenOptions } from '@firebase/util';
88
import { FirebaseApp } from '@firebase/app';
99

10-
// @public
10+
// @public (undocumented)
1111
export function child(parent: Reference, path: string): Reference;
1212

1313
// @public

packages/component/src/provider.ts

+39-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import {
2222
InitializeOptions,
2323
InstantiationMode,
2424
Name,
25-
NameServiceMapping
25+
NameServiceMapping,
26+
OnInitCallBack
2627
} from './types';
2728
import { Component } from './component';
2829

@@ -37,6 +38,7 @@ export class Provider<T extends Name> {
3738
string,
3839
Deferred<NameServiceMapping[T]>
3940
> = new Map();
41+
private onInitCallbacks: Set<OnInitCallBack<T>> = new Set();
4042

4143
constructor(
4244
private readonly name: T,
@@ -250,9 +252,45 @@ export class Provider<T extends Name> {
250252
instanceDeferred.resolve(instance);
251253
}
252254
}
255+
256+
this.invokeOnInitCallbacks(instance, normalizedIdentifier);
253257
return instance;
254258
}
255259

260+
/**
261+
*
262+
* @param callback - a function that will be invoked after the provider has
263+
* been initialized by calling provider.initialize().
264+
* The function is invoked SYNCHRONOUSLY, so it should not execute any
265+
* longrunning tasks in order to not block the program.
266+
*
267+
* @returns a function to unregister the callback
268+
*/
269+
onInit(callback: (instance: NameServiceMapping[T]) => void): () => void {
270+
this.onInitCallbacks.add(callback);
271+
272+
return () => {
273+
this.onInitCallbacks.delete(callback);
274+
};
275+
}
276+
277+
/**
278+
* Invoke onInit callbacks synchronously
279+
* @param instance the service instance`
280+
*/
281+
private invokeOnInitCallbacks(
282+
instance: NameServiceMapping[T],
283+
identifier: string
284+
): void {
285+
for (const callback of this.onInitCallbacks) {
286+
try {
287+
callback(instance, identifier);
288+
} catch {
289+
// ignore errors in the onInit callback
290+
}
291+
}
292+
}
293+
256294
private getOrInitializeService({
257295
instanceIdentifier,
258296
options = {}

packages/component/src/types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,8 @@ export interface NameServiceMapping {}
7575

7676
export type Name = keyof NameServiceMapping;
7777
export type Service = NameServiceMapping[Name];
78+
79+
export type OnInitCallBack<T extends Name> = (
80+
instance: NameServiceMapping[T],
81+
identifier: string
82+
) => void;

packages/database/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ import { Database } from './src/api/Database';
2828
import * as INTERNAL from './src/api/internal';
2929
import { DataSnapshot, Query, Reference } from './src/api/Reference';
3030
import * as TEST_ACCESS from './src/api/test_access';
31-
import { enableLogging } from './src/core/util/util';
3231
import { setSDKVersion } from './src/core/version';
33-
import { repoManagerDatabaseFromApp } from './src/exp/Database';
32+
import { enableLogging, repoManagerDatabaseFromApp } from './src/exp/Database';
3433

3534
const ServerValue = Database.ServerValue;
3635

packages/database/src/api/Database.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
} from '@firebase/util';
2626

2727
import {
28+
FirebaseDatabase as ExpDatabase,
2829
goOnline,
2930
useDatabaseEmulator,
3031
goOffline,
@@ -36,14 +37,6 @@ import {
3637

3738
import { Reference } from './Reference';
3839

39-
// TODO: revert to import {FirebaseDatabase as ExpDatabase} from '@firebase/database' once modular SDK goes GA
40-
/**
41-
* This is a workaround for an issue in the no-modular '@firebase/database' where its typings
42-
* reference types from `@firebase/app-exp`.
43-
*/
44-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
45-
type ExpDatabase = any;
46-
4740
/**
4841
* Class representing a firebase database.
4942
*/

packages/database/src/api/Reference.ts

+3-13
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626

2727
import {
2828
OnDisconnect as ExpOnDisconnect,
29+
DataSnapshot as ExpDataSnapshot,
2930
off,
3031
onChildAdded,
3132
onChildChanged,
@@ -53,6 +54,8 @@ import {
5354
setPriority,
5455
push,
5556
runTransaction,
57+
Query as ExpQuery,
58+
Reference as ExpReference,
5659
_QueryImpl,
5760
_ReferenceImpl,
5861
child
@@ -72,19 +75,6 @@ import { Database } from './Database';
7275
import { OnDisconnect } from './onDisconnect';
7376
import { TransactionResult } from './TransactionResult';
7477

75-
// TODO: revert to import { DataSnapshot as ExpDataSnapshot, Query as ExpQuery,
76-
// Reference as ExpReference,} from '../../exp/index'; once the modular SDK goes GA
77-
/**
78-
* This is part of a workaround for an issue in the no-modular '@firebase/database' where its typings
79-
* reference types from `@firebase/app-exp`.
80-
*/
81-
82-
/* eslint-disable @typescript-eslint/no-explicit-any */
83-
type ExpDataSnapshot = any;
84-
type ExpQuery = any;
85-
type ExpReference = any;
86-
/* eslint-enable @typescript-eslint/no-explicit-any */
87-
8878
/**
8979
* Class representing a firebase data snapshot. It wraps a SnapshotNode and
9080
* surfaces the public methods (val, forEach, etc.) we want to expose.

packages/database/src/api/onDisconnect.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,10 @@
1717

1818
import { validateArgCount, validateCallback, Compat } from '@firebase/util';
1919

20+
import { OnDisconnect as ExpOnDisconnect } from '../../exp/index';
2021
import { Indexable } from '../core/util/misc';
2122
import { warn } from '../core/util/util';
2223

23-
// TODO: revert to import { OnDisconnect as ExpOnDisconnect } from '../../exp/index'; once the modular SDK goes GA
24-
/**
25-
* This is a workaround for an issue in the no-modular '@firebase/database' where its typings
26-
* reference types from `@firebase/app-exp`.
27-
*/
28-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
29-
type ExpOnDisconnect = any;
30-
3124
export class OnDisconnect implements Compat<ExpOnDisconnect> {
3225
constructor(readonly _delegate: ExpOnDisconnect) {}
3326

packages/database/src/core/util/util.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,8 @@ import {
2626
} from '@firebase/util';
2727

2828
import { SessionStorage } from '../storage/storage';
29+
import { QueryContext } from '../view/EventRegistration';
2930

30-
// TODO: revert to import { QueryContext } from '../view/EventRegistration'; once the modular SDK goes GA
31-
/**
32-
* This is part of a workaround for an issue in the no-modular '@firebase/database' where its typings
33-
* reference types from `@firebase/app-exp`.
34-
*/
35-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
36-
type QueryContext = any;
3731
declare const window: Window;
3832
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3933
declare const Windows: any;

packages/firestore/.idea/runConfigurations/All_Tests__Emulator_.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/firestore/.idea/runConfigurations/All_Tests__Emulator_w__Mock_Persistence_.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/firestore/.idea/runConfigurations/Integration_Tests__Emulator_.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/firestore/.idea/runConfigurations/Integration_Tests__Emulator_w__Mock_Persistence_.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/firestore/.idea/runConfigurations/Unit_Tests.xml

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/firestore/.idea/runConfigurations/Unit_Tests__w__Mock_Persistence_.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)