forked from tessel/sync-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 830fe09
Showing
5 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...'); |