Use promises at startup
[KiwiIRC.git] / client / src / index.html.tmpl
index 1d796fc7c5b9b2836b1f31eb3dd8405b6bfbf5ab..d6694f36fdf4f3e2dd975cf759d3adcb916663fd 100644 (file)
                 console[method] = noop;\r
             }\r
         }\r
-    };\r
+    }\r
+\r
+    normalizeConsole();\r
 \r
     function getQueryVariable(variable) {\r
         var query = window.location.search.substring(1);\r
         }\r
     }\r
 \r
-    /**\r
-     * Get alerted when a group of jobs have been completed.\r
-     * Eg. .registerJob('job1'); .registerJob('job2');\r
-     *     .onFinish(function(){ alert('Jobs finished!'); });\r
-     *     .finishJob('job1');\r
-     *     .finishJob('job2');\r
-     */\r
-    function JobManager() {\r
-        var completed_jobs = {  };\r
-\r
-        // Functions to call once all jobs have completed\r
-        var completed_callbacks = [];\r
-\r
+    (function afterPromiseAvailable() {\r
+        var base_path = '<%base_path%>', // Entry path for the kiwi application\r
+            scripts = [],\r
+            opts = {\r
+                container: $('body'),\r
+                base_path: base_path,\r
+                settings_path: base_path + '/assets/settings.json'\r
+            },\r
+            script_promise, script_promise_resolve,\r
+            onload_promise,\r
+            settings_promise;\r
+\r
+        // If the browser doesn't natively support promises load up the polyfill and try again.\r
+        if (!window.Promise) {\r
+            $script(base_path + "/assets/libs/promise.min.js", afterPromiseAvailable);\r
+            return;\r
+        }\r
 \r
-        function callCompletedFunctions(fn) {\r
-            $.each(completed_callbacks, function(idx, fn) {\r
-                fn();\r
+        function loadScripts(scripts) {\r
+            return new Promise(function (resolve, reject) {\r
+                var to_load, idx,\r
+                    base = base_path + '/';\r
+                if (typeof scripts === 'string') {\r
+                    to_load = base + scripts;\r
+                } else {\r
+                    to_load = [];\r
+                    for (idx in scripts) {\r
+                        to_load.push(base + scripts[idx]);\r
+                    }\r
+                }\r
+                $script(to_load, resolve, reject);\r
             });\r
+        }\r
 \r
-            completed_callbacks = [];\r
-        };\r
-\r
+        onload_promise = new Promise(function (resolve) {\r
+            window.onload = resolve;\r
+        });\r
 \r
-        this.finishJob = function(job_name) {\r
-            if (typeof completed_jobs[job_name] === 'undefined') {\r
-                return;\r
+        script_promise = new Promise(function (resolve) {\r
+            script_promise_resolve = resolve;\r
+        });\r
+        script_promise = script_promise.then(function () {\r
+            var idx;\r
+            for (idx = 0; idx < scripts.length; idx++) {\r
+                (function (idx) {\r
+                    script_promise = script_promise.then(function () {\r
+                        return loadScripts(scripts[idx]);\r
+                    });\r
+                })(idx);\r
             }\r
+        });\r
 \r
-            completed_jobs[job_name] = true;\r
-\r
-            // Check if all our jobs have completed\r
-            var all_jobs_completed = true;\r
-            $.each(completed_jobs, function(idx, completed) {\r
-                if (!completed) {\r
-                    all_jobs_completed = false;\r
-                    return false;\r
-                }\r
-            });\r
-\r
-            if (all_jobs_completed) {\r
-                callCompletedFunctions();\r
-            }\r
-        };\r
+        if (!getQueryVariable('debug')) {\r
+            scripts.push(['assets/libs/lodash.min.js?t=<%build_time%>']);\r
+            scripts.push([\r
+                'assets/libs/backbone.min.js?t=<%build_time%>',\r
+                'assets/libs/jed.js?t=<%build_time%>'\r
+            ]);\r
+            scripts.push([\r
+                'assets/kiwi.min.js?t=<%build_time%>',\r
+                'assets/libs/engine.io.bundle.min.js?t=<%build_time%>'\r
+            ]);\r
 \r
+            script_promise_resolve();\r
+        }\r
 \r
-        this.onFinish = function(fn) {\r
-            completed_callbacks.push(fn);\r
-        };\r
+        settings_promise = new Promise(function (resolve) {\r
+            $.getJSON(opts.settings_path, function (data) {\r
+                opts.server_settings = data.server_settings;\r
+                opts.client_plugins = data.client_plugins;\r
+                opts.translations = data.translations;\r
+                opts.locale = data.locale;\r
+                opts.themes = data.themes;\r
 \r
+                if (typeof data.kiwi_server !== 'undefined') {\r
+                    opts.kiwi_server = data.kiwi_server;\r
+                }\r
 \r
-        this.registerJob = function(job_name) {\r
-            completed_jobs[job_name] = false;\r
-        };\r
-    }\r
+                resolve();\r
 \r
-    (function () {\r
-        var base_path = '<%base_path%>', // Entry path for the kiwi application\r
-            scripts = [],\r
-            opts = {\r
-                container: $('body'),\r
-                base_path: base_path,\r
-                settings_path: base_path + '/assets/settings.json'\r
-            };\r
+                // If debugging, grab the debug scripts and load them\r
+                if (getQueryVariable('debug')) {\r
+                    scripts = scripts.concat(data.scripts);\r
+                    script_promise_resolve();\r
+                }\r
 \r
-        normalizeConsole();\r
+                // Load themes\r
+                if (opts.themes) {\r
+                    $.each(opts.themes, function (theme_idx, theme) {\r
+                        var disabled = (opts.server_settings.client.settings.theme.toLowerCase() !== theme.name.toLowerCase()),\r
+                            rel = (disabled?'alternate ':'') + 'stylesheet';\r
 \r
-        /**\r
-         * Job bootup manager\r
-         * Once all jobs have completed, call any registered completed functions\r
-         */\r
-        var jobs = new JobManager();\r
+                        var link = $.parseHTML('<link rel="' + rel + '" type="text/css" data-theme href="'+ opts.base_path + '/assets/themes/' + theme.name.toLowerCase() + '/style.css" title="' + theme.name.toLowerCase() + '" ' + (disabled?'disabled':'') + '/>');\r
+                        link.disabled = disabled;\r
 \r
+                        $(link).appendTo($('head'));\r
+                    });\r
+                }\r
+            });\r
+        });\r
 \r
-        // Run after all dependancies have been loaded\r
-        jobs.onFinish(function startApp() {\r
+        Promise.all([onload_promise, settings_promise]).then(function(){return script_promise}).then(function startApp() {\r
             // Kiwi IRC version this is built from\r
             kiwi.build_version = '<%build_version%>';\r
 \r
                 }\r
             });\r
         });\r
-\r
-\r
-        // Load each script\r
-        jobs.registerJob('load_scripts');\r
-        var cur_script = 0;\r
-        function loadNextScript () {\r
-            var to_load,\r
-                base = base_path + '/';\r
-\r
-            // Start the kiwi app if all scripts have been loaded\r
-            if (cur_script === scripts.length) {\r
-                jobs.finishJob('load_scripts');\r
-                return;\r
-            }\r
-\r
-            if (typeof scripts[cur_script] === 'string') {\r
-                to_load = base + scripts[cur_script];\r
-            } else {\r
-                to_load = [];\r
-                for(var idx in scripts[cur_script]) {\r
-                    to_load.push(base + scripts[cur_script][idx]);\r
-                }\r
-            }\r
-\r
-            $script(to_load, loadNextScript);\r
-\r
-            cur_script++;\r
-        }\r
-\r
-        // If we're not interested in debug libs, start loading production files\r
-        if (!getQueryVariable('debug')) {\r
-            scripts.push(['assets/libs/lodash.min.js?t=<%build_time%>']);\r
-            scripts.push([\r
-                'assets/libs/backbone.min.js?t=<%build_time%>',\r
-                'assets/libs/jed.js?t=<%build_time%>'\r
-            ]);\r
-            scripts.push([\r
-                'assets/kiwi.min.js?t=<%build_time%>',\r
-                'assets/libs/engine.io.bundle.min.js?t=<%build_time%>'\r
-            ]);\r
-\r
-            loadNextScript();\r
-        }\r
-\r
-        // Load application settings\r
-        jobs.registerJob('load_settings');\r
-        $.getJSON(opts.settings_path, function (data) {\r
-            opts.server_settings = data.server_settings;\r
-            opts.client_plugins = data.client_plugins;\r
-            opts.translations = data.translations;\r
-            opts.locale = data.locale;\r
-            opts.themes = data.themes;\r
-\r
-            if (typeof data.kiwi_server !== 'undefined')\r
-                opts.kiwi_server = data.kiwi_server;\r
-\r
-            jobs.finishJob('load_settings');\r
-\r
-            // If debugging, grab the debug scripts and load them\r
-            if (getQueryVariable('debug')) {\r
-                scripts = scripts.concat(data.scripts);\r
-                loadNextScript();\r
-            }\r
-\r
-            // Load themes\r
-            if (opts.themes) {\r
-                $.each(opts.themes, function (theme_idx, theme) {\r
-                    var disabled = (opts.server_settings.client.settings.theme.toLowerCase() !== theme.name.toLowerCase()),\r
-                        rel = (disabled?'alternate ':'') + 'stylesheet' /*+ (disabled?' prefetch':'')*/;\r
-\r
-                    var link = $.parseHTML('<link rel="' + rel + '" type="text/css" data-theme href="'+ opts.base_path + '/assets/themes/' + theme.name.toLowerCase() + '/style.css" title="' + theme.name.toLowerCase() + '" ' + (disabled?'disabled':'') + '/>');\r
-                    link.disabled = disabled;\r
-\r
-                    $(link).appendTo($('head'));\r
-                });\r
-            }\r
-        });\r
-\r
-        jobs.registerJob('window_load');\r
-        window.onload = function() {\r
-            jobs.finishJob('window_load');\r
-        };\r
     })();\r
 </script>\r
 </body>\r