Skip to content

Commit 938c0a9

Browse files
committed
preload if good connection
1 parent 40285d2 commit 938c0a9

File tree

4 files changed

+61
-14
lines changed

4 files changed

+61
-14
lines changed

src/Lifecycle.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
import { h } from 'hyperapp'
3+
4+
/**
5+
* Simple helper to emulate Hyperapp#2 lifecycle events
6+
* Thanks @sergey-shpak for this gist!
7+
* https://gist.github.com/sergey-shpak/ff87e56e03dc16c8ee24dc562c6f7dff
8+
*
9+
*/
10+
11+
const dispatchEvent = (event, target) =>
12+
setTimeout(()=> target.dispatchEvent(
13+
new CustomEvent(event, { detail: target })
14+
))
15+
16+
const defineElement = name => {
17+
customElements.get(name) ||
18+
customElements.define(name, class extends HTMLElement {
19+
appendChild(child){
20+
super.appendChild(child)
21+
dispatchEvent('create', child)
22+
return child
23+
}
24+
removeChild(child){
25+
super.removeChild(child)
26+
dispatchEvent('remove', child)
27+
return child
28+
}
29+
})
30+
}
31+
32+
export const Lifecycle = (props, child) =>
33+
(defineElement('ha-lifecycle'), h('ha-lifecycle', props, [child]))
34+

src/Link.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import { h } from 'hyperapp'
2-
3-
import { Navigate, TriggerRouteLoad } from './actions'
2+
import {Lifecycle} from './Lifecycle'
3+
import { Navigate, TriggerRouteLoad, TriggerRouteLoadIfGoodConnection } from './actions'
44

55
// Link component
66
export const Link = ({ to, ...props }, children) => {
7-
return h('a', {
8-
href: to,
9-
onclick: [
10-
state => state,
11-
ev => {
12-
ev.preventDefault()
13-
}
14-
],
15-
onmousedown: [Navigate, to],
16-
onmouseover: [TriggerRouteLoad, to],
17-
...props
18-
}, children)
7+
return Lifecycle({}, [
8+
h('a', {
9+
href: to,
10+
onclick: [
11+
state => state,
12+
ev => {
13+
ev.preventDefault()
14+
}
15+
],
16+
onmousedown: [Navigate, to],
17+
onmouseover: [TriggerRouteLoad, to],
18+
oncreate: [TriggerRouteLoadIfGoodConnection, to],
19+
...props
20+
}, children)
21+
])
1922
}

src/actions.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ export const Navigate = (state, to) => [
3939
ChangeLocation({ to })
4040
]
4141

42+
43+
export const TriggerRouteLoadIfGoodConnection = (state, path) => {
44+
if (state.goodConnection) {
45+
return TriggerRouteLoad(state, path)
46+
}
47+
48+
return state
49+
}
50+
4251
export const TriggerRouteLoad = (state, path) => {
4352
const routes = Object.keys(state.routes).map(route => state.routes[route])
4453
const matchedRoute = routes.find(route => route.pattern.match(path))

src/getInitialState.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const buildRoutesObject = (routes) => Object.keys(routes).reduce((routesObj, rou
1515

1616
export const getInitialState = (routes, extraInit) => {
1717
const init = {
18+
goodConnection: navigator.connection.downlink > 2, // TODO: use something more reliable, maybe an effect
1819
location: {
1920
path: '/',
2021
params: {},

0 commit comments

Comments
 (0)