From 5cd474d5ff75b123af16b7fef6b81ec78cffe99d Mon Sep 17 00:00:00 2001 From: Graham Kaemmer Date: Sat, 16 Feb 2019 18:37:38 -0800 Subject: [PATCH] Fix inpage.js event handler memory leak Fixes #6176 --- app/scripts/inpage.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/scripts/inpage.js b/app/scripts/inpage.js index fb2a84d1ab41..ae5a375b0c1c 100644 --- a/app/scripts/inpage.js +++ b/app/scripts/inpage.js @@ -29,12 +29,13 @@ console.warn('ATTENTION: In an effort to improve user privacy, MetaMask ' + * @param {Function} handler - event handler * @param {boolean} remove - removes this handler after being triggered */ -function onMessage (messageType, handler, remove) { - window.addEventListener('message', function ({ data }) { +function onMessage (messageType, callback, remove) { + const handler = function ({ data }) { if (!data || data.type !== messageType) { return } remove && window.removeEventListener('message', handler) - handler.apply(window, arguments) - }) + callback.apply(window, arguments) + } + window.addEventListener('message', handler) } //