16
16
*/
17
17
18
18
import { FirebaseApp } from '@firebase/app-types' ;
19
+ import { FirebaseService } from '@firebase/app-types/private' ;
19
20
import firebase from '@firebase/app' ;
20
21
import {
21
22
FirebaseFunctions ,
@@ -53,10 +54,12 @@ function failAfter(millis: number): Promise<HttpResponse> {
53
54
/**
54
55
* The main class for the Firebase Functions SDK.
55
56
*/
56
- export class Service implements FirebaseFunctions {
57
+ export class Service implements FirebaseFunctions , FirebaseService {
57
58
private readonly contextProvider : ContextProvider ;
58
59
private readonly serializer = new Serializer ( ) ;
59
60
private emulatorOrigin : string | null = null ;
61
+ private cancelAllRequests : Promise < void > ;
62
+ private deleteService : Function ;
60
63
61
64
/**
62
65
* Creates a new Functions service for the given app and (optional) region.
@@ -68,12 +71,24 @@ export class Service implements FirebaseFunctions {
68
71
private region_ : string = 'us-central1'
69
72
) {
70
73
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
+ } ) ;
71
80
}
72
81
73
82
get app ( ) : FirebaseApp {
74
83
return this . app_ ;
75
84
}
76
85
86
+ INTERNAL = {
87
+ delete : ( ) : Promise < void > => {
88
+ return this . deleteService ( ) ;
89
+ }
90
+ } ;
91
+
77
92
/**
78
93
* Returns the URL for a callable with the given name.
79
94
* @param name The name of the callable.
@@ -184,9 +199,18 @@ export class Service implements FirebaseFunctions {
184
199
185
200
const response = await Promise . race ( [
186
201
this . postJSON ( url , body , headers ) ,
187
- failAfter ( timeout )
202
+ failAfter ( timeout ) ,
203
+ this . cancelAllRequests
188
204
] ) ;
189
205
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
+
190
214
// Check for an error status, regardless of http status.
191
215
const error = _errorForResponse (
192
216
response . status ,
0 commit comments