Skip to content

Commit 7a8113f

Browse files
authored
Merge pull request #290 from github/ensure-updating-doesnt-hang-updates
ensure #updating doesnt hang updates
2 parents 15ca61e + c0d3799 commit 7a8113f

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

examples/index.html

+10
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ <h2>Localised Dates</h2>
193193
</relative-time>
194194
</p>
195195

196+
<p>
197+
Lazily added datetime via setAttribute
198+
<relative-time id="lazy" tense="past">
199+
This will display in one second.
200+
</relative-time>
201+
</p>
202+
196203
<!-- <script type="module" src="../dist/index.js"></script> -->
197204
<script type="module" src="https://unpkg.com/@github/relative-time-element@latest/dist/bundle.js"></script>
198205
<script>
@@ -203,6 +210,9 @@ <h2>Localised Dates</h2>
203210
document.getElementById('dynamic2').date = new Date(Date.now() - 30000)
204211
document.getElementById('dynamic3').date = new Date(Date.now() + 5000)
205212
document.getElementById('dynamic4').date = new Date()
213+
setTimeout(() => {
214+
document.getElementById('lazy').setAttribute('datetime', new Date().toJSON())
215+
}, 1000)
206216
</script>
207217
</body>
208218
</html>

src/relative-time-element.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
426426
this.#updating = (async () => {
427427
await Promise.resolve()
428428
this.update()
429+
this.#updating = false
429430
})()
430431
}
431432
}
@@ -472,7 +473,6 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
472473
} else {
473474
dateObserver.unobserve(this)
474475
}
475-
this.#updating = false
476476
}
477477
}
478478

test/relative-time.js

+9
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ suite('relative-time', function () {
7070
assert.equal(counter, 1)
7171
})
7272

73+
test('calls update even after nullish datetime', async () => {
74+
const el = document.createElement('relative-time')
75+
el.setAttribute('datetime', '')
76+
await new Promise(resolve => setTimeout(resolve, 10))
77+
el.setAttribute('datetime', new Date().toISOString())
78+
await Promise.resolve()
79+
assert(el.shadowRoot.textContent.length > 0, 'should have set time, but textContent is empty')
80+
})
81+
7382
test('sets title back to default if removed', async () => {
7483
const el = document.createElement('relative-time')
7584
el.setAttribute('datetime', new Date().toISOString())

0 commit comments

Comments
 (0)