Skip to content

Commit

Permalink
Move router event type to router
Browse files Browse the repository at this point in the history
  • Loading branch information
LauraBeatris committed Jul 26, 2020
1 parent ff4171c commit c93b9b1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
10 changes: 9 additions & 1 deletion packages/next/client/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ type SingletonRouterBase = {
ready(cb: () => any): void
}

type RouterEvent =
| 'routeChangeStart'
| 'beforeHistoryChange'
| 'routeChangeComplete'
| 'routeChangeError'
| 'hashChangeStart'
| 'hashChangeComplete'

export { Router, NextRouter }

export type SingletonRouter = SingletonRouterBase & NextRouter
Expand All @@ -38,7 +46,7 @@ const urlPropertyFields = [
'isFallback',
'basePath',
]
const routerEvents = [
const routerEvents: Array<RouterEvent> = [
'routeChangeStart',
'beforeHistoryChange',
'routeChangeComplete',
Expand Down
20 changes: 6 additions & 14 deletions packages/next/next-server/lib/mitt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,30 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
// It's been edited for the needs of this script
// See the LICENSE at the top of the file

type EventType =
| 'routeChangeStart'
| 'beforeHistoryChange'
| 'routeChangeComplete'
| 'routeChangeError'
| 'hashChangeStart'
| 'hashChangeComplete'

type Handler = (...evts: any[]) => void

export type MittEmitter = {
on(type: EventType | string, handler: Handler): void
off(type: EventType | string, handler: Handler): void
emit(type: EventType | string, ...evts: any[]): void
on(type: string, handler: Handler): void
off(type: string, handler: Handler): void
emit(type: string, ...evts: any[]): void
}

export default function mitt(): MittEmitter {
const all: { [s: string]: Handler[] } = Object.create(null)

return {
on(type: EventType | string, handler: Handler) {
on(type: string, handler: Handler) {
;(all[type] || (all[type] = [])).push(handler)
},

off(type: EventType | string, handler: Handler) {
off(type: string, handler: Handler) {
if (all[type]) {
// tslint:disable-next-line:no-bitwise
all[type].splice(all[type].indexOf(handler) >>> 0, 1)
}
},

emit(type: EventType | string, ...evts: any[]) {
emit(type: string, ...evts: any[]) {
// eslint-disable-next-line array-callback-return
;(all[type] || []).slice().map((handler: Handler) => {
handler(...evts)
Expand Down

0 comments on commit c93b9b1

Please sign in to comment.