mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Use mitt instead of EventEmitter. (#1398)
EventEmitter is quite bit and mitt is a pretty good/small replacement.
This commit is contained in:
parent
bb7c8f87c6
commit
57099a544c
|
@ -4,7 +4,7 @@ import Router from '../lib/router'
|
||||||
import fetch from 'unfetch'
|
import fetch from 'unfetch'
|
||||||
|
|
||||||
Router.ready(() => {
|
Router.ready(() => {
|
||||||
Router.router.on('routeChangeComplete', ping)
|
Router.router.events.on('routeChangeComplete', ping)
|
||||||
})
|
})
|
||||||
|
|
||||||
async function ping () {
|
async function ping () {
|
||||||
|
|
|
@ -39,7 +39,7 @@ coreMethodFields.forEach((field) => {
|
||||||
|
|
||||||
routerEvents.forEach((event) => {
|
routerEvents.forEach((event) => {
|
||||||
SingletonRouter.ready(() => {
|
SingletonRouter.ready(() => {
|
||||||
SingletonRouter.router.on(event, (...args) => {
|
SingletonRouter.router.events.on(event, (...args) => {
|
||||||
const eventField = `on${event.charAt(0).toUpperCase()}${event.substring(1)}`
|
const eventField = `on${event.charAt(0).toUpperCase()}${event.substring(1)}`
|
||||||
if (SingletonRouter[eventField]) {
|
if (SingletonRouter[eventField]) {
|
||||||
SingletonRouter[eventField](...args)
|
SingletonRouter[eventField](...args)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { parse, format } from 'url'
|
import { parse, format } from 'url'
|
||||||
import { EventEmitter } from 'events'
|
import mitt from 'mitt'
|
||||||
import fetch from 'unfetch'
|
import fetch from 'unfetch'
|
||||||
import evalScript from '../eval-script'
|
import evalScript from '../eval-script'
|
||||||
import shallowEquals from '../shallow-equals'
|
import shallowEquals from '../shallow-equals'
|
||||||
|
@ -25,9 +25,8 @@ if (typeof window !== 'undefined' && typeof navigator.serviceWorker !== 'undefin
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class Router extends EventEmitter {
|
export default class Router {
|
||||||
constructor (pathname, query, as, { Component, ErrorComponent, err } = {}) {
|
constructor (pathname, query, as, { Component, ErrorComponent, err } = {}) {
|
||||||
super()
|
|
||||||
// represents the current component key
|
// represents the current component key
|
||||||
this.route = toRoute(pathname)
|
this.route = toRoute(pathname)
|
||||||
|
|
||||||
|
@ -37,6 +36,9 @@ export default class Router extends EventEmitter {
|
||||||
// contain a map of promise of fetch routes
|
// contain a map of promise of fetch routes
|
||||||
this.fetchingRoutes = {}
|
this.fetchingRoutes = {}
|
||||||
|
|
||||||
|
// Handling Router Events
|
||||||
|
this.events = mitt()
|
||||||
|
|
||||||
this.prefetchQueue = new PQueue({ concurrency: 2 })
|
this.prefetchQueue = new PQueue({ concurrency: 2 })
|
||||||
this.ErrorComponent = ErrorComponent
|
this.ErrorComponent = ErrorComponent
|
||||||
this.pathname = pathname
|
this.pathname = pathname
|
||||||
|
@ -98,7 +100,7 @@ export default class Router extends EventEmitter {
|
||||||
const url = window.location.href
|
const url = window.location.href
|
||||||
const { pathname, query } = parse(url, true)
|
const { pathname, query } = parse(url, true)
|
||||||
|
|
||||||
this.emit('routeChangeStart', url)
|
this.events.emit('routeChangeStart', url)
|
||||||
const routeInfo = await this.getRouteInfo(route, pathname, query, url)
|
const routeInfo = await this.getRouteInfo(route, pathname, query, url)
|
||||||
const { error } = routeInfo
|
const { error } = routeInfo
|
||||||
|
|
||||||
|
@ -109,11 +111,11 @@ export default class Router extends EventEmitter {
|
||||||
this.notify(routeInfo)
|
this.notify(routeInfo)
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
this.emit('routeChangeError', error, url)
|
this.events.emit('routeChangeError', error, url)
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
|
|
||||||
this.emit('routeChangeComplete', url)
|
this.events.emit('routeChangeComplete', url)
|
||||||
}
|
}
|
||||||
|
|
||||||
back () {
|
back () {
|
||||||
|
@ -156,7 +158,7 @@ export default class Router extends EventEmitter {
|
||||||
const { shallow = false } = options
|
const { shallow = false } = options
|
||||||
let routeInfo = null
|
let routeInfo = null
|
||||||
|
|
||||||
this.emit('routeChangeStart', as)
|
this.events.emit('routeChangeStart', as)
|
||||||
|
|
||||||
// If shallow === false and other conditions met, we reuse the
|
// If shallow === false and other conditions met, we reuse the
|
||||||
// existing routeInfo for this route.
|
// existing routeInfo for this route.
|
||||||
|
@ -173,18 +175,18 @@ export default class Router extends EventEmitter {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
this.emit('beforeHistoryChange', as)
|
this.events.emit('beforeHistoryChange', as)
|
||||||
this.changeState(method, url, as, options)
|
this.changeState(method, url, as, options)
|
||||||
const hash = window.location.hash.substring(1)
|
const hash = window.location.hash.substring(1)
|
||||||
|
|
||||||
this.set(route, pathname, query, as, { ...routeInfo, hash })
|
this.set(route, pathname, query, as, { ...routeInfo, hash })
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
this.emit('routeChangeError', error, as)
|
this.events.emit('routeChangeError', error, as)
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
|
|
||||||
this.emit('routeChangeComplete', as)
|
this.events.emit('routeChangeComplete', as)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -356,7 +358,7 @@ export default class Router extends EventEmitter {
|
||||||
|
|
||||||
abortComponentLoad (as) {
|
abortComponentLoad (as) {
|
||||||
if (this.componentLoadCancel) {
|
if (this.componentLoadCancel) {
|
||||||
this.emit('routeChangeError', new Error('Route Cancelled'), as)
|
this.events.emit('routeChangeError', new Error('Route Cancelled'), as)
|
||||||
this.componentLoadCancel()
|
this.componentLoadCancel()
|
||||||
this.componentLoadCancel = null
|
this.componentLoadCancel = null
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,6 +68,7 @@
|
||||||
"json-loader": "0.5.4",
|
"json-loader": "0.5.4",
|
||||||
"loader-utils": "1.0.3",
|
"loader-utils": "1.0.3",
|
||||||
"minimist": "1.2.0",
|
"minimist": "1.2.0",
|
||||||
|
"mitt": "1.1.0",
|
||||||
"mkdirp-then": "1.2.0",
|
"mkdirp-then": "1.2.0",
|
||||||
"mv": "2.1.1",
|
"mv": "2.1.1",
|
||||||
"mz": "2.6.0",
|
"mz": "2.6.0",
|
||||||
|
|
77
yarn.lock
77
yarn.lock
|
@ -1213,15 +1213,14 @@ chokidar@^1.4.3, chokidar@^1.6.1:
|
||||||
optionalDependencies:
|
optionalDependencies:
|
||||||
fsevents "^1.0.0"
|
fsevents "^1.0.0"
|
||||||
|
|
||||||
chromedriver@2.26.1:
|
chromedriver@2.28.0:
|
||||||
version "2.26.1"
|
version "2.28.0"
|
||||||
resolved "https://registry.npmjs.org/chromedriver/-/chromedriver-2.26.1.tgz#60036732224580d2699afd2626f5f26ea1f6c9fe"
|
resolved "https://registry.yarnpkg.com/chromedriver/-/chromedriver-2.28.0.tgz#ea0c383621dd27db340c612b85fe39414c16ec79"
|
||||||
dependencies:
|
dependencies:
|
||||||
adm-zip "^0.4.7"
|
adm-zip "^0.4.7"
|
||||||
kew "^0.5.0"
|
kew "^0.7.0"
|
||||||
mkdirp "^0.5.0"
|
mkdirp "^0.5.1"
|
||||||
npmconf "^2.1.1"
|
rimraf "^2.5.4"
|
||||||
rimraf "^2.0.3"
|
|
||||||
|
|
||||||
ci-info@^1.0.0:
|
ci-info@^1.0.0:
|
||||||
version "1.0.0"
|
version "1.0.0"
|
||||||
|
@ -1322,13 +1321,6 @@ concat-stream@^1.4.6:
|
||||||
readable-stream "^2.2.2"
|
readable-stream "^2.2.2"
|
||||||
typedarray "^0.0.6"
|
typedarray "^0.0.6"
|
||||||
|
|
||||||
config-chain@~1.1.8:
|
|
||||||
version "1.1.11"
|
|
||||||
resolved "https://registry.npmjs.org/config-chain/-/config-chain-1.1.11.tgz#aba09747dfbe4c3e70e766a6e41586e1859fc6f2"
|
|
||||||
dependencies:
|
|
||||||
ini "^1.3.4"
|
|
||||||
proto-list "~1.2.1"
|
|
||||||
|
|
||||||
console-browserify@^1.1.0:
|
console-browserify@^1.1.0:
|
||||||
version "1.1.0"
|
version "1.1.0"
|
||||||
resolved "https://registry.npmjs.org/console-browserify/-/console-browserify-1.1.0.tgz#f0241c45730a9fc6323b206dbf38edc741d0bb10"
|
resolved "https://registry.npmjs.org/console-browserify/-/console-browserify-1.1.0.tgz#f0241c45730a9fc6323b206dbf38edc741d0bb10"
|
||||||
|
@ -2504,7 +2496,7 @@ inherits@2.0.1:
|
||||||
version "2.0.1"
|
version "2.0.1"
|
||||||
resolved "https://registry.npmjs.org/inherits/-/inherits-2.0.1.tgz#b17d08d326b4423e568eff719f91b0b1cbdf69f1"
|
resolved "https://registry.npmjs.org/inherits/-/inherits-2.0.1.tgz#b17d08d326b4423e568eff719f91b0b1cbdf69f1"
|
||||||
|
|
||||||
ini@^1.2.0, ini@^1.3.4, ini@~1.3.0:
|
ini@~1.3.0:
|
||||||
version "1.3.4"
|
version "1.3.4"
|
||||||
resolved "https://registry.npmjs.org/ini/-/ini-1.3.4.tgz#0537cb79daf59b59a1a517dff706c86ec039162e"
|
resolved "https://registry.npmjs.org/ini/-/ini-1.3.4.tgz#0537cb79daf59b59a1a517dff706c86ec039162e"
|
||||||
|
|
||||||
|
@ -3082,9 +3074,9 @@ jsx-ast-utils@^1.3.4:
|
||||||
dependencies:
|
dependencies:
|
||||||
object-assign "^4.1.0"
|
object-assign "^4.1.0"
|
||||||
|
|
||||||
kew@^0.5.0:
|
kew@^0.7.0:
|
||||||
version "0.5.0"
|
version "0.7.0"
|
||||||
resolved "https://registry.npmjs.org/kew/-/kew-0.5.0.tgz#ece11cb5d8d01a81f8ce804c8d0bba06e6b25ca2"
|
resolved "https://registry.yarnpkg.com/kew/-/kew-0.7.0.tgz#79d93d2d33363d6fdd2970b335d9141ad591d79b"
|
||||||
|
|
||||||
kind-of@^3.0.2:
|
kind-of@^3.0.2:
|
||||||
version "3.1.0"
|
version "3.1.0"
|
||||||
|
@ -3351,6 +3343,10 @@ minimist@1.2.0, minimist@^1.1.0, minimist@^1.1.1, minimist@^1.2.0:
|
||||||
version "1.2.0"
|
version "1.2.0"
|
||||||
resolved "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"
|
resolved "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"
|
||||||
|
|
||||||
|
mitt@1.1.0:
|
||||||
|
version "1.1.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/mitt/-/mitt-1.1.0.tgz#22f0d57e2fedd39620a62bb41b7cdd93667d3c41"
|
||||||
|
|
||||||
mkdirp-then@1.2.0:
|
mkdirp-then@1.2.0:
|
||||||
version "1.2.0"
|
version "1.2.0"
|
||||||
resolved "https://registry.npmjs.org/mkdirp-then/-/mkdirp-then-1.2.0.tgz#a492c879ca4d873f5ee45008f8f55fd0150de3c5"
|
resolved "https://registry.npmjs.org/mkdirp-then/-/mkdirp-then-1.2.0.tgz#a492c879ca4d873f5ee45008f8f55fd0150de3c5"
|
||||||
|
@ -3476,7 +3472,7 @@ nopt@~1.0.10:
|
||||||
dependencies:
|
dependencies:
|
||||||
abbrev "1"
|
abbrev "1"
|
||||||
|
|
||||||
nopt@~3.0.1, nopt@~3.0.6:
|
nopt@~3.0.6:
|
||||||
version "3.0.6"
|
version "3.0.6"
|
||||||
resolved "https://registry.npmjs.org/nopt/-/nopt-3.0.6.tgz#c6465dbf08abcd4db359317f79ac68a646b28ff9"
|
resolved "https://registry.npmjs.org/nopt/-/nopt-3.0.6.tgz#c6465dbf08abcd4db359317f79ac68a646b28ff9"
|
||||||
dependencies:
|
dependencies:
|
||||||
|
@ -3499,20 +3495,6 @@ normalize-path@^2.0.0, normalize-path@^2.0.1:
|
||||||
version "2.0.1"
|
version "2.0.1"
|
||||||
resolved "https://registry.npmjs.org/normalize-path/-/normalize-path-2.0.1.tgz#47886ac1662760d4261b7d979d241709d3ce3f7a"
|
resolved "https://registry.npmjs.org/normalize-path/-/normalize-path-2.0.1.tgz#47886ac1662760d4261b7d979d241709d3ce3f7a"
|
||||||
|
|
||||||
npmconf@^2.1.1:
|
|
||||||
version "2.1.2"
|
|
||||||
resolved "https://registry.npmjs.org/npmconf/-/npmconf-2.1.2.tgz#66606a4a736f1e77a059aa071a79c94ab781853a"
|
|
||||||
dependencies:
|
|
||||||
config-chain "~1.1.8"
|
|
||||||
inherits "~2.0.0"
|
|
||||||
ini "^1.2.0"
|
|
||||||
mkdirp "^0.5.0"
|
|
||||||
nopt "~3.0.1"
|
|
||||||
once "~1.3.0"
|
|
||||||
osenv "^0.1.0"
|
|
||||||
semver "2 || 3 || 4"
|
|
||||||
uid-number "0.0.5"
|
|
||||||
|
|
||||||
npmlog@^4.0.1:
|
npmlog@^4.0.1:
|
||||||
version "4.0.2"
|
version "4.0.2"
|
||||||
resolved "https://registry.npmjs.org/npmlog/-/npmlog-4.0.2.tgz#d03950e0e78ce1527ba26d2a7592e9348ac3e75f"
|
resolved "https://registry.npmjs.org/npmlog/-/npmlog-4.0.2.tgz#d03950e0e78ce1527ba26d2a7592e9348ac3e75f"
|
||||||
|
@ -3650,17 +3632,10 @@ os-locale@^1.4.0:
|
||||||
dependencies:
|
dependencies:
|
||||||
lcid "^1.0.0"
|
lcid "^1.0.0"
|
||||||
|
|
||||||
os-tmpdir@^1.0.0, os-tmpdir@^1.0.1:
|
os-tmpdir@^1.0.1:
|
||||||
version "1.0.2"
|
version "1.0.2"
|
||||||
resolved "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274"
|
resolved "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274"
|
||||||
|
|
||||||
osenv@^0.1.0:
|
|
||||||
version "0.1.4"
|
|
||||||
resolved "https://registry.npmjs.org/osenv/-/osenv-0.1.4.tgz#42fe6d5953df06c8064be6f176c3d05aaaa34644"
|
|
||||||
dependencies:
|
|
||||||
os-homedir "^1.0.0"
|
|
||||||
os-tmpdir "^1.0.0"
|
|
||||||
|
|
||||||
p-limit@^1.1.0:
|
p-limit@^1.1.0:
|
||||||
version "1.1.0"
|
version "1.1.0"
|
||||||
resolved "https://registry.npmjs.org/p-limit/-/p-limit-1.1.0.tgz#b07ff2d9a5d88bec806035895a2bab66a27988bc"
|
resolved "https://registry.npmjs.org/p-limit/-/p-limit-1.1.0.tgz#b07ff2d9a5d88bec806035895a2bab66a27988bc"
|
||||||
|
@ -3846,10 +3821,6 @@ promise@^7.1.1:
|
||||||
dependencies:
|
dependencies:
|
||||||
asap "~2.0.3"
|
asap "~2.0.3"
|
||||||
|
|
||||||
proto-list@~1.2.1:
|
|
||||||
version "1.2.4"
|
|
||||||
resolved "https://registry.npmjs.org/proto-list/-/proto-list-1.2.4.tgz#212d5bfe1318306a420f6402b8e26ff39647a849"
|
|
||||||
|
|
||||||
prr@~0.0.0:
|
prr@~0.0.0:
|
||||||
version "0.0.0"
|
version "0.0.0"
|
||||||
resolved "https://registry.npmjs.org/prr/-/prr-0.0.0.tgz#1a84b85908325501411853d0081ee3fa86e2926a"
|
resolved "https://registry.npmjs.org/prr/-/prr-0.0.0.tgz#1a84b85908325501411853d0081ee3fa86e2926a"
|
||||||
|
@ -4152,7 +4123,7 @@ right-align@^0.1.1:
|
||||||
dependencies:
|
dependencies:
|
||||||
align-text "^0.1.1"
|
align-text "^0.1.1"
|
||||||
|
|
||||||
rimraf@2, rimraf@^2.0.3, rimraf@^2.2.8, rimraf@^2.3.3, rimraf@^2.4.3, rimraf@^2.4.4, rimraf@^2.5.4:
|
rimraf@2, rimraf@^2.2.8, rimraf@^2.3.3, rimraf@^2.4.3, rimraf@^2.4.4, rimraf@^2.5.4:
|
||||||
version "2.6.1"
|
version "2.6.1"
|
||||||
resolved "https://registry.npmjs.org/rimraf/-/rimraf-2.6.1.tgz#c2338ec643df7a1b7fe5c54fa86f57428a55f33d"
|
resolved "https://registry.npmjs.org/rimraf/-/rimraf-2.6.1.tgz#c2338ec643df7a1b7fe5c54fa86f57428a55f33d"
|
||||||
dependencies:
|
dependencies:
|
||||||
|
@ -4204,10 +4175,6 @@ sax@^1.2.1:
|
||||||
version "1.2.2"
|
version "1.2.2"
|
||||||
resolved "https://registry.npmjs.org/sax/-/sax-1.2.2.tgz#fd8631a23bc7826bef5d871bdb87378c95647828"
|
resolved "https://registry.npmjs.org/sax/-/sax-1.2.2.tgz#fd8631a23bc7826bef5d871bdb87378c95647828"
|
||||||
|
|
||||||
"semver@2 || 3 || 4":
|
|
||||||
version "4.3.6"
|
|
||||||
resolved "https://registry.npmjs.org/semver/-/semver-4.3.6.tgz#300bc6e0e86374f7ba61068b5b1ecd57fc6532da"
|
|
||||||
|
|
||||||
"semver@2 || 3 || 4 || 5", semver@^5.3.0, semver@~5.3.0:
|
"semver@2 || 3 || 4 || 5", semver@^5.3.0, semver@~5.3.0:
|
||||||
version "5.3.0"
|
version "5.3.0"
|
||||||
resolved "https://registry.npmjs.org/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f"
|
resolved "https://registry.npmjs.org/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f"
|
||||||
|
@ -4670,10 +4637,6 @@ uglify-to-browserify@~1.0.0:
|
||||||
version "1.0.2"
|
version "1.0.2"
|
||||||
resolved "https://registry.npmjs.org/uglify-to-browserify/-/uglify-to-browserify-1.0.2.tgz#6e0924d6bda6b5afe349e39a6d632850a0f882b7"
|
resolved "https://registry.npmjs.org/uglify-to-browserify/-/uglify-to-browserify-1.0.2.tgz#6e0924d6bda6b5afe349e39a6d632850a0f882b7"
|
||||||
|
|
||||||
uid-number@0.0.5:
|
|
||||||
version "0.0.5"
|
|
||||||
resolved "https://registry.npmjs.org/uid-number/-/uid-number-0.0.5.tgz#5a3db23ef5dbd55b81fce0ec9a2ac6fccdebb81e"
|
|
||||||
|
|
||||||
uid-number@~0.0.6:
|
uid-number@~0.0.6:
|
||||||
version "0.0.6"
|
version "0.0.6"
|
||||||
resolved "https://registry.npmjs.org/uid-number/-/uid-number-0.0.6.tgz#0ea10e8035e8eb5b8e4449f06da1c730663baa81"
|
resolved "https://registry.npmjs.org/uid-number/-/uid-number-0.0.6.tgz#0ea10e8035e8eb5b8e4449f06da1c730663baa81"
|
||||||
|
@ -4907,9 +4870,9 @@ write-file-atomic@^1.1.4:
|
||||||
imurmurhash "^0.1.4"
|
imurmurhash "^0.1.4"
|
||||||
slide "^1.1.5"
|
slide "^1.1.5"
|
||||||
|
|
||||||
write-file-webpack-plugin@3.4.2:
|
write-file-webpack-plugin@4.0.0:
|
||||||
version "3.4.2"
|
version "4.0.0"
|
||||||
resolved "https://registry.npmjs.org/write-file-webpack-plugin/-/write-file-webpack-plugin-3.4.2.tgz#16f7a1bbadb781fa661a2960e31c499f0a61b9bb"
|
resolved "https://registry.yarnpkg.com/write-file-webpack-plugin/-/write-file-webpack-plugin-4.0.0.tgz#2a7e4520fdcc02e687e8430d371bb41400b3cc0c"
|
||||||
dependencies:
|
dependencies:
|
||||||
chalk "^1.1.1"
|
chalk "^1.1.1"
|
||||||
filesize "^3.2.1"
|
filesize "^3.2.1"
|
||||||
|
|
Loading…
Reference in a new issue