From df777bcf3f95d87223bd4b774580b01cb716f3d2 Mon Sep 17 00:00:00 2001 From: Andreas Holstenson Date: Thu, 17 Jun 2021 11:25:04 +0200 Subject: [PATCH] docs(services): Update module docs with new services examples --- packages/services/src/index.ts | 59 +++++++++++++++++----------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/packages/services/src/index.ts b/packages/services/src/index.ts index ab8aa6e..8a899f2 100644 --- a/packages/services/src/index.ts +++ b/packages/services/src/index.ts @@ -5,50 +5,49 @@ * the network. * * ```javascript - * import { Services } from 'ataraxia-services'; + * import { Services, ServiceContract, stringType } from 'ataraxia-services'; * * const net = ... // setup network with at least one transport * * const services = new Services(net); * - * services.onServiceAvailable(service => console.log(service.id, 'is now available')); - * services.onServiceUnavailable(service => console.log(service.id, 'is no longer available')); + * services.onAvailable(service => console.log(service.id, 'is now available')); + * services.onUnavailable(service => console.log(service.id, 'is no longer available')); * * // Join the network * await net.join(); * - * // Join the services layer on top of the network + * // Join the services on top of the network * await services.join(); * - * // Register a service as a plain object - * const handle = services.register({ - * id: 'service-id', - * - * hello() { - * return 'Hello world'; - * } - * }); - * - * // Classes can be registered and created - * services.register(class Test { - * constructor(handle) { - * this.handle = handle; - * - * this.id = 'service-id'; - * } - * - * hello() { - * return 'Hello World'; + * // Use contracts to describe services + * const EchoService = new ServiceContract() + * .defineMethod('echo', { + * returnType: stringType, + * parameters: [ + * { + * name: 'message', + * type: stringType + * } + * ] + * }); + * + * // Easily register and expose services to other nodes + * services.register('echo', EchoService.implement({ + * echo(message) { + * return Promise.resolve(message); * } - * }); + * })); * - * // Interact with services - * const service = services.get('service-id'); - * if(service) { - * console.log('Service found', service); + * // Consume a service registered anywhere, local or remote + * const echoService = services.get('echo'); + * if(echoService.available) { + * // Call methods + * await echoService.call('echo', 'Hello world'); * - * // Call functions on the service - * const reply = await service.hello(); + * // Or create a proxy for a cleaner API + * const proxied = echoService.as(EchoService); + * await proxied.echo('Hello world'); * } * ``` *