If a service with this id is currently available. To listen for changes in availability use onAvailable and onUnavailable.
Get the events available for this service.
Identifier of the service.
Get methods available for this service.
Event emitted when this service becomes available.
service.onAvailable(() => console.log('Service is now available'));
Event emitted when this service becomes unavailable.
service.onUnavailable(() => console.log('Service is no longer available'));
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
});
Call a method on this service passing the arguments as an array.
const result = await service.apply('echo', [ 'arg1' ]);
promise that resolves with the result of the call or rejects if an error occurs
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');
proxy for the contract
Call a method on this service passing the arguments as rest parameters.
const result = await service.call('echo', 'arg1');
promise that resolves with the result of the call or rejects if an error occurs
Get information about an available event.
event if available or null
if event doesn't exist
Get the definition for the given method.
information about method if found, null
otherwise
Check if a specific event is available.
true
if the event exists
Check if a certain method is available.
true
if the method is available
Check if the service as it is seen currently matches the given contract.
if the service matches
Subscribe to an event.
promise with subscription handle, resolves when the listener is fully subscribed
Unsubscribe from an event.
promise that resolves when the listener is unsubscribed
Generated using TypeDoc
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.
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:
A proxied service can then be called as a normal class:
Events work similar to how they would locally:
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.