0.17.1 • Published 2 months ago

@zimi/event-emitter v0.17.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

@zimi/event-emitter

事件触发器

install

yarn add @zimi/event-emitter

examples

const eventBus = new EventEmitter<{
  'eat': ['apple' | 'peach', number];
  'touch': [string];
}>()

eventBus.addListener('eat', (food, count) => {
  console.log(`[eat]: I had eat ${count} ${food}`)
})

eventBus.addListener('touch', (something) => {
  console.log(`[touch]: I'm touching ${something}`)
}, {
  // only listen twice; Infinity if not specified
  times: 2,
})

eventBus.addListener('BUILT_IN_EMIT', ({ type, args }) => {
  switch (type) {
    case 'eat': {
      const [food, count] = args
      console.log(`[BUILT_IN_EMIT]: I had eat ${count} ${food}`)
      break
    }
    case 'touch': {
      const [something] = args
      console.log(`[BUILT_IN_EMIT]: I'm touching ${something}`)
      break
    }
    default:
      break
  }
})

console.log('\nwill eat 2 apple')
eventBus.emit('eat', 'apple', 2)

console.log('\nwill touch apple')
eventBus.emit('touch', 'apple')

console.log('\nwill touch peach')
eventBus.emit('touch', 'peach')

console.log(`
will touch apple,
but [touch] listener can NOT listen this,
as it only listen twice`)
eventBus.emit('touch', 'apple')
0.17.1

2 months ago

0.17.0

5 months ago

0.15.0

1 year ago

0.16.0

1 year ago

0.11.0

1 year ago

0.9.0

1 year ago

0.12.0

1 year ago

0.13.0

1 year ago

0.9.1

1 year ago

0.8.0

1 year ago

0.7.0

1 year ago

0.6.0

1 year ago

0.5.0

1 year ago

0.4.0

1 year ago

0.3.0

1 year ago

0.2.0

1 year ago

0.1.16

1 year ago

0.1.10

1 year ago

0.1.9

2 years ago