diff --git a/addon/index.ts b/addon/index.ts index c714ef1..82097cb 100644 --- a/addon/index.ts +++ b/addon/index.ts @@ -1,8 +1,6 @@ import { getOwner } from '@ember/application'; import { warn } from '@ember/debug'; import { isDestroyed, isDestroying, registerDestructor } from '@ember/destroyable'; -import { schedule } from '@ember/runloop'; -import { waitForPromise } from '@ember/test-waiters'; import type ApplicationInstance from '@ember/application/instance'; import type { Route } from '@ember/routing'; @@ -117,7 +115,7 @@ async function setupHashSupport(router: EmberRouter) { const CACHE = new WeakMap(); -async function eventuallyTryScrollingTo(owner: ApplicationInstance, url?: string) { +function eventuallyTryScrollingTo(owner: ApplicationInstance, url?: string) { // Prevent quick / rapid transitions from continuing to observer beyond their URL-scope CACHE.get(owner)?.disconnect(); @@ -127,62 +125,9 @@ async function eventuallyTryScrollingTo(owner: ApplicationInstance, url?: string if (!hash) return; - await waitForPromise(uiSettled(owner)); - if (isDestroyed(owner) || isDestroying(owner)) { return; } scrollToHash(hash); } - -const TIME_SINCE_LAST_MUTATION = 500; // ms -const MAX_TIMEOUT = 2000; // ms - -// exported for testing -export async function uiSettled(owner: ApplicationInstance) { - let timeStarted = new Date().getTime(); - let lastMutationAt = Infinity; - let totalTimeWaited = 0; - - let observer = new MutationObserver(() => { - lastMutationAt = new Date().getTime(); - }); - - CACHE.set(owner, observer); - - observer.observe(document.body, { childList: true, subtree: true }); - - /** - * Wait for DOM mutations to stop until MAX_TIMEOUT - */ - await new Promise((resolve) => { - let frame: number; - - function requestTimeCheck() { - if (frame) cancelAnimationFrame(frame); - - if (isDestroyed(owner) || isDestroying(owner)) { - return; - } - - frame = requestAnimationFrame(() => { - totalTimeWaited = new Date().getTime() - timeStarted; - - let timeSinceLastMutation = new Date().getTime() - lastMutationAt; - - if (totalTimeWaited >= MAX_TIMEOUT) { - return resolve(totalTimeWaited); - } - - if (timeSinceLastMutation >= TIME_SINCE_LAST_MUTATION) { - return resolve(totalTimeWaited); - } - - schedule('afterRender', requestTimeCheck); - }); - } - - schedule('afterRender', requestTimeCheck); - }); -} diff --git a/tests/integration/hash-test.ts b/tests/integration/hash-test.ts index 7fa6d9a..9ed83a2 100644 --- a/tests/integration/hash-test.ts +++ b/tests/integration/hash-test.ts @@ -6,7 +6,7 @@ import { hbs } from 'ember-cli-htmlbars'; import { module, test } from 'qunit'; import { setupApplicationTest } from 'ember-qunit'; -import { scrollToHash, uiSettled } from 'ember-url-hash-polyfill'; +import { scrollToHash } from 'ember-url-hash-polyfill'; import { setupRouter } from './-helpers'; @@ -223,14 +223,12 @@ module('Hash', function (hooks) { debugAssert(`Expected all test elements to exist`, container); router.transitionTo('/foo'); - await uiSettled(this.owner); assert.true(isVisible(find('#foo-first'), container), 'first header is visible'); assert.false(isVisible(find('#foo-second'), container), 'second header is not visible'); assert.equal(location.hash, '', 'initially, has no hash'); router.transitionTo('/bar#bar-second'); - await uiSettled(this.owner); await scrollSettled(); assert.false(isVisible(find('#bar-first'), container), 'first header is not visible'); @@ -238,7 +236,6 @@ module('Hash', function (hooks) { assert.equal(location.hash, '#bar-second', 'clicked hash appears in URL'); router.transitionTo('/foo#foo-second'); - await uiSettled(this.owner); await scrollSettled(); assert.false(isVisible(find('#foo-first'), container), 'first header is not visible');