Skip to content

Commit 82d316b

Browse files
crack two
1 parent 1d0c11c commit 82d316b

File tree

3 files changed

+58
-56
lines changed

3 files changed

+58
-56
lines changed

lib/pubsub/index.js

+53-40
Original file line numberDiff line numberDiff line change
@@ -169,74 +169,76 @@ PubSub.prototype.createTopic = function(name, callback) {
169169
* Your provided callback will either be invoked with an error object, if an API
170170
* error occurred, or a {@linkcode module:pubsub/subscription} object.
171171
*
172-
* @throws {Error} If a name is not provided.
172+
* @throws {Error} If a Topic instance or topic name is not provided.
173+
* @throws {Error} If a subName is not provided.
173174
*
174-
* @param {string} name - The name of the subscription.
175+
* @param {module:pubsub/topic|string} - topic - The Topic to create a
176+
* subscription to.
177+
* @param {string} subName - The name of the subscription.
175178
* @param {object=} options - Configuration object.
176-
* @param {number=} options.ackDeadlineSeconds - The maximum time after
177-
* receiving a message that you must ack a message before it is redelivered.
178-
* @param {boolean=} options.autoAck - Automatically acknowledge the message
179-
* once it's pulled. (default: false)
180-
* @param {number=} options.interval - Interval in milliseconds to check for new
179+
* @param {number} options.ackDeadlineSeconds - The maximum time after receiving
180+
* a message that you must ack a message before it is redelivered.
181+
* @param {boolean} options.autoAck - Automatically acknowledge the message once
182+
* it's pulled. (default: false)
183+
* @param {number} options.interval - Interval in milliseconds to check for new
181184
* messages. (default: 10)
182-
* @param {string=} options.projectId - The projectId where the topic resource
183-
* exists.
184-
* @param {boolean=} options.reuseExisting - If the subscription already exists,
185+
* @param {boolean} options.reuseExisting - If the subscription already exists,
185186
* reuse it. The options of the existing subscription are not changed. If
186187
* false, attempting to create a subscription that already exists will fail.
187188
* (default: false)
188189
* @param {function} callback - The callback function.
189190
*
190191
* @example
191192
* // Without specifying any options.
192-
* var topic = 'messageTopic';
193-
* var sub = 'messageSubscription';
193+
* var topic = pubsub.topic('messageCenter');
194+
* var name = 'newMessages';
194195
*
195-
* pubsub.subscribe(topic, sub, function(err, subscription, apiResponse) {});
196+
* pubsub.subscribe(topic, name, function(err, subscription, apiResponse) {});
196197
*
197198
* // With options.
198-
* pubsub.subscribe(topic, sub, {
199+
* pubsub.subscribe(topic, name, {
199200
* ackDeadlineSeconds: 90,
200201
* autoAck: true,
201202
* interval: 30
202203
* }, function(err, subscription, apiResponse) {});
203204
*/
204-
PubSub.prototype.subscribe = function(topicName, subName, options, callback) {
205-
var self = this;
206-
207-
if (!topicName) {
208-
throw new Error('A Topic name is required for a new subscription.');
205+
PubSub.prototype.subscribe = function(topic, subName, options, callback) {
206+
if (!util.is(topic, 'string') && !(topic instanceof Topic)) {
207+
throw new Error('A Topic is required for a new subscription.');
209208
}
210209

211210
if (!subName) {
212-
throw new Error('A Subscription name is required for a new subscription.');
211+
throw new Error('A subscription name is required for a new subscription.');
213212
}
214213

215214
if (!callback) {
216215
callback = options;
217216
options = {};
218217
}
219218

219+
options = options || {};
220+
221+
if (util.is(topic, 'string')) {
222+
topic = this.topic(topic);
223+
}
224+
220225
var body = {
221-
topic: topicName
226+
topic: topic.name
222227
};
223228

224229
if (options.ackDeadlineSeconds) {
225230
body.ackDeadlineSeconds = options.ackDeadlineSeconds;
226231
}
227232

228-
var projectId = options.projectId || this.projectId;
233+
var subscription = topic.subscription(subName, options);
229234

230-
var path = Subscription.formatName_(projectId, subName);
231-
232-
this.makeReq_('PUT', path, null, body, function(err, result) {
233-
if (options.reuseExisting && err && err.code === 409) {
234-
callback(null, self.subscription(subName, options), result);
235-
} else if (err) {
235+
this.makeReq_('PUT', subscription.name, null, body, function(err, result) {
236+
if (err && !(err.code === 409 && options.reuseExisting)) {
236237
callback(err, null, result);
237-
} else {
238-
callback(null, self.subscription(subName, options), result);
238+
return;
239239
}
240+
241+
callback(null, subscription, result);
240242
});
241243
};
242244

@@ -276,7 +278,6 @@ PubSub.prototype.subscription = function(name, options) {
276278

277279
options = options || {};
278280
options.name = name;
279-
280281
return new Subscription(this, options);
281282
};
282283

@@ -346,8 +347,23 @@ PubSub.prototype.getSubscriptions = function(options, callback) {
346347

347348
options = options || {};
348349

350+
var projectId = this.projectId;
351+
var topicName;
349352
var query = {};
350353

354+
if (util.is(options.topic, 'string')) {
355+
topicName = options.topic;
356+
} else if (options.topic instanceof Topic) {
357+
// Return Subscriptions to the user under the Topic's PubSub instance.
358+
self = options.topic.pubsub;
359+
projectId = self.projectId;
360+
topicName = options.topic.unformattedName;
361+
}
362+
363+
if (options.projectId) {
364+
projectId = options.projectId;
365+
}
366+
351367
if (options.pageSize) {
352368
query.pageSize = options.pageSize;
353369
}
@@ -356,11 +372,9 @@ PubSub.prototype.getSubscriptions = function(options, callback) {
356372
query.pageToken = options.pageToken;
357373
}
358374

359-
var projectId = options.projectId || this.projectId;
360-
361375
var apiPath = util.format('{projectPath}{topicPath}/subscriptions', {
362376
projectPath: 'projects/' + projectId,
363-
topicPath: options.topic ? '/topics/' + options.topic : ''
377+
topicPath: topicName ? '/topics/' + topicName : ''
364378
});
365379

366380
this.makeReq_('GET', apiPath, query, null, function(err, result) {
@@ -370,14 +384,13 @@ PubSub.prototype.getSubscriptions = function(options, callback) {
370384
}
371385

372386
var subscriptions = (result.subscriptions || []).map(function(sub) {
373-
// Depending on if we're using a subscriptions.list or
374-
// topics.subscriptions.list API endpoint, we will get back a Subscription
375-
// resource or just the name of the subscription.
376-
var subName = sub.name || sub;
377-
378387
return new Subscription(self, {
379388
projectId: projectId,
380-
name: subName
389+
390+
// Depending on if we're using a subscriptions.list or
391+
// topics.subscriptions.list API endpoint, we will get back a
392+
// Subscription resource or just the name of the subscription.
393+
name: sub.name || sub
381394
});
382395
});
383396

lib/pubsub/subscription.js

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ function Subscription(pubsub, options) {
117117
var projectId = options.projectId || pubsub.projectId;
118118

119119
this.name = Subscription.formatName_(projectId, options.name);
120+
this.unformattedName = options.name;
120121

121122
this.makeReq_ = pubsub.makeReq_.bind(pubsub);
122123

lib/pubsub/topic.js

+4-16
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ Topic.prototype.getSubscriptions = function(options, callback) {
237237
options = {};
238238
}
239239

240-
options.projectId = this.pubsub.projectId;
241-
options.topic = this.unformattedName;
240+
options = options || {};
241+
options.topic = this;
242242

243243
this.pubsub.getSubscriptions(options, callback);
244244
};
@@ -277,18 +277,8 @@ Topic.prototype.getSubscriptions = function(options, callback) {
277277
* interval: 30
278278
* }, function(err, subscription, apiResponse) {});
279279
*/
280-
Topic.prototype.subscribe = function(name, options, callback) {
281-
if (!callback) {
282-
callback = options;
283-
options = {};
284-
}
285-
286-
var topicName = this.name;
287-
288-
options = options || {};
289-
options.projectId = this.pubsub.projectId;
290-
291-
return this.pubsub.subscribe(topicName, name, options, callback);
280+
Topic.prototype.subscribe = function(subName, options, callback) {
281+
return this.pubsub.subscribe(this, subName, options, callback);
292282
};
293283

294284
/**
@@ -319,8 +309,6 @@ Topic.prototype.subscribe = function(name, options, callback) {
319309
* });
320310
*/
321311
Topic.prototype.subscription = function(name, options) {
322-
options = options || {};
323-
options.projectId = this.pubsub.projectId;
324312
return this.pubsub.subscription(name, options);
325313
};
326314

0 commit comments

Comments
 (0)