@@ -41,9 +41,14 @@ export class SpeechClient {
41
41
private _descriptors : Descriptors = { page : { } , stream : { } , longrunning : { } } ;
42
42
private _innerApiCalls : { [ name : string ] : Function } ;
43
43
private _terminated = false ;
44
+ private _opts : ClientOptions ;
45
+ private _gaxModule : typeof gax | typeof gax . fallback ;
46
+ private _gaxGrpc : gax . GrpcClient | gax . fallback . GrpcClient ;
47
+ private _protos : { } ;
48
+ private _defaults : { [ method : string ] : gax . CallSettings } ;
44
49
auth : gax . GoogleAuth ;
45
50
operationsClient : gax . OperationsClient ;
46
- speechStub : Promise < { [ name : string ] : Function } > ;
51
+ speechStub ? : Promise < { [ name : string ] : Function } > ;
47
52
48
53
/**
49
54
* Construct an instance of SpeechClient.
@@ -67,8 +72,6 @@ export class SpeechClient {
67
72
* app is running in an environment which supports
68
73
* {@link https://developers.google.com/identity/protocols/application-default-credentials Application Default Credentials},
69
74
* your project ID will be detected automatically.
70
- * @param {function } [options.promise] - Custom promise module to use instead
71
- * of native Promises.
72
75
* @param {string } [options.apiEndpoint] - The domain name of the
73
76
* API remote host.
74
77
*/
@@ -98,25 +101,28 @@ export class SpeechClient {
98
101
// If we are in browser, we are already using fallback because of the
99
102
// "browser" field in package.json.
100
103
// But if we were explicitly requested to use fallback, let's do it now.
101
- const gaxModule = ! isBrowser && opts . fallback ? gax . fallback : gax ;
104
+ this . _gaxModule = ! isBrowser && opts . fallback ? gax . fallback : gax ;
102
105
103
106
// Create a `gaxGrpc` object, with any grpc-specific options
104
107
// sent to the client.
105
108
opts . scopes = ( this . constructor as typeof SpeechClient ) . scopes ;
106
- const gaxGrpc = new gaxModule . GrpcClient ( opts ) ;
109
+ this . _gaxGrpc = new this . _gaxModule . GrpcClient ( opts ) ;
110
+
111
+ // Save options to use in initialize() method.
112
+ this . _opts = opts ;
107
113
108
114
// Save the auth object to the client, for use by other methods.
109
- this . auth = gaxGrpc . auth as gax . GoogleAuth ;
115
+ this . auth = this . _gaxGrpc . auth as gax . GoogleAuth ;
110
116
111
117
// Determine the client header string.
112
- const clientHeader = [ `gax/${ gaxModule . version } ` , `gapic/${ version } ` ] ;
118
+ const clientHeader = [ `gax/${ this . _gaxModule . version } ` , `gapic/${ version } ` ] ;
113
119
if ( typeof process !== 'undefined' && 'versions' in process ) {
114
120
clientHeader . push ( `gl-node/${ process . versions . node } ` ) ;
115
121
} else {
116
- clientHeader . push ( `gl-web/${ gaxModule . version } ` ) ;
122
+ clientHeader . push ( `gl-web/${ this . _gaxModule . version } ` ) ;
117
123
}
118
124
if ( ! opts . fallback ) {
119
- clientHeader . push ( `grpc/${ gaxGrpc . grpcVersion } ` ) ;
125
+ clientHeader . push ( `grpc/${ this . _gaxGrpc . grpcVersion } ` ) ;
120
126
}
121
127
if ( opts . libName && opts . libVersion ) {
122
128
clientHeader . push ( `${ opts . libName } /${ opts . libVersion } ` ) ;
@@ -132,14 +138,14 @@ export class SpeechClient {
132
138
'protos' ,
133
139
'protos.json'
134
140
) ;
135
- const protos = gaxGrpc . loadProto (
141
+ this . _protos = this . _gaxGrpc . loadProto (
136
142
opts . fallback ? require ( '../../protos/protos.json' ) : nodejsProtoPath
137
143
) ;
138
144
139
145
// Some of the methods on this service provide streaming responses.
140
146
// Provide descriptors for these.
141
147
this . _descriptors . stream = {
142
- streamingRecognize : new gaxModule . StreamDescriptor (
148
+ streamingRecognize : new this . _gaxModule . StreamDescriptor (
143
149
gax . StreamType . BIDI_STREAMING
144
150
) ,
145
151
} ;
@@ -148,13 +154,15 @@ export class SpeechClient {
148
154
// an Operation object that allows for tracking of the operation,
149
155
// rather than holding a request open.
150
156
const protoFilesRoot = opts . fallback
151
- ? gaxModule . protobuf . Root . fromJSON ( require ( '../../protos/protos.json' ) )
152
- : gaxModule . protobuf . loadSync ( nodejsProtoPath ) ;
157
+ ? this . _gaxModule . protobuf . Root . fromJSON (
158
+ require ( '../../protos/protos.json' )
159
+ )
160
+ : this . _gaxModule . protobuf . loadSync ( nodejsProtoPath ) ;
153
161
154
- this . operationsClient = gaxModule
162
+ this . operationsClient = this . _gaxModule
155
163
. lro ( {
156
164
auth : this . auth ,
157
- grpc : 'grpc' in gaxGrpc ? gaxGrpc . grpc : undefined ,
165
+ grpc : 'grpc' in this . _gaxGrpc ? this . _gaxGrpc . grpc : undefined ,
158
166
} )
159
167
. operationsClient ( opts ) ;
160
168
const longRunningRecognizeResponse = protoFilesRoot . lookup (
@@ -165,15 +173,15 @@ export class SpeechClient {
165
173
) as gax . protobuf . Type ;
166
174
167
175
this . _descriptors . longrunning = {
168
- longRunningRecognize : new gaxModule . LongrunningDescriptor (
176
+ longRunningRecognize : new this . _gaxModule . LongrunningDescriptor (
169
177
this . operationsClient ,
170
178
longRunningRecognizeResponse . decode . bind ( longRunningRecognizeResponse ) ,
171
179
longRunningRecognizeMetadata . decode . bind ( longRunningRecognizeMetadata )
172
180
) ,
173
181
} ;
174
182
175
183
// Put together the default options sent with requests.
176
- const defaults = gaxGrpc . constructSettings (
184
+ this . _defaults = this . _gaxGrpc . constructSettings (
177
185
'google.cloud.speech.v1.Speech' ,
178
186
gapicConfig as gax . ClientConfig ,
179
187
opts . clientConfig || { } ,
@@ -184,17 +192,35 @@ export class SpeechClient {
184
192
// of calling the API is handled in `google-gax`, with this code
185
193
// merely providing the destination and request information.
186
194
this . _innerApiCalls = { } ;
195
+ }
196
+
197
+ /**
198
+ * Initialize the client.
199
+ * Performs asynchronous operations (such as authentication) and prepares the client.
200
+ * This function will be called automatically when any class method is called for the
201
+ * first time, but if you need to initialize it before calling an actual method,
202
+ * feel free to call initialize() directly.
203
+ *
204
+ * You can await on this method if you want to make sure the client is initialized.
205
+ *
206
+ * @returns {Promise } A promise that resolves to an authenticated service stub.
207
+ */
208
+ initialize ( ) {
209
+ // If the client stub promise is already initialized, return immediately.
210
+ if ( this . speechStub ) {
211
+ return this . speechStub ;
212
+ }
187
213
188
214
// Put together the "service stub" for
189
215
// google.cloud.speech.v1.Speech.
190
- this . speechStub = gaxGrpc . createStub (
191
- opts . fallback
192
- ? ( protos as protobuf . Root ) . lookupService (
216
+ this . speechStub = this . _gaxGrpc . createStub (
217
+ this . _opts . fallback
218
+ ? ( this . _protos as protobuf . Root ) . lookupService (
193
219
'google.cloud.speech.v1.Speech'
194
220
)
195
221
: // tslint:disable-next-line no-any
196
- ( protos as any ) . google . cloud . speech . v1 . Speech ,
197
- opts
222
+ ( this . _protos as any ) . google . cloud . speech . v1 . Speech ,
223
+ this . _opts
198
224
) as Promise < { [ method : string ] : Function } > ;
199
225
200
226
// Iterate over each of the methods that the service provides
@@ -218,9 +244,9 @@ export class SpeechClient {
218
244
}
219
245
) ;
220
246
221
- const apiCall = gaxModule . createApiCall (
247
+ const apiCall = this . _gaxModule . createApiCall (
222
248
innerCallPromise ,
223
- defaults [ methodName ] ,
249
+ this . _defaults [ methodName ] ,
224
250
this . _descriptors . page [ methodName ] ||
225
251
this . _descriptors . stream [ methodName ] ||
226
252
this . _descriptors . longrunning [ methodName ]
@@ -234,6 +260,8 @@ export class SpeechClient {
234
260
return apiCall ( argument , callOptions , callback ) ;
235
261
} ;
236
262
}
263
+
264
+ return this . speechStub ;
237
265
}
238
266
239
267
/**
@@ -352,6 +380,7 @@ export class SpeechClient {
352
380
options = optionsOrCallback as gax . CallOptions ;
353
381
}
354
382
options = options || { } ;
383
+ this . initialize ( ) ;
355
384
return this . _innerApiCalls . recognize ( request , options , callback ) ;
356
385
}
357
386
@@ -367,6 +396,7 @@ export class SpeechClient {
367
396
* will emit objects representing [StreamingRecognizeResponse]{@link google.cloud.speech.v1.StreamingRecognizeResponse} on 'data' event asynchronously.
368
397
*/
369
398
_streamingRecognize ( options ?: gax . CallOptions ) : gax . CancellableStream {
399
+ this . initialize ( ) ;
370
400
return this . _innerApiCalls . streamingRecognize ( options ) ;
371
401
}
372
402
@@ -455,6 +485,7 @@ export class SpeechClient {
455
485
options = optionsOrCallback as gax . CallOptions ;
456
486
}
457
487
options = options || { } ;
488
+ this . initialize ( ) ;
458
489
return this . _innerApiCalls . longRunningRecognize ( request , options , callback ) ;
459
490
}
460
491
@@ -464,8 +495,9 @@ export class SpeechClient {
464
495
* The client will no longer be usable and all future behavior is undefined.
465
496
*/
466
497
close ( ) : Promise < void > {
498
+ this . initialize ( ) ;
467
499
if ( ! this . _terminated ) {
468
- return this . speechStub . then ( stub => {
500
+ return this . speechStub ! . then ( stub => {
469
501
this . _terminated = true ;
470
502
stub . close ( ) ;
471
503
} ) ;
0 commit comments