From b2622b6bc264cf3efc66f2d43ef5e9ceeac68728 Mon Sep 17 00:00:00 2001 From: Mikhail Cheshkov Date: Wed, 18 Jul 2018 16:48:48 +0300 Subject: [PATCH] Use p-event in ClusterProcess, WorkerPool and Worker --- lib/cluster_process.js | 6 +++--- lib/worker-pool.js | 4 +--- lib/worker.js | 8 +++----- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/cluster_process.js b/lib/cluster_process.js index d51c23f..8c9e241 100644 --- a/lib/cluster_process.js +++ b/lib/cluster_process.js @@ -7,6 +7,8 @@ const cluster = require('cluster'), LusterClusterProcessError = require('./errors').LusterClusterProcessError, LusterConfigurationError = require('./errors').LusterConfigurationError; +const pEvent = require('p-event'); + /** * @param {Object} context * @param {String} propName @@ -59,9 +61,7 @@ class ClusterProcess extends EventEmitterEx { * @type Promise * @private * */ - this._initPromise = new Promise(resolve => { - this.once('initialized', resolve); - }); + this._initPromise = pEvent(this, 'initialized'); /** * @type {Configuration} diff --git a/lib/worker-pool.js b/lib/worker-pool.js index 5b7addb..d6724fc 100644 --- a/lib/worker-pool.js +++ b/lib/worker-pool.js @@ -35,9 +35,7 @@ class WorkerPool extends EventEmitterEx { */ this._restartQueue = new RestartQueue(key); - this._runningPromise = new Promise(resolve => { - this.once('running', resolve); - }); + this._runningPromise = pEvent(this, 'running'); this.dead = false; diff --git a/lib/worker.js b/lib/worker.js index 248ce41..8d961a6 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -4,6 +4,8 @@ const cluster = require('cluster'), ClusterProcess = require('./cluster_process'), LusterWorkerError = require('./errors').LusterWorkerError; +const pEvent = require('p-event'); + const wid = parseInt(process.env.LUSTER_WID, 10); /** @@ -19,11 +21,7 @@ class Worker extends ClusterProcess { const broadcastEvent = this._broadcastEvent; - this._foreignPropertiesReceivedPromise = new Promise(resolve => { - this.once('foreign properties received', () => { - resolve(); - }); - }); + this._foreignPropertiesReceivedPromise = pEvent(this, 'foreign properties received'); this.on('configured', broadcastEvent.bind(this, 'configured')); this.on('extension loaded', broadcastEvent.bind(this, 'extension loaded'));