Skip to content

Commit

Permalink
Custom Events should be synchronous by default
Browse files Browse the repository at this point in the history
Most custom events should be synchronous.  This allows handlers for
onBeforeBoomerangBeacon to fire before the beacon is sent so they
have the ability to modify the beacon.

The only event that should be asynchronous is the onBoomerangLoaded
event since that needs to fire after all plugins have loaded.
  • Loading branch information
Philip Tellis committed Jan 12, 2015
1 parent e4e19b7 commit a80ff0c
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions boomerang.js
Expand Up @@ -154,20 +154,35 @@ if (!BOOMR.plugins) { BOOMR.plugins = {}; }
}
}());

dispatchEvent = function(e_name, e_data) {
/**
dispatch a custom event to the browser
@param e_name The custom event name that consumers can subscribe to
@param e_data Any data passed to subscribers of the custom event via the `event.detail` property
@param async By default, custom events are dispatched immediately.
Set to true if the event should be dispatched once the browser has finished its current
JavaScript execution.
*/
dispatchEvent = function(e_name, e_data, async) {
var ev = createCustomEvent(e_name, {"detail": e_data});
if (!ev) {
return;
}

BOOMR.setImmediate(function() {
function dispatch() {
if(d.dispatchEvent) {
d.dispatchEvent(ev);
}
else if(d.fireEvent) {
d.fireEvent("onpropertychange", ev);
}
});
}

if(async) {
BOOMR.setImmediate(dispatch);
}
else {
dispatch();
}
};

// visibilitychange is useful to detect if the page loaded through prerender
Expand Down Expand Up @@ -1126,7 +1141,7 @@ if (!BOOMR.xhr_excludes) {

}());

dispatchEvent("onBoomerangLoaded", { "BOOMR": BOOMR } );
dispatchEvent("onBoomerangLoaded", { "BOOMR": BOOMR }, true );

}(window));

Expand Down

0 comments on commit a80ff0c

Please sign in to comment.