Skip to content

Commit

Permalink
Add generic type for mitt
Browse files Browse the repository at this point in the history
Co-Authored-By: Tim Suchanek <timsuchanek@users.noreply.github.com>
  • Loading branch information
timneutkens and timsuchanek committed Jul 29, 2020
1 parent c93b9b1 commit 0476b70
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 33 deletions.
13 changes: 4 additions & 9 deletions packages/next/client/router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* global window */
import React from 'react'
import Router, { NextRouter } from '../next-server/lib/router/router'
import Router, {
NextRouter,
RouterEvent,
} from '../next-server/lib/router/router'
import { RouterContext } from '../next-server/lib/router-context'

type ClassArguments<T> = T extends new (...args: infer U) => any ? U : any
Expand All @@ -13,14 +16,6 @@ type SingletonRouterBase = {
ready(cb: () => any): void
}

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

export { Router, NextRouter }

export type SingletonRouter = SingletonRouterBase & NextRouter
Expand Down
20 changes: 5 additions & 15 deletions packages/next/next-server/lib/mitt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,19 @@ 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 type MittEmitter = {
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)

export default function mitt<T extends string>() {
const all: Record<T, Handler[]> = Object.create(null)
return {
on(type: string, handler: Handler) {
on(type: T, handler: Handler) {
;(all[type] || (all[type] = [])).push(handler)
},

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

emit(type: string, ...evts: any[]) {
emit(type: T, ...evts: any[]) {
// eslint-disable-next-line array-callback-return
;(all[type] || []).slice().map((handler: Handler) => {
handler(...evts)
Expand Down
20 changes: 13 additions & 7 deletions packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { ParsedUrlQuery } from 'querystring'
import { ComponentType } from 'react'
import { UrlObject } from 'url'
import mitt, { MittEmitter } from '../mitt'
import mitt from '../mitt'
import {
AppContextType,
formatWithValidation,
Expand Down Expand Up @@ -108,6 +108,14 @@ export type NextRouter = BaseRouter &
| 'isFallback'
>

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

export type PrefetchOptions = {
priority?: boolean
}
Expand Down Expand Up @@ -189,13 +197,15 @@ export default class Router implements BaseRouter {
clc: ComponentLoadCancel
pageLoader: any
_bps: BeforePopStateCallback | undefined
events: MittEmitter
// Backwards compat for Router.router.events
// TODO: Should be remove the following major version as it was never documented
events = Router.events
_wrapApp: (App: ComponentType) => any
isSsr: boolean
isFallback: boolean
_inFlightRoute?: string

static events: MittEmitter = mitt()
static events = mitt<RouterEvent>()

constructor(
pathname: string,
Expand Down Expand Up @@ -241,10 +251,6 @@ export default class Router implements BaseRouter {

this.components['/_app'] = { Component: App }

// Backwards compat for Router.router.events
// TODO: Should be remove the following major version as it was never documented
this.events = Router.events

this.pageLoader = pageLoader
this.pathname = pathname
this.query = query
Expand Down
4 changes: 2 additions & 2 deletions packages/next/next-server/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { defaultHead } from '../lib/head'
import { HeadManagerContext } from '../lib/head-manager-context'
import Loadable from '../lib/loadable'
import { LoadableContext } from '../lib/loadable-context'
import mitt, { MittEmitter } from '../lib/mitt'
import mitt from '../lib/mitt'
import { RouterContext } from '../lib/router-context'
import { NextRouter } from '../lib/router/router'
import { isDynamicRoute } from '../lib/router/utils/is-dynamic'
Expand Down Expand Up @@ -61,7 +61,7 @@ class ServerRouter implements NextRouter {
events: any
isFallback: boolean
// TODO: Remove in the next major version, as this would mean the user is adding event listeners in server-side `render` method
static events: MittEmitter = mitt()
static events = mitt<string>()

constructor(
pathname: string,
Expand Down

0 comments on commit 0476b70

Please sign in to comment.