Skip to content

Commit f1f2709

Browse files
committed
Add Events to Raven
1 parent 5db4716 commit f1f2709

File tree

1 file changed

+123
-1
lines changed

1 file changed

+123
-1
lines changed

src/raven.js

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ var TK = TraceKit.noConflict();
2222
// Disable Tracekit's remote fetching by default
2323
TK.remoteFetching = false;
2424

25+
var ravenCallbacks = {};
26+
2527
/*
2628
* The core Raven singleton
2729
*
@@ -214,9 +216,111 @@ var Raven = {
214216
globalUser = user;
215217

216218
return Raven;
219+
},
220+
221+
on: function(eventType, eventCallback, eventContext) {
222+
var eventCallbacks;
223+
224+
if (!eventType || typeof eventType !== "string") {
225+
throw new TypeError("Unknown Event: " + eventType);
226+
}
227+
228+
if (!isFunction(eventCallback)) {
229+
throw new TypeError("Cannot attach non-function as a callback: " + eventCallback);
230+
}
231+
232+
eventCallbacks = ravenCallbacks[eventType] || (ravenCallbacks[eventType] = []);
233+
234+
eventCallbacks.push({
235+
callback: eventCallback,
236+
context: eventContext
237+
});
238+
239+
return Raven;
240+
},
241+
242+
off: function(eventType, eventFunction, eventContext) {
243+
var eventCallbacks, matches, key;
244+
245+
if (typeof eventType !== "string" && eventType != null) {
246+
throw new TypeError("Event must be null or a string to remove: " + eventType);
247+
}
248+
249+
if (eventFunction == null && eventContext == null) {
250+
matches = callbackMatchesAlways;
251+
} else if (eventFunction == null && eventContext != null) {
252+
matches = callbackMatchesContext;
253+
} else if (eventFunction != null && eventContext == null) {
254+
matches = callbackMatchesFunction;
255+
} else {
256+
matches = callbackMatchesBoth;
257+
}
258+
259+
eventCallbacks = ravenCallbacks[eventType];
260+
261+
if (eventType == null) {
262+
for (key in ravenCallbacks) {
263+
if (ravenCallbacks.hasOwnProperty(key)) {
264+
eventCallbacks = ravenCallbacks[key];
265+
removeCallbacks(eventCallbacks, matches, eventFunction, eventContext);
266+
}
267+
}
268+
} else {
269+
removeCallbacks(eventCallbacks, matches, eventFunction, eventContext);
270+
}
271+
272+
return Raven;
217273
}
218274
};
219275

276+
function callbackMatchesFunction(callback, func, ctx) {
277+
return callback.callback === func;
278+
}
279+
280+
function callbackMatchesContext(callback, func, ctx) {
281+
return callback.context === ctx;
282+
}
283+
284+
function callbackMatchesBoth(callback, func, ctx) {
285+
return callbackMatchesFunction.apply(null, arguments) && callbackMatchesContext.apply(null, arguments);
286+
}
287+
288+
function callbackMatchesAlways() {
289+
return true;
290+
}
291+
292+
function removeCallbacks(callbacks, matches, func, ctx) {
293+
var i, callback, len;
294+
295+
if (!callbacks) {
296+
return;
297+
}
298+
299+
for (i = 0, len = callbacks.length; i < len;) {
300+
callback = callbacks[i];
301+
if (callback && matches(callback, func, ctx)) {
302+
callbacks.splice(i, 1);
303+
len -= 1;
304+
} else {
305+
i += 1;
306+
}
307+
}
308+
}
309+
310+
function triggerEvent(eventType, args) {
311+
var eventCallbacks = ravenCallbacks[eventType],
312+
len,
313+
i;
314+
315+
if (!eventCallbacks) {
316+
return;
317+
}
318+
319+
for (i = 0, len = eventCallbacks.length; i < len; ++i) {
320+
eventCallbacks[i].callback.apply(eventCallbacks[i].context, args);
321+
}
322+
}
323+
220324
var uriKeys = 'source protocol authority userInfo user password host port relative path directory file query anchor'.split(' '),
221325
uriPattern = /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
222326

@@ -287,6 +391,8 @@ function handleStackInfo(stackInfo, options) {
287391
});
288392
}
289393

394+
triggerEvent('handle', [stackInfo, options]);
395+
290396
processException(
291397
stackInfo.name,
292398
stackInfo.message,
@@ -458,8 +564,24 @@ function send(data) {
458564
makeRequest(data);
459565
}
460566

567+
461568
function makeRequest(data) {
462-
new Image().src = globalServer + getAuthQueryString() + '&sentry_data=' + encodeURIComponent(JSON.stringify(data));
569+
var img, src;
570+
571+
function success() {
572+
triggerEvent('success', [data, src]);
573+
}
574+
575+
function failure() {
576+
triggerEvent('failure', [data, src]);
577+
}
578+
579+
src = globalServer + getAuthQueryString() + '&sentry_data=' + encodeURIComponent(JSON.stringify(data));
580+
img = new Image();
581+
img.onload = success;
582+
img.onerror = failure;
583+
img.onabort = failure;
584+
img.src = src;
463585
}
464586

465587
function isSetup() {

0 commit comments

Comments
 (0)