Created
April 8, 2019 17:35
-
-
Save freaktechnik/ccf2635ceb6d3aca8efa236a6e508b70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const uuid = require("uuid/v4"); | |
exports.DynamicMultipleThings = class MultipleThings { | |
/** | |
* | |
* @param {string} name | |
*/ | |
constructor(name) { | |
this.name = name; | |
this.things = new Map(); | |
} | |
/** | |
* | |
* @param {string} idx | |
* @return {WebSocketThing} | |
*/ | |
getThing(idx) { | |
return this.things.get(idx); | |
} | |
getThings() { | |
return Array.from(this.things.values()); | |
} | |
getName() { | |
return this.name; | |
} | |
/** | |
* | |
* @param {WebSocketThing} thing | |
* @param {string} [id=uuid] | |
* @return {string} | |
*/ | |
register(thing, id = uuid()) { | |
this.things.set(id, thing); | |
thing.setHrefPrefix(`/${id}`); | |
return id; | |
} | |
/** | |
* | |
* @param {WebSocketThing} thing | |
*/ | |
unregister(thing) { | |
const entry = Array.from(this.things.entries()).find(([i, e]) => e === thing); | |
if(entry) { | |
this.things.delete(entry[0]) | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment