Skip to content

Schedule everything on the AsyncQueue #3701

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
Sep 9, 2020
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
2 changes: 1 addition & 1 deletion packages-exp/installations-exp/src/api/on-id-change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type IdChangeUnsubscribeFn = () => void;
/**
* Sets a new callback that will get called when Installation ID changes.
* Returns an unsubscribe function that will remove the callback when called.
*
*
* @public
*/
export function onIdChange(
Expand Down
2 changes: 1 addition & 1 deletion packages-exp/installations-types-exp/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface FirebaseInstallations {}

/**
* An interface for Firebase internal SDKs use only.
*
*
* @internal
*/
export interface _FirebaseInstallationsInternal {
Expand Down
14 changes: 2 additions & 12 deletions packages/firestore/exp/src/api/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import { Firestore } from './database';
import { PersistenceSettings } from '../../../src/core/firestore_client';
import { Code, FirestoreError } from '../../../src/util/error';
import {
MemoryOfflineComponentProvider,
OfflineComponentProvider,
Expand Down Expand Up @@ -115,7 +114,7 @@ export async function setOnlineComponentProvider(
function getOfflineComponentProvider(
firestore: Firestore
): Promise<OfflineComponentProvider> {
verifyNotTerminated(firestore);
firestore._queue.verifyOperationInProgress();

if (!offlineComponentProviders.has(firestore)) {
logDebug(LOG_TAG, 'Using default OfflineComponentProvider');
Expand All @@ -132,7 +131,7 @@ function getOfflineComponentProvider(
function getOnlineComponentProvider(
firestore: Firestore
): Promise<OnlineComponentProvider> {
verifyNotTerminated(firestore);
firestore._queue.verifyOperationInProgress();

if (!onlineComponentProviders.has(firestore)) {
logDebug(LOG_TAG, 'Using default OnlineComponentProvider');
Expand All @@ -142,15 +141,6 @@ function getOnlineComponentProvider(
return onlineComponentProviders.get(firestore)!;
}

function verifyNotTerminated(firestore: Firestore): void {
if (firestore._terminated) {
throw new FirestoreError(
Code.FAILED_PRECONDITION,
'The client has already been terminated.'
);
}
}

// Note: These functions cannot be `async` since we want to throw an exception
// when Firestore is terminated (via `getOnlineComponentProvider()`).

Expand Down
122 changes: 67 additions & 55 deletions packages/firestore/exp/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ import { _FirebaseService, FirebaseApp } from '@firebase/app-types-exp';
import { Provider } from '@firebase/component';

import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
import {
enqueueNetworkEnabled,
enqueueWaitForPendingWrites,
MAX_CONCURRENT_LIMBO_RESOLUTIONS
} from '../../../src/core/firestore_client';
import { MAX_CONCURRENT_LIMBO_RESOLUTIONS } from '../../../src/core/firestore_client';
import {
AsyncQueue,
wrapInUserErrorIfRecoverable
Expand Down Expand Up @@ -62,6 +58,11 @@ import { AutoId } from '../../../src/util/misc';
import { User } from '../../../src/auth/user';
import { CredentialChangeListener } from '../../../src/api/credentials';
import { logDebug } from '../../../src/util/log';
import { registerPendingWritesCallback } from '../../../src/core/sync_engine';
import {
remoteStoreDisableNetwork,
remoteStoreEnableNetwork
} from '../../../src/remote/remote_store';

const LOG_TAG = 'Firestore';

Expand Down Expand Up @@ -157,6 +158,15 @@ export class Firestore
});
return deferred.promise;
}

_verifyNotTerminated(): void {
if (this._terminated) {
throw new FirestoreError(
Code.FAILED_PRECONDITION,
'The client has already been terminated.'
);
}
}
}

export function initializeFirestore(
Expand Down Expand Up @@ -199,17 +209,19 @@ export function enableIndexedDbPersistence(
// `getOnlineComponentProvider()`
const settings = firestoreImpl._getSettings();

// TODO(firestoreexp): Add forceOwningTab
return setOfflineComponentProvider(
firestoreImpl,
{
durable: true,
synchronizeTabs: false,
cacheSizeBytes:
settings.cacheSizeBytes || LruParams.DEFAULT_CACHE_SIZE_BYTES,
forceOwningTab: false
},
new IndexedDbOfflineComponentProvider()
return firestoreImpl._queue.enqueue(() =>
// TODO(firestoreexp): Add forceOwningTab
setOfflineComponentProvider(
firestoreImpl,
{
durable: true,
synchronizeTabs: false,
cacheSizeBytes:
settings.cacheSizeBytes || LruParams.DEFAULT_CACHE_SIZE_BYTES,
forceOwningTab: false
},
new IndexedDbOfflineComponentProvider()
)
);
}

Expand All @@ -229,19 +241,20 @@ export function enableMultiTabIndexedDbPersistence(
const offlineComponentProvider = new MultiTabOfflineComponentProvider(
onlineComponentProvider
);
return setOfflineComponentProvider(
firestoreImpl,
{
durable: true,
synchronizeTabs: true,
cacheSizeBytes:
settings.cacheSizeBytes || LruParams.DEFAULT_CACHE_SIZE_BYTES,
forceOwningTab: false
},
offlineComponentProvider
).then(() =>
setOnlineComponentProvider(firestoreImpl, onlineComponentProvider)
);
return firestoreImpl._queue.enqueue(async () => {
await setOfflineComponentProvider(
firestoreImpl,
{
durable: true,
synchronizeTabs: true,
cacheSizeBytes:
settings.cacheSizeBytes || LruParams.DEFAULT_CACHE_SIZE_BYTES,
forceOwningTab: false
},
offlineComponentProvider
);
await setOnlineComponentProvider(firestoreImpl, onlineComponentProvider);
});
}

export function clearIndexedDbPersistence(
Expand Down Expand Up @@ -277,43 +290,42 @@ export function waitForPendingWrites(
firestore: firestore.FirebaseFirestore
): Promise<void> {
const firestoreImpl = cast(firestore, Firestore);
return getSyncEngine(firestoreImpl).then(syncEngine =>
enqueueWaitForPendingWrites(firestoreImpl._queue, syncEngine)
);
firestoreImpl._verifyNotTerminated();

const deferred = new Deferred<void>();
firestoreImpl._queue.enqueueAndForget(async () => {
const syncEngine = await getSyncEngine(firestoreImpl);
return registerPendingWritesCallback(syncEngine, deferred);
});
return deferred.promise;
}

export function enableNetwork(
firestore: firestore.FirebaseFirestore
): Promise<void> {
const firestoreImpl = cast(firestore, Firestore);
return Promise.all([
getRemoteStore(firestoreImpl),
getPersistence(firestoreImpl)
]).then(([remoteStore, persistence]) =>
enqueueNetworkEnabled(
firestoreImpl._queue,
remoteStore,
persistence,
/* enabled= */ true
)
);
firestoreImpl._verifyNotTerminated();

return firestoreImpl._queue.enqueue(async () => {
const remoteStore = await getRemoteStore(firestoreImpl);
const persistence = await getPersistence(firestoreImpl);
persistence.setNetworkEnabled(true);
return remoteStoreEnableNetwork(remoteStore);
});
}

export function disableNetwork(
firestore: firestore.FirebaseFirestore
): Promise<void> {
const firestoreImpl = cast(firestore, Firestore);
return Promise.all([
getRemoteStore(firestoreImpl),
getPersistence(firestoreImpl)
]).then(([remoteStore, persistence]) =>
enqueueNetworkEnabled(
firestoreImpl._queue,
remoteStore,
persistence,
/* enabled= */ false
)
);
firestoreImpl._verifyNotTerminated();

return firestoreImpl._queue.enqueue(async () => {
const remoteStore = await getRemoteStore(firestoreImpl);
const persistence = await getPersistence(firestoreImpl);
persistence.setNetworkEnabled(false);
return remoteStoreDisableNetwork(remoteStore);
});
}

export function terminate(
Expand All @@ -325,7 +337,7 @@ export function terminate(
}

function verifyNotInitialized(firestore: Firestore): void {
if (firestore._initialized) {
if (firestore._initialized || firestore._terminated) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: what's the reason to check for _terminated? Can an instance be terminated without being initialized? If yes, consider adding this bit to the error message below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a user calls "terminate()" on an uninitialized Firestore, we should also not initialize it. The error message is actually shared between the new SDK and the old SDK and hence, I would rather not touch it.

throw new FirestoreError(
Code.FAILED_PRECONDITION,
'Firestore has already been started and persistence can no longer be ' +
Expand Down
Loading