|
| 1 | +import * as stencila from '@stencila/schema'; |
| 2 | +import Executor, { Method, Capabilities } from './Executor'; |
| 3 | +import Request from './Request'; |
| 4 | +import Response from './Response'; |
| 5 | + |
| 6 | +/** |
| 7 | + * A base client class which acts as a proxy to a remote `Executor`. |
| 8 | + * |
| 9 | + * Implements aynchronous, proxy methods for `Executor` methods `compile`, `build`, `execute`, etc. |
| 10 | + * Those methods send JSON-RPC requests to a `Server` that is serving the remote `Executor`. |
| 11 | + */ |
| 12 | +export default abstract class Client extends Executor { |
| 13 | + |
| 14 | + /** |
| 15 | + * A map of requests to which responses can be paired against |
| 16 | + */ |
| 17 | + private requests: {[key: number]: (response: Request) => void } = {} |
| 18 | + |
| 19 | + /** |
| 20 | + * Call the remote `Executor`'s `capabilities` method |
| 21 | + */ |
| 22 | + async capabilities (): Promise<Capabilities> { |
| 23 | + return this.call<Capabilities>(Method.capabilities) |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Call the remote `Executor`'s `convert` method |
| 28 | + */ |
| 29 | + async convert (node: string | stencila.Node, from: string = 'json', to: string = 'json'): Promise<string> { |
| 30 | + return this.call<string>(Method.convert, node, from, to) |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Call the remote `Executor`'s `compile` method |
| 35 | + */ |
| 36 | + async compile (node: string | stencila.Node, format: string = 'json'): Promise<stencila.Node> { |
| 37 | + return this.call<stencila.Node>(Method.compile, node, format) |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Call the remote `Executor`'s `build` method |
| 42 | + */ |
| 43 | + async build (node: string | stencila.Node, format: string = 'json'): Promise<stencila.Node> { |
| 44 | + return this.call<stencila.Node>(Method.build, node, format) |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Call the remote `Executor`'s `execute` method |
| 49 | + */ |
| 50 | + async execute (node: string | stencila.Node, format: string = 'json'): Promise<stencila.Node> { |
| 51 | + return this.call<stencila.Node>(Method.execute, node, format) |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Call a method of a remote `Executor`. |
| 56 | + * |
| 57 | + * @param method The name of the method |
| 58 | + * @param args Any method arguments |
| 59 | + */ |
| 60 | + private async call<Type> (method: Method, ...args: Array<any>): Promise<Type> { |
| 61 | + const request = new Request(method, args) |
| 62 | + const promise = new Promise<Type>((resolve, reject) => { |
| 63 | + this.requests[request.id] = (response: Response) => { |
| 64 | + if (response.error) return reject(new Error(response.error.message)) |
| 65 | + resolve(response.result) |
| 66 | + } |
| 67 | + }) |
| 68 | + this.send(request) |
| 69 | + return promise |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Send a request to the server. |
| 74 | + * |
| 75 | + * This method must be overriden by derived client classes to |
| 76 | + * send the request over the transport used by that class. |
| 77 | + * |
| 78 | + * @param request The JSON-RPC request |
| 79 | + */ |
| 80 | + protected abstract send (request: Request): void |
| 81 | + |
| 82 | + /** |
| 83 | + * Receive a response from the server. |
| 84 | + * |
| 85 | + * Usually called asynchronously via the `send` method of a derived class |
| 86 | + * when a response is returned. Uses the `id` of the response to match it to the corresponding |
| 87 | + * request and resolve it's promise. |
| 88 | + * |
| 89 | + * @param response The JSON-RPC response |
| 90 | + */ |
| 91 | + protected receive (response: string | Response): void { |
| 92 | + if (typeof response === 'string') response = JSON.parse(response) as Response |
| 93 | + if (response.id < 0) throw new Error(`Response is missing id: ${response}`) |
| 94 | + const resolve = this.requests[response.id] |
| 95 | + if (resolve === undefined) throw new Error(`No request found for response with id: ${response.id}`) |
| 96 | + resolve(response) |
| 97 | + delete this.requests[response.id] |
| 98 | + } |
| 99 | +} |
0 commit comments