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

Fixing nextTick not running (shouldn't be the same as waiting and merging state, so separated them) #129

Merged
merged 3 commits into from
Jun 7, 2018
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
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