UNPKG

@pandorajs/hub

Version:

pandora.js messenge hub

151 lines 4.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ObjectConsumer = void 0; const const_1 = require("../const"); const DefaultObjectProxy_1 = require("./DefaultObjectProxy"); const EventEmitter = require("events"); class ObjectConsumer extends EventEmitter { constructor(objectDescription, hubClient, providerManager, extInfo) { super(); this.subscriberPublished = false; this.objectDescription = objectDescription; this.hubClient = hubClient; this.providerManager = providerManager; if (extInfo) { this.timeout = extInfo.timeout; } } /** * Invoke a method from Remote Object * @param {string} method * @param {any[]} params * @return {Promise<any>} */ async invoke(method, params) { const res = await this.hubClient.invoke({ objectName: this.objectDescription.name, objectTag: this.objectDescription.tag, }, const_1.OBJECT_ACTION_INVOKE, { timeout: this.timeout, propertyName: method, data: params, }); if (res.error) { throw res.error; } return res.data; } /** * Invoke a method from Remote Object * @param {string} method * @param {any[]} params * @return {Promise<any>} */ async multipleInvoke(method, params, selectors) { const res = await this.hubClient.multipleInvoke({ objectName: this.objectDescription.name, objectTag: this.objectDescription.tag, ...(selectors || {}), }, const_1.OBJECT_ACTION_INVOKE, { timeout: this.timeout, propertyName: method, data: params, }); return res; } /** * Get a property from Remote Object * @param {string} name * @return {Promise<any>} */ async getProperty(name) { const res = await this.hubClient.invoke({ objectName: this.objectDescription.name, objectTag: this.objectDescription.tag, }, const_1.OBJECT_ACTION_GET_PROPERTY, { timeout: this.timeout, propertyName: name, }); if (res.error) { throw res.error; } return res.data; } async subscribe(register, fn) { const cnt = this.listenerCount(register); this.addListener(register, fn); if (!this.subscriberPublished) { this.subscriberPublished = true; await this.providerManager.publish({ callback: (register, params) => { this.emit(register, ...params); }, }, { ...this.objectDescription, name: this.objectDescription.name + '@subscriber', }); } if (cnt === 0) { const res = await this.hubClient.invoke({ objectName: this.objectDescription.name, objectTag: this.objectDescription.tag, }, const_1.OBJECT_ACTION_SUBSCRIBE, { timeout: this.timeout, register: register, }); if (res.error) { throw res.error; } return res.data; } } async unsubscribe(register, fn) { if (fn) { this.removeListener(register, fn); } else { this.removeAllListeners(register); } if (this.listenerCount(register) === 0) { const res = await this.hubClient.invoke({ objectName: this.objectDescription.name, objectTag: this.objectDescription.tag, }, const_1.OBJECT_ACTION_UNSUBSCRIBE, { timeout: this.timeout, register: register, }); if (res.error) { throw res.error; } return res.data; } } /** * Get Introspection from Remote Object * @return {Promise<Introspection>} */ async introspect() { const res = await this.hubClient.invoke({ objectName: this.objectDescription.name, objectTag: this.objectDescription.tag, }, const_1.OBJECT_ACTION_INTROSPECT, null); if (res.error) { throw res.error; } return res.data; } /** * Get Object Proxy * @return {Promise<T & DefaultObjectProxy>} */ async getProxy() { if (this.objectProxy) { return this.objectProxy; } const introspection = await this.introspect(); this.objectProxy = new DefaultObjectProxy_1.DefaultObjectProxy(this, introspection); return this.objectProxy; } } exports.ObjectConsumer = ObjectConsumer; //# sourceMappingURL=ObjectConsumer.js.map