Skip to content

Commit

Permalink
switch to promise batching on attributeChangedCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Nov 25, 2022
1 parent cd93168 commit a1bd31e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/relative-time-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const dateObserver = new (class {

export default class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFormatOptions {
#customTitle = false
#updating = false
#updating: false | Promise<void> = false

get #lang() {
return this.closest('[lang]')?.getAttribute('lang') ?? 'default'
Expand Down Expand Up @@ -341,12 +341,14 @@ export default class RelativeTimeElement extends HTMLElement implements Intl.Dat
this.#customTitle = newValue !== null && this.getFormattedTitle() !== newValue
}
if (!this.#updating && !(attrName === 'title' && this.#customTitle)) {
this.update()
this.#updating = (async () => {
await Promise.resolve()
this.update()
})()
}
}

update() {
this.#updating = true
const oldText: string = this.#renderRoot.textContent || ''
const oldTitle: string = this.getAttribute('title') || ''
let newTitle: string = oldTitle
Expand Down
2 changes: 1 addition & 1 deletion test/relative-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ suite('relative-time', function () {
el.setAttribute('hour', '2-digit')
el.setAttribute('minute', '2-digit')
await Promise.resolve()
assert.equal(counter, 3)
assert.equal(counter, 1)
})

test('sets title back to default if removed', async () => {
Expand Down

0 comments on commit a1bd31e

Please sign in to comment.