Permalink
Browse files

all actions now get queued

  • Loading branch information...
1 parent ced41d4 commit 71397509dbe36cf776c055903346c6c49704accb @pkrumins committed Oct 8, 2010
Showing with 42 additions and 0 deletions.
  1. +42 −0 lib/store.js
View
@@ -7,6 +7,8 @@ function Store(opts, cb) {
if (!(this instanceof Store)) return new Store(opts, cb);
if (cb === undefined) cb = function () {};
var self = this;
+ var actionQueue = [];
+ var ready = false;
if (typeof opts === 'string') {
var opts = {
@@ -20,6 +22,14 @@ function Store(opts, cb) {
opts.json = opts.json || false;
+ self.on('ready', function () {
+ ready = true;
+ actionQueue.forEach(function (action) {
+ self[action.action].apply(action.args);
+ });
+ delete actionQueue;
+ });
+
var db = new SQLite();
db.open(opts.filename, function (error) {
if (error) {
@@ -65,7 +75,15 @@ function Store(opts, cb) {
);
}
+ function queue(action, args) {
+ actionQueue.push({ action : action, args : [].concat.call([], args) });
+ }
+
self.set = function (key, value, cb) {
+ if (!ready) {
+ queue('set', arguments);
+ return;
+ }
if (cb === undefined) cb = function () {}
db.query(
"INSERT OR REPLACE INTO store (key, value) VALUES (?, ?)",
@@ -83,6 +101,10 @@ function Store(opts, cb) {
};
self.get = function (key, cb) {
+ if (!ready) {
+ queue('get', arguments);
+ return;
+ }
if (cb === undefined) cb = function () {}
var hadRow = false;
db.query(
@@ -106,6 +128,10 @@ function Store(opts, cb) {
};
self.remove = function (key, cb) {
+ if (!ready) {
+ queue('remove', arguments);
+ return;
+ }
if (cb === undefined) cb = function () {};
db.query(
"DELETE FROM store WHERE key = ?",
@@ -125,6 +151,10 @@ function Store(opts, cb) {
};
self.filter = function (pred, cb, done) {
+ if (!ready) {
+ queue('filter', arguments);
+ return;
+ }
if (cb === undefined) cb = function () {};
if (done === undefined) done = function () {};
db.query(
@@ -148,6 +178,10 @@ function Store(opts, cb) {
}
self.length = function (cb) {
+ if (!ready) {
+ queue('length', arguments);
+ return;
+ }
if (cb === undefined) cb = function () {};
db.query(
"SELECT COUNT(*) as count FROM store",
@@ -164,6 +198,10 @@ function Store(opts, cb) {
}
self.forEach = function (cb, done) {
+ if (!ready) {
+ queue('forEach', arguments);
+ return;
+ }
if (cb === undefined) cb = function () {};
if (done === undefined) done = function () {};
db.query(
@@ -184,6 +222,10 @@ function Store(opts, cb) {
}
self.all = function (cb) {
+ if (!ready) {
+ queue('all', arguments);
+ return;
+ }
var keys = [];
var vals = [];
self.forEach(

0 comments on commit 7139750

Please sign in to comment.