Skip to content

Commit

Permalink
Add generic for mitt event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
LauraBeatris committed Jul 30, 2020
1 parent 0476b70 commit 3483e1a
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/next/next-server/lib/mitt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
// See the LICENSE at the top of the file

type Handler = (...evts: any[]) => void
export default function mitt<T extends string>() {
const all: Record<T, Handler[]> = Object.create(null)
export default function mitt<T extends string, H extends Handler = Handler>() {
const all: Record<T, H[]> = Object.create(null)
return {
on(type: T, handler: Handler) {
on(type: T, handler: H) {
;(all[type] || (all[type] = [])).push(handler)
},
off(type: T, handler: Handler) {
off(type: T, handler: H) {
if (all[type]) {
// tslint:disable-next-line:no-bitwise
all[type].splice(all[type].indexOf(handler) >>> 0, 1)
}
},
emit(type: T, ...evts: any[]) {
// eslint-disable-next-line array-callback-return
;(all[type] || []).slice().map((handler: Handler) => {
;(all[type] || []).slice().map((handler: H) => {
handler(...evts)
})
},
Expand Down

0 comments on commit 3483e1a

Please sign in to comment.