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([
"command",
"settings!user",

View file

@ -8,9 +8,6 @@ define([
var local = {};
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 cloneArray = function(a) {
var n = [];
@ -94,7 +91,7 @@ define([
}
var merge = function() {
sync.get(name, function(data) {
sync.get(name).then(function(data) {
if (data) {
local[name] = data;
} else {

View file

@ -25,49 +25,66 @@ define(function() {
};
return {
get: function(key, callback) {
get: function(key) {
if (cache) {
var result = decode(key);
return callback(result);
return Promise.resolve(result);
}
requests.push(callback);
if (pending) return;
chrome.storage.sync.get(function(all) {
cache = all;
var result = decode(key);
return callback(result);
var only = function(data) {
return decode(key);
}
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) {
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) {
remove: function(key) {
var seed = cache[key];
if (typeof seed == "number") {
for (var i = 0; i < seed; i++) {
chrome.storage.sync.remove(key + i);
return new Promise(function(ok) {
if (typeof seed == "number") {
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);
}
}
chrome.storage.sync.remove(key, callback);
cache = null;
cache = null;
chrome.storage.sync.remove(key, ok);
});
}
}

View file

@ -1,7 +1,8 @@
define([
"storage/syncFS",
"command"
], function(sync, command) {
"command",
"util/manos"
], function(sync, command, M) {
/*
@ -32,24 +33,25 @@ define([
open: function(name, c) {
this.name = name;
this.entry.name = name;
if (c) {
c(this);
}
var self = this;
var promise = new Promise(function(ok, fail) {
resolve(self);
});
if (c) M.pton(promise, c);
return promise;
},
read: function(c) {
var name = this.name;
sync.get(this.name, function(data) {
c(null, data[name]);
});
return sync.get(this.name);
},
write: function(content, c) {
var self = this;
sync.set(this.name, content, function() {
return sync.set(this.name, content).then(function() {
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;