Module: heartBeat

Factory function that creates heartbeat objects with a default Timeout of DEFAULT_TIMEOUT seconds and a default BeatInterval of DEFAULT_INTERVAL seconds.
The heartbeat returned is stopped and needs to be started to execute.

Version:
  • 1.0.3
Author:
  • Pedro Miguel Pereira Serrano Martins
Source:

Methods


<inner> getBeatInterval()

Returns the current hearbeat interval.
The heartbeat interval is the interval at which the heartbeat will run the ping function.

Source:
Returns:

The current heartbeat interval.

Type
Number

<inner> getBeatTimeout()

Returns the current hearbeat timeout.
The heartbeat timeout is the amount of time that must pass for the hasTimedOut to return true.

Source:
See:
  • hasTimedOut
Returns:

The current timeout.

Type
Number

<inner> getPing()

Returns the ping object being used.

Source:
Returns:

The current object being used as a ping.

Type
Object

<inner> getPong()

Returns the pong object being used.

Source:
Returns:

The current object being used as a pong.

Type
Object

<inner> hasTimedOut()

Used to detected if a heartbeat has timedout.
A heartbeat times out when it sends a ping, and receives no pong after a given period of time.
The timeout period can be manipulated via setBeatTimeout.

Source:
See:
  • setBeatTimeout
Returns:

true if the heartbeat has timedout,
false otherwise.

Type
Boolean

<inner> isBeating()

Returns true if the heartbeat is active, false otherwise.
A heartbeat is considered active if it was started and has not beend stopped yet.

Source:
Returns:

true if the heartbeat is active,
false otherwise.

Type
Boolean

<inner> onTimeout(fn)

Runs the given function when the heartbeat detects a timeout.
A timeout is deteceted if receivedPong is not called within the defined 'timeout' period.

Parameters:
Name Type Description
fn function

The function to be executed when a timeout
occurs.

Source:
Throws:

If fn is not a function.

Type
TypeError

<inner> receivedPong()

Notifies the hearbeat that it has received a pong from the target.

Source:

<inner> reset()

Stops the heartbeat if it is beating, and resets all properties to the original default values.

Source:

<inner> setBeatInterval(newInterval)

Sets the current heartbeat interval to the given one.
Note that setting the heartbeat interval will not affetct current heartbeat running. You must stop them and then start them for the new interval to be applied.

Parameters:
Name Type Description
newInterval Number

The new heartbeat interval.

Source:
See:
  • stop
  • start
Throws:

If newInterval is not a Number.

Type
TypeError

<inner> setBeatTimeout(newTimeout)

Sets the current timeout to the given one.
Setting the timeout this way will immediatly affect the hasTimedOut method without the need to restart the heartbeat object.
Invoking this method does restart the timer controlling the onTimeout event.

Parameters:
Name Type Description
newTimeout Number

The new newTimeout.

Source:
See:
  • hasTimedOut
  • onTimeout
Throws:

If newTimeout is not a Number.

Type
TypeError

<inner> setPing(newPing)

Sets the current ping object.
A ping object can be anything that the receiver accepts, from a Buffer of bytes to plain Object to a primitive.

Parameters:
Name Type Description
newPing Object

The new ping object.

Source:

<inner> setPong(newPong)

Sets the pong object we expect to receive from the target of the heartbeats.
This is only needed if there is a need to distinguish between normal messages from the target of heartbeat and pong messages that need to be processed differently.

Parameters:
Name Type Description
newPong Object

The new pong object.

Source:

<inner> start(fn)

Starts the heartbeat object, executing the given function fn every interval.
If you want to send a ping to an object every interval, this is where you defined that.

Parameters:
Name Type Description
fn function

The function that will be executed periodically
by the heartbeat object.

Source:
Throws:

If fn is not a function.

Type
TypeError

<inner> stop()

Stops the heartbeat object and clears all internal states.

Source: