Skip to content

Commit 556b345

Browse files
stephenpluspluscallmehiphop
authored andcommitted
compute: use common/Operation (#1773)
1 parent 31da868 commit 556b345

File tree

4 files changed

+69
-302
lines changed

4 files changed

+69
-302
lines changed

packages/compute/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,13 @@
5050
"compute engine"
5151
],
5252
"dependencies": {
53-
"@google-cloud/common": "^0.7.0",
53+
"@google-cloud/common": "^0.8.0",
5454
"arrify": "^1.0.0",
5555
"async": "^2.0.1",
5656
"create-error-class": "^3.0.2",
5757
"extend": "^3.0.0",
5858
"gce-images": "^0.3.0",
5959
"is": "^3.0.1",
60-
"modelo": "^4.2.0",
6160
"string-format-obj": "^1.0.0"
6261
},
6362
"devDependencies": {

packages/compute/src/operation.js

Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
'use strict';
2222

2323
var common = require('@google-cloud/common');
24-
var events = require('events');
25-
var modelo = require('modelo');
24+
var util = require('util');
2625

2726
/*! Developer Documentation
2827
*
@@ -159,23 +158,17 @@ function Operation(scope, name) {
159158
get: true
160159
};
161160

162-
common.ServiceObject.call(this, {
161+
common.Operation.call(this, {
163162
parent: scope,
164163
baseUrl: isCompute ? '/global/operations' : '/operations',
165164
id: name,
166165
methods: methods
167166
});
168167

169-
events.EventEmitter.call(this);
170-
171-
this.completeListeners = 0;
172-
this.hasActiveListeners = false;
173168
this.name = name;
174-
175-
this.listenForEvents_();
176169
}
177170

178-
modelo.inherits(Operation, common.ServiceObject, events.EventEmitter);
171+
util.inherits(Operation, common.Operation);
179172

180173
/**
181174
* Get the operation's metadata. For a detailed description of metadata see
@@ -230,62 +223,6 @@ Operation.prototype.getMetadata = function(callback) {
230223
});
231224
};
232225

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-
289226
/**
290227
* Poll `getMetadata` to check the operation's status. This runs a loop to ping
291228
* the API on an interval.
@@ -295,21 +232,17 @@ Operation.prototype.listenForEvents_ = function() {
295232
*
296233
* @private
297234
*/
298-
Operation.prototype.startPolling_ = function() {
235+
Operation.prototype.poll_ = function(callback) {
299236
var self = this;
300237

301-
if (!this.hasActiveListeners) {
302-
return;
303-
}
304-
305238
this.getMetadata(function(err, metadata, apiResponse) {
306239
// Parsing the response body will automatically create an ApiError object if
307240
// the operation failed.
308241
var parsedHttpRespBody = common.util.parseHttpRespBody(apiResponse);
309242
err = err || parsedHttpRespBody.err;
310243

311244
if (err) {
312-
self.emit('error', err);
245+
callback(err);
313246
return;
314247
}
315248

@@ -319,12 +252,12 @@ Operation.prototype.startPolling_ = function() {
319252
}
320253

321254
if (metadata.status !== 'DONE') {
322-
setTimeout(self.startPolling_.bind(self), 500);
255+
callback();
323256
return;
324257
}
325258

326259
self.status = metadata.status;
327-
self.emit('complete', metadata);
260+
callback(null, metadata);
328261
});
329262
};
330263

packages/compute/system-test/compute.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,16 @@ describe('Compute', function() {
253253
});
254254
});
255255
});
256+
257+
it('should run operation as a promise', function() {
258+
var snapshot = disk.snapshot(generateName('snapshot'));
259+
260+
return snapshot.create()
261+
.then(function(response) {
262+
var operation = response[1];
263+
return operation.promise();
264+
});
265+
});
256266
});
257267

258268
describe('firewalls', function() {

0 commit comments

Comments
 (0)