Home Reference Source
public class | source

ServiceBuilder

A ServiceBuilder build a service into the prototype of a protocol, including the service method, the handler & the possible hooks.

Constructor Summary

Public Constructor
public

constructor(serviceName: string)

Constructor

Member Summary

Public Members
public get

after: Object: {"send": *, "receive": *}

Helper used to define hooks executed after a message is sent/received.

public get

before: Object: {"send": *, "receive": *}

Helper used to define hooks executed before a message is sent/received.

public get

is: Object: {"unicast": *, "broadcast": *}

Helper used to define the type of the service.

public get

on: Object: {"receive": *}

Helper used to define the callback invoked when a message is received for this service.

Private Members
private

_afterHooks: {"send": *, "receive": *}

private

_beforeHooks: {"send": *, "receive": *}

private
private
private

Method Summary

Public Methods
public

apply(protocol: function): void

Apply the builder on a protocol subclass to build the service in it.

Private Methods
private

Validate the builder

Public Constructors

public constructor(serviceName: string) source

Constructor

Params:

NameTypeAttributeDescription
serviceName string

The name of the service

Public Members

public get after: Object: {"send": *, "receive": *} source

Helper used to define hooks executed after a message is sent/received.

Return:

Object

Example:

// define a hook after a message is sent
mysService.after.send(msg => console.log(`You just send ${msg}`));

// define a hook after a message is received
mysService.after.receive(msg => console.log(`You juste finished processing ${msg}`));

public get before: Object: {"send": *, "receive": *} source

Helper used to define hooks executed before a message is sent/received.

Return:

Object

Example:

// define a hook before a message is sent
mysService.before.send(msg => console.log(`You are going to send ${msg}`));

// define a hook before a message is received
mysService.before.receive(msg => console.log(`You are about to receive ${msg}`));

public get is: Object: {"unicast": *, "broadcast": *} source

Helper used to define the type of the service.

Return:

Object

Example:

// define an unicast service
myService.is.unicast();
// define a broadcast service
myService.is.broadcast();

public get on: Object: {"receive": *} source

Helper used to define the callback invoked when a message is received for this service.

Return:

Object

Example:

myService.on.receive((msg, reply, reject) => {
 if (msg.number % 2 === 0)
   reply('You send an even number');
 else
   reject('You send a odd number!');
});

Private Members

private _afterHooks: {"send": *, "receive": *} source

private _beforeHooks: {"send": *, "receive": *} source

private _builder: * source

private _handler: * source

private _serviceName: * source

Public Methods

public apply(protocol: function): void source

Apply the builder on a protocol subclass to build the service in it. The builder must have been properly configured before calling this function, otherwise an error will be thrown. A valid builder has a type and a handler set.

Params:

NameTypeAttributeDescription
protocol function

The protocol class

Return:

void

Private Methods

private _validate(): boolean source

Validate the builder

Return:

boolean

true if the builder is valid, False otherwise