From: Darren Date: Mon, 14 Apr 2014 09:13:46 +0000 (+0100) Subject: Resolving text theme loading race condition X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=28990b2a0360bd7bd5210acd431ea00a6c135150;p=KiwiIRC.git Resolving text theme loading race condition --- diff --git a/client/src/app.js b/client/src/app.js index 68993a5..fdc2f59 100644 --- a/client/src/app.js +++ b/client/src/app.js @@ -110,16 +110,19 @@ _kiwi.global = { // Entry point to start the kiwi application init: function (opts, callback) { - var continueStart, locale, igniteTextTheme, text_theme; + var continueStart, locale, jobs, igniteTextTheme, text_theme; opts = opts || {}; + jobs = new JobManager(); + jobs.onFinish(callback); + continueInit = function (locale, s, xhr) { if (locale) { _kiwi.global.i18n = new Jed(locale); } else { _kiwi.global.i18n = new Jed(); } - + _kiwi.app = new _kiwi.model.Application(opts); // Start the client up @@ -128,14 +131,14 @@ _kiwi.global = { // Now everything has started up, load the plugin manager for third party plugins _kiwi.global.plugins = new _kiwi.model.PluginManager(); - callback && callback(); + jobs.finishJob('load_locale'); }; - + igniteTextTheme = function(text_theme, s, xhr) { _kiwi.global.text_theme = new _kiwi.view.TextTheme(text_theme); - - callback && callback(); - } + + jobs.finishJob('load_text_theme'); + }; // Set up the settings datastore _kiwi.global.settings = _kiwi.model.DataStore.instance('kiwi.settings'); @@ -144,6 +147,7 @@ _kiwi.global = { // Set the window title window.document.title = opts.server_settings.client.window_title || 'Kiwi IRC'; + jobs.registerJob('load_locale'); locale = _kiwi.global.settings.get('locale'); if (!locale) { $.getJSON(opts.base_path + '/assets/locales/magic.json', continueInit); @@ -151,6 +155,7 @@ _kiwi.global = { $.getJSON(opts.base_path + '/assets/locales/' + locale + '.json', continueInit); } + jobs.registerJob('load_text_theme'); text_theme = opts.text_theme; if (!text_theme) { $.getJSON(opts.base_path + '/assets/text_themes/default.json', igniteTextTheme); diff --git a/client/src/index.html.tmpl b/client/src/index.html.tmpl index 97bbdc0..f244e99 100644 --- a/client/src/index.html.tmpl +++ b/client/src/index.html.tmpl @@ -424,6 +424,61 @@ } } + /** + * Get alerted when a group of jobs have been completed. + * Eg. .registerJob('job1'); .registerJob('job2'); + * .onFinish(function(){ alert('Jobs finished!'); }); + * .finishJob('job1'); + * .finishJob('job2'); + */ + function JobManager() { + var completed_jobs = { }; + + // Functions to call once all jobs have completed + var completed_callbacks = []; + + + function callCompletedFunctions(fn) { + $.each(completed_callbacks, function(idx, fn) { + fn(); + }); + + completed_callbacks = []; + }; + + + this.finishJob = function(job_name) { + if (typeof completed_jobs[job_name] === 'undefined') { + return; + } + + completed_jobs[job_name] = true; + + // Check if all our jobs have completed + var all_jobs_completed = true; + $.each(completed_jobs, function(idx, completed) { + if (!completed) { + all_jobs_completed = false; + return false; + } + }); + + if (all_jobs_completed) { + callCompletedFunctions(); + } + }; + + + this.onFinish = function(fn) { + completed_callbacks.push(fn); + }; + + + this.registerJob = function(job_name) { + completed_jobs[job_name] = false; + }; + } + (function () { var base_path = '<%base_path%>', // Entry path for the kiwi application scripts = [], @@ -439,53 +494,7 @@ * Job bootup manager * Once all jobs have completed, call any registered completed functions */ - var jobs = new (function BootManager() { - var completed_jobs = { }; - - // Functions to call once all jobs have completed - var completed_callbacks = []; - - - function callCompletedFunctions(fn) { - $.each(completed_callbacks, function(idx, fn) { - fn(); - }); - - completed_callbacks = []; - }; - - - this.finishJob = function(job_name) { - if (typeof completed_jobs[job_name] === 'undefined') { - return; - } - - completed_jobs[job_name] = true; - - // Check if all our jobs have completed - var all_jobs_completed = true; - $.each(completed_jobs, function(idx, completed) { - if (!completed) { - all_jobs_completed = false; - return false; - } - }); - - if (all_jobs_completed) { - callCompletedFunctions(); - } - }; - - - this.onFinish = function(fn) { - completed_callbacks.push(fn); - }; - - - this.registerJob = function(job_name) { - completed_jobs[job_name] = false; - }; - })(); + var jobs = new JobManager(); // Run after all dependancies have been loaded