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

Fork ember-url-hash-polyfill and remove 2-second delay for our use case #1

Merged
merged 1 commit into from
Oct 10, 2022
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
57 changes: 1 addition & 56 deletions addon/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -117,7 +115,7 @@ async function setupHashSupport(router: EmberRouter) {

const CACHE = new WeakMap<ApplicationInstance, MutationObserver>();

async function eventuallyTryScrollingTo(owner: ApplicationInstance, url?: string) {
function eventuallyTryScrollingTo(owner: ApplicationInstance, url?: string) {
Copy link

Choose a reason for hiding this comment

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

Does this need to be called eventually try scrolling if there is no longer a delay?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Probably not - I was trying to minimize changes to the original project, but if people feel strongly about it, I can rename.

// Prevent quick / rapid transitions from continuing to observer beyond their URL-scope
CACHE.get(owner)?.disconnect();

Expand All @@ -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);

Choose a reason for hiding this comment

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

this is the actual bug here, it will always mutate DOM.

});
}

schedule('afterRender', requestTimeCheck);
});
}
5 changes: 1 addition & 4 deletions tests/integration/hash-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -223,22 +223,19 @@ 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');
assert.true(isVisible(find('#bar-second'), container), 'second header is visible');
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');
Expand Down