forwardit
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

forwardit

Forwarding marker and installer.

Installation

npm i forwardit

Usage

Basic usage in JavaScript:

const { forward, installForwards, uninstallForwards } = require('forwardit');

class Data {
  @forward
  value = 0;

  @forward
  inc() {
    this.value++;
  }
}

const data = new Data();
const broker = {};

installForwards(broker, data);
console.log(broker.value, data.value); // 0, 0

broker.inc();
console.log(broker.value, data.value); // 1, 1

data.inc();
console.log(broker.value, data.value); // 2, 2

uninstallForwards(broker, data);
console.log(broker.value, data.value); // undefined, 2

For TypeScript, we need to argument broker's type information to avoid compiler errors and make code completion working correctly:

// declare Broker as usual
class Broker {
  // Broker's own properties
}

// argument Broker with selected properties from Data
interface Broker extends Pick<Data, 'value' | 'inc'> {}

// create instances, install forwards...

// now code completion works for "value" and "inc", no more compiler errors
broker.value;
broker.inc;

Package Sidebar

Install

npm i forwardit

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

9.11 kB

Total Files

5

Last publish

Collaborators

  • bsdelf