npm.io
0.1.2 • Published 7 years ago

@antfu/ts-event

Licence
MIT
Version
0.1.2
Deps
0
Size
21 kB
Vulns
0
Weekly
0
Stars
2

TSEvent

Typescript Event Emitter with the same API of VS Code

Basic Usage

class Counter {
  // define the emitter
  private _onChanged = new EventEmitter<number>()
  public readonly onChanged = this._onChanged.event

  private _value = 0

  get value() {
    return this._value
  }

  set value(v) {
    this._value = v
    this._onChanged.fire(this.value)
  }
}

const counter = new Counter()

counter.onChanged(n => { // n is number here
  console.log(`The new value is ${n}`)
})

counter.value = 5 // Output: The new value is 5

License

MIT antfu 2019