diff --git a/js/main.js b/js/main.js index 39772e5..f27c17a 100644 --- a/js/main.js +++ b/js/main.js @@ -1,3 +1,5 @@ +chrome.version = window.navigator.appVersion.match(/Chrome\/(\d+)/)[1] * 1 || 0; + require([ "command", "settings!user", diff --git a/js/settings.js b/js/settings.js index d717e3a..8479996 100644 --- a/js/settings.js +++ b/js/settings.js @@ -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 { diff --git a/js/storage/syncFS.js b/js/storage/syncFS.js index 8f904f0..b80e12e 100644 --- a/js/storage/syncFS.js +++ b/js/storage/syncFS.js @@ -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); + }); } } diff --git a/js/storage/syncfile.js b/js/storage/syncfile.js index cf537bf..e288dce 100644 --- a/js/storage/syncfile.js +++ b/js/storage/syncfile.js @@ -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;