Skip to content

Commit

Permalink
refactored storage API
Browse files Browse the repository at this point in the history
  • Loading branch information
rksm committed Oct 8, 2013
1 parent 4e92028 commit 5971849
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
11 changes: 6 additions & 5 deletions VersionedFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,20 @@ util._extend(VersionedFileSystem.prototype, d.bindMethods({
}, next);
},
function(fileRecords, next) {
fileRecords.forEach(self.addVersion.bind(self));
next();
self.addVersions(fileRecords, function(err) { next(err); });
},
function(next) { self.emit('initialized'); next(); }
], thenDo);
},

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// versioning
addVersion: function(versionData) {
addVersion: function(versionData, thenDo) {
// options = {change, version, author, date, content, path}
var record = this.storage.store(versionData);
return record;
this.storage.store(versionData, thenDo);
},
addVersions: function(versionDatasets, thenDo) {
this.storage.storeAll(versionDatasets, thenDo);
},

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Expand Down
9 changes: 8 additions & 1 deletion memory-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ util._extend(MemoryStore.prototype, EventEmitter.prototype);

util._extend(MemoryStore.prototype, d.bindMethods({

store: function(versionData) {
store: function(versionData, thenDo) {
var versions = this.versions[versionData.path]
|| (this.versions[versionData.path] = []);
// if no versionId specified we try to auto increment:
Expand All @@ -27,6 +27,13 @@ util._extend(MemoryStore.prototype, d.bindMethods({
date: versionData.date, path: versionData.path, stat: versionData.stat,
};
versions.push(version);
thenDo && thenDo(null, version)
},

storeAll: function(versionDataSets, thenDo) {
versionDataSets.forEach(function(versionData) {
this.store(versionData); }, this);
thenDo(null);
},

getVersionsFor: function(fn, thenDo) {
Expand Down
24 changes: 17 additions & 7 deletions repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,24 @@ util._extend(Repository.prototype, d.bindMethods({
isSynchronized: function() { return this.pendingChangeQueue.length === 0 },

commitPendingChanges: function() {
var change = this.pendingChangeQueue[0];
if (!change || !change.canBeCommitted()) return;
this.pendingChangeQueue.shift();
this.fs.addVersion(change.record);
if (!this.pendingChangeQueue.length) {
console.log("all pending changes process");
this.emit('synchronized');
var repo = this,
q = this.pendingChangeQueue,
toCommit = [];
for (var i = 0; i < q.length; i++) {
if (!q[i].canBeCommitted()) break;
toCommit.push(q[i].record);
}
if (!toCommit.length) return;
repo.pendingChangeQueue.splice(0, toCommit.length);
repo.fs.addVersions(toCommit, function(err, version) {
if (err) {
console.error('error in addVersions for records ', toCommit);
}
if (!repo.pendingChangeQueue.length) {
console.log("all pending changes process");
repo.emit('synchronized');
}
});
},

discardPendingChange: function(change) {
Expand Down

0 comments on commit 5971849

Please sign in to comment.