Skip to content

Commit 830fe09

Browse files
committed
Initial commit
0 parents  commit 830fe09

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

LICENSE-MIT

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Copyright (c) 2014, Technical Machine, Inc.
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
* The name Michael Bostock may not be used to endorse or promote products
15+
derived from this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
DISCLAIMED. IN NO EVENT SHALL MICHAEL BOSTOCK BE LIABLE FOR ANY DIRECT,
21+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#Sync-Queue
2+
3+
## Install
4+
`npm install sync-queue`
5+
6+
## Usage
7+
8+
```.js
9+
var Queue = require('sync-queue')
10+
var queue = new Queue();
11+
12+
queue.place(function one() {
13+
console.log('I'm func one);
14+
15+
setTimeout(function() {
16+
console.log("and I'm still finishing up...");
17+
queue.next();
18+
}, 1000);
19+
})
20+
21+
queue.place(function two() {
22+
console.log.bind(this, "I'm the last func.");
23+
queue.next();
24+
});
25+
```
26+
27+
## Description
28+
Use `place` to put things in the queue. They will start being executed automatically. Call `next` when you want the thing you put in the queue is finished.

index.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function queue() {
2+
3+
// Create an empty array of commands
4+
var queue = [];
5+
// We're inactive to begin with
6+
queue.active = false;
7+
// Method for adding command chain to the queue
8+
queue.place = function (command) {
9+
// Push the command onto the command array
10+
queue.push(command);
11+
// If we're currently inactive, start processing
12+
if (!queue.active) queue.next();
13+
};
14+
// Method for calling the next command chain in the array
15+
queue.next = function () {
16+
// If this is the end of the queue
17+
if (!queue.length) {
18+
// We're no longer active
19+
queue.active = false;
20+
// Stop execution
21+
return;
22+
}
23+
// Grab the next command
24+
var command = queue.shift();
25+
// We're active
26+
queue.active = true;
27+
// Call the command
28+
command();
29+
};
30+
31+
return queue;
32+
}
33+
34+
module.exports = queue;

package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "sync-queue",
3+
"version": "0.0.1",
4+
"description": "Simple queue for completing tasks in series",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": "[email protected]:technicalmachine/sync-queue.git",
10+
"contributors": [
11+
{"name" : "Tim Cameron Ryan", "email" : "[email protected]" },
12+
{"name" : "Jon McKay", "email" : "[email protected]" }
13+
],
14+
"license": "MIT"
15+
}

test/test.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var Queue = require('../');
2+
var queue = new Queue();
3+
4+
queue.place(function one() {
5+
console.log("I'm func one");
6+
7+
setTimeout(function() {
8+
console.log("and I'm still finishing up...");
9+
queue.next();
10+
}, 1000);
11+
})
12+
13+
queue.place(function two() {
14+
console.log("I'm the last func.");
15+
queue.next();
16+
});
17+
18+
console.log('placed both...');

0 commit comments

Comments
 (0)