Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyman727 committed May 3, 2014
0 parents commit 830fe09
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
26 changes: 26 additions & 0 deletions LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright (c) 2014, Technical Machine, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* The name Michael Bostock may not be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL MICHAEL BOSTOCK BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#Sync-Queue

## Install
`npm install sync-queue`

## Usage

```.js
var Queue = require('sync-queue')
var queue = new Queue();

queue.place(function one() {
console.log('I'm func one);

setTimeout(function() {
console.log("and I'm still finishing up...");
queue.next();
}, 1000);
})

queue.place(function two() {
console.log.bind(this, "I'm the last func.");
queue.next();
});
```

## Description
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.
34 changes: 34 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function queue() {

// Create an empty array of commands
var queue = [];
// We're inactive to begin with
queue.active = false;
// Method for adding command chain to the queue
queue.place = function (command) {
// Push the command onto the command array
queue.push(command);
// If we're currently inactive, start processing
if (!queue.active) queue.next();
};
// Method for calling the next command chain in the array
queue.next = function () {
// If this is the end of the queue
if (!queue.length) {
// We're no longer active
queue.active = false;
// Stop execution
return;
}
// Grab the next command
var command = queue.shift();
// We're active
queue.active = true;
// Call the command
command();
};

return queue;
}

module.exports = queue;
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "sync-queue",
"version": "0.0.1",
"description": "Simple queue for completing tasks in series",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": "[email protected]:technicalmachine/sync-queue.git",
"contributors": [
{"name" : "Tim Cameron Ryan", "email" : "[email protected]" },
{"name" : "Jon McKay", "email" : "[email protected]" }
],
"license": "MIT"
}
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var Queue = require('../');
var queue = new Queue();

queue.place(function one() {
console.log("I'm func one");

setTimeout(function() {
console.log("and I'm still finishing up...");
queue.next();
}, 1000);
})

queue.place(function two() {
console.log("I'm the last func.");
queue.next();
});

console.log('placed both...');

0 comments on commit 830fe09

Please sign in to comment.