Skip to content

Commit a56478a

Browse files
authored
Merge pull request #10 from developit/patch-1
Fix memory leak
2 parents 50a61ac + 1e0a36c commit a56478a

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
*/
55
let callbacks = [];
66

7-
const channel = new MessageChannel();
7+
let channel = new MessageChannel();
8+
9+
let postMessage = (function() {
10+
this.postMessage(undefined);
11+
}).bind(channel.port2);
812

913
// Flush the callback queue when a message is posted to the message channel
1014
channel.port1.onmessage = () => {
@@ -22,9 +26,8 @@ channel.port1.onmessage = () => {
2226
}
2327
};
2428

25-
function postMessage() {
26-
channel.port2.postMessage(undefined);
27-
}
29+
// If the onmessage handler closes over the MessageChannel, the MessageChannel never gets GC'd:
30+
channel = null;
2831

2932
/**
3033
* Invoke the given callback after the browser renders the next frame

0 commit comments

Comments
 (0)