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 1010 "umd:main" : " dist/mitt.umd.js" ,
1111 "typings" : " dist/index.d.ts" ,
1212 "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" ,
1415 "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" ,
1617 "bundle" : " microbundle" ,
1718 "build" : " npm-run-all --silent clean -p bundle -s docs" ,
1819 "clean" : " rimraf dist" ,
2324 "keywords" : [
2425 " events" ,
2526 " eventemitter" ,
27+ " emitter" ,
2628 " pubsub"
2729 ],
2830 "homepage" : " https://github.com/developit/mitt" ,
4749 "env" : {
4850 "browser" : true ,
4951 "mocha" : true ,
52+ "jest" : false ,
5053 "es6" : true
5154 },
5255 "globals" : {
5760 2 ,
5861 " always"
5962 ],
63+ "jest/valid-expect" : 0 ,
6064 "@typescript-eslint/no-explicit-any" : 0 ,
6165 "@typescript-eslint/explicit-function-return-type" : 0 ,
6266 "@typescript-eslint/explicit-module-boundary-types" : 0 ,
6367 "@typescript-eslint/no-empty-function" : 0
6468 }
6569 },
70+ "eslintIgnore" : [
71+ " dist"
72+ ],
6673 "devDependencies" : {
6774 "@types/chai" : " ^4.2.11" ,
6875 "@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 ;
22
33// An event handler can take an optional event argument
44// 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
77
88// 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 > ;
1111
1212// 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 > ;
1414
1515export interface Emitter {
1616 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 '..' ;
22import chai , { expect } from 'chai' ;
33import { spy } from 'sinon' ;
44import sinonChai from 'sinon-chai' ;
55chai . use ( sinonChai ) ;
66
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+ } ) ;
923} ) ;
1024
1125describe ( '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