Add Settings.pull() for modules that want to request configs asynchronously instead of blocking load

This commit is contained in:
Thomas Wilburn 2014-01-20 22:10:06 -08:00
parent 57fec531c8
commit 57d2b986b5
7 changed files with 103 additions and 75 deletions

View file

@ -10,6 +10,7 @@
{ "command": "session:save-file-as", "label": "Save File As..." },
{ "command": "session:revert-file", "label": "Revert File" },
{ "command": "session:close-tab", "label": "Close File" },
{ "command": "editor:print", "label": "Print File" },
{ "command": "session:close-to-right", "label": "Close Tabs to the Right"},
{ "label": "Undo", "command": "ace:command", "argument": "undo" },

View file

@ -3,7 +3,7 @@ define([
"storage/file",
"ui/dialog",
"command",
"settings!user",
"storage/settingsProvider",
"util/manos",
"ui/projectManager"
], function(sessions, File, dialog, command, Settings, M, projectManager) {
@ -79,7 +79,7 @@ define([
command.on("session:save-file-as", function(c) {
var tab = sessions.getCurrent();
tab.save(true).then(function() {
var mode = tab.detectSyntax(Settings.get("user"));
var mode = tab.detectSyntax();
sessions.renderTabs();
command.fire("session:syntax", mode);
if (c) c();
@ -140,61 +140,65 @@ define([
var init = function(complete) {
openFromLaunchData();
if (Settings.get("user").disableTabRestore) return "fileManager";
chrome.storage.local.get("retained", function(data) {
var failures = [];
if (data.retained && data.retained.length) {
//try to restore items in order
M.map(
data.retained,
function(id, i, c) {
var file = new File();
file
.restore(id)
.then(file.read.bind(file))
.then(function(data) {
c({
value: data,
file: file
})
}, function(err) {
failures.push(id);
c(null);
Settings.pull("user").then(function(data) {
if (data.user.disableTabRestore) complete("fileManager");
chrome.storage.local.get("retained", function(data) {
var failures = [];
if (data.retained && data.retained.length) {
//try to restore items in order
M.map(
data.retained,
function(id, i, c) {
var file = new File();
file
.restore(id)
.then(file.read.bind(file))
.then(function(data) {
c({
value: data,
file: file
})
}, function(err) {
failures.push(id);
c(null);
});
},
function(restored) {
restored = restored.filter(function(d) { return d });
for (var i = 0; i < restored.length; i++) {
var tab = restored[i];
sessions.addFile(tab.value, tab.file);
}
complete("fileManager");
if (!failures.length) return;
chrome.storage.local.get("retained", function(data) {
if (!data.retained) return;
chrome.storage.local.set({
retained: data.retained.filter(function(d) { return failures.indexOf(d) == -1 })
});
});
},
function(restored) {
restored = restored.filter(function(d) { return d });
for (var i = 0; i < restored.length; i++) {
var tab = restored[i];
sessions.addFile(tab.value, tab.file);
}
if (complete) complete("fileManager");
if (!failures.length) return;
chrome.storage.local.get("retained", function(data) {
if (!data.retained) return;
chrome.storage.local.set({
retained: data.retained.filter(function(d) { return failures.indexOf(d) == -1 })
});
});
}
);
}
);
}
});
});
};
var reset = function() {
var tabs = sessions.getAllTabs();
var virtuals = [];
tabs.forEach(function(tab) {
if (tab.file && tab.file.virtual) {
var setting = tab.fileName.replace(".json", "");
Settings.load(setting, function() {
var value = Settings.getAsString(setting);
tab.setValue(value);
var v = tab.file.read().then(function(data) {
tab.setValue(data);
tab.modified = false;
sessions.renderTabs();
});
virtuals.push(v);
}
});
Promise.all(virtuals).then(function() {
setTimeout(sessions.renderTabs, 10);
});
};
command.on("init:startup", init);

View file

@ -4,7 +4,7 @@ define([
"ui/contextMenus",
"command",
"tab",
"settings!ace,user",
"storage/settingsProvider",
"ui/statusbar",
"util/manos",
"aceBindings"
@ -20,8 +20,6 @@ define([
*/
var tabs = [];
var cfg = Settings.get("ace");
var userConfig = Settings.get("user");
var syntax = document.find(".syntax");
var stack = [];
var stackOffset = 0;
@ -79,7 +77,7 @@ define([
status.toast(loaded, 2);
});
}
tab.detectSyntax(userConfig);
tab.detectSyntax();
raiseTab(tab);
return tab;
};
@ -310,12 +308,14 @@ define([
});
var init = function() {
cfg.modes.forEach(function(mode) {
var option = document.createElement("option");
option.innerHTML = mode.label;
option.value = mode.name;
syntax.append(option);
});
Settings.pull("ace").then(function(data) {
data.ace.modes.forEach(function(mode) {
var option = document.createElement("option");
option.innerHTML = mode.label;
option.value = mode.name;
syntax.append(option);
});
})
if (!tabs.length) addTab("");
renderTabs();
enableTabDragDrop();
@ -325,10 +325,9 @@ define([
};
var reset = function() {
userConfig = Settings.get("user");
tabs.forEach(function(tab) {
tab.detectSyntax(userConfig);
})
tab.detectSyntax();
});
};
command.on("init:startup", init);
@ -348,13 +347,11 @@ define([
return {
addFile: addTab,
addDefaultsFile: function(name) {
Settings.load(name, function() {
var tab = addTab(Settings.getAsString(name, true));
tab.syntaxMode = "javascript";
tab.detectSyntax(userConfig);
tab.fileName = name + ".json";
renderTabs();
});
var tab = addTab(Settings.getAsString(data[name], true));
tab.syntaxMode = "javascript";
tab.detectSyntax();
tab.fileName = name + ".json";
renderTabs();
},
getAllTabs: function() {
return tabs;

View file

@ -2,6 +2,8 @@ define([
"command",
"storage/settingsProvider",
], function(command, Settings) {
/* A plugin that handles loading Settings "synchronously" */
return {
load: function(name, parentRequire, onLoad, config) {

View file

@ -1,8 +1,9 @@
define([
"storage/syncFS",
"storage/syncfile",
"command"
], function(sync, syncFile, command) {
"command",
"util/manos"
], function(sync, SyncFile, command, M) {
var defaults = {};
var local = {};
@ -122,6 +123,24 @@ define([
clearProject: function() {
project = {};
command.fire("settings:change-local");
},
//load/get all requested settings via a promise
pull: function() {
var deferred = M.deferred();
var names = [].slice.call(arguments);
var pending = names.map(function(name) {
return new Promise(function(ok) {
Settings.load(name, ok);
});
});
Promise.all(pending).then(function() {
var collected = {};
names.forEach(function(name) {
collected[name] = Settings.get(name);
});
deferred.done(collected);
});
return deferred.promise();
}
};

View file

@ -119,14 +119,17 @@ define([
}
Tab.prototype.detectSyntax = function(userConfig) {
//this won't ever change, safe to get each time
var aceConfig = Settings.get("ace");
this.setUseSoftTabs(!userConfig.useTabs);
this.setTabSize(userConfig.indentation || 2);
this.setUseWrapMode(userConfig.wordWrap);
this.setWrapLimit(userConfig.wrapLimit || null);
this.setNewLineMode(userConfig.lineEnding || "auto");
this.setUseWorker(userConfig.useWorker);
//settings are async
Settings.pull("user").then(function(data) {
var userConfig = data.user;
this.setUseSoftTabs(!userConfig.useTabs);
this.setTabSize(userConfig.indentation || 2);
this.setUseWrapMode(userConfig.wordWrap);
this.setWrapLimit(userConfig.wrapLimit || null);
this.setNewLineMode(userConfig.lineEnding || "auto");
this.setUseWorker(userConfig.useWorker);
});
//syntax, however, is sync
var syntaxValue = "plain_text";
if (this.syntaxMode) {
syntaxValue = this.syntaxMode;
@ -138,6 +141,8 @@ define([
} else if (this.file.entry) {
var found = false;
var extension = this.file.entry.name.split(".").pop();
//this won't ever change, safe to get each time
var aceConfig = Settings.get("ace");
for (var i = 0; i < aceConfig.modes.length; i++) {
var mode = aceConfig.modes[i];
if (mode.extensions.indexOf(extension) > -1) {

View file

@ -1,5 +1,5 @@
define([
"settings!",
"storage/settingsProvider",
"command",
"sessions",
"storage/file",