Skip to content

Repository files navigation

createSwappableProxy

Creates a Proxy around any object. Retarget the proxy with setTarget.

Installation

yarn add @metamask/swappable-obj-proxy

or

npm install @metamask/swappable-obj-proxy

Usage

createSwappableProxy

const{ createSwappableProxy }=require('@metamask/swappable-obj-proxy');constoriginal={sayHello: ()=>'hi'};constnext={sayHello: ()=>'haay'};constproxy=createEventEmitterProxy(original);proxy.sayHello();//=> "hi"proxy.setTarget(next);proxy.sayHello();//=> "haay"

createEventEmitterProxy

Creates a Proxy around an EventEmitter. If the proxy has setTarget called with a different EventEmitter, all events will be removed from the old target and transferred to the new EventEmitter.

const{ createEventEmitterProxy }=require('@metamask/swappable-obj-proxy');constoriginal=newEventEmitter();constnext=newEventEmitter();constproxy=createEventEmitterProxy(original);proxy.on('event',()=>console.log('saw event!'));// triggers the event handleroriginal.emit('event');// moves listeners over to nextproxy.setTarget(next);// does NOT trigger the event handleroriginal.emit('event');// DOES trigger the event handlernext.emit('event');

About

No description, website, or topics provided.

Resources

Code of conduct

Security policy

Stars

4 stars

Watchers

32 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages