Détail du package

@metamask/swappable-obj-proxy

MetaMask91.5kISC2.3.0

Tools for creating Proxys around objects that are swappable via setTarget

readme

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');

const original = { sayHello: () => 'hi' };
const next = { sayHello: () => 'haay' };
const proxy = 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');

const original = new EventEmitter();
const next = new EventEmitter();
const proxy = createEventEmitterProxy(original);

proxy.on('event', () => console.log('saw event!'));

// triggers the event handler
original.emit('event');

// moves listeners over to next
proxy.setTarget(next);

// does NOT trigger the event handler
original.emit('event');
// DOES trigger the event handler
next.emit('event');

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

Unreleased

2.3.0

Added

  • Export EventEmitterLike type (#56)

2.2.0

Changed

  • Only migrate events that were added via the proxy (#53)
    • Previously the proxy assumed that all events on the target were added via the proxy, so they would be migrated when the proxy target changed. This introduced bugs when the target was used directly, or when two proxies pointed at the same target.
    • Effectively this change adds support for using the target independently of the proxy, and for using multiple proxies for the same event emitter.

2.1.0

Added

  • Convert this library to TypeScript (#27)
    • You should now be able to use this library in a TypeScript codebase without having to provide your own types.

2.0.0

Added

  • Re-release of this package
    • This package was previously released under swappable-obj-proxy (latest version: 1.1.0). We've begun the version history of @metamask/swappable-obj-proxy at 2.0.0 in order to prevent any confusion. Past releases of swappable-obj-proxy are not explicitly recorded, but can be traced through the commit history. All entries after this line are new additions since the previous release.
  • Support proxying instances of class that reference private fields (#10)
  • Add type definitions for TypeScript projects (#13)