A persistent data structure for storing the history of a toggleable (binary) button. Events in the history are points where the button's state toggles from on to off and vice versa. This data structure supports efficient queries for the amount of time a button is held and how frequently/or whether it is pressed at any point in history.
//Create a button
var createButton = require('persistent-button')
var button = createButton()
//Press button at time t=0.5
button.appendEvent(0.5, true)
//Release button at time t=1
button.appendEvent(1, false)
//Press button again at time t=2
button.appendEvent(2, true)
//Test the button state at different points in history
console.log(button.down(0.75)) //Prints out: true
console.log(button.down(1.5)) //Prints out: false
//Find how many time the button was pressed up to t=3
console.log(button.press(3)) //Prints out: 2
//Print out how long the button was held from beginning of time to t=2.5
console.log(button.hold(2.5)) //Prints out: 1
npm install persistent-button
var createButton = require('persistent-button')
Creates a persistent button
history
is an optional history of the button (as created by button.serialize)
Returns A new persistent button object
Adds an event to the button's history
t
is the time of the eventstate
is the new state of the button (must be eithertrue
orfalse
)
The number of events in the history of the button
Gives the time of the i
th event
i
is the index of the event which is being queries
Returns The time associated to the i
th event
Counts the number of times the button has been toggled from the beginning of time up to time t
t
is the time which is being queried
Returns The number of times the button has been toggled
Tests if the button is down at time t
t
is the time to query
Returns true
if the button was held down at time t
, otherwise false
Counts the number of time the button has been pressed since the beginning of time.
t
is the query time
Returns The number of times the button has been pressed since the beginning of time and ending at time t
Counts the number of times the button has been released since the beginning of time.
t
is the query time
Returns The number of times the button has been released since the beginning of time
Gives the number of time units that the button has been held since the beginning of time
t
is the query time
Returns How long the button has been held up to time t
Saves the state of the button to an array for serialization.
Returns An array of times representing the points in history where the button state toggled
Restores the state of the button from an array of toggle events labeled by time.
history
is an array of toggle events
(c) 2014 Mikola Lysenko. MIT License