-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a basic solution based on level-sublevel.
- Loading branch information
Showing
2 changed files
with
52 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,7 @@ | ||
|
||
function setup(run, callback) { | ||
console.log(arguments) | ||
} | ||
|
||
module.exports = setup | ||
module.exports.async = true |
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,45 @@ | ||
var sublevel = require('level-sublevel') | ||
|
||
function Queue(db, name) { | ||
if (!(this instanceof Queue)) return new Queue(db, name) | ||
|
||
this._db = sublevel(db).sublevel(name) | ||
} | ||
|
||
module.exports = Queue | ||
|
||
Queue.prototype.push = function(element, callback) { | ||
var key = (new Date()).toISOString() | ||
|
||
this._db.put(key, element, callback) | ||
|
||
return this; | ||
} | ||
|
||
Queue.prototype.shift = function(callback) { | ||
var stream = this._db.createReadStream({ | ||
limit: 1 | ||
}) | ||
|
||
, db = this._db | ||
|
||
stream.once('data', function(data) { | ||
db.del(data.key, function(err) { | ||
callback(err, data.value) | ||
}) | ||
}) | ||
|
||
stream.once('error', callback) | ||
|
||
return this; | ||
} | ||
|
||
Queue.prototype.clear = function(callback) { | ||
this._db.createReadStream() | ||
.pipe(this._db.createWriteStream({ | ||
type: 'del' | ||
})) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
.on('close', callback) | ||
|
||
return this; | ||
} |
aah, now I get what you pointed me to in that level-delete-stream issue :D