Skip to content
This repository was archived by the owner on Apr 2, 2020. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion fallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
// Libraries that are imported are stored here.
libraries: {},
libraries_count: 0,
libraries_props: {},
libraries_props: {},

// timeouts for libraries
timeouts: {},
timeouthandler: 0,

// Spawned libraries, so they don't run over themselves.
spawned: [],
Expand Down Expand Up @@ -456,6 +460,7 @@
var me = fallback;
var element;
var type = 'js';
var now = new Date();

var payload = {
loaded: false,
Expand Down Expand Up @@ -493,6 +498,9 @@

me.add_sri_attributes(element, payload.library_props);

me.timeouts[library] = { timeout:now.setSeconds(now.getSeconds() + 5), payload:payload };
me.settimer();

element.onload = function() {
// Checks for JavaScript library.
if (type === 'js' && !me.is_defined(library)) {
Expand Down Expand Up @@ -552,6 +560,7 @@
var me = fallback;

// Mark the library and url as successful.
delete me.timeouts[payload.library];
if (!payload.loaded) {
payload.loaded = true;
me.success[payload.library] = payload.url;
Expand All @@ -565,6 +574,37 @@
return me.ready_invocation();
};

fallback.isObjectEmpty = function(obj) {
for(var prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
return false;
}
}
return true;
}

fallback.settimer = function() {
var me = fallback;
if(me.timeouthandler === 0) {
me.timeouthandler = setInterval(function() {
var now = new Date;

// check list of me.timeouts if any lib is overdue
for (var library in me.timeouts) {
if ((me.timeouts[library] !== "") && (now > me.timeouts[library].timeout)) {
me.spawn.failed(me.timeouts[library].payload);
}
}

// reset timer if no open timeout checks
if (me.isObjectEmpty(me.timeouts)) {
clearInterval(me.timeouthandler);
me.timeouthandler = 0;
}
}, 100);
}
}

fallback.bootstrap();

window.fallback = window.fbk = fallback;
Expand Down