diff options
author | frankiebee <frankie.diamond@gmail.com> | 2017-09-27 02:52:57 +0800 |
---|---|---|
committer | frankiebee <frankie.diamond@gmail.com> | 2017-09-27 04:38:27 +0800 |
commit | b654eb9b1f37cd3757e4614bb048884ab89d2986 (patch) | |
tree | feef7673d9fc25863471a75e0598e3bc5fe548ec /app/scripts/lib/events-proxy.js | |
parent | 87fc44c45a5bb511fe60b2c3fe7e7c78f5f6584f (diff) | |
download | tangerine-wallet-browser-b654eb9b1f37cd3757e4614bb048884ab89d2986.tar tangerine-wallet-browser-b654eb9b1f37cd3757e4614bb048884ab89d2986.tar.gz tangerine-wallet-browser-b654eb9b1f37cd3757e4614bb048884ab89d2986.tar.bz2 tangerine-wallet-browser-b654eb9b1f37cd3757e4614bb048884ab89d2986.tar.lz tangerine-wallet-browser-b654eb9b1f37cd3757e4614bb048884ab89d2986.tar.xz tangerine-wallet-browser-b654eb9b1f37cd3757e4614bb048884ab89d2986.tar.zst tangerine-wallet-browser-b654eb9b1f37cd3757e4614bb048884ab89d2986.zip |
wrap block tracker in events proxy
Diffstat (limited to 'app/scripts/lib/events-proxy.js')
-rw-r--r-- | app/scripts/lib/events-proxy.js | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/app/scripts/lib/events-proxy.js b/app/scripts/lib/events-proxy.js new file mode 100644 index 000000000..d1199a278 --- /dev/null +++ b/app/scripts/lib/events-proxy.js @@ -0,0 +1,31 @@ +module.exports = function createEventEmitterProxy(eventEmitter, listeners) { + let target = eventEmitter + const eventHandlers = listeners || {} + const proxy = new Proxy({}, { + get: (obj, name) => { + // intercept listeners + if (name === 'on') return addListener + if (name === 'setTarget') return setTarget + if (name === 'proxyEventHandlers') return eventHandlers + return target[name] + }, + set: (obj, name, value) => { + target[name] = value + return true + }, + }) + function setTarget (eventEmitter) { + target = eventEmitter + // migrate listeners + Object.keys(eventHandlers).forEach((name) => { + eventHandlers[name].forEach((handler) => target.on(name, handler)) + }) + } + function addListener (name, handler) { + if (!eventHandlers[name]) eventHandlers[name] = [] + eventHandlers[name].push(handler) + target.on(name, handler) + } + if (listeners) proxy.setTarget(eventEmitter) + return proxy +}
\ No newline at end of file |