Skip to content
Browse files

arguments support

  • Loading branch information...
1 parent 4c0d5d9 commit 478943e79a0d050131fa53cff95981660408945f Thorsten Lorenz committed Sep 27, 2012
Showing with 40 additions and 17 deletions.
  1. +6 −4 README.md
  2. +7 −5 index.js
  3. +27 −8 test/kebab.js
View
10 README.md
@@ -17,9 +17,9 @@ var kebab = require('kebab')
### enqueue
-***kebab.enqueue(item)***
+***kebab.enqueue(arg1 [, arg2, .., argn])***
-item is what ever you want to pass when a subscriber callback is called.
+arg1 .. argn are the arguments you want to pass when a subscriber callback is called.
**Example:**
@@ -31,9 +31,11 @@ kb.enqueue('hello world');
***kebab.once(callback)***
-subscribe to a queued item. If one is currently available, callback will be invoked with it immediately.
+Subscribe to a queued be called back with queued arguments.
-Otherwise the callback will be invoked one time when an item is enqueued in the future.
+If queue is currently holding arguments, callback will be invoked with them immediately.
+
+Otherwise the callback will be invoked one time when an arguments are enqueued in the future.
**Example:**
View
12 index.js
@@ -1,16 +1,18 @@
+var slice = Array.prototype.slice;
+
function kebab() {
var queue = []
, subscriptions = [];
return {
- enqueue : function (item) {
- queue.push(item);
+ enqueue : function () {
+ queue.push(slice.call(arguments));
var sub = subscriptions.shift();
- if (sub) sub(queue.shift());
+ if (sub) sub.apply(this, queue.shift());
}
, once : function (cb) {
- var item = queue.shift();
- return item ? cb(item) : subscriptions.push(cb);
+ var args = queue.shift();
+ return args ? cb.apply(this, args) : subscriptions.push(cb);
}
};
}
View
35 test/kebab.js
@@ -4,18 +4,18 @@ var kebab = require('..')
, test = require('tap').test;
function setup () {
- queue = kebab();
+ kb = kebab();
}
test('when an item is in the queue', function (t) {
setup();
t.plan(1);
- queue.enqueue('uno');
+ kb.enqueue('uno');
t.test('# once calls back immediately with that item', function (t) {
t.plan(1);
- queue.once(function (item) {
+ kb.once(function (item) {
t.equal('uno', item);
t.end();
})
@@ -31,7 +31,7 @@ test('when no item is in the queue', function (t) {
t.plan(1);
- queue.once(function (item) {
+ kb.once(function (item) {
calledBack = true;
})
@@ -52,15 +52,15 @@ test('when no item is in the queue', function (t) {
t.plan(1);
- queue.once(function (item) {
+ kb.once(function (item) {
calledBack = true;
calledWith = item;
})
t.test('# # and enqueue an item after', function (t) {
t.plan(3);
- queue.enqueue('uno');
+ kb.enqueue('uno');
t.equal(calledBack, true, 'calls back');
t.equal('uno', calledWith, 'with queued item');
@@ -69,14 +69,14 @@ test('when no item is in the queue', function (t) {
t.plan(2);
calledBack = false;
- queue.enqueue('dos');
+ kb.enqueue('dos');
t.equal(calledBack, false, 'does not call back again');
t.test('# # # # and I listen once again', function (t) {
t.plan(1);
- queue.once(function (item) {
+ kb.once(function (item) {
t.equal('dos', item, 'calls back with the other item');
t.end();
})
@@ -86,4 +86,23 @@ test('when no item is in the queue', function (t) {
})
})
+test('when I queue multiple arguments', function (t) {
+ setup();
+ kb.enqueue(1, 2, 'foo', { uno: 'eins' });
+
+ t.test('and I listen once', function (t) {
+ t.plan(4);
+
+ kb.once(function ($1, $2, foo, unoeins) {
+ t.equal($1, 1, 'passes first number');
+ t.equal($2, 2, 'passes second number');
+ t.equal(foo, 'foo', 'passes string');
+ t.deepEqual(unoeins, { uno: 'eins' }, 'passes object');
+ t.end();
+ })
+ })
+})
+
+
+

0 comments on commit 478943e

Please sign in to comment.
Something went wrong with that request. Please try again.