antfu / ts-event

Typescript Event Emitter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TS-Event

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

Install

npm i @antfu/ts-event

License

MIT © antfu 2019

About

Typescript Event Emitter

License:MIT License


Languages

Language:TypeScript 89.2%Language:JavaScript 10.8%