|
|
"use strict"; |
|
|
Object.defineProperty(exports, "__esModule", { value: true }); |
|
|
exports.cacheParentResults = cacheParentResults; |
|
|
var querying_js_1 = require("./querying.js"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function cacheParentResults(next, _a, matches) { |
|
|
var adapter = _a.adapter, cacheResults = _a.cacheResults; |
|
|
if (cacheResults === false || typeof WeakMap === "undefined") { |
|
|
return function (elem) { return next(elem) && matches(elem); }; |
|
|
} |
|
|
|
|
|
|
|
|
var resultCache = new WeakMap(); |
|
|
function addResultToCache(elem) { |
|
|
var result = matches(elem); |
|
|
resultCache.set(elem, result); |
|
|
return result; |
|
|
} |
|
|
return function cachedMatcher(elem) { |
|
|
if (!next(elem)) |
|
|
return false; |
|
|
if (resultCache.has(elem)) { |
|
|
return resultCache.get(elem); |
|
|
} |
|
|
|
|
|
var node = elem; |
|
|
do { |
|
|
var parent = (0, querying_js_1.getElementParent)(node, adapter); |
|
|
if (parent === null) { |
|
|
return addResultToCache(elem); |
|
|
} |
|
|
node = parent; |
|
|
} while (!resultCache.has(node)); |
|
|
return resultCache.get(node) && addResultToCache(elem); |
|
|
}; |
|
|
} |
|
|
|