Skip to content

Commit c38a7a1

Browse files
kamilogorekHazAT
authored andcommitted
feat: allow for multiple onLoad callbacks in loader (#1797)
1 parent 5b6d39b commit c38a7a1

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

packages/browser/src/loader.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
}
2323

2424
var injected = false;
25-
var onLoadCallback;
25+
var onLoadCallbacks = [];
2626

2727
// Create a namespace and attach function that will store captured exception
2828
// Because functions are also objects, we can attach the queue itself straight to it and save some bytes
@@ -40,13 +40,13 @@
4040
// We only want to lazy inject/load the sdk bundle if
4141
// an error or promise rejection occured
4242
// OR someone called `capture...` on the SDK
43-
injectSdk(onLoadCallback);
43+
injectSdk(onLoadCallbacks);
4444
}
4545
queue.data.push(content);
4646
};
4747
queue.data = [];
4848

49-
function injectSdk(callback) {
49+
function injectSdk(callbacks) {
5050
if (injected) {
5151
return;
5252
}
@@ -85,7 +85,7 @@
8585
oldInit(target);
8686
};
8787

88-
sdkLoaded(callback, SDK);
88+
sdkLoaded(callbacks, SDK);
8989
} catch (o_O) {
9090
console.error(o_O);
9191
}
@@ -94,11 +94,14 @@
9494
_currentScriptTag.parentNode.insertBefore(_newScriptTag, _currentScriptTag);
9595
}
9696

97-
function sdkLoaded(callback, SDK) {
97+
function sdkLoaded(callbacks, SDK) {
9898
try {
99-
if (callback) {
100-
callback();
99+
for (var i = 0; i < callbacks.length; i++) {
100+
if (typeof callbacks[i] === 'function') {
101+
callbacks[i]();
102+
}
101103
}
104+
102105
var data = queue.data;
103106

104107
// We want to replay all calls to Sentry first to make sure init is called before
@@ -142,18 +145,18 @@
142145
// We don't want to _window.Sentry = _window.Sentry || { ... } since we want to make sure
143146
// that the first Sentry "instance" is our with onLoad
144147
_window[_namespace] = {
145-
onLoad: function(callback) {
148+
onLoad: function (callback) {
149+
onLoadCallbacks.push(callback);
146150
if (lazy && !forceLoad) {
147-
onLoadCallback = callback;
148-
} else {
149-
injectSdk(callback);
151+
return;
150152
}
153+
injectSdk(onLoadCallbacks);
151154
},
152155
forceLoad: function() {
153156
forceLoad = true;
154157
if (lazy) {
155158
setTimeout(function() {
156-
injectSdk(onLoadCallback);
159+
injectSdk(onLoadCallbacks);
157160
});
158161
}
159162
}
@@ -197,7 +200,7 @@
197200

198201
if (!lazy) {
199202
setTimeout(function() {
200-
injectSdk(onLoadCallback);
203+
injectSdk(onLoadCallbacks);
201204
});
202205
}
203206
})(window, document, 'script', 'onerror', 'onunhandledrejection', 'Sentry', 'loader.js', '../../build/bundle.js', {

0 commit comments

Comments
 (0)