Skip to content

Commit 780c302

Browse files
authored
Add internal delete method to functions (#1687)
Implement `INTERNAL.delete()` method for Functions service.
1 parent eb82794 commit 780c302

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

packages/functions/src/api/service.ts

+26-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
import { FirebaseApp } from '@firebase/app-types';
19+
import { FirebaseService } from '@firebase/app-types/private';
1920
import firebase from '@firebase/app';
2021
import {
2122
FirebaseFunctions,
@@ -53,10 +54,12 @@ function failAfter(millis: number): Promise<HttpResponse> {
5354
/**
5455
* The main class for the Firebase Functions SDK.
5556
*/
56-
export class Service implements FirebaseFunctions {
57+
export class Service implements FirebaseFunctions, FirebaseService {
5758
private readonly contextProvider: ContextProvider;
5859
private readonly serializer = new Serializer();
5960
private emulatorOrigin: string | null = null;
61+
private cancelAllRequests: Promise<void>;
62+
private deleteService: Function;
6063

6164
/**
6265
* Creates a new Functions service for the given app and (optional) region.
@@ -68,12 +71,24 @@ export class Service implements FirebaseFunctions {
6871
private region_: string = 'us-central1'
6972
) {
7073
this.contextProvider = new ContextProvider(app_);
74+
// Cancels all ongoing requests when resolved.
75+
this.cancelAllRequests = new Promise(resolve => {
76+
this.deleteService = () => {
77+
return resolve();
78+
};
79+
});
7180
}
7281

7382
get app(): FirebaseApp {
7483
return this.app_;
7584
}
7685

86+
INTERNAL = {
87+
delete: (): Promise<void> => {
88+
return this.deleteService();
89+
}
90+
};
91+
7792
/**
7893
* Returns the URL for a callable with the given name.
7994
* @param name The name of the callable.
@@ -184,9 +199,18 @@ export class Service implements FirebaseFunctions {
184199

185200
const response = await Promise.race([
186201
this.postJSON(url, body, headers),
187-
failAfter(timeout)
202+
failAfter(timeout),
203+
this.cancelAllRequests
188204
]);
189205

206+
// If service was deleted, interrupted response throws an error.
207+
if (!response) {
208+
throw new HttpsErrorImpl(
209+
'cancelled',
210+
'Firebase Functions instance was deleted.'
211+
);
212+
}
213+
190214
// Check for an error status, regardless of http status.
191215
const error = _errorForResponse(
192216
response.status,

0 commit comments

Comments
 (0)