Skip to content

Commit 81231bf

Browse files
authored
Merge branch 'master' into vmihalkov/bug-6321
2 parents 9157515 + 048301c commit 81231bf

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

projects/igniteui-angular/src/lib/core/setImmediate.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
*/
2121

2222
// Note: Originally copied from core-js-pure package and modified. (https://github.com/zloirock/core-js)
23-
const windowLocation = window.location;
24-
let counter = 0;
23+
2524
const queue = {};
25+
let counter = 0;
26+
let eventListenerAdded = false;
2627

2728
const run = function (id) {
2829
if (queue.hasOwnProperty(id)) {
@@ -36,15 +37,16 @@ const listener = function (event) {
3637
run(event.data);
3738
};
3839

39-
if (!window.setImmediate) {
40-
window.addEventListener('message', listener, false);
41-
}
42-
4340
export function setImmediate(cb: any) {
4441
if (window.setImmediate) {
4542
return window.setImmediate(cb);
4643
}
4744

45+
if (!eventListenerAdded) {
46+
eventListenerAdded = true;
47+
window.addEventListener('message', listener, false);
48+
}
49+
4850
const args = [];
4951
let i = 1;
5052

@@ -56,6 +58,7 @@ export function setImmediate(cb: any) {
5658
(typeof cb === 'function' ? cb : Function(cb)).apply(undefined, args);
5759
};
5860

61+
const windowLocation = window.location;
5962
window.postMessage(counter + '', windowLocation.protocol + '//' + windowLocation.host);
6063

6164
return counter;

0 commit comments

Comments
 (0)