Options
All
  • Public
  • Public/Protected
  • All
Menu

Service that may be callable somewhere in the network. Provides utilities to listen for availability, retrieve metadata about methods and events and to cast it to a proxy based on a contract.

Availability

The property available will indicate if a service is currently available and can be called. Events are available in the form of onAvailable, onUnavailable and onUpdate.

service.onAvailable(() => console.log('This service has become available'));
service.onUnavailable(() => console.log('This service has become unavailable'));
service.onUpdate(() => console.log('This service has updated'));

Contracts

Services keep track of the methods and events that are available and these can be inspected to determine if a service supports something specific.

The method matches can be used to check if a specific contract is supported by a service.

Proxies

Creating a proxy is the recommended way to call methods and listen to events from a service. It allows for a very similar style of use for services that are remote as for services that are local.

Proxies are created from a contract and use the methods and events defined in them to create the proxied instance:

const echoService = service.as(EchoService);

A proxied service can then be called as a normal class:

const result = await echoService.echo('Hello world!');

Events work similar to how they would locally:

await echoService.onEcho(message => console.log('Echoed:', message));

Methods in a contract will become a method on the proxy that returns a Promise, so method calls must always be awaited to retrieve the result. Events will become an instance of AsyncSubscribable.

Hierarchy

  • Service

Index

Properties

Readonly available

available: boolean

If a service with this id is currently available. To listen for changes in availability use onAvailable and onUnavailable.

Readonly events

events: ServiceEventDef[]

Get the events available for this service.

returns

array with available events

Readonly id

id: string

Identifier of the service.

Readonly methods

methods: ServiceMethodDef[]

Get methods available for this service.

returns

array with methods

Readonly onAvailable

onAvailable: Subscribable<Service, []>

Event emitted when this service becomes available.

service.onAvailable(() => console.log('Service is now available'));

Readonly onUnavailable

onUnavailable: Subscribable<Service, []>

Event emitted when this service becomes unavailable.

service.onUnavailable(() => console.log('Service is no longer available'));

Readonly onUpdate

onUpdate: Subscribable<Service, []>

Event emitted when this service is updated. This can be either that it has become available or unavailable, or that the contract supported is now different.

Contracts may change if a service is registered multiple times in the network.

service.onUpdate(() => {
// Check if this service still matches our needs
});

Methods

apply

  • Call a method on this service passing the arguments as an array.

    const result = await service.apply('echo', [ 'arg1' ]);
    

    Parameters

    • method: string
      • the method to call
    • args: readonly BasicValue[]
      • the arguments to pass to the method

    Returns Promise<BasicValue>

    promise that resolves with the result of the call or rejects if an error occurs

as

  • Get a version of the service that implements the given contract and that can be easily invoked.

    const EchoService = new ServiceContract()
    .defineMethod('echo', {
    returnType: stringType,
    parameters: [
    {
    name: 'message',
    type: stringType
    }
    ]
    });

    // Implement the contract for us
    const proxied = service.as(EchoService);

    // Invoke methods on the proxied object
    const result = await proxied.echo('message to echo');

    Type parameters

    • T: object

    Parameters

    Returns T

    proxy for the contract

call

  • Call a method on this service passing the arguments as rest parameters.

    const result = await service.call('echo', 'arg1');
    

    Parameters

    • method: string
      • the method to call
    • Rest ...args: readonly BasicValue[]
      • the arguments to pass to the method

    Returns Promise<BasicValue>

    promise that resolves with the result of the call or rejects if an error occurs

getEvent

  • getEvent(name: string): null | ServiceEventDef
  • Get information about an available event.

    Parameters

    • name: string
      • name of the event

    Returns null | ServiceEventDef

    event if available or null if event doesn't exist

getMethod

  • getMethod(name: string): null | ServiceMethodDef
  • Get the definition for the given method.

    Parameters

    • name: string
      • name of the method

    Returns null | ServiceMethodDef

    information about method if found, null otherwise

hasEvent

  • hasEvent(name: string): boolean

hasMethod

  • hasMethod(name: string): boolean

matches

subscribe

  • subscribe(event: string, listener: Listener<void, BasicValue[]>): Promise<AsyncSubscriptionHandle>
  • Subscribe to an event.

    Parameters

    Returns Promise<AsyncSubscriptionHandle>

    promise with subscription handle, resolves when the listener is fully subscribed

unsubscribe

Generated using TypeDoc