Skip to content

Instantly share code, notes, and snippets.

@freaktechnik
Created April 8, 2019 17:35
Show Gist options
  • Save freaktechnik/ccf2635ceb6d3aca8efa236a6e508b70 to your computer and use it in GitHub Desktop.
Save freaktechnik/ccf2635ceb6d3aca8efa236a6e508b70 to your computer and use it in GitHub Desktop.
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