Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use mitt for the internal RouterEvents API. #1398

Merged
merged 2 commits into from
Mar 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/on-demand-entries-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Router from '../lib/router'
import fetch from 'unfetch'

Router.ready(() => {
Router.router.on('routeChangeComplete', ping)
Router.router.events.on('routeChangeComplete', ping)
})

async function ping () {
Expand Down
2 changes: 1 addition & 1 deletion lib/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ coreMethodFields.forEach((field) => {

routerEvents.forEach((event) => {
SingletonRouter.ready(() => {
SingletonRouter.router.on(event, (...args) => {
SingletonRouter.router.events.on(event, (...args) => {
const eventField = `on${event.charAt(0).toUpperCase()}${event.substring(1)}`
if (SingletonRouter[eventField]) {
SingletonRouter[eventField](...args)
Expand Down
24 changes: 13 additions & 11 deletions lib/router/router.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parse, format } from 'url'
import { EventEmitter } from 'events'
import mitt from 'mitt'
import fetch from 'unfetch'
import evalScript from '../eval-script'
import shallowEquals from '../shallow-equals'
Expand All @@ -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 } = {}) {
super()
// represents the current component key
this.route = toRoute(pathname)

Expand All @@ -37,6 +36,9 @@ export default class Router extends EventEmitter {
// contain a map of promise of fetch routes
this.fetchingRoutes = {}

// Handling Router Events
this.events = mitt()

this.prefetchQueue = new PQueue({ concurrency: 2 })
this.ErrorComponent = ErrorComponent
this.pathname = pathname
Expand Down Expand Up @@ -98,7 +100,7 @@ export default class Router extends EventEmitter {
const url = window.location.href
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 { error } = routeInfo

Expand All @@ -109,11 +111,11 @@ export default class Router extends EventEmitter {
this.notify(routeInfo)

if (error) {
this.emit('routeChangeError', error, url)
this.events.emit('routeChangeError', error, url)
throw error
}

this.emit('routeChangeComplete', url)
this.events.emit('routeChangeComplete', url)
}

back () {
Expand Down Expand Up @@ -156,7 +158,7 @@ export default class Router extends EventEmitter {
const { shallow = false } = options
let routeInfo = null

this.emit('routeChangeStart', as)
this.events.emit('routeChangeStart', as)

// If shallow === false and other conditions met, we reuse the
// existing routeInfo for this route.
Expand All @@ -173,18 +175,18 @@ export default class Router extends EventEmitter {
return false
}

this.emit('beforeHistoryChange', as)
this.events.emit('beforeHistoryChange', as)
this.changeState(method, url, as, options)
const hash = window.location.hash.substring(1)

this.set(route, pathname, query, as, { ...routeInfo, hash })

if (error) {
this.emit('routeChangeError', error, as)
this.events.emit('routeChangeError', error, as)
throw error
}

this.emit('routeChangeComplete', as)
this.events.emit('routeChangeComplete', as)
return true
}

Expand Down Expand Up @@ -356,7 +358,7 @@ export default class Router extends EventEmitter {

abortComponentLoad (as) {
if (this.componentLoadCancel) {
this.emit('routeChangeError', new Error('Route Cancelled'), as)
this.events.emit('routeChangeError', new Error('Route Cancelled'), as)
this.componentLoadCancel()
this.componentLoadCancel = null
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"json-loader": "0.5.4",
"loader-utils": "1.0.3",
"minimist": "1.2.0",
"mitt": "1.1.0",
"mkdirp-then": "1.2.0",
"mv": "2.1.1",
"mz": "2.6.0",
Expand Down
77 changes: 20 additions & 57 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1213,15 +1213,14 @@ chokidar@^1.4.3, chokidar@^1.6.1:
optionalDependencies:
fsevents "^1.0.0"

chromedriver@2.26.1:
version "2.26.1"
resolved "https://registry.npmjs.org/chromedriver/-/chromedriver-2.26.1.tgz#60036732224580d2699afd2626f5f26ea1f6c9fe"
chromedriver@2.28.0:
version "2.28.0"
resolved "https://registry.yarnpkg.com/chromedriver/-/chromedriver-2.28.0.tgz#ea0c383621dd27db340c612b85fe39414c16ec79"
dependencies:
adm-zip "^0.4.7"
kew "^0.5.0"
mkdirp "^0.5.0"
npmconf "^2.1.1"
rimraf "^2.0.3"
kew "^0.7.0"
mkdirp "^0.5.1"
rimraf "^2.5.4"

ci-info@^1.0.0:
version "1.0.0"
Expand Down Expand Up @@ -1322,13 +1321,6 @@ concat-stream@^1.4.6:
readable-stream "^2.2.2"
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:
version "1.1.0"
resolved "https://registry.npmjs.org/console-browserify/-/console-browserify-1.1.0.tgz#f0241c45730a9fc6323b206dbf38edc741d0bb10"
Expand Down Expand Up @@ -2504,7 +2496,7 @@ [email protected]:
version "2.0.1"
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"
resolved "https://registry.npmjs.org/ini/-/ini-1.3.4.tgz#0537cb79daf59b59a1a517dff706c86ec039162e"

Expand Down Expand Up @@ -3082,9 +3074,9 @@ jsx-ast-utils@^1.3.4:
dependencies:
object-assign "^4.1.0"

kew@^0.5.0:
version "0.5.0"
resolved "https://registry.npmjs.org/kew/-/kew-0.5.0.tgz#ece11cb5d8d01a81f8ce804c8d0bba06e6b25ca2"
kew@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/kew/-/kew-0.7.0.tgz#79d93d2d33363d6fdd2970b335d9141ad591d79b"

kind-of@^3.0.2:
version "3.1.0"
Expand Down Expand Up @@ -3351,6 +3343,10 @@ [email protected], minimist@^1.1.0, minimist@^1.1.1, minimist@^1.2.0:
version "1.2.0"
resolved "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"

[email protected]:
version "1.1.0"
resolved "https://registry.yarnpkg.com/mitt/-/mitt-1.1.0.tgz#22f0d57e2fedd39620a62bb41b7cdd93667d3c41"

[email protected]:
version "1.2.0"
resolved "https://registry.npmjs.org/mkdirp-then/-/mkdirp-then-1.2.0.tgz#a492c879ca4d873f5ee45008f8f55fd0150de3c5"
Expand Down Expand Up @@ -3476,7 +3472,7 @@ nopt@~1.0.10:
dependencies:
abbrev "1"

nopt@~3.0.1, nopt@~3.0.6:
nopt@~3.0.6:
version "3.0.6"
resolved "https://registry.npmjs.org/nopt/-/nopt-3.0.6.tgz#c6465dbf08abcd4db359317f79ac68a646b28ff9"
dependencies:
Expand All @@ -3499,20 +3495,6 @@ normalize-path@^2.0.0, normalize-path@^2.0.1:
version "2.0.1"
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:
version "4.0.2"
resolved "https://registry.npmjs.org/npmlog/-/npmlog-4.0.2.tgz#d03950e0e78ce1527ba26d2a7592e9348ac3e75f"
Expand Down Expand Up @@ -3650,17 +3632,10 @@ os-locale@^1.4.0:
dependencies:
lcid "^1.0.0"

os-tmpdir@^1.0.0, os-tmpdir@^1.0.1:
os-tmpdir@^1.0.1:
version "1.0.2"
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:
version "1.1.0"
resolved "https://registry.npmjs.org/p-limit/-/p-limit-1.1.0.tgz#b07ff2d9a5d88bec806035895a2bab66a27988bc"
Expand Down Expand Up @@ -3846,10 +3821,6 @@ promise@^7.1.1:
dependencies:
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:
version "0.0.0"
resolved "https://registry.npmjs.org/prr/-/prr-0.0.0.tgz#1a84b85908325501411853d0081ee3fa86e2926a"
Expand Down Expand Up @@ -4152,7 +4123,7 @@ right-align@^0.1.1:
dependencies:
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"
resolved "https://registry.npmjs.org/rimraf/-/rimraf-2.6.1.tgz#c2338ec643df7a1b7fe5c54fa86f57428a55f33d"
dependencies:
Expand Down Expand Up @@ -4204,10 +4175,6 @@ sax@^1.2.1:
version "1.2.2"
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:
version "5.3.0"
resolved "https://registry.npmjs.org/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f"
Expand Down Expand Up @@ -4670,10 +4637,6 @@ uglify-to-browserify@~1.0.0:
version "1.0.2"
resolved "https://registry.npmjs.org/uglify-to-browserify/-/uglify-to-browserify-1.0.2.tgz#6e0924d6bda6b5afe349e39a6d632850a0f882b7"

[email protected]:
version "0.0.5"
resolved "https://registry.npmjs.org/uid-number/-/uid-number-0.0.5.tgz#5a3db23ef5dbd55b81fce0ec9a2ac6fccdebb81e"

uid-number@~0.0.6:
version "0.0.6"
resolved "https://registry.npmjs.org/uid-number/-/uid-number-0.0.6.tgz#0ea10e8035e8eb5b8e4449f06da1c730663baa81"
Expand Down Expand Up @@ -4907,9 +4870,9 @@ write-file-atomic@^1.1.4:
imurmurhash "^0.1.4"
slide "^1.1.5"

write-file-webpack-plugin@3.4.2:
version "3.4.2"
resolved "https://registry.npmjs.org/write-file-webpack-plugin/-/write-file-webpack-plugin-3.4.2.tgz#16f7a1bbadb781fa661a2960e31c499f0a61b9bb"
write-file-webpack-plugin@4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/write-file-webpack-plugin/-/write-file-webpack-plugin-4.0.0.tgz#2a7e4520fdcc02e687e8430d371bb41400b3cc0c"
dependencies:
chalk "^1.1.1"
filesize "^3.2.1"
Expand Down