Skip to content
Merged
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
28 changes: 26 additions & 2 deletions packages/functions/src/api/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import { FirebaseApp } from '@firebase/app-types';
import { FirebaseService } from '@firebase/app-types/private';
import firebase from '@firebase/app';
import {
FirebaseFunctions,
Expand Down Expand Up @@ -53,10 +54,12 @@ function failAfter(millis: number): Promise<HttpResponse> {
/**
* The main class for the Firebase Functions SDK.
*/
export class Service implements FirebaseFunctions {
export class Service implements FirebaseFunctions, FirebaseService {
private readonly contextProvider: ContextProvider;
private readonly serializer = new Serializer();
private emulatorOrigin: string | null = null;
private cancelAllRequests: Promise<void>;
private deleteService: Function;

/**
* Creates a new Functions service for the given app and (optional) region.
Expand All @@ -68,12 +71,24 @@ export class Service implements FirebaseFunctions {
private region_: string = 'us-central1'
) {
this.contextProvider = new ContextProvider(app_);
// Cancels all ongoing requests when resolved.
this.cancelAllRequests = new Promise(resolve => {
this.deleteService = () => {
return resolve();
};
});
}

get app(): FirebaseApp {
return this.app_;
}

INTERNAL = {
delete: (): Promise<void> => {
return this.deleteService();
}
};

/**
* Returns the URL for a callable with the given name.
* @param name The name of the callable.
Expand Down Expand Up @@ -184,9 +199,18 @@ export class Service implements FirebaseFunctions {

const response = await Promise.race([
this.postJSON(url, body, headers),
failAfter(timeout)
failAfter(timeout),
this.cancelAllRequests
]);

// If service was deleted, interrupted response throws an error.
if (!response) {
throw new HttpsErrorImpl(
'cancelled',
'Firebase Functions instance was deleted.'
);
}

// Check for an error status, regardless of http status.
const error = _errorForResponse(
response.status,
Expand Down