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
- Source:
Methods
-
<inner> getBeatInterval()
-
Returns the current hearbeat interval.
The heartbeat interval is the interval at which the heartbeat will run thepingfunction.- 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 thehasTimedOutto returntrue.- 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 viasetBeatTimeout.- Source:
- See:
-
setBeatTimeout
Returns:
trueif the heartbeat has timedout,
falseotherwise.- Type
- Boolean
-
<inner> isBeating()
-
Returns
trueif the heartbeat is active,falseotherwise.
A heartbeat is considered active if it was started and has not beend stopped yet.- Source:
Returns:
trueif the heartbeat is active,
falseotherwise.- Type
- Boolean
-
<inner> onTimeout(fn)
-
Runs the given function when the heartbeat detects a timeout.
A timeout is deteceted ifreceivedPongis not called within the defined 'timeout' period.Parameters:
Name Type Description fnfunction The function to be executed when a timeout
occurs.- Source:
Throws:
-
If
fnis 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 muststopthem and thenstartthem for the new interval to be applied.Parameters:
Name Type Description newIntervalNumber The new heartbeat interval.
- Source:
- See:
-
stopstart
Throws:
-
If
newIntervalis not a Number. - Type
- TypeError
-
<inner> setBeatTimeout(newTimeout)
-
Sets the current timeout to the given one.
Setting the timeout this way will immediatly affect thehasTimedOutmethod without the need to restart the heartbeat object.
Invoking this method does restart the timer controlling theonTimeoutevent.Parameters:
Name Type Description newTimeoutNumber The new newTimeout.
- Source:
- See:
-
hasTimedOutonTimeout
Throws:
-
If
newTimeoutis 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 newPingObject 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 newPongObject The new pong object.
- Source:
-
<inner> start(fn)
-
Starts the heartbeat object, executing the given function
fnevery interval.
If you want to send a ping to an object every interval, this is where you defined that.Parameters:
Name Type Description fnfunction The function that will be executed periodically
by the heartbeat object.- Source:
Throws:
-
If
fnis not a function. - Type
- TypeError
-
<inner> stop()
-
Stops the heartbeat object and clears all internal states.
- Source: