4.0.22 • Published 8 months ago

powerful-mitt v4.0.22

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

PowerfulMitt

Execute event prioritily, nestly, fail when previous listenser return true.At last, you can unresiger specific command by key.

Feature

  • Executing by prioriy
  • Registering and emit nested command
  • Failing when previous listenser return true
  • Unregistering by key

...

If you can imagine any other feature, please submit a issue

Get Start

Install

npm install --save powerful-mitt

Basic Using

import {createEmitter, Emitter} from "powerful-mitt";
const emitter = createEmitter()
const FOO_COMMAND = createCommand<[string, string], void>('foo')
emitter.on(FOO_COMMAND, (obj1: string, obj2: string) => {
  console.log(obj1, obj2);
})
emitter.emit(FOO_COMMAND, 'a', 'b')

Expect result

a b

Powerful Using

import {createEmitter, Emitter} from "powerful-mitt";
const emitter = createEmitter({
                key: true,
                priority: true,
                failEmit: true,
                commandNest: true
            })

const FOO_COMMAND = createCommand<[string], void>('foo')
const NODE_COMMAND = createCommand<[string], void>('node')
const NODE_FOO_COMMAND = createNestCommand(NODE_COMMAND, FOO_COMMAND)
let result: string[]
const a = () => {
  result.push('a')
}
const b = () => {
  result.push('b')
  return true
}
const c = () => {
  result.push('c')
}
emitter.on(NODE_FOO_COMMAND, a, {
  priority: 1,
  key: 'alpha'
})
emitter.on(NODE_FOO_COMMAND, b, {
  priority: 2,
  key: 'beta'
})
emitter.on(NODE_FOO_COMMAND, c, {
  priority: 3,
  key: 'alpha'
})

No listeners executed

emitter.emit(NODE_COMMAND, '');
emitter.emit(FOO_COMMAND, '');

Expect result

result === [] // deepEqual

All listeners executed, but listener a

Because listener b executed ahead, and it return true.

emitter.emit(createNestCommand(NODE_COMMAND, ALL_COMMAND), '');

Expect result

result === ['c', 'b'] // deepEqual

After unresigster, some listeners executed

Only listener b execute.

emitter.off(NODE_FOO_COMMAND, undefined, {
  key: 'alpha'
})
emitter.emit(createNestCommand(NODE_COMMAND, ALL_COMMAND), '');

Expect result

result === ['c', 'b', 'b'] // deepEqual

License

MIT License © Meogic

4.0.21

8 months ago

4.0.22

8 months ago

4.0.20

9 months ago

4.0.19

1 year ago

4.0.16

1 year ago

4.0.15

1 year ago

4.0.18

1 year ago

4.0.17

1 year ago

4.0.14

1 year ago

4.0.12

1 year ago

4.0.11

1 year ago

4.0.13

1 year ago

4.0.10

2 years ago

4.0.9

2 years ago

4.0.8

2 years ago

4.0.7

2 years ago

4.0.6

2 years ago

4.0.5

2 years ago

4.0.4

2 years ago

4.0.3

2 years ago

4.0.2

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

0.1.7

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

0.1.0

2 years ago