Comparing version 1.0.1 to 1.1.0
{ | ||
"name": "hotloader", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "simple lib for fast html hotloading", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -0,1 +1,3 @@ | ||
/* global HOTLOADER_CLIENT_ID location */ | ||
const virtualize = require('snabbdom-virtualize').default | ||
@@ -12,4 +14,2 @@ const patch = require('snabbdom').init([ | ||
/* global HOTLOADER_CLIENT_ID location */ | ||
const findBody = htmlVNode => { | ||
@@ -21,2 +21,13 @@ const { children } = htmlVNode | ||
const handleError = html => (msg, e) => { | ||
console.error([ | ||
msg, | ||
'Rebuilding entire page', | ||
'Error message: ' + e.message | ||
].join('\n')) | ||
document.documentElement.innerHTML = html | ||
document.getElementsByTagName('body')[0].focus() | ||
} | ||
const hotloader = () => { | ||
@@ -34,12 +45,22 @@ console.log('Starting hotloader') | ||
const recieveUpdate = html => { | ||
if (!html) throw new Error('got empty html!') | ||
const newVNode = findBody(virtualize(html)) | ||
if (!html) return | ||
if (newVNode) { | ||
vNode = patch(vNode, virtualize(html)) | ||
const fallback = handleError(html) | ||
// hack for chrome. | ||
// changes to tag class were not taking effect until mouse over | ||
document.getElementsByTagName('body')[0].focus() | ||
let newVNode | ||
try { | ||
newVNode = virtualize(html) | ||
} catch (e) { | ||
return fallback('Failed to virtualize incoming html.', e) | ||
} | ||
try { | ||
vNode = patch(vNode, findBody(newVNode)) | ||
} catch (e) { | ||
return fallback('Patching DOM failed.', e) | ||
} | ||
// hack for chrome, force repaint on unfocused window. | ||
document.getElementsByTagName('body')[0].focus() | ||
} | ||
@@ -46,0 +67,0 @@ self.recieveUpdate = recieveUpdate |
Sorry, the diff of this file is too big to display
203962
6811