Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
fixed race condition using pub/sub
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoscaceres committed Sep 17, 2015
1 parent 314809a commit d9cf231
Showing 1 changed file with 36 additions and 19 deletions.
55 changes: 36 additions & 19 deletions js/core/include-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,45 @@

define(
[],
function() {
function () {
'use strict';
return {
run: function(conf, doc, cb, msg) {
msg.pub("start", "core/include-config");
var script = doc.createElement("script");
script.id = "initialUserConfig";
var confFilter = function(key, val) {
// DefinitionMap contains array of DOM elements that aren't serializable
// we replace them by their id
if (key === "definitionMap") {
var ret = {};
Object.keys(val).forEach(function(k) {
ret[k] = val[k].map(function(d) { return d[0].id});
});
return ret;
run: function (conf, doc, cb, msg) {
msg.pub('start', 'core/include-config');
var initialUserConfig;
try {
if (Object.assign) {
initialUserConfig = Object.assign({}, conf);
} else {
initialUserConfig = JSON.parse(JSON.stringify(conf));
}
return val;
} catch (err) {
initialUserConfig = {};
}
script.innerHTML = JSON.stringify(conf.initialUserConfig, confFilter, 2);
script.type = "application/json";
doc.head.appendChild(script);
msg.pub("end", "core/include-config");
msg.sub('end-all', function () {
var script = doc.createElement('script');
script.id = 'initialUserConfig';
var confFilter = function (key, val) {
// DefinitionMap contains array of DOM elements that aren't serializable
// we replace them by their id
if (key === 'definitionMap') {
var ret = {};
Object
.keys(val)
.forEach(function (k) {
ret[k] = val[k].map(function (d) {
return d[0].id;
});
});
return ret;
}
return val;
};
script.innerHTML = JSON.stringify(initialUserConfig, confFilter, 2);
script.type = 'application/json';
doc.head.appendChild(script);
});
msg.pub('end', 'core/include-config');
cb();
}
};
Expand Down

0 comments on commit d9cf231

Please sign in to comment.