@devgrid/netron
Version:
Event bus, streams and remote object invocation.
60 lines • 2.06 kB
JavaScript
import { Interface } from './interface';
import { isServiceInterface } from './predicates';
export class AbstractPeer {
constructor(netron, id) {
this.netron = netron;
this.id = id;
this.interfaces = new Map();
}
unexposeAllServices() {
for (const ctxId of this.getServiceNames()) {
this.unexposeService(ctxId);
}
}
async queryInterface(serviceName) {
const def = this.getDefinitionByServiceName(serviceName);
return this.queryInterfaceByDefId(def.id, def);
}
async queryInterfaceByDefId(defId, def) {
if (!def) {
def = this.getDefinitionById(defId);
}
let iInfo = this.interfaces.get(defId);
if (iInfo !== void 0) {
iInfo.refCount++;
return iInfo.instance;
}
const instance = Interface.create(def, this);
iInfo = { instance, refCount: 1 };
this.interfaces.set(def.id, iInfo);
return instance;
}
async releaseInterface(iInstance) {
if (!isServiceInterface(iInstance)) {
throw new Error('Not a service interface');
}
if (!iInstance.$def) {
throw new Error('Invalid interface');
}
const iInfo = this.interfaces.get(iInstance.$def.id);
if (!iInfo) {
throw new Error('Invalid interface');
}
iInfo.refCount--;
if (iInfo.refCount === 0) {
this.interfaces.delete(iInstance.$def.id);
const releaseChildInterfaces = (defId) => {
for (const i of this.interfaces.values()) {
if (i.instance.$def?.parentId === defId) {
this.releaseInterface(i.instance);
}
}
};
releaseChildInterfaces(iInstance.$def.id);
await this.releaseInterfaceInternal(iInstance);
iInstance.$def = undefined;
iInstance.$peer = undefined;
}
}
}
//# sourceMappingURL=abstract-peer.js.map