File tree Expand file tree Collapse file tree 5 files changed +63
-11
lines changed Expand file tree Collapse file tree 5 files changed +63
-11
lines changed Original file line number Diff line number Diff line change 10
10
"umd:main" : " dist/mitt.umd.js" ,
11
11
"typings" : " dist/index.d.ts" ,
12
12
"scripts" : {
13
- "testonly" : " mocha --require esm --require ts-node/register test/**/*.js" ,
13
+ "test" : " npm-run-all --silent typecheck lint testonly" ,
14
+ "testonly" : " mocha --require esm test/**/*.js" ,
14
15
"lint" : " eslint src test --ext ts --ext js" ,
15
- "test " : " tsc src/index .ts --noEmit && npm run lint && npm run testonly " ,
16
+ "typecheck " : " tsc **/* .ts --noEmit" ,
16
17
"bundle" : " microbundle" ,
17
18
"build" : " npm-run-all --silent clean -p bundle -s docs" ,
18
19
"clean" : " rimraf dist" ,
23
24
"keywords" : [
24
25
" events" ,
25
26
" eventemitter" ,
27
+ " emitter" ,
26
28
" pubsub"
27
29
],
28
30
"homepage" : " https://github.com/developit/mitt" ,
47
49
"env" : {
48
50
"browser" : true ,
49
51
"mocha" : true ,
52
+ "jest" : false ,
50
53
"es6" : true
51
54
},
52
55
"globals" : {
57
60
2 ,
58
61
" always"
59
62
],
63
+ "jest/valid-expect" : 0 ,
60
64
"@typescript-eslint/no-explicit-any" : 0 ,
61
65
"@typescript-eslint/explicit-function-return-type" : 0 ,
62
66
"@typescript-eslint/explicit-module-boundary-types" : 0 ,
63
67
"@typescript-eslint/no-empty-function" : 0
64
68
}
65
69
},
70
+ "eslintIgnore" : [
71
+ " dist"
72
+ ],
66
73
"devDependencies" : {
67
74
"@types/chai" : " ^4.2.11" ,
68
75
"@types/mocha" : " ^7.0.2" ,
Original file line number Diff line number Diff line change 1
- type EventType = string | symbol ;
1
+ export type EventType = string | symbol ;
2
2
3
3
// An event handler can take an optional event argument
4
4
// and should not return a value
5
- type Handler = ( event ?: any ) => void ;
6
- type WildcardHandler = ( type : EventType , event ?: any ) => void
5
+ export type Handler = ( event ?: any ) => void ;
6
+ export type WildcardHandler = ( type : EventType , event ?: any ) => void
7
7
8
8
// An array of all currently registered event handlers for a type
9
- type EventHandlerList = Array < Handler > ;
10
- type WildCardEventHandlerList = Array < WildcardHandler > ;
9
+ export type EventHandlerList = Array < Handler > ;
10
+ export type WildCardEventHandlerList = Array < WildcardHandler > ;
11
11
12
12
// A map of event types and their corresponding event handlers.
13
- type EventHandlerMap = Map < EventType , EventHandlerList | WildCardEventHandlerList > ;
13
+ export type EventHandlerMap = Map < EventType , EventHandlerList | WildCardEventHandlerList > ;
14
14
15
15
export interface Emitter {
16
16
on ( type : EventType , handler : Handler ) : void ;
Original file line number Diff line number Diff line change 1
- import mitt from '../src ' ;
1
+ import mitt from '..' ;
2
2
import chai , { expect } from 'chai' ;
3
3
import { spy } from 'sinon' ;
4
4
import sinonChai from 'sinon-chai' ;
5
5
chai . use ( sinonChai ) ;
6
6
7
- it ( 'should default export be a function' , ( ) => {
8
- expect ( mitt ) . to . be . a ( 'function' ) ;
7
+ describe ( 'mitt' , ( ) => {
8
+ it ( 'should default export be a function' , ( ) => {
9
+ expect ( mitt ) . to . be . a ( 'function' ) ;
10
+ } ) ;
11
+
12
+ it ( 'should accept an optional event handler map' , ( ) => {
13
+ expect ( ( ) => mitt ( new Map ( ) ) ) . not . to . throw ;
14
+ const map = new Map ( ) ;
15
+ const a = spy ( ) ;
16
+ const b = spy ( ) ;
17
+ map . set ( 'foo' , [ a , b ] ) ;
18
+ const events = mitt ( map ) ;
19
+ events . emit ( 'foo' ) ;
20
+ expect ( a ) . to . have . been . calledOnce ;
21
+ expect ( b ) . to . have . been . calledOnce ;
22
+ } ) ;
9
23
} ) ;
10
24
11
25
describe ( 'mitt#' , ( ) => {
Original file line number Diff line number Diff line change
1
+ import mitt , { EventHandlerList , EventHandlerMap } from '..' ;
2
+
3
+ const events = mitt ( ) ;
4
+ function foo ( ) { }
5
+ events . on ( 'foo' , foo ) ;
6
+ events . emit ( 'foo' , 'hello' ) ;
7
+
8
+ // handler return type should be ignored:
9
+ events . on ( 'foo' , async e => e * 42 ) ;
10
+
11
+ // event map type
12
+ const map = new Map < string , EventHandlerList > ( [
13
+ [ 'foo' , [ foo ] ]
14
+ ] ) ;
15
+ const events2 = mitt ( map ) ;
16
+ events2 . emit ( 'foo' , 'hello' ) ;
17
+
18
+ // event map type & iterables
19
+ const map2 : EventHandlerMap = new Map ( Object . entries ( ( { foo : [ foo ] } ) ) ) ;
20
+ mitt ( map2 ) ;
Original file line number Diff line number Diff line change
1
+ {
2
+ "compileOnSave" : false ,
3
+ "compilerOptions" : {
4
+ "noEmit" : true ,
5
+ "declaration" : true ,
6
+ "moduleResolution" : " node"
7
+ },
8
+ "exclude" : [
9
+ " test"
10
+ ]
11
+ }
You can’t perform that action at this time.
0 commit comments