@@ -146,16 +146,24 @@ Subscription.formatName_ = function(projectId, name) {
146146 * @private
147147 */
148148Subscription . formatMessage_ = function ( msg ) {
149+ var event = msg . pubsubEvent ;
150+
149151 var message = {
150- id : msg . ackId
152+ ackId : msg . ackId
151153 } ;
152- var evt = msg . pubsubEvent ;
153- if ( evt && evt . message && evt . message . data ) {
154- message . data = new Buffer ( evt . message . data , 'base64' ) . toString ( 'utf-8' ) ;
155- try {
156- message . data = JSON . parse ( message . data ) ;
157- } catch ( e ) { }
154+
155+ if ( event && event . message ) {
156+ message . id = event . message . messageId ;
157+
158+ if ( event . message . data ) {
159+ message . data = new Buffer ( event . message . data , 'base64' ) . toString ( 'utf-8' ) ;
160+
161+ try {
162+ message . data = JSON . parse ( message . data ) ;
163+ } catch ( e ) { }
164+ }
158165 }
166+
159167 return message ;
160168} ;
161169
@@ -216,6 +224,7 @@ Subscription.prototype.startPulling_ = function() {
216224 return ;
217225 }
218226 this . pull ( {
227+ maxResults : 1 ,
219228 returnImmediately : false
220229 } , function ( err , message ) {
221230 if ( err ) {
@@ -243,7 +252,7 @@ Subscription.prototype.startPulling_ = function() {
243252Subscription . prototype . ack = function ( ids , callback ) {
244253 if ( ! ids || ids . length === 0 ) {
245254 throw new Error (
246- 'At least one ID must be specified before it can be acknowledged' ) ;
255+ 'At least one ID must be specified before it can be acknowledged. ' ) ;
247256 }
248257 ids = util . arrayize ( ids ) ;
249258 var body = {
@@ -278,50 +287,86 @@ Subscription.prototype.delete = function(callback) {
278287
279288/**
280289 * Pull messages from the subscribed topic. If messages were found, your
281- * callback is executed with the message object .
290+ * callback is executed with an array of message objects .
282291 *
283292 * Note that messages are pulled automatically once you register your first
284293 * event listener to the subscription, thus the call to `pull` is handled for
285294 * you. If you don't want to start pulling, simply don't register a
286295 * `subscription.on('message', function() {})` event handler.
287296 *
297+ * @todo Should not be racing with other pull.
298+ * @todo Fix API to return a list of messages.
299+ *
288300 * @param {object= } options - Configuration object.
289- * @param {boolean= } options.returnImmediately - If set, the system will respond
301+ * @param {boolean } options.returnImmediately - If set, the system will respond
290302 * immediately. Otherwise, wait until new messages are available. Returns if
291303 * timeout is reached.
304+ * @param {number } options.maxResults - Limit the amount of messages pulled.
292305 * @param {function } callback - The callback function.
293306 *
294307 * @example
295- * subscription.pull(function(err, message) {
296- * // message.id = ID used to acknowledge its receival.
297- * // message.data = Contents of the message.
308+ * //-
309+ * // Pull all available messages.
310+ * //-
311+ * subscription.pull(function(err, messages) {
312+ * // messages = [
313+ * // {
314+ * // ackId: '', // ID used to acknowledge its receival.
315+ * // id: '', // Unique message ID.
316+ * // data: '' // Contents of the message.
317+ * // },
318+ * // // ...
319+ * // ]
298320 * });
321+ *
322+ * //-
323+ * // Pull a single message.
324+ * //-
325+ * var opts = {
326+ * maxResults: 1
327+ * };
328+ *
329+ * subscription.pull(opts, function(err, messages) {});
299330 */
300331Subscription . prototype . pull = function ( options , callback ) {
301332 var that = this ;
302- // TODO(jbd): Should not be racing with other pull.
333+ var MAX_EVENTS_LIMIT = 1000 ;
334+ var apiEndpoint = 'subscriptions/pullBatch' ;
335+
303336 if ( ! callback ) {
304337 callback = options ;
305338 options = { } ;
306339 }
340+
341+ if ( ! util . is ( options . maxResults , 'number' ) ) {
342+ options . maxResults = MAX_EVENTS_LIMIT ;
343+ }
344+
307345 var body = {
308346 subscription : this . name ,
309- returnImmediately : ! ! options . returnImmediately
347+ returnImmediately : ! ! options . returnImmediately ,
348+ maxEvents : options . maxResults
310349 } ;
311- this . makeReq_ (
312- 'POST' , 'subscriptions/pull' , null , body , function ( err , message ) {
313- // TODO(jbd): Fix API to return a list of messages.
350+
351+ this . makeReq_ ( 'POST' , apiEndpoint , null , body , function ( err , response ) {
314352 if ( err ) {
315353 callback ( err ) ;
316354 return ;
317355 }
318- message = Subscription . formatMessage_ ( message ) ;
356+
357+ var messages = response . pullResponses || [ response ] ;
358+ messages = messages . map ( Subscription . formatMessage_ ) ;
359+
319360 if ( that . autoAck ) {
320- that . ack ( message . id , function ( err ) {
321- callback ( err , message ) ;
361+ var ackIds = messages . map ( function ( message ) {
362+ return message . ackId ;
363+ } ) ;
364+
365+ that . ack ( ackIds , function ( err ) {
366+ callback ( err , messages ) ;
322367 } ) ;
323368 } else {
324- callback ( null , message ) ;
369+ callback ( null , messages ) ;
325370 }
326371 } ) ;
327372} ;
0 commit comments