Skip to content

Commit

Permalink
Merge pull request #1 from nubs/master
Browse files Browse the repository at this point in the history
Initial commit of library.
  • Loading branch information
nubs committed Feb 25, 2015
2 parents 0364de1 + f4f65e4 commit 5ae1546
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/docs/
/node_modules/
87 changes: 87 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
// JSHint Default Configuration File (as on JSHint website)
// See http://jshint.com/docs/ for more details

"maxerr" : 50, // {int} Maximum error before stopping

// Enforcing
"bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.)
"camelcase" : false, // true: Identifiers must be in camelCase
"curly" : true, // true: Require {} for every new block or scope
"eqeqeq" : true, // true: Require triple equals (===) for comparison
"freeze" : true, // true: prohibits overwriting prototypes of native objects such as Array, Date etc.
"forin" : true, // true: Require filtering for..in loops with obj.hasOwnProperty()
"immed" : false, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());`
"indent" : 3, // {int} Number of spaces to use for indentation
"latedef" : false, // true: Require variables/functions to be defined before being used
"newcap" : false, // true: Require capitalization of all constructor functions e.g. `new F()`
"noarg" : true, // true: Prohibit use of `arguments.caller` and `arguments.callee`
"noempty" : true, // true: Prohibit use of empty blocks
"nonbsp" : true, // true: Prohibit "non-breaking whitespace" characters.
"nonew" : false, // true: Prohibit use of constructors for side-effects (without assignment)
"plusplus" : false, // true: Prohibit use of `++` & `--`
"quotmark" : false, // Quotation mark consistency:
// false : do nothing (default)
// true : ensure whatever is used is consistent
// "single" : require single quotes
// "double" : require double quotes
"undef" : true, // true: Require all non-global variables to be declared (prevents global leaks)
"unused" : true, // true: Require all defined variables be used
"strict" : true, // true: Requires all functions run in ES5 Strict Mode
"maxparams" : false, // {int} Max number of formal params allowed per function
"maxdepth" : false, // {int} Max depth of nested blocks (within functions)
"maxstatements" : false, // {int} Max number statements per function
"maxcomplexity" : false, // {int} Max cyclomatic complexity per function
"maxlen" : false, // {int} Max number of characters per line

// Relaxing
"asi" : false, // true: Tolerate Automatic Semicolon Insertion (no semicolons)
"boss" : false, // true: Tolerate assignments where comparisons would be expected
"debug" : false, // true: Allow debugger statements e.g. browser breakpoints.
"eqnull" : false, // true: Tolerate use of `== null`
"es5" : false, // true: Allow ES5 syntax (ex: getters and setters)
"esnext" : false, // true: Allow ES.next (ES6) syntax (ex: `const`)
"moz" : false, // true: Allow Mozilla specific syntax (extends and overrides esnext features)
// (ex: `for each`, multiple try/catch, function expression…)
"evil" : false, // true: Tolerate use of `eval` and `new Function()`
"expr" : false, // true: Tolerate `ExpressionStatement` as Programs
"funcscope" : false, // true: Tolerate defining variables inside control statements
"globalstrict" : false, // true: Allow global "use strict" (also enables 'strict')
"iterator" : false, // true: Tolerate using the `__iterator__` property
"lastsemic" : false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block
"laxbreak" : false, // true: Tolerate possibly unsafe line breakings
"laxcomma" : false, // true: Tolerate comma-first style coding
"loopfunc" : false, // true: Tolerate functions being defined in loops
"multistr" : false, // true: Tolerate multi-line strings
"noyield" : false, // true: Tolerate generator functions with no yield statement in them.
"notypeof" : false, // true: Tolerate invalid typeof operator values
"proto" : false, // true: Tolerate using the `__proto__` property
"scripturl" : false, // true: Tolerate script-targeted URLs
"shadow" : false, // true: Allows re-define variables later in code e.g. `var x=1; x=2;`
"sub" : false, // true: Tolerate using `[]` notation when it can still be expressed in dot notation
"supernew" : false, // true: Tolerate `new function () { ... };` and `new Object;`
"validthis" : false, // true: Tolerate using this in a non-constructor function

// Environments
"browser" : true, // Web Browser (window, document, etc)
"browserify" : true, // Browserify (node.js code in the browser)
"couch" : false, // CouchDB
"devel" : true, // Development/debugging (alert, confirm, etc)
"dojo" : false, // Dojo Toolkit
"jasmine" : false, // Jasmine
"jquery" : true, // jQuery
"mocha" : false, // Mocha
"mootools" : false, // MooTools
"node" : true, // Node.js
"nonstandard" : false, // Widely adopted globals (escape, unescape, etc)
"prototypejs" : false, // Prototype and Scriptaculous
"qunit" : false, // QUnit
"rhino" : false, // Rhino
"shelljs" : false, // ShellJS
"worker" : false, // Web Workers
"wsh" : false, // Windows Scripting Host
"yui" : false, // Yahoo User Interface

// Custom Globals
"globals" : {"define": true} // additional predefined global variables
}
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2015 Help.com

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# timeout-manager.js
Manages a collection of timeouts by key, with simple interaction.

## Usage
Take a look at the [docco][docco]-generated [docs][docs].

[docco]: https://jashkenas.github.io/docco/
[docs]: https://helpdotcom.github.io/timeout-manager.js/
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "timeout-manager",
"version": "0.1.0",
"description": "Manages a collection of timeouts by key, with simple interaction.",
"main": "timeout-manager.js",
"author": "Help.com",
"license": "MIT",
"repository": {
"type": "git",
"url": "git://github.com/helpdotcom/timeout-manager.js.git"
},
"dependencies": {
},
"devDependencies": {
"docco": "~0.7.0",
"jshint": "~2.6"
},
"scripts": {
"doc": "docco timeout-manager.js",
"lint": "jshint timeout-manager.js"
}
}
82 changes: 82 additions & 0 deletions timeout-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
(function() {
'use strict';

// ## TimeoutManager
// This timeout manager provides a simple timeout container. Functions are
// added with a unique key and executed when the configured timeout period
// expires. The timeouts can be cleared as well by their key.
//
// var TimeoutManager = require('timeout-manager');
// var timeoutManager = new TimeoutManager();
// timeoutManager.add('greet', function() {
// console.log('Hello there!');
// });

// ### TimeoutManager *constructor*
// Initiate the timeout manager with an optional timeout period (default is
// 60000, or 60s).
//
// var timeoutManager = new TimeoutManager({timeout: 10000});
var TimeoutManager = function(opts) {
this._timeout = opts.timeout || 60000;
this._list = {};
};

// ### TimeoutManager.add
// Starts a timer that will execute the given function when time is done.
// The timer is stored by the given id which can be used to stop the timer.
// If this is called again with the same key before the previous timer has
// fired, this will remove the existing timer and start a new one with the
// new function.
//
// timeoutManager.add('greet', function() {
// console.log('Hello there!');
// });
TimeoutManager.prototype.add = function(key, fn) {
if (this._list.hasOwnProperty(key)) {
this.remove(key);
}

var self = this;
this._list[key] = setTimeout(function() {
self.remove(key);
fn();
}, this._timeout);
};

// ### TimeoutManager.remove
// Stops the timer with the given key and removes it from the manager. If
// the timer with the given key does not exist or has already fired, this
// returns without error.
//
// timeoutManager.remove('greet');
TimeoutManager.prototype.remove = function(key) {
if (!this._list.hasOwnProperty(key)) {
return;
}

clearTimeout(this._list[key]);
delete this._list[key];
};

// ---
// ## Exports

// Handle node.js and browser includes.
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = TimeoutManager;
} else {
exports.TimeoutManager = TimeoutManager;
}
} else {
this.TimeoutManager = TimeoutManager;
}

// Handle AMD autoloaders.
if (typeof define === 'function' && define.amd) {
define('TimeoutManager', [], function() {
return TimeoutManager;
});
}
}.call(this));

0 comments on commit 5ae1546

Please sign in to comment.