diff --git a/index.js b/index.js index 582ee6a..b6427c6 100644 --- a/index.js +++ b/index.js @@ -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); @@ -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 = () => {}; @@ -30,7 +21,6 @@ export default function elementReady(selector, { let shouldStop = false; const stop = () => { - cache.delete(cacheKey, promise); shouldStop = true; }; @@ -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; } diff --git a/package.json b/package.json index 03fcb31..7f10786 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/test.js b/test.js index 48fbc4f..f7cea26 100644 --- a/test.js +++ b/test.js @@ -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});