|
| 1 | +/*! |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/*! |
| 18 | + * @module common/operation |
| 19 | + */ |
| 20 | + |
| 21 | +'use strict'; |
| 22 | + |
| 23 | +var events = require('events'); |
| 24 | +var extend = require('extend'); |
| 25 | +var modelo = require('modelo'); |
| 26 | + |
| 27 | +/** |
| 28 | + * @type {module:common/service} |
| 29 | + * @private |
| 30 | + */ |
| 31 | +var GrpcService = require('./grpc-service.js'); |
| 32 | + |
| 33 | +/** |
| 34 | + * @type {module:common/serviceObject} |
| 35 | + * @private |
| 36 | + */ |
| 37 | +var ServiceObject = require('./service-object.js'); |
| 38 | + |
| 39 | +// jscs:disable maximumLineLength |
| 40 | +/** |
| 41 | + * An Operation object allows you to interact with APIs that take longer to |
| 42 | + * process things. |
| 43 | + * |
| 44 | + * @constructor |
| 45 | + * @alias module:common/operation |
| 46 | + * |
| 47 | + * @param {object} config - Configuration object. |
| 48 | + * @param {module:common/service|module:common/serviceObject|module:common/grpcService|module:common/grpcServiceObject} config.parent - The |
| 49 | + * parent object. |
| 50 | + * @param {string} id - The operation ID. |
| 51 | + */ |
| 52 | +// jscs:enable maximumLineLength |
| 53 | +function Operation(config) { |
| 54 | + var methods = { |
| 55 | + /** |
| 56 | + * Checks to see if an operation exists. |
| 57 | + */ |
| 58 | + exists: true, |
| 59 | + |
| 60 | + /** |
| 61 | + * Retrieves the operation. |
| 62 | + */ |
| 63 | + get: true, |
| 64 | + |
| 65 | + /** |
| 66 | + * Retrieves metadata for the operation. |
| 67 | + */ |
| 68 | + getMetadata: { |
| 69 | + reqOpts: { |
| 70 | + name: config.id |
| 71 | + } |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + config = extend({ |
| 76 | + baseUrl: '' |
| 77 | + }, config); |
| 78 | + |
| 79 | + config.methods = config.methods || methods; |
| 80 | + |
| 81 | + ServiceObject.call(this, config); |
| 82 | + events.EventEmitter.call(this); |
| 83 | + |
| 84 | + this.completeListeners = 0; |
| 85 | + this.hasActiveListeners = false; |
| 86 | + |
| 87 | + this.listenForEvents_(); |
| 88 | +} |
| 89 | + |
| 90 | +modelo.inherits(Operation, ServiceObject, events.EventEmitter); |
| 91 | + |
| 92 | +/** |
| 93 | + * Wraps the `complete` and `error` events in a Promise. |
| 94 | + * |
| 95 | + * @return {promise} |
| 96 | + */ |
| 97 | +Operation.prototype.promise = function() { |
| 98 | + var self = this; |
| 99 | + |
| 100 | + return new self.Promise(function(resolve, reject) { |
| 101 | + self |
| 102 | + .on('error', reject) |
| 103 | + .on('complete', function(metadata) { |
| 104 | + resolve([metadata]); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}; |
| 108 | + |
| 109 | +/** |
| 110 | + * Begin listening for events on the operation. This method keeps track of how |
| 111 | + * many "complete" listeners are registered and removed, making sure polling is |
| 112 | + * handled automatically. |
| 113 | + * |
| 114 | + * As long as there is one active "complete" listener, the connection is open. |
| 115 | + * When there are no more listeners, the polling stops. |
| 116 | + * |
| 117 | + * @private |
| 118 | + */ |
| 119 | +Operation.prototype.listenForEvents_ = function() { |
| 120 | + var self = this; |
| 121 | + |
| 122 | + this.on('newListener', function(event) { |
| 123 | + if (event === 'complete') { |
| 124 | + self.completeListeners++; |
| 125 | + |
| 126 | + if (!self.hasActiveListeners) { |
| 127 | + self.hasActiveListeners = true; |
| 128 | + self.startPolling_(); |
| 129 | + } |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + this.on('removeListener', function(event) { |
| 134 | + if (event === 'complete' && --self.completeListeners === 0) { |
| 135 | + self.hasActiveListeners = false; |
| 136 | + } |
| 137 | + }); |
| 138 | +}; |
| 139 | + |
| 140 | +/** |
| 141 | + * Poll for a status update. Execute the callback: |
| 142 | + * |
| 143 | + * - callback(err): Operation failed |
| 144 | + * - callback(): Operation incomplete |
| 145 | + * - callback(null, metadata): Operation complete |
| 146 | + * |
| 147 | + * @private |
| 148 | + * |
| 149 | + * @param {function} callback |
| 150 | + */ |
| 151 | +Operation.prototype.poll_ = function(callback) { |
| 152 | + this.getMetadata(function(err, resp) { |
| 153 | + if (err || resp.error) { |
| 154 | + callback(err || GrpcService.decorateGrpcStatus_(resp.error)); |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + if (!resp.done) { |
| 159 | + callback(); |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + callback(null, resp); |
| 164 | + }); |
| 165 | +}; |
| 166 | + |
| 167 | +/** |
| 168 | + * Poll `getMetadata` to check the operation's status. This runs a loop to ping |
| 169 | + * the API on an interval. |
| 170 | + * |
| 171 | + * Note: This method is automatically called once a "complete" event handler is |
| 172 | + * registered on the operation. |
| 173 | + * |
| 174 | + * @private |
| 175 | + */ |
| 176 | +Operation.prototype.startPolling_ = function() { |
| 177 | + var self = this; |
| 178 | + |
| 179 | + if (!this.hasActiveListeners) { |
| 180 | + return; |
| 181 | + } |
| 182 | + |
| 183 | + this.poll_(function(err, metadata) { |
| 184 | + if (err) { |
| 185 | + self.emit('error', err); |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + if (!metadata) { |
| 190 | + setTimeout(self.startPolling_.bind(self), 500); |
| 191 | + return; |
| 192 | + } |
| 193 | + |
| 194 | + self.emit('complete', metadata); |
| 195 | + }); |
| 196 | +}; |
| 197 | + |
| 198 | +module.exports = Operation; |
0 commit comments