Skip to content

Commit 5ae1546

Browse files
committed
Merge pull request #1 from nubs/master
Initial commit of library.
2 parents 0364de1 + f4f65e4 commit 5ae1546

File tree

6 files changed

+221
-0
lines changed

6 files changed

+221
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/docs/
2+
/node_modules/

.jshintrc

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{
2+
// JSHint Default Configuration File (as on JSHint website)
3+
// See http://jshint.com/docs/ for more details
4+
5+
"maxerr" : 50, // {int} Maximum error before stopping
6+
7+
// Enforcing
8+
"bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.)
9+
"camelcase" : false, // true: Identifiers must be in camelCase
10+
"curly" : true, // true: Require {} for every new block or scope
11+
"eqeqeq" : true, // true: Require triple equals (===) for comparison
12+
"freeze" : true, // true: prohibits overwriting prototypes of native objects such as Array, Date etc.
13+
"forin" : true, // true: Require filtering for..in loops with obj.hasOwnProperty()
14+
"immed" : false, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());`
15+
"indent" : 3, // {int} Number of spaces to use for indentation
16+
"latedef" : false, // true: Require variables/functions to be defined before being used
17+
"newcap" : false, // true: Require capitalization of all constructor functions e.g. `new F()`
18+
"noarg" : true, // true: Prohibit use of `arguments.caller` and `arguments.callee`
19+
"noempty" : true, // true: Prohibit use of empty blocks
20+
"nonbsp" : true, // true: Prohibit "non-breaking whitespace" characters.
21+
"nonew" : false, // true: Prohibit use of constructors for side-effects (without assignment)
22+
"plusplus" : false, // true: Prohibit use of `++` & `--`
23+
"quotmark" : false, // Quotation mark consistency:
24+
// false : do nothing (default)
25+
// true : ensure whatever is used is consistent
26+
// "single" : require single quotes
27+
// "double" : require double quotes
28+
"undef" : true, // true: Require all non-global variables to be declared (prevents global leaks)
29+
"unused" : true, // true: Require all defined variables be used
30+
"strict" : true, // true: Requires all functions run in ES5 Strict Mode
31+
"maxparams" : false, // {int} Max number of formal params allowed per function
32+
"maxdepth" : false, // {int} Max depth of nested blocks (within functions)
33+
"maxstatements" : false, // {int} Max number statements per function
34+
"maxcomplexity" : false, // {int} Max cyclomatic complexity per function
35+
"maxlen" : false, // {int} Max number of characters per line
36+
37+
// Relaxing
38+
"asi" : false, // true: Tolerate Automatic Semicolon Insertion (no semicolons)
39+
"boss" : false, // true: Tolerate assignments where comparisons would be expected
40+
"debug" : false, // true: Allow debugger statements e.g. browser breakpoints.
41+
"eqnull" : false, // true: Tolerate use of `== null`
42+
"es5" : false, // true: Allow ES5 syntax (ex: getters and setters)
43+
"esnext" : false, // true: Allow ES.next (ES6) syntax (ex: `const`)
44+
"moz" : false, // true: Allow Mozilla specific syntax (extends and overrides esnext features)
45+
// (ex: `for each`, multiple try/catch, function expression…)
46+
"evil" : false, // true: Tolerate use of `eval` and `new Function()`
47+
"expr" : false, // true: Tolerate `ExpressionStatement` as Programs
48+
"funcscope" : false, // true: Tolerate defining variables inside control statements
49+
"globalstrict" : false, // true: Allow global "use strict" (also enables 'strict')
50+
"iterator" : false, // true: Tolerate using the `__iterator__` property
51+
"lastsemic" : false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block
52+
"laxbreak" : false, // true: Tolerate possibly unsafe line breakings
53+
"laxcomma" : false, // true: Tolerate comma-first style coding
54+
"loopfunc" : false, // true: Tolerate functions being defined in loops
55+
"multistr" : false, // true: Tolerate multi-line strings
56+
"noyield" : false, // true: Tolerate generator functions with no yield statement in them.
57+
"notypeof" : false, // true: Tolerate invalid typeof operator values
58+
"proto" : false, // true: Tolerate using the `__proto__` property
59+
"scripturl" : false, // true: Tolerate script-targeted URLs
60+
"shadow" : false, // true: Allows re-define variables later in code e.g. `var x=1; x=2;`
61+
"sub" : false, // true: Tolerate using `[]` notation when it can still be expressed in dot notation
62+
"supernew" : false, // true: Tolerate `new function () { ... };` and `new Object;`
63+
"validthis" : false, // true: Tolerate using this in a non-constructor function
64+
65+
// Environments
66+
"browser" : true, // Web Browser (window, document, etc)
67+
"browserify" : true, // Browserify (node.js code in the browser)
68+
"couch" : false, // CouchDB
69+
"devel" : true, // Development/debugging (alert, confirm, etc)
70+
"dojo" : false, // Dojo Toolkit
71+
"jasmine" : false, // Jasmine
72+
"jquery" : true, // jQuery
73+
"mocha" : false, // Mocha
74+
"mootools" : false, // MooTools
75+
"node" : true, // Node.js
76+
"nonstandard" : false, // Widely adopted globals (escape, unescape, etc)
77+
"prototypejs" : false, // Prototype and Scriptaculous
78+
"qunit" : false, // QUnit
79+
"rhino" : false, // Rhino
80+
"shelljs" : false, // ShellJS
81+
"worker" : false, // Web Workers
82+
"wsh" : false, // Windows Scripting Host
83+
"yui" : false, // Yahoo User Interface
84+
85+
// Custom Globals
86+
"globals" : {"define": true} // additional predefined global variables
87+
}

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Help.com
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# timeout-manager.js
2+
Manages a collection of timeouts by key, with simple interaction.
3+
4+
## Usage
5+
Take a look at the [docco][docco]-generated [docs][docs].
6+
7+
[docco]: https://jashkenas.github.io/docco/
8+
[docs]: https://helpdotcom.github.io/timeout-manager.js/

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "timeout-manager",
3+
"version": "0.1.0",
4+
"description": "Manages a collection of timeouts by key, with simple interaction.",
5+
"main": "timeout-manager.js",
6+
"author": "Help.com",
7+
"license": "MIT",
8+
"repository": {
9+
"type": "git",
10+
"url": "git://github.com/helpdotcom/timeout-manager.js.git"
11+
},
12+
"dependencies": {
13+
},
14+
"devDependencies": {
15+
"docco": "~0.7.0",
16+
"jshint": "~2.6"
17+
},
18+
"scripts": {
19+
"doc": "docco timeout-manager.js",
20+
"lint": "jshint timeout-manager.js"
21+
}
22+
}

timeout-manager.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
(function() {
2+
'use strict';
3+
4+
// ## TimeoutManager
5+
// This timeout manager provides a simple timeout container. Functions are
6+
// added with a unique key and executed when the configured timeout period
7+
// expires. The timeouts can be cleared as well by their key.
8+
//
9+
// var TimeoutManager = require('timeout-manager');
10+
// var timeoutManager = new TimeoutManager();
11+
// timeoutManager.add('greet', function() {
12+
// console.log('Hello there!');
13+
// });
14+
15+
// ### TimeoutManager *constructor*
16+
// Initiate the timeout manager with an optional timeout period (default is
17+
// 60000, or 60s).
18+
//
19+
// var timeoutManager = new TimeoutManager({timeout: 10000});
20+
var TimeoutManager = function(opts) {
21+
this._timeout = opts.timeout || 60000;
22+
this._list = {};
23+
};
24+
25+
// ### TimeoutManager.add
26+
// Starts a timer that will execute the given function when time is done.
27+
// The timer is stored by the given id which can be used to stop the timer.
28+
// If this is called again with the same key before the previous timer has
29+
// fired, this will remove the existing timer and start a new one with the
30+
// new function.
31+
//
32+
// timeoutManager.add('greet', function() {
33+
// console.log('Hello there!');
34+
// });
35+
TimeoutManager.prototype.add = function(key, fn) {
36+
if (this._list.hasOwnProperty(key)) {
37+
this.remove(key);
38+
}
39+
40+
var self = this;
41+
this._list[key] = setTimeout(function() {
42+
self.remove(key);
43+
fn();
44+
}, this._timeout);
45+
};
46+
47+
// ### TimeoutManager.remove
48+
// Stops the timer with the given key and removes it from the manager. If
49+
// the timer with the given key does not exist or has already fired, this
50+
// returns without error.
51+
//
52+
// timeoutManager.remove('greet');
53+
TimeoutManager.prototype.remove = function(key) {
54+
if (!this._list.hasOwnProperty(key)) {
55+
return;
56+
}
57+
58+
clearTimeout(this._list[key]);
59+
delete this._list[key];
60+
};
61+
62+
// ---
63+
// ## Exports
64+
65+
// Handle node.js and browser includes.
66+
if (typeof exports !== 'undefined') {
67+
if (typeof module !== 'undefined' && module.exports) {
68+
exports = module.exports = TimeoutManager;
69+
} else {
70+
exports.TimeoutManager = TimeoutManager;
71+
}
72+
} else {
73+
this.TimeoutManager = TimeoutManager;
74+
}
75+
76+
// Handle AMD autoloaders.
77+
if (typeof define === 'function' && define.amd) {
78+
define('TimeoutManager', [], function() {
79+
return TimeoutManager;
80+
});
81+
}
82+
}.call(this));

0 commit comments

Comments
 (0)