Skip to content

Add internal delete method to functions #1687

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 6 commits into from
Apr 16, 2019
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