From 9cb12e68c9f88566f993ba2713486f02d54dd6ac Mon Sep 17 00:00:00 2001 From: Daniel <nieltansah@gmail.com> Date: Tue, 21 Nov 2017 21:40:03 +0700 Subject: [PATCH] app: model: Simplify Hub model --- app/model/Hub.js | 48 ++++++++++++++++++------------------------------ 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/app/model/Hub.js b/app/model/Hub.js index 558fefa..c630a02 100644 --- a/app/model/Hub.js +++ b/app/model/Hub.js @@ -39,39 +39,23 @@ class Hub { let indexLambda = index.bind(this) let unindexLambda = unindex.bind(this) - this.wait = (id, locations) => { - let rejectCallback = null - - let promise = (async () => { - let promise = new Promise((resolve, reject) => { - this.drivers[id] = { - resolve: resolve, - reject: reject - } - - // Keep reject callback. - rejectCallback = reject - - // Index locations. - indexLambda(id, locations) - }) - - try { - let userId = await promise - return userId - } finally { - // Remove indexed locations. - unindexLambda(id, locations) + this.waitAsync = async (id, locations) => { + let promise = new Promise((resolve, reject) => { + this.drivers[id] = { + resolve: resolve, + reject: reject } - })() - return { - promise: promise, + // Index locations. + indexLambda(id, locations) + }) - reject (err) { - rejectCallback(err) - } - } + let userId = await promise + + // Remove indexed locations. + unindexLambda(id, locations) + + return userId } } @@ -82,6 +66,10 @@ class Hub { pick (id, userId) { this.drivers[id].resolve(userId) } + + reject (id, reason) { + this.drivers[id].reject(reason) + } } module.exports = Hub -- GitLab