js: embed: Make init, fetchComments wait on /config
Those two functions need to wait for the config to be
fetched from the server. Utilize the newly introduced
`wait_for` helper mechanism to coordinate using registered
triggers instead of using timeout/retry.
@@ -13,6 +13,7 @@ var count = require("app/count");
var $ = require("app/dom");
var svg = require("app/svg");
var template = require("app/template");
+var utils = require("app/utils");
"use strict";
@@ -25,7 +26,12 @@ var isso_thread;
var heading;
var postbox;
+// Track whether config has been fetched from server
+var config_fetched = utils.wait_for();
+
function init() {
+ config_fetched.reset()
+
// Fetch config from server, will override any local data-isso-* attributes
api.config().then(
function (rv) {
@@ -40,11 +46,6 @@ function init() {
}
config[setting] = rv.config[setting]
}
- },
- function(err) {
- console.log(err);
- }
- );
if (config["css"] && $("style#isso-style") === null) {
var style = $.new("link");
@@ -59,12 +60,14 @@ function init() {
heading = $.new('h4.isso-thread-heading');
postbox = new isso.Postbox(null);
+ // Relies on i18n.pluralize, but doesn't need to wait for server config
count();
if (isso_thread === null) {
return console.log("abort, #isso-thread is missing");
}
+ // Depends on whether feed is enabled on server
if (config["feed"]) {
var feedLink = $.new('a', i18n.translate('atom-feed'));
var feedLinkWrapper = $.new('span.isso-feedlink');
@@ -72,7 +75,6 @@ function init() {
feedLinkWrapper.appendChild(feedLink);
isso_thread.append(feedLinkWrapper);
}
-
// Only insert elements if not already present, respecting Single-Page-Apps
if (!$('h4.isso-thread-heading')) {
isso_thread.append(heading);
@@ -86,6 +88,13 @@ function init() {
isso_thread.append('<div id="isso-root"></div>');
}
+ config_fetched.on_ready();
+ },
+ function(err) {
+ console.log(err);
+ }
+ );
+
window.addEventListener('hashchange', function() {
if (!window.location.hash.match("^#isso-[0-9]+$")) {
return;
@@ -107,7 +116,8 @@ function init() {
function fetchComments() {
var isso_root = $('#isso-root');
- if (!isso_root) {
+ if (!isso_root || !config_fetched.is_ready()) {
+ config_fetched.register(fetchComments);
return;
}
isso_root.textContent = '';
