2013-11-22 21:12:31 +00:00
|
|
|
define(function(require) {
|
2013-11-27 00:12:25 +00:00
|
|
|
var FILE_SYSTEM_NAME = require('src/constants').FILE_SYSTEM_NAME;
|
2013-11-22 21:12:31 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
// Based on https://github.com/caolan/async/blob/master/lib/async.js
|
|
|
|
var nextTick = (function() {
|
|
|
|
if (typeof process === 'undefined' || !(process.nextTick)) {
|
|
|
|
if (typeof setImmediate === 'function') {
|
|
|
|
return function (fn) {
|
|
|
|
// not a direct alias for IE10 compatibility
|
|
|
|
setImmediate(fn);
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return function (fn) {
|
|
|
|
setTimeout(fn, 0);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return process.nextTick;
|
|
|
|
}());
|
|
|
|
|
|
|
|
function asyncCallback(callback) {
|
|
|
|
nextTick(callback);
|
|
|
|
}
|
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function MemoryContext(db, readOnly) {
|
|
|
|
this.readOnly = readOnly;
|
|
|
|
this.objectStore = db;
|
|
|
|
}
|
|
|
|
MemoryContext.prototype.clear = function(callback) {
|
|
|
|
if(this.readOnly) {
|
2014-01-21 21:25:09 +00:00
|
|
|
asyncCallback(function() {
|
|
|
|
callback("[MemoryContext] Error: write operation on read only context");
|
|
|
|
});
|
|
|
|
return;
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
|
|
|
var objectStore = this.objectStore;
|
|
|
|
Object.keys(objectStore).forEach(function(key){
|
|
|
|
delete objectStore[key];
|
|
|
|
});
|
2014-01-21 21:25:09 +00:00
|
|
|
asyncCallback(callback);
|
2013-11-22 21:12:31 +00:00
|
|
|
};
|
|
|
|
MemoryContext.prototype.get = function(key, callback) {
|
2014-01-21 21:25:09 +00:00
|
|
|
var that = this;
|
|
|
|
asyncCallback(function() {
|
|
|
|
callback(null, that.objectStore[key]);
|
|
|
|
});
|
2013-11-22 21:12:31 +00:00
|
|
|
};
|
|
|
|
MemoryContext.prototype.put = function(key, value, callback) {
|
|
|
|
if(this.readOnly) {
|
2014-01-21 21:25:09 +00:00
|
|
|
asyncCallback(function() {
|
|
|
|
callback("[MemoryContext] Error: write operation on read only context");
|
|
|
|
});
|
|
|
|
return;
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
|
|
|
this.objectStore[key] = value;
|
2014-01-21 21:25:09 +00:00
|
|
|
asyncCallback(callback);
|
2013-11-22 21:12:31 +00:00
|
|
|
};
|
|
|
|
MemoryContext.prototype.delete = function(key, callback) {
|
|
|
|
if(this.readOnly) {
|
2014-01-21 21:25:09 +00:00
|
|
|
asyncCallback(function() {
|
|
|
|
callback("[MemoryContext] Error: write operation on read only context");
|
|
|
|
});
|
|
|
|
return;
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
|
|
|
delete this.objectStore[key];
|
2014-01-21 21:25:09 +00:00
|
|
|
asyncCallback(callback);
|
2013-11-22 21:12:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function Memory(name) {
|
2013-11-27 00:12:25 +00:00
|
|
|
this.name = name || FILE_SYSTEM_NAME;
|
2013-11-22 21:12:31 +00:00
|
|
|
this.db = {};
|
|
|
|
}
|
|
|
|
Memory.isSupported = function() {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
Memory.prototype.open = function(callback) {
|
2014-01-21 21:25:09 +00:00
|
|
|
asyncCallback(function() {
|
|
|
|
callback(null, true);
|
|
|
|
});
|
2013-11-22 21:12:31 +00:00
|
|
|
};
|
|
|
|
Memory.prototype.getReadOnlyContext = function() {
|
|
|
|
return new MemoryContext(this.db, true);
|
|
|
|
};
|
|
|
|
Memory.prototype.getReadWriteContext = function() {
|
|
|
|
return new MemoryContext(this.db, false);
|
|
|
|
};
|
|
|
|
|
|
|
|
return Memory;
|
|
|
|
});
|