Constructor
new RobustTimers(options)
Just constructor.
Parameters:
Name | Type | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object | object containing options for construction.
Properties
|
Methods
activate(name)
Starts the timer lifecycle.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | name of the timer. |
assignDataSource(dataSource)
Registers save/restore handlers from the given object.
Parameters:
Name | Type | Description |
---|---|---|
dataSource |
Object | object with two properties "save" and "restore" containing save and restore functions respectively. THIS OBJECT IS NOT USED AS A CONTEXT WHEN THIS FUNCTIONS ARE CALLED! |
deactivate(name)
Pauses the timer lifecycle.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | name of the timer. |
interval(name, interval, handler)
Shortcut to register simple interval-based timer.
Similar to simple JS setInterval.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | name of the timer. |
interval |
number | interval of the timer. |
handler |
function | handler of the timer. |
once(name, interval, handler)
Shortcut to register simple timer, that should be fired only once.
Similar to simple JS setTimeout.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | name of the timer. |
interval |
number | interval of the timer. |
handler |
function | handler of the timer. |
register(options)
Registers new timer and runs it if needed.
Parameters:
Name | Type | Description | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object | object containing options of timer.
Properties
|
Throws:
Will throw an exception if there are no handler or interval provided in options.
restore(handler)
If handler is provided - registers it as default restoring function this.__onRestoreHandler.
If not - trying to restore all inner state using this.__onRestoreHandler.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
handler |
function | null | async function that gets one param - this instance and should restore all it's inner state basing on anything (DB/Local Storage/etc.). |
Throws:
Will throw an exception if there are no restore handler, or if restore handler did not return promise.
save(handler)
If handler is provided - registers it as default saving function this.__onSaveHandler.
If not - trying to save all inner state using this.__onSaveHandler.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
handler |
function | null | async function that gets one param - this instance and should save all it's inner state in anywhere (DB/Local Storage/etc.). |
Throws:
Will throw an exception if there are no save handler, or if save handler did not return promise.
start()
If all event handlers and other staff is registered, timers states are restored,
and we are ready to go at all - starts all registered timers lifecycles.
unregister(name)
Unregisters timer and stops it if needed.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | name of the timer. |