Skip to content

Instantly share code, notes, and snippets.

@asicfr
Last active February 15, 2018 13:18
function DataLayerReceiver(callBack) {
this.dataLayerByName = [];
this.callBack = callBack;
}
DataLayerReceiver.prototype.emit = function (publisherName, publisherData) {
console.log('emit from ', publisherName);
var complete = true;
for (var i = 0; i < this.dataLayerByName.length; i++) {
if (this.dataLayerByName[i].name === publisherName) {
console.log('.... emit from well known ', this.dataLayerByName[i].name);
this.dataLayerByName[i].emitted = true;
if (publisherData) {
this.dataLayerByName[i].data = publisherData;
}
}
if (this.dataLayerByName[i].emitted === false) {
console.log('.... emitted === false for ', this.dataLayerByName[i].name);
complete = false;
}
}
if (complete) {
console.log('.... emit complete !!!!!!');
this.callBack(this.dataLayerByName);
}
};
DataLayerReceiver.prototype.attach = function (publisherName) {
console.log('attach ', publisherName);
this.dataLayerByName.push({ name: publisherName, emitted: false });
return this.emit.bind(this, publisherName);
};
function myCallBack(data) {
console.log('declenchement du myCallBack');
document.getElementById('result').innerHTML = "Test terminé avec succès !!! "
}
var t1 = new DataLayerReceiver(myCallBack);
// En tout premier, on attache un emitter lié a l'evenement pubDOMContentLoaded
// Ce faux emitter nous permet d'etre sur que tous les emitteurs ce soient inscrits
// avant d'envoyer les donnees (execution du callback)
var pubDOMContentLoaded = t1.attach("pubDOMContentLoaded");
window.addEventListener("DOMContentLoaded", function () {
console.log('DOM Content Loaded fired from pubDOMContentLoaded');
pubDOMContentLoaded(); // On n'envoie pas de donnees reelles !!!
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment