Finish converting syncFS to Promises.

This commit is contained in:
Thomas Wilburn 2013-12-23 10:27:33 -08:00
parent 576cfdaa16
commit 529b3d2951
4 changed files with 68 additions and 50 deletions

View file

@ -1,3 +1,5 @@
chrome.version = window.navigator.appVersion.match(/Chrome\/(\d+)/)[1] * 1 || 0;
require([ require([
"command", "command",
"settings!user", "settings!user",

View file

@ -8,9 +8,6 @@ define([
var local = {}; var local = {};
var project = {}; var project = {};
//put this here because Settings is pretty early in load process
chrome.version = window.navigator.appVersion.match(/Chrome\/(\d+)/)[1] * 1 || 0;
var clone = function(item) { var clone = function(item) {
var cloneArray = function(a) { var cloneArray = function(a) {
var n = []; var n = [];
@ -94,7 +91,7 @@ define([
} }
var merge = function() { var merge = function() {
sync.get(name, function(data) { sync.get(name).then(function(data) {
if (data) { if (data) {
local[name] = data; local[name] = data;
} else { } else {

View file

@ -25,49 +25,66 @@ define(function() {
}; };
return { return {
get: function(key, callback) { get: function(key) {
if (cache) { if (cache) {
var result = decode(key); var result = decode(key);
return callback(result); return Promise.resolve(result);
} }
requests.push(callback);
if (pending) return;
chrome.storage.sync.get(function(all) { var only = function(data) {
cache = all; return decode(key);
var result = decode(key); }
return callback(result);
var fetch = function() {
return new Promise(function(ok) {
chrome.storage.sync.get(function(all) {
cache = all;
ok(all);
pending = null;
});
});
};
if (!pending) {
pending = fetch();
}
return pending.then(only);
},
set: function(key, data) {
cache = null;
return new Promise(function(ok, fail) {
if (data.length < 3000) {
var hash = {};
hash[key] = data;
chrome.storage.sync.set(hash, ok);
} else {
var chunks = [];
for (var i = 0; i < data.length; i += 3000) {
chunks.push(data.substr(i, i + 3000));
}
var hash = {};
hash[key] = chunks.length;
chunks.map(function(chunk, i) {
hash[key + i] = chunk;
});
chrome.storage.sync.set(hash, ok)
}
}); });
}, },
set: function(key, data, callback) { remove: function(key) {
cache = null;
data = data;
if (data.length < 3000) {
var hash = {};
hash[key] = data;
chrome.storage.sync.set(hash, callback);
} else {
var chunks = [];
for (var i = 0; i < data.length; i += 3000) {
chunks.push(data.substr(i, i + 3000));
}
var hash = {};
hash[key] = chunks.length;
chunks.map(function(chunk, i) {
hash[key + i] = chunk;
});
chrome.storage.sync.set(hash, callback)
}
},
remove: function(key, callback) {
var seed = cache[key]; var seed = cache[key];
if (typeof seed == "number") { return new Promise(function(ok) {
for (var i = 0; i < seed; i++) { if (typeof seed == "number") {
chrome.storage.sync.remove(key + i); var waiting = [];
var p = Promise.all(new Array(seed).map(function(i) {
new Promise(function(ok) {
chrome.storage.sync.remove(key + i, ok);
});
}));
return p.then(ok);
} }
} cache = null;
chrome.storage.sync.remove(key, callback); chrome.storage.sync.remove(key, ok);
cache = null; });
} }
} }

View file

@ -1,7 +1,8 @@
define([ define([
"storage/syncFS", "storage/syncFS",
"command" "command",
], function(sync, command) { "util/manos"
], function(sync, command, M) {
/* /*
@ -32,24 +33,25 @@ define([
open: function(name, c) { open: function(name, c) {
this.name = name; this.name = name;
this.entry.name = name; this.entry.name = name;
if (c) { var self = this;
c(this); var promise = new Promise(function(ok, fail) {
} resolve(self);
});
if (c) M.pton(promise, c);
return promise;
}, },
read: function(c) { read: function(c) {
var name = this.name; var name = this.name;
sync.get(this.name, function(data) { return sync.get(this.name);
c(null, data[name]);
});
}, },
write: function(content, c) { write: function(content, c) {
var self = this; var self = this;
sync.set(this.name, content, function() { return sync.set(this.name, content).then(function() {
command.fire("settings:change-local"); command.fire("settings:change-local");
if (c) c(null, self);
}); });
}, },
retain: function() { return false; } retain: function() { return false; },
restore: function() { return new Promise.reject() }
}; };
return SyncFile; return SyncFile;