Skip to content

Commit

Permalink
Merge pull request #129 from lorengreenfield/fix-nextTick
Browse files Browse the repository at this point in the history
Fixing nextTick not running (shouldn't be the same as waiting and merging state, so separated them)
  • Loading branch information
lorengreenfield authored Jun 7, 2018
2 parents 57d0466 + bf18145 commit 6085bb3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
14 changes: 11 additions & 3 deletions halfcab.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,24 @@ function resetTouched (holidingPen) {

let waitingAlready = false

function nextTick (func) {
function debounce (func) {
if (!waitingAlready) {
waitingAlready = true
requestAnimationFrame(() => {
nextTick(() => {
func()
waitingAlready = false
})
}
}

function nextTick (func) {
if (typeof window !== 'undefined' && window.requestAnimationFrame) {
window.requestAnimationFrame(func)
} else {
setTimeout(func, 17)
}
}

function stateUpdated () {
rootEl && update(rootEl, components(state))
}
Expand All @@ -223,7 +231,7 @@ function updateState (updateObject, options) {
}
}

nextTick(stateUpdated)
debounce(stateUpdated)

if (process.env.NODE_ENV !== 'production') {
console.log('------STATE UPDATE------')
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "halfcab",
"version": "10.0.1",
"version": "10.0.4",
"description": "A simple universal JavaScript framework focused on making use of es2015 template strings to build components.",
"main": "halfcab.mjs",
"module": "halfcab.mjs",
Expand Down
22 changes: 12 additions & 10 deletions test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,14 @@ let serverHtml = (strings, ...values) => {
}

let halfcab, ssr, html, defineRoute, gotoRoute, formField, cache, updateState, injectMarkdown, formIsValid,
css, state, getRouteComponent
css, state, getRouteComponent, nextTick

function intialData (dataInitial) {
let el = document.createElement('div')
el.setAttribute('data-initial', dataInitial)
document.body.appendChild(el)
}

global.requestAnimationFrame = func => {
func()
}

describe('halfcab', () => {

describe('Server', () => {
Expand All @@ -53,7 +49,8 @@ describe('halfcab', () => {
injectMarkdown,
formIsValid,
css,
getRouteComponent
getRouteComponent,
nextTick
} = halfcabModule)
halfcab = halfcabModule.default
})
Expand Down Expand Up @@ -88,7 +85,8 @@ describe('halfcab', () => {
injectMarkdown,
formIsValid,
css,
getRouteComponent
getRouteComponent,
nextTick
} = halfcabModule)
halfcab = halfcabModule.default
})
Expand Down Expand Up @@ -121,15 +119,19 @@ describe('halfcab', () => {
})
})

it('updating state causes a rerender with state', () => {
return halfcab({
it('updating state causes a rerender with state', (done) => {
halfcab({
components (args) {
return html`<div>${args.testing || ''}</div>`
}
})
.then(({rootEl, state}) => {
updateState({testing: 'works'})
expect(rootEl.innerHTML.includes('works')).to.be.true()
nextTick(()=> {
expect(rootEl.innerHTML.includes('works')).to.be.true()
done()
})

})
})

Expand Down

0 comments on commit 6085bb3

Please sign in to comment.