Resolving text theme loading race condition
authorDarren <darren@darrenwhitlen.com>
Mon, 14 Apr 2014 09:13:46 +0000 (10:13 +0100)
committerDarren <darren@darrenwhitlen.com>
Mon, 14 Apr 2014 09:13:46 +0000 (10:13 +0100)
client/src/app.js
client/src/index.html.tmpl

index 68993a5e65ff0080e9c47e6abb29684c7cc99d44..fdc2f59da5b5cf0d304ced47968ce1c9c4a0330b 100644 (file)
@@ -110,16 +110,19 @@ _kiwi.global = {
 \r
     // Entry point to start the kiwi application\r
     init: function (opts, callback) {\r
-        var continueStart, locale, igniteTextTheme, text_theme;\r
+        var continueStart, locale, jobs, igniteTextTheme, text_theme;\r
         opts = opts || {};\r
 \r
+        jobs = new JobManager();\r
+        jobs.onFinish(callback);\r
+\r
         continueInit = function (locale, s, xhr) {\r
             if (locale) {\r
                 _kiwi.global.i18n = new Jed(locale);\r
             } else {\r
                 _kiwi.global.i18n = new Jed();\r
             }\r
-            \r
+\r
             _kiwi.app = new _kiwi.model.Application(opts);\r
 \r
             // Start the client up\r
@@ -128,14 +131,14 @@ _kiwi.global = {
             // Now everything has started up, load the plugin manager for third party plugins\r
             _kiwi.global.plugins = new _kiwi.model.PluginManager();\r
 \r
-            callback && callback();\r
+            jobs.finishJob('load_locale');\r
         };\r
-        \r
+\r
         igniteTextTheme = function(text_theme, s, xhr) {\r
             _kiwi.global.text_theme = new _kiwi.view.TextTheme(text_theme);\r
-            \r
-            callback && callback();\r
-        }\r
+\r
+            jobs.finishJob('load_text_theme');\r
+        };\r
 \r
         // Set up the settings datastore\r
         _kiwi.global.settings = _kiwi.model.DataStore.instance('kiwi.settings');\r
@@ -144,6 +147,7 @@ _kiwi.global = {
         // Set the window title\r
         window.document.title = opts.server_settings.client.window_title || 'Kiwi IRC';\r
 \r
+        jobs.registerJob('load_locale');\r
         locale = _kiwi.global.settings.get('locale');\r
         if (!locale) {\r
             $.getJSON(opts.base_path + '/assets/locales/magic.json', continueInit);\r
@@ -151,6 +155,7 @@ _kiwi.global = {
             $.getJSON(opts.base_path + '/assets/locales/' + locale + '.json', continueInit);\r
         }\r
 \r
+        jobs.registerJob('load_text_theme');\r
         text_theme = opts.text_theme;\r
         if (!text_theme) {\r
             $.getJSON(opts.base_path + '/assets/text_themes/default.json', igniteTextTheme);\r
index 97bbdc00b48ad96bb9730df46589a60ff5297f37..f244e999b6c28961f7353c155c43a7ddb6cc3294 100644 (file)
         }\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
+\r
+        function callCompletedFunctions(fn) {\r
+            $.each(completed_callbacks, function(idx, fn) {\r
+                fn();\r
+            });\r
+\r
+            completed_callbacks = [];\r
+        };\r
+\r
+\r
+        this.finishJob = function(job_name) {\r
+            if (typeof completed_jobs[job_name] === 'undefined') {\r
+                return;\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
+\r
+\r
+        this.onFinish = function(fn) {\r
+            completed_callbacks.push(fn);\r
+        };\r
+\r
+\r
+        this.registerJob = function(job_name) {\r
+            completed_jobs[job_name] = false;\r
+        };\r
+    }\r
+\r
     (function () {\r
         var base_path = '<%base_path%>', // Entry path for the kiwi application\r
             scripts = [],\r
          * Job bootup manager\r
          * Once all jobs have completed, call any registered completed functions\r
          */\r
-        var jobs = new (function BootManager() {\r
-            var completed_jobs = {  };\r
-\r
-            // Functions to call once all jobs have completed\r
-            var completed_callbacks = [];\r
-\r
-\r
-            function callCompletedFunctions(fn) {\r
-                $.each(completed_callbacks, function(idx, fn) {\r
-                    fn();\r
-                });\r
-\r
-                completed_callbacks = [];\r
-            };\r
-\r
-\r
-            this.finishJob = function(job_name) {\r
-                if (typeof completed_jobs[job_name] === 'undefined') {\r
-                    return;\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
-\r
-\r
-            this.onFinish = function(fn) {\r
-                completed_callbacks.push(fn);\r
-            };\r
-\r
-\r
-            this.registerJob = function(job_name) {\r
-                completed_jobs[job_name] = false;\r
-            };\r
-        })();\r
+        var jobs = new JobManager();\r
 \r
 \r
         // Run after all dependancies have been loaded\r