UNPKG

@devgrid/netron

Version:
44 lines 1.48 kB
import { Definition } from './definition'; import { isNetronService, isServiceReference, isServiceInterface, isServiceDefinition } from './predicates'; export class ServiceStub { constructor(peer, instance, metaOrDefinition) { this.peer = peer; this.instance = instance; if (isServiceDefinition(metaOrDefinition)) { this.definition = metaOrDefinition; } else { this.definition = new Definition(Definition.nextId(), peer.id, metaOrDefinition); } } set(prop, value) { Reflect.set(this.instance, prop, this.processValue(value)); } get(prop) { return this.processResult(this.instance[prop]); } async call(method, args) { const processedArgs = this.processArgs(args); let result = this.instance[method](...processedArgs); if (result instanceof Promise) { result = await result; } return this.processResult(result); } processResult(result) { if (isNetronService(result) || isServiceInterface(result)) { return this.peer.refService(result, this.definition); } return result; } processArgs(args) { return args.map((arg) => this.processValue(arg)); } processValue(obj) { if (isServiceReference(obj)) { return this.peer.queryInterfaceByDefId(obj.defId); } return obj; } } //# sourceMappingURL=service-stub.js.map