pandora-hub
Version:
pandora.js messenge hub
49 lines • 1.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("util");
const ObjectDispatchHandler_1 = require("./ObjectDispatchHandler");
const ObjectUtils_1 = require("./ObjectUtils");
class ProviderManager {
constructor(hubClient) {
this.objectMap = new Map();
this.hubClient = hubClient;
this.hubClient.pushDispatchHandler(new ObjectDispatchHandler_1.ObjectDispatchHandler(this));
}
/**
* Get an published Object by an ObjectDescription
* @param {ObjectDescription} objectDescription
* @return {Promise<any>}
*/
getPublishedObject(objectDescription) {
const idWithTag = ObjectUtils_1.ObjectUtils.objectDescriptionToId(objectDescription);
if (this.objectMap.has(idWithTag)) {
return this.objectMap.get(idWithTag);
}
return this.objectMap.get(objectDescription.name);
}
/**
* Publish an Object to Hub
* @param impl
* @param {ObjectDescription} objectDescription
* @return {Promise<void>}
*/
async publish(impl, objectDescription) {
const id = ObjectUtils_1.ObjectUtils.objectDescriptionToId(objectDescription);
if (this.objectMap.has(id)) {
throw new Error(util_1.format('object called %j already exist', id));
}
let obj = null;
if (typeof impl === 'function') {
obj = new impl;
}
else {
obj = impl;
}
this.objectMap.set(id, obj);
const location = this.hubClient.getLocation();
const selector = Object.assign({}, location, { objectName: objectDescription.name, objectTag: objectDescription.tag });
await this.hubClient.publish(selector);
}
}
exports.ProviderManager = ProviderManager;
//# sourceMappingURL=ProviderManager.js.map