Skip to content
Snippets Groups Projects
Commit 7835bd0d authored by Daniel's avatar Daniel
Browse files

app: model: Add Hub.js

parent 2dc65db2
Branches
No related merge requests found
/*
* Tugas Besar 3 WBD - AMEN
*/
'use strict'
// Utilities.
let index = function (id, locations) {
for (let location of locations) {
let ids = this.locations[location]
if (!ids) {
ids = new Set()
this.locations[location] = ids
}
ids.add(id)
}
}
let unindex = function (id, locations) {
for (let location of locations) {
let ids = this.locations[location]
if (ids) {
ids.delete(id)
}
}
}
// Main routine.
class Hub {
constructor () {
this.drivers = {}
this.locations = {}
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)
}
})()
return {
promise: promise,
reject (err) {
rejectCallback(err)
}
}
}
}
filter (location) {
return this.locations[location]
}
pick (id, userId) {
this.drivers[id].resolve(userId)
}
}
module.exports = Hub
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment