From 9f67dc1f877ff2f69862fe5b6b0a5474a3e9dbd0 Mon Sep 17 00:00:00 2001 From: vijay-qlogic <36055624+vijay-qlogic@users.noreply.github.com> Date: Thu, 22 Nov 2018 23:53:02 +0530 Subject: [PATCH] refactor(ts): added ts style fix for src/publisher.ts (#357) --- src/publisher.ts | 68 +++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/src/publisher.ts b/src/publisher.ts index c33ca5a9e..b6b273550 100644 --- a/src/publisher.ts +++ b/src/publisher.ts @@ -14,12 +14,13 @@ * limitations under the License. */ -import * as arrify from 'arrify'; import {promisifyAll} from '@google-cloud/promisify'; +import * as arrify from 'arrify'; + const each = require('async-each'); import * as extend from 'extend'; import * as is from 'is'; -import { Topic } from './topic'; +import {Topic} from './topic'; /** * A Publisher object allows you to publish messages to a specific topic. @@ -48,6 +49,7 @@ import { Topic } from './topic'; * const publisher = topic.publisher(); */ export class Publisher { + // tslint:disable-next-line variable-name Promise?: PromiseConstructor; topic: Topic; inventory_; @@ -58,16 +60,14 @@ export class Publisher { this.Promise = topic.Promise; } options = extend( - true, - { - batching: { - maxBytes: Math.pow(1024, 2) * 5, - maxMessages: 1000, - maxMilliseconds: 100, + true, { + batching: { + maxBytes: Math.pow(1024, 2) * 5, + maxMessages: 1000, + maxMilliseconds: 100, + }, }, - }, - options - ); + options); /** * The topic of this publisher. * @@ -157,20 +157,19 @@ export class Publisher { attributes = {}; } // Ensure the `attributes` object only has string values - for (const key in attributes) { + for (const key of Object.keys(attributes)) { const value = attributes[key]; if (!is.string(value)) { throw new TypeError(`All attributes must be in the form of a string. \nInvalid value of type "${typeof value}" provided for "${key}".`); } } + const opts = this.settings.batching; // if this message puts us over the maxBytes option, then let's ship // what we have and add it to the next batch - if ( - this.inventory_.bytes > 0 && - this.inventory_.bytes + data.length > opts.maxBytes - ) { + if (this.inventory_.bytes > 0 && + this.inventory_.bytes + data.length > opts.maxBytes) { this.publish_(); } // add it to the queue! @@ -184,10 +183,8 @@ export class Publisher { } // otherwise let's set a timeout to send the next batch if (!this.timeoutHandle_) { - this.timeoutHandle_ = setTimeout( - this.publish_.bind(this), - opts.maxMilliseconds - ); + this.timeoutHandle_ = + setTimeout(this.publish_.bind(this), opts.maxMilliseconds); } } /** @@ -205,24 +202,23 @@ export class Publisher { this.timeoutHandle_ = null; const reqOpts = { topic: this.topic.name, - messages: messages, + messages, }; this.topic.request( - { - client: 'PublisherClient', - method: 'publish', - reqOpts: reqOpts, - gaxOpts: this.settings.gaxOpts, - }, - (err, resp) => { - const messageIds = arrify(resp && resp.messageIds); - each(callbacks, (callback, next) => { - const messageId = messageIds[callbacks.indexOf(callback)]; - callback(err, messageId); - next(); + { + client: 'PublisherClient', + method: 'publish', + reqOpts, + gaxOpts: this.settings.gaxOpts, + }, + (err, resp) => { + const messageIds = arrify(resp && resp.messageIds); + each(callbacks, (callback, next) => { + const messageId = messageIds[callbacks.indexOf(callback)]; + callback(err, messageId); + next(); + }); }); - } - ); } /** * Queues message to be sent to the server. @@ -235,7 +231,7 @@ export class Publisher { */ queue_(data, attrs, callback) { this.inventory_.queued.push({ - data: data, + data, attributes: attrs, }); this.inventory_.bytes += data.length;