From 6c50d4ddfd8672b2f44f36d0332c1fda395d676f Mon Sep 17 00:00:00 2001 From: Alok Kumar Sahoo <45600289+aloks98@users.noreply.github.com> Date: Fri, 29 Oct 2021 14:35:23 +0530 Subject: [PATCH] chore: make /packages/medusa/src/services/event-bus.js pass linting (#575) --- .eslintignore | 1 - .eslintrc.js | 1 + packages/medusa/src/services/event-bus.js | 35 ++++++++++++----------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/.eslintignore b/.eslintignore index 37366f12a8..e909e2a1cd 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,7 +1,6 @@ # FILES TODO /packages/medusa/src/services/cart.js -/packages/medusa/src/services/event-bus.js /packages/medusa/src/services/fulfillment-provider.js /packages/medusa/src/services/middleware.js /packages/medusa/src/services/payment-provider.js diff --git a/.eslintrc.js b/.eslintrc.js index 5d1b40e35e..fc467a5dc3 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -16,6 +16,7 @@ module.exports = { semi: `off`, "no-unused-expressions": `off`, camelcase: `off`, + "no-invalid-this": `off`, }, env: { es6: true, diff --git a/packages/medusa/src/services/event-bus.js b/packages/medusa/src/services/event-bus.js index 2716934dfd..b2771c5640 100644 --- a/packages/medusa/src/services/event-bus.js +++ b/packages/medusa/src/services/event-bus.js @@ -4,7 +4,7 @@ import Redis from "ioredis" /** * Can keep track of multiple subscribers to different events and run the * subscribers when events happen. Events will run asynchronously. - * @interface + * @class */ class EventBusService { constructor( @@ -13,7 +13,7 @@ class EventBusService { singleton = true ) { const opts = { - createClient: type => { + createClient: (type) => { switch (type) { case "client": return redisClient @@ -127,7 +127,10 @@ class EventBusService { } /** - * + * Adds a function to a list of event subscribers. + * @param {string} event - the event that the subscriber will listen for. + * @param {func} subscriber - the function to be called when a certain event + * happens. Subscribers must return a Promise. */ registerCronHandler_(event, subscriber) { if (typeof subscriber !== "function") { @@ -165,7 +168,7 @@ class EventBusService { } async sleep(ms) { - return new Promise(resolve => { + return new Promise((resolve) => { setTimeout(resolve, ms) }) } @@ -194,7 +197,7 @@ class EventBusService { const jobs = await sjRepo.find({}, listConfig) await Promise.all( - jobs.map(job => { + jobs.map((job) => { this.queue_ .add( { eventName: job.event_name, data: job.data }, @@ -212,13 +215,13 @@ class EventBusService { /** * Handles incoming jobs. - * @param job {{ eventName: (string), data: (any) }} - * eventName - the name of the event to process - * data - data to send to the subscriber + * @param {Object} job The job object + * @param {string} job.eventName The name of the event to process + * @param {any} job.data Data to send to the subscriber * - * @returns {Promise} resolves to the results of the subscriber calls. + * @return {Promise} resolves to the results of the subscriber calls. */ - worker_ = job => { + worker_ = (job) => { const { eventName, data } = job.data const eventObservers = this.observers_[eventName] || [] const wildcardObservers = this.observers_["*"] || [] @@ -230,8 +233,8 @@ class EventBusService { ) return Promise.all( - observers.map(subscriber => { - return subscriber(data, eventName).catch(err => { + observers.map((subscriber) => { + return subscriber(data, eventName).catch((err) => { this.logger_.warn( `An error occured while processing ${eventName}: ${err}` ) @@ -242,14 +245,14 @@ class EventBusService { ) } - cronWorker_ = job => { + cronWorker_ = (job) => { const { eventName, data } = job.data const observers = this.cronHandlers_[eventName] || [] this.logger_.info(`Processing cron job: ${eventName}`) return Promise.all( - observers.map(subscriber => { - return subscriber(data, eventName).catch(err => { + observers.map((subscriber) => { + return subscriber(data, eventName).catch((err) => { this.logger_.warn( `An error occured while processing ${eventName}: ${err}` ) @@ -265,7 +268,7 @@ class EventBusService { * @param {object} data - the data to be sent with the event * @param {string} cron - the cron pattern * @param {function} handler - the handler to call on each cron job - * @return void + * @return {void} */ createCronJob(eventName, data, cron, handler) { this.logger_.info(`Registering ${eventName}`)