From ae007e1e0ad6ed96d3a9c5a77272e844da308c97 Mon Sep 17 00:00:00 2001 From: Jack Allnutt Date: Sat, 19 Jul 2014 12:50:27 +0100 Subject: [PATCH] Use promises at startup Refactored the js code in index.html.tmpl and app.js to use promises instead of the custom JobManager object. --- client/assets/libs/promise.min.js | 5 + client/src/app.js | 69 ++++----- client/src/index.html.tmpl | 233 ++++++++++++------------------ 3 files changed, 126 insertions(+), 181 deletions(-) create mode 100644 client/assets/libs/promise.min.js diff --git a/client/assets/libs/promise.min.js b/client/assets/libs/promise.min.js new file mode 100644 index 0000000..a6ed593 --- /dev/null +++ b/client/assets/libs/promise.min.js @@ -0,0 +1,5 @@ +/* + (c) Yehuda Katz, Tom Dale, Stefan Penner and contributors + License: https://github.com/jakearchibald/es6-promise/blob/1.0.0/LICENSE +*/ +!function(){var a,b,c,d;!function(){var e={},f={};a=function(a,b,c){e[a]={deps:b,callback:c}},d=c=b=function(a){function c(b){if("."!==b.charAt(0))return b;for(var c=b.split("/"),d=a.split("/").slice(0,-1),e=0,f=c.length;f>e;e++){var g=c[e];if(".."===g)d.pop();else{if("."===g)continue;d.push(g)}}return d.join("/")}if(d._eak_seen=e,f[a])return f[a];if(f[a]={},!e[a])throw new Error("Could not find module "+a);for(var g,h=e[a],i=h.deps,j=h.callback,k=[],l=0,m=i.length;m>l;l++)"exports"===i[l]?k.push(g={}):k.push(b(c(i[l])));var n=j.apply(this,k);return f[a]=g||n}}(),a("promise/all",["./utils","exports"],function(a,b){"use strict";function c(a){var b=this;if(!d(a))throw new TypeError("You must pass an array to all.");return new b(function(b,c){function d(a){return function(b){f(a,b)}}function f(a,c){h[a]=c,0===--i&&b(h)}var g,h=[],i=a.length;0===i&&b([]);for(var j=0;j']); + scripts.push([ + 'assets/libs/backbone.min.js?t=<%build_time%>', + 'assets/libs/jed.js?t=<%build_time%>' + ]); + scripts.push([ + 'assets/kiwi.min.js?t=<%build_time%>', + 'assets/libs/engine.io.bundle.min.js?t=<%build_time%>' + ]); + script_promise_resolve(); + } - this.onFinish = function(fn) { - completed_callbacks.push(fn); - }; + settings_promise = new Promise(function (resolve) { + $.getJSON(opts.settings_path, function (data) { + opts.server_settings = data.server_settings; + opts.client_plugins = data.client_plugins; + opts.translations = data.translations; + opts.locale = data.locale; + opts.themes = data.themes; + if (typeof data.kiwi_server !== 'undefined') { + opts.kiwi_server = data.kiwi_server; + } - this.registerJob = function(job_name) { - completed_jobs[job_name] = false; - }; - } + resolve(); - (function () { - var base_path = '<%base_path%>', // Entry path for the kiwi application - scripts = [], - opts = { - container: $('body'), - base_path: base_path, - settings_path: base_path + '/assets/settings.json' - }; + // If debugging, grab the debug scripts and load them + if (getQueryVariable('debug')) { + scripts = scripts.concat(data.scripts); + script_promise_resolve(); + } - normalizeConsole(); + // Load themes + if (opts.themes) { + $.each(opts.themes, function (theme_idx, theme) { + var disabled = (opts.server_settings.client.settings.theme.toLowerCase() !== theme.name.toLowerCase()), + rel = (disabled?'alternate ':'') + 'stylesheet'; - /** - * Job bootup manager - * Once all jobs have completed, call any registered completed functions - */ - var jobs = new JobManager(); + var link = $.parseHTML(''); + link.disabled = disabled; + $(link).appendTo($('head')); + }); + } + }); + }); - // Run after all dependancies have been loaded - jobs.onFinish(function startApp() { + Promise.all([onload_promise, settings_promise]).then(function(){return script_promise}).then(function startApp() { // Kiwi IRC version this is built from kiwi.build_version = '<%build_version%>'; @@ -523,88 +552,6 @@ } }); }); - - - // Load each script - jobs.registerJob('load_scripts'); - var cur_script = 0; - function loadNextScript () { - var to_load, - base = base_path + '/'; - - // Start the kiwi app if all scripts have been loaded - if (cur_script === scripts.length) { - jobs.finishJob('load_scripts'); - return; - } - - if (typeof scripts[cur_script] === 'string') { - to_load = base + scripts[cur_script]; - } else { - to_load = []; - for(var idx in scripts[cur_script]) { - to_load.push(base + scripts[cur_script][idx]); - } - } - - $script(to_load, loadNextScript); - - cur_script++; - } - - // If we're not interested in debug libs, start loading production files - if (!getQueryVariable('debug')) { - scripts.push(['assets/libs/lodash.min.js?t=<%build_time%>']); - scripts.push([ - 'assets/libs/backbone.min.js?t=<%build_time%>', - 'assets/libs/jed.js?t=<%build_time%>' - ]); - scripts.push([ - 'assets/kiwi.min.js?t=<%build_time%>', - 'assets/libs/engine.io.bundle.min.js?t=<%build_time%>' - ]); - - loadNextScript(); - } - - // Load application settings - jobs.registerJob('load_settings'); - $.getJSON(opts.settings_path, function (data) { - opts.server_settings = data.server_settings; - opts.client_plugins = data.client_plugins; - opts.translations = data.translations; - opts.locale = data.locale; - opts.themes = data.themes; - - if (typeof data.kiwi_server !== 'undefined') - opts.kiwi_server = data.kiwi_server; - - jobs.finishJob('load_settings'); - - // If debugging, grab the debug scripts and load them - if (getQueryVariable('debug')) { - scripts = scripts.concat(data.scripts); - loadNextScript(); - } - - // Load themes - if (opts.themes) { - $.each(opts.themes, function (theme_idx, theme) { - var disabled = (opts.server_settings.client.settings.theme.toLowerCase() !== theme.name.toLowerCase()), - rel = (disabled?'alternate ':'') + 'stylesheet' /*+ (disabled?' prefetch':'')*/; - - var link = $.parseHTML(''); - link.disabled = disabled; - - $(link).appendTo($('head')); - }); - } - }); - - jobs.registerJob('window_load'); - window.onload = function() { - jobs.finishJob('window_load'); - }; })(); -- 2.25.1