21
21
'use strict' ;
22
22
23
23
var common = require ( '@google-cloud/common' ) ;
24
- var events = require ( 'events' ) ;
25
- var modelo = require ( 'modelo' ) ;
24
+ var util = require ( 'util' ) ;
26
25
27
26
/*! Developer Documentation
28
27
*
@@ -159,23 +158,17 @@ function Operation(scope, name) {
159
158
get : true
160
159
} ;
161
160
162
- common . ServiceObject . call ( this , {
161
+ common . Operation . call ( this , {
163
162
parent : scope ,
164
163
baseUrl : isCompute ? '/global/operations' : '/operations' ,
165
164
id : name ,
166
165
methods : methods
167
166
} ) ;
168
167
169
- events . EventEmitter . call ( this ) ;
170
-
171
- this . completeListeners = 0 ;
172
- this . hasActiveListeners = false ;
173
168
this . name = name ;
174
-
175
- this . listenForEvents_ ( ) ;
176
169
}
177
170
178
- modelo . inherits ( Operation , common . ServiceObject , events . EventEmitter ) ;
171
+ util . inherits ( Operation , common . Operation ) ;
179
172
180
173
/**
181
174
* Get the operation's metadata. For a detailed description of metadata see
@@ -230,62 +223,6 @@ Operation.prototype.getMetadata = function(callback) {
230
223
} ) ;
231
224
} ;
232
225
233
- /**
234
- * Convenience method that wraps the `complete` and `error` events in a
235
- * Promise.
236
- *
237
- * @return {promise }
238
- *
239
- * @example
240
- * operation.promise().then(function(metadata) {
241
- * // The operation is complete.
242
- * }, function(err) {
243
- * // An error occurred during the operation.
244
- * });
245
- */
246
- Operation . prototype . promise = function ( ) {
247
- var self = this ;
248
-
249
- return new self . Promise ( function ( resolve , reject ) {
250
- self
251
- . on ( 'error' , reject )
252
- . on ( 'complete' , function ( metadata ) {
253
- resolve ( [ metadata ] ) ;
254
- } ) ;
255
- } ) ;
256
- } ;
257
-
258
- /**
259
- * Begin listening for events on the operation. This method keeps track of how
260
- * many "complete" listeners are registered and removed, making sure polling is
261
- * handled automatically.
262
- *
263
- * As long as there is one active "complete" listener, the connection is open.
264
- * When there are no more listeners, the polling stops.
265
- *
266
- * @private
267
- */
268
- Operation . prototype . listenForEvents_ = function ( ) {
269
- var self = this ;
270
-
271
- this . on ( 'newListener' , function ( event ) {
272
- if ( event === 'complete' ) {
273
- self . completeListeners ++ ;
274
-
275
- if ( ! self . hasActiveListeners ) {
276
- self . hasActiveListeners = true ;
277
- self . startPolling_ ( ) ;
278
- }
279
- }
280
- } ) ;
281
-
282
- this . on ( 'removeListener' , function ( event ) {
283
- if ( event === 'complete' && -- self . completeListeners === 0 ) {
284
- self . hasActiveListeners = false ;
285
- }
286
- } ) ;
287
- } ;
288
-
289
226
/**
290
227
* Poll `getMetadata` to check the operation's status. This runs a loop to ping
291
228
* the API on an interval.
@@ -295,21 +232,17 @@ Operation.prototype.listenForEvents_ = function() {
295
232
*
296
233
* @private
297
234
*/
298
- Operation . prototype . startPolling_ = function ( ) {
235
+ Operation . prototype . poll_ = function ( callback ) {
299
236
var self = this ;
300
237
301
- if ( ! this . hasActiveListeners ) {
302
- return ;
303
- }
304
-
305
238
this . getMetadata ( function ( err , metadata , apiResponse ) {
306
239
// Parsing the response body will automatically create an ApiError object if
307
240
// the operation failed.
308
241
var parsedHttpRespBody = common . util . parseHttpRespBody ( apiResponse ) ;
309
242
err = err || parsedHttpRespBody . err ;
310
243
311
244
if ( err ) {
312
- self . emit ( 'error' , err ) ;
245
+ callback ( err ) ;
313
246
return ;
314
247
}
315
248
@@ -319,12 +252,12 @@ Operation.prototype.startPolling_ = function() {
319
252
}
320
253
321
254
if ( metadata . status !== 'DONE' ) {
322
- setTimeout ( self . startPolling_ . bind ( self ) , 500 ) ;
255
+ callback ( ) ;
323
256
return ;
324
257
}
325
258
326
259
self . status = metadata . status ;
327
- self . emit ( 'complete' , metadata ) ;
260
+ callback ( null , metadata ) ;
328
261
} ) ;
329
262
} ;
330
263
0 commit comments