From 57d2b986b54f6ead069b85bda68a76fea26ef589 Mon Sep 17 00:00:00 2001 From: Thomas Wilburn Date: Mon, 20 Jan 2014 22:10:06 -0800 Subject: [PATCH] Add Settings.pull() for modules that want to request configs asynchronously instead of blocking load --- config/commands.json | 1 + js/fileManager.js | 92 ++++++++++++++++++---------------- js/sessions.js | 37 +++++++------- js/settings.js | 2 + js/storage/settingsProvider.js | 23 ++++++++- js/tab.js | 21 +++++--- js/ui/projectManager.js | 2 +- 7 files changed, 103 insertions(+), 75 deletions(-) diff --git a/config/commands.json b/config/commands.json index 246fb80..5414745 100644 --- a/config/commands.json +++ b/config/commands.json @@ -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" }, diff --git a/js/fileManager.js b/js/fileManager.js index 2df15b9..8ad75a4 100644 --- a/js/fileManager.js +++ b/js/fileManager.js @@ -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); diff --git a/js/sessions.js b/js/sessions.js index c4486da..96313a8 100644 --- a/js/sessions.js +++ b/js/sessions.js @@ -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; diff --git a/js/settings.js b/js/settings.js index c187b91..b8155f2 100644 --- a/js/settings.js +++ b/js/settings.js @@ -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) { diff --git a/js/storage/settingsProvider.js b/js/storage/settingsProvider.js index 1f929fd..c74cdf8 100644 --- a/js/storage/settingsProvider.js +++ b/js/storage/settingsProvider.js @@ -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(); } }; diff --git a/js/tab.js b/js/tab.js index ce8e0c5..bee3f59 100644 --- a/js/tab.js +++ b/js/tab.js @@ -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) { diff --git a/js/ui/projectManager.js b/js/ui/projectManager.js index c085a1a..48dd111 100644 --- a/js/ui/projectManager.js +++ b/js/ui/projectManager.js @@ -1,5 +1,5 @@ define([ - "settings!", + "storage/settingsProvider", "command", "sessions", "storage/file",