Home Reference Source Repository
public class | source

Backend

Extends:

events~EventEmitter → Backend

Handles connection with the backend.

Responsible for establishing a WebSocket (socket.io) connection with the backend and handling its requests.

It also instantiates Replicator instances for data and log replication. The stateChange events from these are forwarded as: "replicator:data:state" and "replicator:log:state".

Constructor Summary

Public Constructor
public

constructor(name: String, logger: Bunyan, dataDB: PouchDB, logDB: PouchDB)

Constructor.

Member Summary

Public Members
public

Replicator instance for dataDB.

public

Replicator instance for logDB.

public

Station name.

Private Members
private

_log: Bunyan

Logger instance.

private

_socket: IO

Current socket connection with the backend.

private

Current socket state.

Method Summary

Public Methods
public

async cleanup(): Promise

Triggers Replicator#cleanup on the data and log replicators.

public

connect(url: String)

Connects to backend at [url].

public

Disconnects from the backend.

Protected Methods
protected

_updateState(state: String, extraData: Object)

Updates the socket state.

Public Constructors

public constructor(name: String, logger: Bunyan, dataDB: PouchDB, logDB: PouchDB) source

Constructor.

Params:

NameTypeAttributeDescription
name String

Station name.

logger Bunyan

Logger instance.

dataDB PouchDB

Packet database.

logDB PouchDB

Log entries database.

Public Members

public dataReplicator: Replicator source

Replicator instance for dataDB.

public logReplicator: Replicator source

Replicator instance for logDB.

public name: String source

Station name.

Private Members

private _log: Bunyan source

Logger instance.

private _socket: IO source

Current socket connection with the backend.

private _state: String source

Current socket state.

Public Methods

public async cleanup(): Promise source

Triggers Replicator#cleanup on the data and log replicators.

Return:

Promise

Emit:

*

state('cleanup')

public connect(url: String) source

Connects to backend at [url].

Params:

NameTypeAttributeDescription
url String

Backend URL.

Emit:

*

state(socketState): socket connection/disconnection.

public disconnect() source

Disconnects from the backend. Stops the socket. The replicators will keep running until a new connection makes them connect to a different database.

Emit:

*

state(socketState): socket connection/disconnection.

Protected Methods

protected _updateState(state: String, extraData: Object) source

Updates the socket state.

Params:

NameTypeAttributeDescription
state String

New Backend state.

extraData Object
  • optional

Extra data related to the new state (error, no. of connection attempts...) to be included in logs.

Emit:

*

state(socketState): socket connection/disconnection.