Skip to content

Commit 930d908

Browse files
committed
Change callbacks to Native events
1 parent f1f2709 commit 930d908

File tree

1 file changed

+29
-96
lines changed

1 file changed

+29
-96
lines changed

src/raven.js

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

25-
var ravenCallbacks = {};
26-
2725
/*
2826
* The core Raven singleton
2927
*
@@ -216,108 +214,34 @@ var Raven = {
216214
globalUser = user;
217215

218216
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;
273217
}
274218
};
275219

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-
}
220+
function triggerEvent(eventType, options) {
221+
var event, key;
291222

292-
function removeCallbacks(callbacks, matches, func, ctx) {
293-
var i, callback, len;
223+
eventType = 'raven' + eventType[0].toUpperCase() + eventType.substr(1);
294224

295-
if (!callbacks) {
296-
return;
225+
if (document.createEvent) {
226+
event = document.createEvent("HTMLEvents");
227+
event.initEvent(eventType, true, true);
228+
} else {
229+
event = document.createEventObject();
230+
event.eventType = eventType;
297231
}
298232

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-
}
233+
if (typeof options !== "object") {
234+
options = {}
307235
}
308-
}
309236

310-
function triggerEvent(eventType, args) {
311-
var eventCallbacks = ravenCallbacks[eventType],
312-
len,
313-
i;
314-
315-
if (!eventCallbacks) {
316-
return;
237+
for (key in options) if (options.hasOwnProperty(key)) {
238+
event[key] = options[key]
317239
}
318240

319-
for (i = 0, len = eventCallbacks.length; i < len; ++i) {
320-
eventCallbacks[i].callback.apply(eventCallbacks[i].context, args);
241+
if (document.createEvent) {
242+
document.dispatchEvent(event);
243+
} else {
244+
document.fireEvent("on" + event.eventType.toLowerCase(), event);
321245
}
322246
}
323247

@@ -391,7 +315,10 @@ function handleStackInfo(stackInfo, options) {
391315
});
392316
}
393317

394-
triggerEvent('handle', [stackInfo, options]);
318+
triggerEvent('handle', {
319+
stackInfo: stackInfo,
320+
options: options
321+
});
395322

396323
processException(
397324
stackInfo.name,
@@ -569,11 +496,17 @@ function makeRequest(data) {
569496
var img, src;
570497

571498
function success() {
572-
triggerEvent('success', [data, src]);
499+
triggerEvent('success', {
500+
data: data,
501+
src: src
502+
});
573503
}
574504

575505
function failure() {
576-
triggerEvent('failure', [data, src]);
506+
triggerEvent('failure', {
507+
data: data,
508+
src: src
509+
});
577510
}
578511

579512
src = globalServer + getAuthQueryString() + '&sentry_data=' + encodeURIComponent(JSON.stringify(data));

0 commit comments

Comments
 (0)