Merge pull request #197 from humphd/issue196

Fixes #196
This commit is contained in:
Alan K 2014-05-22 15:21:18 -04:00
commit 73392d3f35
2 changed files with 53 additions and 3 deletions

View File

@ -1,8 +1,24 @@
define(function(require) { define(function(require) {
var FILE_SYSTEM_NAME = require('src/constants').FILE_SYSTEM_NAME; var FILE_SYSTEM_NAME = require('src/constants').FILE_SYSTEM_NAME;
var asyncCallback = require('async').nextTick; var asyncCallback = require('async').nextTick;
/**
* Make shared in-memory DBs possible when using the same name.
*/
var createDB = (function() {
var pool = {};
return function getOrCreate(name) {
var firstAccess = !pool.hasOwnProperty(name);
if(firstAccess) {
pool[name] = {};
}
return {
firstAccess: firstAccess,
db: pool[name]
};
};
}());
function MemoryContext(db, readOnly) { function MemoryContext(db, readOnly) {
this.readOnly = readOnly; this.readOnly = readOnly;
this.objectStore = db; this.objectStore = db;
@ -50,15 +66,16 @@ define(function(require) {
function Memory(name) { function Memory(name) {
this.name = name || FILE_SYSTEM_NAME; this.name = name || FILE_SYSTEM_NAME;
this.db = {};
} }
Memory.isSupported = function() { Memory.isSupported = function() {
return true; return true;
}; };
Memory.prototype.open = function(callback) { Memory.prototype.open = function(callback) {
var result = createDB(this.name);
this.db = result.db;
asyncCallback(function() { asyncCallback(function() {
callback(null, true); callback(null, result.firstAccess);
}); });
}; };
Memory.prototype.getReadOnlyContext = function() { Memory.prototype.getReadOnlyContext = function() {

View File

@ -12,6 +12,39 @@ define(["Filer"], function(Filer) {
expect(memoryProvider.getReadWriteContext).to.be.a('function'); expect(memoryProvider.getReadWriteContext).to.be.a('function');
}); });
describe("Memory provider DBs are sharable", function() {
it("should share a single memory db when name is the same", function(done) {
var provider1;
var provider2;
var provider3;
var name1 = 'memory-db';
var name2 = 'memory-db2';
provider1 = new Filer.FileSystem.providers.Memory(name1);
provider1.open(function(error, firstAccess) {
expect(error).not.to.exist;
expect(firstAccess).to.be.true;
provider2 = new Filer.FileSystem.providers.Memory(name1);
provider2.open(function(error, firstAccess) {
expect(error).not.to.exist;
expect(firstAccess).to.be.false;
expect(provider1.db).to.equal(provider2.db);
provider3 = new Filer.FileSystem.providers.Memory(name2);
provider3.open(function(error, firstAccess) {
expect(error).not.to.exist;
expect(firstAccess).to.be.true;
expect(provider3.db).not.to.equal(provider2.db);
done();
});
});
});
});
});
});
describe("open an Memory provider", function() { describe("open an Memory provider", function() {
it("should open a new Memory database", function(done) { it("should open a new Memory database", function(done) {
var provider = new Filer.FileSystem.providers.Memory(); var provider = new Filer.FileSystem.providers.Memory();