Caret/js/storage/syncfile.js

56 lines
1.6 KiB
JavaScript
Raw Normal View History

define([
"storage/syncFS",
2013-12-23 18:27:33 +00:00
"command",
"util/manos"
], function(sync, command, M) {
/*
SyncFile is a wrapper around Chrome's synchronized storage use the same
interface as the File module. This means you can assign a SyncFile
containing Caret's settings JSON to a tab just as you would a regular file,
and the tab can read/write from it without any customization. The one
difference is the virtual flag, which is used to prevent some file-only
behaviors (such as retainEntry calls).
As soon as chrome.syncFileSystem allows us to create a filesystem even if
the device is offline for the first request, we'll swap these over to a
wrapper around file entries from that storage. The change should remain
entirely transparent as far as other modules are concerned, although there
are some abstraction leaks in settings.js that we'll need to handle.
*/
var SyncFile = function(name, c) {
this.entry = {};
if (name) {
this.open(name, c);
}
this.virtual = true;
};
SyncFile.prototype = {
name: "",
open: function(name, c) {
this.name = name;
this.entry.name = name;
if (c) c(null, this);
},
read: function(c) {
var name = this.name;
sync.get(this.name).then(function(data) {
c(null, data);
});
},
write: function(content, c) {
2013-12-23 18:27:33 +00:00
return sync.set(this.name, content).then(function() {
command.fire("settings:change-local");
c();
});
},
2013-12-23 18:27:33 +00:00
retain: function() { return false; },
restore: function(c) { c("Cannot restore sync storage files") }
};
return SyncFile;
});