Skip to content

Commit

Permalink
make life-cycle event connected happen *after* render if render is as…
Browse files Browse the repository at this point in the history
…ync or a generator function
  • Loading branch information
heapwolf committed Jul 13, 2020
1 parent 03b3010 commit 8577520
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 41 deletions.
2 changes: 1 addition & 1 deletion dist/tonic.min.js

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

66 changes: 27 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ class Tonic extends window.HTMLElement {
return `tonic${Tonic._index++}`
}

static _maybePromise (p) {
if (p && typeof p.then === 'function' && typeof p.catch === 'function') {
p.catch(err => setTimeout(() => { throw err }, 0))
}
}

static _splitName (s) {
return s.match(/[A-Z][a-z]*/g).join('-')
}
Expand Down Expand Up @@ -195,23 +189,20 @@ class Tonic extends window.HTMLElement {
scheduleReRender (oldProps) {
if (this.pendingReRender) return this.pendingReRender

this.pendingReRender = new Promise(resolve => {
window.setTimeout(() => {
const p = this._set(this.root, this.render)
this.pendingReRender = null

if (p && p.then) {
Tonic._maybePromise(p.then(() => {
if (this.updated) this.updated(oldProps)
resolve()
}))
return
}
this.pendingReRender = new Promise(resolve => window.setTimeout(() => {
const p = this._set(this.root, this.render)
this.pendingReRender = null

if (this.updated) this.updated(oldProps)
resolve()
}, 0)
})
if (p && p.then) {
return p.then(() => {
if (this.updated) this.updated(oldProps)
resolve()
})
}

if (this.updated) this.updated(oldProps)
resolve()
}, 0))

return this.pendingReRender
}
Expand All @@ -227,12 +218,11 @@ class Tonic extends window.HTMLElement {
}

handleEvent (e) {
Tonic._maybePromise(this[e.type](e))
this[e.type](e)
}

_drainIterator (target, iterator) {
const p = iterator.next()
return p.then((result) => {
return iterator.next().then((result) => {
this._set(target, null, result.value)
if (result.done) return
return this._drainIterator(target, iterator)
Expand All @@ -249,19 +239,14 @@ class Tonic extends window.HTMLElement {
}

if (render instanceof Tonic.AsyncFunction) {
const promise = render.call(this) || ''
return promise.then((content) => {
return this._apply(target, content)
})
return render.call(this).then(content => this._apply(target, content))
} else if (render instanceof Tonic.AsyncFunctionGenerator) {
const itr = render.call(this)
return this._drainIterator(target, itr)
return this._drainIterator(target, render.call(this))
} else if (render === null) {
this._apply(target, content)
} else if (render instanceof Function) {
content = render.call(this) || ''
return this._apply(target, content)
this._apply(target, render.call(this) || '')
}

return this._apply(target, content)
}

_apply (target, content) {
Expand Down Expand Up @@ -314,7 +299,7 @@ class Tonic extends window.HTMLElement {
}
}

connectedCallback () {
async connectedCallback () {
this.root = this.shadowRoot || this

if (this.wrap) {
Expand Down Expand Up @@ -354,20 +339,23 @@ class Tonic extends window.HTMLElement {
this._id = this._id || Tonic._createId()

this.willConnect && this.willConnect()

if (!this.preventRenderOnReconnect) {
if (!this._source) {
this._source = this.innerHTML
} else {
this.innerHTML = this._source
}

Tonic._maybePromise(this._set(this.root, this.render))
const p = this._set(this.root, this.render)
if (p && p.then) await p
}
Tonic._maybePromise(this.connected && this.connected())

this.connected && this.connected()
}

disconnectedCallback () {
Tonic._maybePromise(this.disconnected && this.disconnected())
this.disconnected && this.disconnected()
delete Tonic._data[this._id]
delete Tonic._children[this._id]
}
Expand Down
Loading

0 comments on commit 8577520

Please sign in to comment.