Skip to content

Commit

Permalink
Merge pull request #290 from github/ensure-updating-doesnt-hang-updates
Browse files Browse the repository at this point in the history
ensure #updating doesnt hang updates
  • Loading branch information
keithamus authored Aug 23, 2024
2 parents 15ca61e + c0d3799 commit 7a8113f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
10 changes: 10 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ <h2>Localised Dates</h2>
</relative-time>
</p>

<p>
Lazily added datetime via setAttribute
<relative-time id="lazy" tense="past">
This will display in one second.
</relative-time>
</p>

<!-- <script type="module" src="../dist/index.js"></script> -->
<script type="module" src="https://unpkg.com/@github/relative-time-element@latest/dist/bundle.js"></script>
<script>
Expand All @@ -203,6 +210,9 @@ <h2>Localised Dates</h2>
document.getElementById('dynamic2').date = new Date(Date.now() - 30000)
document.getElementById('dynamic3').date = new Date(Date.now() + 5000)
document.getElementById('dynamic4').date = new Date()
setTimeout(() => {
document.getElementById('lazy').setAttribute('datetime', new Date().toJSON())
}, 1000)
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion src/relative-time-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
this.#updating = (async () => {
await Promise.resolve()
this.update()
this.#updating = false
})()
}
}
Expand Down Expand Up @@ -472,7 +473,6 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
} else {
dateObserver.unobserve(this)
}
this.#updating = false
}
}

Expand Down
9 changes: 9 additions & 0 deletions test/relative-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ suite('relative-time', function () {
assert.equal(counter, 1)
})

test('calls update even after nullish datetime', async () => {
const el = document.createElement('relative-time')
el.setAttribute('datetime', '')
await new Promise(resolve => setTimeout(resolve, 10))
el.setAttribute('datetime', new Date().toISOString())
await Promise.resolve()
assert(el.shadowRoot.textContent.length > 0, 'should have set time, but textContent is empty')
})

test('sets title back to default if removed', async () => {
const el = document.createElement('relative-time')
el.setAttribute('datetime', new Date().toISOString())
Expand Down

0 comments on commit 7a8113f

Please sign in to comment.