[Pkg-javascript-commits] [node-throttleit] 01/03: Imported Upstream version 1.0.0

Sebastiaan Couwenberg sebastic at moszumanska.debian.org
Sat Mar 21 20:39:48 UTC 2015


This is an automated email from the git hooks/post-receive script.

sebastic pushed a commit to branch master
in repository node-throttleit.

commit ab4eba7e0067b57779dc371e000c68faea8c068a
Author: Bas Couwenberg <sebastic at xs4all.nl>
Date:   Sat Mar 21 20:15:50 2015 +0100

    Imported Upstream version 1.0.0
---
 .gitignore     |  3 +++
 History.md     | 19 +++++++++++++++
 Makefile       | 18 +++++++++++++++
 Readme.md      | 32 +++++++++++++++++++++++++
 component.json | 13 +++++++++++
 example.js     | 14 +++++++++++
 index.js       | 32 +++++++++++++++++++++++++
 package.json   | 22 ++++++++++++++++++
 test.js        | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 9 files changed, 226 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..665aa21
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+components
+build
+node_modules
diff --git a/History.md b/History.md
new file mode 100644
index 0000000..4847d91
--- /dev/null
+++ b/History.md
@@ -0,0 +1,19 @@
+
+1.0.0 / 2015-02-27
+==================
+
+ - Internal refactor
+ - Removed `max` argument
+ - Context for invocation is cached/cleared properly
+ - More tests
+
+0.0.2 / 2013-03-26
+==================
+
+ - Cache the return value
+ - Don't use `setTimeout()`
+
+0.0.1 / 2013-03-26
+==================
+
+ - Initial release
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..096ef6c
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,18 @@
+
+build: components index.js
+	@component build --dev
+
+components: component.json
+	@component install --dev
+
+clean:
+	rm -fr build components template.js
+
+test: node_modules
+	@./node_modules/mocha/bin/mocha \
+		--reporter spec
+
+node_modules: package.json
+	@npm install
+
+.PHONY: clean
diff --git a/Readme.md b/Readme.md
new file mode 100644
index 0000000..03969a5
--- /dev/null
+++ b/Readme.md
@@ -0,0 +1,32 @@
+
+# throttle
+
+  Throttle a function
+
+## Installation
+
+    $ component install component/throttle
+
+## Example
+
+    var throttle = require('throttle');
+    window.onresize = throttle(resize, 200);
+
+    function resize(e) {
+      console.log('height', window.innerHeight);
+      console.log('width', window.innerWidth);
+    }
+
+## API
+
+### throttle(fn, wait)
+
+Creates a function that will call `fn` at most once every `wait` milliseconds.
+
+Supports leading and trailing invocation.
+
+`fn` will receive last context (`this`) and last arguments passed to a throttled wrapper before `fn` was invoked.
+
+## License
+
+  MIT
diff --git a/component.json b/component.json
new file mode 100644
index 0000000..7cf81a0
--- /dev/null
+++ b/component.json
@@ -0,0 +1,13 @@
+{
+  "name": "throttle",
+  "repo": "component/throttle",
+  "description": "Throttle a function",
+  "version": "0.0.2",
+  "keywords": [],
+  "dependencies": {},
+  "development": {},
+  "license": "MIT",
+  "scripts": [
+    "index.js"
+  ]
+}
diff --git a/example.js b/example.js
new file mode 100644
index 0000000..a7f0f62
--- /dev/null
+++ b/example.js
@@ -0,0 +1,14 @@
+
+var throttle = require('./');
+
+function onprogress(n) {
+  console.log('progress %s%', n);
+}
+
+onprogress = throttle(onprogress, 500);
+
+var n = 0;
+setInterval(function(){
+  if (n >= 100) return;
+  onprogress(n++);
+}, 50);
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..1cc3ad6
--- /dev/null
+++ b/index.js
@@ -0,0 +1,32 @@
+module.exports = throttle;
+
+/**
+ * Returns a new function that, when invoked, invokes `func` at most once per `wait` milliseconds.
+ *
+ * @param {Function} func Function to wrap.
+ * @param {Number} wait Number of milliseconds that must elapse between `func` invocations.
+ * @return {Function} A new function that wraps the `func` function passed in.
+ */
+
+function throttle (func, wait) {
+  var ctx, args, rtn, timeoutID; // caching
+  var last = 0;
+
+  return function throttled () {
+    ctx = this;
+    args = arguments;
+    var delta = new Date() - last;
+    if (!timeoutID)
+      if (delta >= wait) call();
+      else timeoutID = setTimeout(call, wait - delta);
+    return rtn;
+  };
+
+  function call () {
+    timeoutID = 0;
+    last = +new Date();
+    rtn = func.apply(ctx, args);
+    ctx = null;
+    args = null;
+  }
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..fce464f
--- /dev/null
+++ b/package.json
@@ -0,0 +1,22 @@
+{
+  "name": "throttleit",
+  "description": "Throttle a function",
+  "version": "1.0.0",
+  "keywords": [],
+  "repository": {
+    "type": "git",
+    "url": "git://github.com/component/throttle.git"
+  },
+  "devDependencies": {
+    "mocha": "^1.18.0"
+  },
+  "license": "MIT",
+  "component": {
+    "scripts": {
+      "throttle/index.js": "index.js"
+    }
+  },
+  "scripts": {
+    "test": "mocha --reporter spec"
+  }
+}
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..d59eb1b
--- /dev/null
+++ b/test.js
@@ -0,0 +1,73 @@
+var assert = require('assert');
+var throttle = require('./');
+
+describe('throttle', function(){
+  function counter() {
+    function count(){
+      count.invoked++;
+    }
+    count.invoked = 0;
+    return count;
+  }
+
+  it('should throttle a function', function(done){
+    var count = counter();
+    var wait = 100;
+    var total = 500;
+    var fn = throttle(count, wait);
+    var interval = setInterval(fn, 20);
+    setTimeout(function(){
+      clearInterval(interval);
+      assert(count.invoked === (total / wait));
+      done();
+    }, total + 5);
+  });
+
+  it('should call the function last time', function(done){
+    var count = counter();
+    var wait = 100;
+    var fn = throttle(count, wait);
+    fn();
+    fn();
+    assert(count.invoked === 1);
+    setTimeout(function(){
+      assert(count.invoked === 2);
+      done();
+    }, wait + 5);
+  });
+
+  it('should pass last context', function(done){
+    var wait = 100;
+    var ctx;
+    var fn = throttle(logctx, wait);
+    var foo = {};
+    var bar = {};
+    fn.call(foo);
+    fn.call(bar);
+    assert(ctx === foo);
+    setTimeout(function(){
+      assert(ctx === bar);
+      done();
+    }, wait + 5);
+    function logctx() {
+      ctx = this;
+    }
+  });
+
+  it('should pass last arguments', function(done){
+    var wait = 100;
+    var args;
+    var fn = throttle(logargs, wait);
+    fn.call(null, 1);
+    fn.call(null, 2);
+    assert(args && args[0] === 1);
+    setTimeout(function(){
+      assert(args && args[0] === 2);
+      done();
+    }, wait + 5);
+    function logargs() {
+      args = arguments;
+    }
+  });
+
+});
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-throttleit.git



More information about the Pkg-javascript-commits mailing list