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

ensure #updating doesnt hang updates #290

Merged
merged 1 commit into from
Aug 23, 2024
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
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 () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the quick fix! 🙇🏻‍♀️

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
Loading