Comparing version 0.0.4 to 0.0.5
59
index.js
'use strict'; | ||
var has = Object.prototype.hasOwnProperty; | ||
// | ||
// Attempt to detect if we have support for setImmediate or process.nextTick so | ||
// we can use it in our setImmediate function. | ||
// | ||
var next = 'object' === typeof process && 'function' === process.nextTick | ||
? process.nextTick | ||
: null; | ||
// | ||
// The process.nexTick doesn't have a way to cancel the scheduled tick so we | ||
// detect it first and know that when next is a function we cannot clear it. | ||
// | ||
if ('function' === typeof setImmediate) next = { | ||
clearImmediate: clearImmediate, | ||
setImmediate: setImmediate | ||
}; | ||
/** | ||
@@ -59,4 +78,4 @@ * Simple timer management. | ||
this.timers[name] = { | ||
timer: setTimeout(this.tock(name, true), Tick.parse(ms)), | ||
clear: clearTimeout, | ||
timer: setTimeout(this.tock(name, true), Tick.parse(ms)), | ||
fns: [fn] | ||
@@ -84,4 +103,4 @@ }; | ||
this.timers[name] = { | ||
timer: setInterval(this.tock(name), Tick.parse(ms)), | ||
clear: clearInterval, | ||
timer: setInterval(this.tock(name), Tick.parse(ms)), | ||
fns: [fn] | ||
@@ -94,2 +113,26 @@ }; | ||
/** | ||
* | ||
* @param {String} name Name of the timer. | ||
* @param {Function} fn Completion callback. | ||
* @returns {Tick} | ||
* @api public | ||
*/ | ||
Tick.prototype.setImmediate = function immediate(name, fn) { | ||
if (!next) return this.setTimeout(name, fn, 0); | ||
if (this.timers[name]) { | ||
this.timers[name].fns.push(fn); | ||
return this; | ||
} | ||
this.timers[name] = { | ||
timer: (next.setImmediate || next)(this.tock(name)), | ||
clear: next.clearImmediate, | ||
fns: [fn] | ||
}; | ||
return this; | ||
}; | ||
/** | ||
* Check if we have a timer set. | ||
@@ -122,10 +165,14 @@ * | ||
if (!args.length) { | ||
for (timer in this.timers) args.push(timer); | ||
for (timer in this.timers) { | ||
if (has.call(this.timers, timer)) args.push(timer); | ||
} | ||
} | ||
for (i = 0, l = args.length; i < l; i++) { | ||
if (!(args[i] in this.timers)) continue; | ||
timer = this.timers[args[i]]; | ||
this.timers[args[i]].clear(this.timers[args[i]].timer); | ||
this.timers[args[i]].fns.length = 0; | ||
if (!timer) continue; | ||
if (timer.clear) timer.clear(timer.timer); | ||
timer.fns.length = 0; | ||
delete this.timers[args[i]]; | ||
@@ -132,0 +179,0 @@ } |
{ | ||
"name": "tick-tock", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Timer management, never forget to clear timers again", | ||
@@ -33,5 +33,5 @@ "main": "index.js", | ||
"istanbul": "0.3.x", | ||
"mocha": "1.21.x", | ||
"mocha": "2.0.x", | ||
"pre-commit": "0.0.x" | ||
} | ||
} |
@@ -30,4 +30,18 @@ # tick-tock | ||
All methods return `this` unless stated otherwise. | ||
All methods return `this` unless stated otherwise. The constructor can be | ||
initialized with 1 argument: | ||
1. `context` This is the default context in which each `setTimeout` or | ||
`setInterval` function is executed (it sets the `this` value). If nothing is | ||
supplied it will default to your `tick-tock` instance. | ||
The following methods are available on your constructed instance: | ||
- [Tock.setTimeout(name, fn, timeout)](#tocksettimeout) | ||
- [Tock.setInterval(name, fn, interval)](#tocksetinterval) | ||
- [Tock.clear(name, name, ..)](#tockclear) | ||
- [Tock.active(name)](#tockactive) | ||
- [Tock.adjust(name, duration)](#tockadjust) | ||
- [Tock.end()](#tockend) | ||
### Tock.setTimeout() | ||
@@ -96,4 +110,16 @@ | ||
### Tock.end() | ||
You no longer wish to interact with your instance and wants it to be fully shut | ||
down. This kills all active timers using `tock.clear()` and nulls the internal | ||
properties. It will return `true` if it's the first time it's destroyed and | ||
`false` if was already destroyed before. If you call any of the other methods | ||
after destruction, they will throw errors. | ||
```js | ||
tock.end(); | ||
``` | ||
## License | ||
MIT |
36
test.js
@@ -0,1 +1,2 @@ | ||
/* istanbul ignore next */ | ||
describe('ticktock', function () { | ||
@@ -170,2 +171,37 @@ 'use strict'; | ||
describe('#setImmediate', function () { | ||
it('adds a setImmediate', function (next) { | ||
var start = Date.now(); | ||
tock.setImmediate('test', function () { | ||
var taken = Date.now() - start; | ||
assume(this).equals(tock); | ||
assume(taken).is.below(5); | ||
next(); | ||
}); | ||
}); | ||
it('can be called with a custom context', function (next) { | ||
var tock = new Tick(context); | ||
tock.setImmediate('test', function () { | ||
assume(this).deep.equals(context); | ||
next(); | ||
}); | ||
}); | ||
it('can be cleared', function (next) { | ||
var tock = new Tick(context); | ||
tock.setImmediate('test', function () { | ||
throw new Error('I should die'); | ||
}); | ||
tock.clear('test'); | ||
setTimeout(next, 100); | ||
}); | ||
}); | ||
describe('#setTimeout', function () { | ||
@@ -172,0 +208,0 @@ it('adds a setTimeout', function (next) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19915
508
124