Skip to content

Commit 377d6f4

Browse files
awearydevelopit
authored andcommitted
Add Flow type annotations (#23)
* Add flowtype annotations * Add flow to test script
1 parent f147246 commit 377d6f4

File tree

6 files changed

+53
-9
lines changed

6 files changed

+53
-9
lines changed

.babelrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"presets": ["es2015", "stage-0"]
2+
"presets": ["es2015", "stage-0"],
3+
"plugins": [
4+
"transform-flow-strip-types"
5+
]
36
}

.flowconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[ignore]
2+
3+
[include]
4+
5+
[libs]
6+
7+
[options]

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"main": "dist/mitt.js",
88
"umd:main": "dist/mitt.umd.js",
99
"scripts": {
10-
"test": "eslint src test && mocha --compilers js:babel-register test/**/*.js",
10+
"test": "flow && eslint src test && mocha --compilers js:babel-register test/**/*.js",
1111
"build": "npm-run-all clean -p rollup:* -p minify:* -s docs size",
1212
"clean": "rimraf dist && mkdirp dist",
1313
"rollup:cjs": "rollup -c rollup.config.js -m -f cjs -n $npm_package_amdName $npm_package_jsnext_main -o $npm_package_main",
@@ -53,12 +53,14 @@
5353
"devDependencies": {
5454
"babel-core": "^6.9.1",
5555
"babel-eslint": "^7.1.1",
56+
"babel-plugin-transform-flow-strip-types": "^6.21.0",
5657
"babel-preset-es2015": "^6.9.0",
5758
"babel-preset-stage-0": "^6.5.0",
5859
"babel-register": "^6.9.0",
5960
"chai": "^3.5.0",
6061
"documentation": "^4.0.0-beta4",
6162
"eslint": "^3.13.1",
63+
"flow-bin": "^0.38.0",
6264
"gzip-size-cli": "^1.0.0",
6365
"mkdirp": "^0.5.1",
6466
"mocha": "^3.2.0",
@@ -67,6 +69,7 @@
6769
"rimraf": "^2.5.2",
6870
"rollup": "^0.41.4",
6971
"rollup-plugin-buble": "^0.15.0",
72+
"rollup-plugin-flow": "^1.1.1",
7073
"sinon": "^1.17.4",
7174
"sinon-chai": "^2.8.0",
7275
"strip-json-comments-cli": "^1.0.1",

rollup.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import buble from 'rollup-plugin-buble';
2+
import flow from 'rollup-plugin-flow';
23

34
export default {
45
useStrict: false,
56
plugins: [
7+
flow(),
68
buble()
79
]
810
};

src/index.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1+
// @flow
2+
// An event handler can take an optional event argument
3+
// and should not return a value
4+
type EventHandler = (event?: any) => void;
5+
// An array of all currently registered event handlers for a type
6+
type EventHandlerList = Array<EventHandler>;
7+
// A map of event types and their corresponding event handlers.
8+
type EventHandlerMap = {
9+
[type: string]: EventHandlerList,
10+
};
11+
112
/** Mitt: Tiny (~200b) functional event emitter / pubsub.
213
* @name mitt
314
* @returns {Mitt}
415
*/
5-
export default function mitt(all) {
16+
export default function mitt(all: EventHandlerMap) {
617
all = all || Object.create(null);
718

819
return {
@@ -14,7 +25,7 @@ export default function mitt(all) {
1425
* @return {Object} the `mitt` instance for chaining
1526
* @memberOf mitt
1627
*/
17-
on(type, handler) {
28+
on(type: string, handler: EventHandler) {
1829
(all[type] || (all[type] = [])).push(handler);
1930
},
2031

@@ -26,7 +37,7 @@ export default function mitt(all) {
2637
* @return {Object} the `mitt` instance for chaining
2738
* @memberOf mitt
2839
*/
29-
off(type, handler) {
40+
off(type: string, handler: EventHandler) {
3041
let e = all[type] || (all[type] = []);
3142
e.splice(e.indexOf(handler) >>> 0, 1);
3243
},
@@ -40,7 +51,7 @@ export default function mitt(all) {
4051
* @return {Object} the `mitt` instance for chaining
4152
* @memberof mitt
4253
*/
43-
emit(type, evt) {
54+
emit(type: string, evt: EventHandler) {
4455
(all[type] || []).map((handler) => { handler(evt); });
4556
(all['*'] || []).map((handler) => { handler(type, evt); });
4657
}

yarn.lock

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ babel-plugin-transform-export-extensions@^6.3.13:
670670
babel-plugin-syntax-export-extensions "^6.8.0"
671671
babel-runtime "^6.0.0"
672672

673-
babel-plugin-transform-flow-strip-types@^6.3.13:
673+
babel-plugin-transform-flow-strip-types@^6.21.0, babel-plugin-transform-flow-strip-types@^6.3.13:
674674
version "6.21.0"
675675
resolved "https://registry.yarnpkg.com/babel-plugin-transform-flow-strip-types/-/babel-plugin-transform-flow-strip-types-6.21.0.tgz#2eea3f8b5bb234339b47283feac155cfb237b948"
676676
dependencies:
@@ -867,7 +867,7 @@ babelify@^7.2.0:
867867
babel-core "^6.0.14"
868868
object-assign "^4.0.0"
869869

870-
babylon@^6.11.0, babylon@^6.13.0, babylon@^6.5.2:
870+
babylon@^6.11.0, babylon@^6.13.0, babylon@^6.15.0, babylon@^6.5.2:
871871
version "6.15.0"
872872
resolved "https://registry.yarnpkg.com/babylon/-/babylon-6.15.0.tgz#ba65cfa1a80e1759b0e89fb562e27dccae70348e"
873873

@@ -1683,6 +1683,17 @@ flat-cache@^1.2.1:
16831683
graceful-fs "^4.1.2"
16841684
write "^0.2.1"
16851685

1686+
flow-bin@^0.38.0:
1687+
version "0.38.0"
1688+
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.38.0.tgz#3ae096d401c969cc8b5798253fb82381e2d0237a"
1689+
1690+
flow-remove-types@^1.1.0:
1691+
version "1.2.0"
1692+
resolved "https://registry.yarnpkg.com/flow-remove-types/-/flow-remove-types-1.2.0.tgz#c285516eabba72177a1b10bfb58f6ffb675a8877"
1693+
dependencies:
1694+
babylon "^6.15.0"
1695+
vlq "^0.2.1"
1696+
16861697
for-in@^0.1.5:
16871698
version "0.1.6"
16881699
resolved "https://registry.yarnpkg.com/for-in/-/for-in-0.1.6.tgz#c9f96e89bfad18a545af5ec3ed352a1d9e5b4dc8"
@@ -3365,7 +3376,14 @@ rollup-plugin-buble@^0.15.0:
33653376
buble "^0.15.0"
33663377
rollup-pluginutils "^1.5.0"
33673378

3368-
rollup-pluginutils@^1.5.0:
3379+
rollup-plugin-flow@^1.1.1:
3380+
version "1.1.1"
3381+
resolved "https://registry.yarnpkg.com/rollup-plugin-flow/-/rollup-plugin-flow-1.1.1.tgz#6ce568f1dd559666b77ab76b4bae251407528db6"
3382+
dependencies:
3383+
flow-remove-types "^1.1.0"
3384+
rollup-pluginutils "^1.5.1"
3385+
3386+
rollup-pluginutils@^1.5.0, rollup-pluginutils@^1.5.1:
33693387
version "1.5.2"
33703388
resolved "https://registry.yarnpkg.com/rollup-pluginutils/-/rollup-pluginutils-1.5.2.tgz#1e156e778f94b7255bfa1b3d0178be8f5c552408"
33713389
dependencies:

0 commit comments

Comments
 (0)