Skip to content

Commit

Permalink
Drop the cache
Browse files Browse the repository at this point in the history
Related to #51
  • Loading branch information
sindresorhus committed Nov 4, 2024
1 parent 5e2474e commit 1c3bd2a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 44 deletions.
46 changes: 15 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import ManyKeysMap from 'many-keys-map';
import requestAnimationFrames from 'request-animation-frames';
import domMutations from 'dom-mutations';

const cache = new ManyKeysMap();

const isDomReady = target =>
['interactive', 'complete'].includes((target.ownerDocument ?? target).readyState);

Expand All @@ -14,13 +11,7 @@ export default function elementReady(selector, {
timeout = Number.POSITIVE_INFINITY,
predicate,
} = {}) {
const cacheKey = [selector, stopOnDomReady, timeout, waitForChildren, target];
const cachedPromise = cache.get(cacheKey);
if (cachedPromise) {
return cachedPromise;
}

// Not necessary, it just acts faster and avoids cache/listener setup
// Not necessary, it just acts faster and avoids listener setup
if (stopOnDomReady && isDomReady(target)) {
const promise = Promise.resolve(getMatchingElement({target, selector, predicate}));
promise.stop = () => {};
Expand All @@ -30,7 +21,6 @@ export default function elementReady(selector, {
let shouldStop = false;

const stop = () => {
cache.delete(cacheKey, promise);
shouldStop = true;
};

Expand All @@ -40,37 +30,31 @@ export default function elementReady(selector, {

// Interval to keep checking for it to come into the DOM
const promise = (async () => {
try {
for await (const _ of requestAnimationFrames()) { // eslint-disable-line no-unused-vars
if (shouldStop) {
return;
}
for await (const _ of requestAnimationFrames()) { // eslint-disable-line no-unused-vars
if (shouldStop) {
return;
}

const element = getMatchingElement({target, selector, predicate});
const element = getMatchingElement({target, selector, predicate});

// When it's ready, only stop if requested or found
if (isDomReady(target) && (stopOnDomReady || element)) {
// When it's ready, only stop if requested or found
if (isDomReady(target) && (stopOnDomReady || element)) {
return element;
}

let current = element;
while (current) {
if (!waitForChildren || current.nextSibling) {
return element;
}

let current = element;
while (current) {
if (!waitForChildren || current.nextSibling) {
return element;
}

current = current.parentElement;
}
current = current.parentElement;
}
} finally {
cache.delete(cacheKey, promise);
}
})();

promise.stop = stop;

cache.set(cacheKey, promise);

return promise;
}

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
],
"dependencies": {
"dom-mutations": "^0.2.0",
"many-keys-map": "^2.0.1",
"request-animation-frames": "^0.1.1",
"typed-query-selector": "^2.11.0"
},
Expand Down
12 changes: 0 additions & 12 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,6 @@ test('check if element ready before timeout', async t => {
t.is(await elementCheck, element);
});

test('ensure only one promise is returned on multiple calls passing the same selector', t => {
const elementCheck = elementReady('#not-found', {stopOnDomReady: false});

for (let i = 0; i <= 10; i++) {
if (elementReady('#not-found', {stopOnDomReady: false}) !== elementCheck) {
t.fail();
}
}

t.pass();
});

test('check if wait can be stopped', async t => {
const elementCheck = elementReady('#dofle', {stopOnDomReady: false});

Expand Down

0 comments on commit 1c3bd2a

Please sign in to comment.