Skip to content

Commit

Permalink
docs(services): Update module docs with new services examples
Browse files Browse the repository at this point in the history
  • Loading branch information
aholstenson committed Jun 17, 2021
1 parent 5e74483 commit df777bc
Showing 1 changed file with 29 additions and 30 deletions.
59 changes: 29 additions & 30 deletions packages/services/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
* }
* ```
*
Expand Down

0 comments on commit df777bc

Please sign in to comment.