optimize combinatorics and defaults, now it's twice as fast!
This commit is contained in:
parent
1cfdde819b
commit
1af8ca29f3
@ -146,13 +146,12 @@ const combinationsMatch = (criteria, subject, strict) => {
|
|||||||
if (criteria.component !== subject.component) return false
|
if (criteria.component !== subject.component) return false
|
||||||
|
|
||||||
// All variants inherit from normal
|
// All variants inherit from normal
|
||||||
const subjectVariant = Object.prototype.hasOwnProperty.call(subject, 'variant') ? subject.variant : 'normal'
|
if (subject.variant !== 'normal' || strict) {
|
||||||
if (subjectVariant !== 'normal' || strict) {
|
|
||||||
if (criteria.variant !== subject.variant) return false
|
if (criteria.variant !== subject.variant) return false
|
||||||
}
|
}
|
||||||
|
|
||||||
const subjectStatesSet = new Set(['normal', ...(subject.state || [])])
|
const subjectStatesSet = new Set(subject.state)
|
||||||
const criteriaStatesSet = new Set(['normal', ...(criteria.state || [])])
|
const criteriaStatesSet = new Set(criteria.state)
|
||||||
|
|
||||||
// Subject states > 1 essentially means state is "normal" and therefore matches
|
// Subject states > 1 essentially means state is "normal" and therefore matches
|
||||||
if (subjectStatesSet.size > 1 || strict) {
|
if (subjectStatesSet.size > 1 || strict) {
|
||||||
@ -198,7 +197,12 @@ export const init = (extraRuleset, palette) => {
|
|||||||
const ruleset = [
|
const ruleset = [
|
||||||
...Object.values(components).map(c => c.defaultRules.map(r => ({ component: c.name, ...r })) || []).reduce((acc, arr) => [...acc, ...arr], []),
|
...Object.values(components).map(c => c.defaultRules.map(r => ({ component: c.name, ...r })) || []).reduce((acc, arr) => [...acc, ...arr], []),
|
||||||
...extraRuleset
|
...extraRuleset
|
||||||
]
|
].map(rule => {
|
||||||
|
rule.variant = rule.variant ?? 'normal'
|
||||||
|
rule.state = [...new Set(['normal', ...(rule.state || [])])]
|
||||||
|
|
||||||
|
return rule
|
||||||
|
})
|
||||||
|
|
||||||
const virtualComponents = new Set(Object.values(components).filter(c => c.virtual).map(c => c.name))
|
const virtualComponents = new Set(Object.values(components).filter(c => c.virtual).map(c => c.name))
|
||||||
|
|
||||||
@ -344,7 +348,9 @@ export const init = (extraRuleset, palette) => {
|
|||||||
const variants = { normal: '', ...originalVariants }
|
const variants = { normal: '', ...originalVariants }
|
||||||
const innerComponents = validInnerComponents.map(name => components[name])
|
const innerComponents = validInnerComponents.map(name => components[name])
|
||||||
|
|
||||||
const stateCombinations = getAllPossibleCombinations(Object.keys(states))
|
// Optimization: we only really need combinations without "normal" because all states implicitly have it
|
||||||
|
const permutationStateKeys = Object.keys(states).filter(s => s !== 'normal')
|
||||||
|
const stateCombinations = [['normal'], ...getAllPossibleCombinations(permutationStateKeys).map(combination => ['normal', ...combination])]
|
||||||
|
|
||||||
const stateVariantCombination = Object.keys(variants).map(variant => {
|
const stateVariantCombination = Object.keys(variants).map(variant => {
|
||||||
return stateCombinations.map(state => ({ variant, state }))
|
return stateCombinations.map(state => ({ variant, state }))
|
||||||
|
Loading…
Reference in New Issue
Block a user