Skip to content

Commit

Permalink
changed behavior of scheduleUpdate: Now, an interval of 0 will trigge…
Browse files Browse the repository at this point in the history
…r an immediate update
  • Loading branch information
mwittig committed Apr 29, 2017
1 parent a8f987c commit b808b5d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
4 changes: 3 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Release History

* next release
* 20170429, V0.9.5
* Optimized periodic timer support
* Changed behavior of scheduleUpdate: Now, an interval of 0 will trigger an
immediate update
* Improved API documentation

* 20170420, V0.9.4
Expand Down
11 changes: 6 additions & 5 deletions src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -227,26 +227,27 @@ module.exports = (env) ->
is called after 'interval' milliseconds. Repeated call will
remove any previous schedule.
@param {Function} func - update function to be called
@param {Number} interval - interval in milliseconds greater than 0
@param {Number} interval - interval in milliseconds. 0 will
trigger an immediate update
@param [...] - additional parameters which are passed through
to the function specified by func once the
timer expires
###
scheduleUpdate: (func, interval=0) ->
scheduleUpdate: (func, interval) ->
members.cancelUpdate()

if (typeof func is 'undefined')
throw new Error "Missing function parameter"
if interval > 0
if (typeof interval is 'undefined' or interval < 0)
throw new Error 'Missing or invalid interval parameter'
else
members.debug "Next Request in #{interval} ms"
args = Array.prototype.splice.call arguments, 2
device.__timeoutObject = setTimeout( ->
device.__timeoutObject = null
func.apply device, args
, interval
)
else
throw new Error 'Missing or invalid interval parameter'

###
Normalize a given value to match the given lowerRange and
Expand Down
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,13 @@ describe("Testing the base device functions", function() {
base.scheduleUpdate(update, 500, "TEST1", "TEST2");
});

it("shall throw if interval is 0", function(done) {
it("shall throw if interval is less than 0", function(done) {
function update() {
expect(true).toBe(false);
done();
}
try {
base.scheduleUpdate(update, 0);
base.scheduleUpdate(update, -100);
expect(true).toBe(false);
}
catch (e) {
Expand Down

0 comments on commit b808b5d

Please sign in to comment.