Skip to content

Commit be5237f

Browse files
authored
Migrate to TypeScript and use Map (#99)
Migrate to TypeScript & Microbundle, move to Map for event handler storage
1 parent cfd246c commit be5237f

File tree

8 files changed

+152
-221
lines changed

8 files changed

+152
-221
lines changed

README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -95,57 +95,57 @@ const emitter: mitt.Emitter = mitt();
9595

9696
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
9797

98-
### mitt
98+
#### Table of Contents
9999

100-
Mitt: Tiny (~200b) functional event emitter / pubsub.
100+
- [mitt](#mitt)
101+
- [on](#on)
102+
- [Parameters](#parameters)
103+
- [off](#off)
104+
- [Parameters](#parameters-1)
105+
- [emit](#emit)
106+
- [Parameters](#parameters-2)
101107

102-
**Parameters**
108+
### mitt
103109

104-
- `all` **EventHandlerMap**
110+
Mitt: Tiny (~200b) functional event emitter / pubsub.
105111

106112
Returns **Mitt**
107113

108114
### on
109115

110116
Register an event handler for the given type.
111117

112-
**Parameters**
118+
#### Parameters
113119

114-
- `type` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to listen for, or `"*"` for all events
120+
- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to listen for, or `"*"` for all events
115121
- `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Function to call in response to given event
116122

117123
### off
118124

119125
Remove an event handler for the given type.
120126

121-
**Parameters**
127+
#### Parameters
122128

123-
- `type` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to unregister `handler` from, or `"*"`
129+
- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to unregister `handler` from, or `"*"`
124130
- `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Handler function to remove
125131

126132
### emit
127133

128134
Invoke all handlers for the given type.
129135
If present, `"*"` handlers are invoked after type-matched handlers.
130136

131-
_Note: Manually firing "*" handlers is not supported._
137+
Note: Manually firing "\*" handlers is not supported.
132138

133-
**Parameters**
139+
#### Parameters
134140

135-
- `type` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The event type to invoke
141+
- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** The event type to invoke
136142
- `evt` **Any?** Any value (object is recommended and powerful), passed to each handler
137143

138144
## Contribute
139145

140146
First off, thanks for taking the time to contribute!
141147
Now, take a moment to be sure your contributions make sense to everyone else.
142148

143-
Development Start:
144-
145-
This project is typed with Flow Type annotations. To ensure you have the proper typings for this project run
146-
147-
`flow-typed install`
148-
149149
### Reporting Issues
150150

151151
Found a problem? Want a new feature? First of all see if your issue or idea has [already been reported](../../issues).

mitt.d.ts

Lines changed: 0 additions & 53 deletions
This file was deleted.

package.json

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
"version": "1.2.0",
44
"description": "Tiny 200b functional Event Emitter / pubsub.",
55
"jsnext:main": "dist/mitt.es.js",
6+
"source": "src/index.ts",
67
"module": "dist/mitt.es.js",
78
"main": "dist/mitt.js",
89
"umd:main": "dist/mitt.umd.js",
10+
"typings": "dist/index.d.ts",
911
"scripts": {
1012
"bump": "standard-version",
11-
"testonly": "mocha --require esm --require flow-remove-types/register test/**/*.js",
12-
"lint": "eslint src test",
13-
"test": "flow && npm run lint && npm run testonly",
14-
"build": "npm-run-all --silent clean -p rollup -p minify:* -s docs size",
13+
"testonly": "mocha --require esm --require ts-node/register test/**/*.js",
14+
"lint": "eslint src test --ext ts --ext js",
15+
"test": "tsc src/index.ts --noEmit && npm run lint && npm run testonly",
16+
"bundle": "microbundle",
17+
"build": "npm-run-all --silent clean -p bundle -s docs size",
1518
"clean": "rimraf dist",
16-
"rollup": "rollup -c",
17-
"minify:cjs": "uglifyjs $npm_package_main -cm toplevel -o $npm_package_main -p relative --in-source-map ${npm_package_main}.map --source-map ${npm_package_main}.map",
18-
"minify:umd": "uglifyjs $npm_package_umd_main -cm -o $npm_package_umd_main -p relative --in-source-map ${npm_package_umd_main}.map --source-map ${npm_package_umd_main}.map",
19-
"docs": "documentation readme src/index.js --section API -q",
19+
"docs": "documentation readme src/index.ts --section API -q --parse-extension ts",
2020
"size": "echo \"Gzipped Size: $(strip-json-comments --no-whitespace $npm_package_main | gzip-size | pretty-bytes)\"",
2121
"release": "npm run build -s && npm run bump && git push --follow-tags origin master && npm publish"
2222
},
@@ -33,21 +33,15 @@
3333
"license": "MIT",
3434
"files": [
3535
"src",
36-
"dist",
37-
"mitt.d.ts"
36+
"dist"
3837
],
39-
"babel": {
40-
"presets": [
41-
"es2015",
42-
"stage-0"
43-
],
44-
"plugins": [
45-
"transform-flow-strip-types"
46-
]
47-
},
4838
"eslintConfig": {
49-
"extends": "eslint:recommended",
50-
"parser": "babel-eslint",
39+
"extends": [
40+
"eslint:recommended",
41+
"plugin:@typescript-eslint/eslint-recommended",
42+
"plugin:@typescript-eslint/recommended"
43+
],
44+
"parser": "@typescript-eslint/parser",
5145
"parserOptions": {
5246
"sourceType": "module"
5347
},
@@ -63,34 +57,30 @@
6357
"semi": [
6458
2,
6559
"always"
66-
]
60+
],
61+
"@typescript-eslint/no-explicit-any": 0,
62+
"@typescript-eslint/explicit-function-return-type": 0,
63+
"@typescript-eslint/no-empty-function": 0
6764
}
6865
},
69-
"typings": "./mitt.d.ts",
7066
"devDependencies": {
71-
"babel-core": "^6.9.1",
72-
"babel-eslint": "^10.0.3",
73-
"babel-plugin-transform-flow-strip-types": "^6.21.0",
74-
"babel-preset-es2015": "^6.9.0",
75-
"babel-preset-stage-0": "^6.5.0",
67+
"@typescript-eslint/eslint-plugin": "^2.34.0",
68+
"@typescript-eslint/parser": "^2.34.0",
7669
"chai": "^3.5.0",
77-
"documentation": "^4.0.0-beta4",
70+
"documentation": "^13.0.0",
7871
"eslint": "^6.5.1",
7972
"esm": "^3.2.25",
80-
"flow-bin": "^0.38.0",
81-
"flow-remove-types": "^1.2.0",
8273
"gzip-size-cli": "^1.0.0",
74+
"microbundle": "^0.12.0",
8375
"mocha": "^3.2.0",
8476
"npm-run-all": "^2.1.1",
8577
"pretty-bytes-cli": "^2.0.0",
8678
"rimraf": "^2.5.2",
87-
"rollup": "^0.41.4",
88-
"rollup-plugin-buble": "^0.15.0",
89-
"rollup-plugin-flow": "^1.1.1",
9079
"sinon": "^1.17.4",
9180
"sinon-chai": "^2.8.0",
9281
"standard-version": "^4.0.0",
9382
"strip-json-comments-cli": "^1.0.1",
94-
"uglify-js": "^2.6.2"
83+
"ts-node": "^8.10.1",
84+
"typescript": "^3.9.3"
9585
}
9686
}

rollup.config.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/index.js

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/index.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
type EventType = string | symbol;
2+
3+
// An event handler can take an optional event argument
4+
// and should not return a value
5+
type Handler = (event?: any) => void;
6+
type WildcardHandler= (type: EventType, event?: any) => void
7+
8+
// An array of all currently registered event handlers for a type
9+
type EventHandlerList = Array<Handler>;
10+
type WildCardEventHandlerList = Array<WildcardHandler>;
11+
12+
// A map of event types and their corresponding event handlers.
13+
type EventHandlerMap = Map<EventType, EventHandlerList | WildCardEventHandlerList>;
14+
15+
export interface Emitter {
16+
on(type: EventType, handler: Handler): void;
17+
on(type: "*", handler: WildcardHandler): void;
18+
19+
off(type: EventType, handler: Handler): void;
20+
off(type: "*", handler: WildcardHandler): void;
21+
22+
emit<T = any>(type: EventType, event?: T): void;
23+
emit(type: "*", event?: any): void;
24+
}
25+
26+
/** Mitt: Tiny (~200b) functional event emitter / pubsub.
27+
* @name mitt
28+
* @returns {Mitt}
29+
*/
30+
export default function mitt(all: EventHandlerMap): Emitter {
31+
all = all || new Map();
32+
33+
return {
34+
/**
35+
* Register an event handler for the given type.
36+
*
37+
* @param {string|symbol} type Type of event to listen for, or `"*"` for all events
38+
* @param {Function} handler Function to call in response to given event
39+
* @memberOf mitt
40+
*/
41+
on(type: EventType, handler: Handler) {
42+
const handlers = (all.get(type) || []);
43+
handlers.push(handler);
44+
all.set(type, handlers);
45+
},
46+
47+
/**
48+
* Remove an event handler for the given type.
49+
*
50+
* @param {string|symbol} type Type of event to unregister `handler` from, or `"*"`
51+
* @param {Function} handler Handler function to remove
52+
* @memberOf mitt
53+
*/
54+
off(type: EventType, handler: Handler) {
55+
if (all.has(type)) {
56+
all.get(type).splice(all.get(type).indexOf(handler) >>> 0, 1);
57+
}
58+
},
59+
60+
/**
61+
* Invoke all handlers for the given type.
62+
* If present, `"*"` handlers are invoked after type-matched handlers.
63+
*
64+
* Note: Manually firing "*" handlers is not supported.
65+
*
66+
* @param {string|symbol} type The event type to invoke
67+
* @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler
68+
* @memberOf mitt
69+
*/
70+
emit(type: EventType, evt: any) {
71+
((all.get(type) || []) as EventHandlerList).slice().map((handler) => { handler(evt); });
72+
((all.get('*') || []) as WildCardEventHandlerList).slice().map((handler) => { handler(type, evt); });
73+
}
74+
};
75+
}

0 commit comments

Comments
 (0)