-
Notifications
You must be signed in to change notification settings - Fork 12
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
Add memoization to Cache
#1720
base: main
Are you sure you want to change the base?
Add memoization to Cache
#1720
Changes from all commits
a4dc1ae
98572d1
83c6d94
c2b7761
a456962
a67d52d
839da97
b83811c
a9a999f
48eff37
83540d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@siteimprove/alfa-cache": minor | ||
--- | ||
|
||
**Added:** A `Cache.memoize` decorator is now available. | ||
|
||
It can decorate methods, or wrap functions, whose parameters are all objects. It will automatically create a cache with the various parameters and correctly call it. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ import { Context } from "@siteimprove/alfa-selector"; | |
import { Style } from "@siteimprove/alfa-style"; | ||
|
||
const { hasAttribute, isElement } = Element; | ||
const { or, test, equals } = Predicate; | ||
const { or, equals } = Predicate; | ||
const { and } = Refinement; | ||
const { hasComputedStyle } = Style; | ||
|
||
|
@@ -34,39 +34,28 @@ export function isProgrammaticallyHidden( | |
); | ||
} | ||
|
||
const cache = Cache.empty<Device, Cache<Context, Cache<Node, boolean>>>(); | ||
|
||
function hasHiddenAncestors( | ||
function _hasHiddenAncestors( | ||
device: Device, | ||
context: Context = Context.empty(), | ||
context: Context, | ||
): Predicate<Node> { | ||
return (node) => | ||
cache | ||
.get(device, Cache.empty) | ||
.get(context, Cache.empty) | ||
.get(node, () => | ||
test( | ||
or( | ||
// Either it is a programmatically hidden element | ||
and( | ||
isElement, | ||
or( | ||
hasComputedStyle( | ||
"display", | ||
({ values: [outside] }) => outside.value === "none", | ||
device, | ||
context, | ||
), | ||
hasAttribute("aria-hidden", equals("true")), | ||
), | ||
), | ||
// Or its parent is programmatically hidden | ||
(node: Node) => | ||
node | ||
.parent(Node.fullTree) | ||
.some(hasHiddenAncestors(device, context)), | ||
), | ||
node, | ||
return or( | ||
// Either it is a programmatically hidden element | ||
and( | ||
isElement, | ||
or( | ||
hasComputedStyle( | ||
"display", | ||
({ values: [outside] }) => outside.value === "none", | ||
device, | ||
context, | ||
), | ||
); | ||
hasAttribute("aria-hidden", equals("true")), | ||
), | ||
), | ||
// Or its parent is programmatically hidden | ||
(node: Node) => | ||
node.parent(Node.fullTree).some(_hasHiddenAncestors(device, context)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: Is it on purpose the call is to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ouch. Good point. 🤔 |
||
); | ||
} | ||
|
||
const hasHiddenAncestors = Cache.memoize(_hasHiddenAncestors); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just a demo of using the new decorator, not directly related to the changes.