-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
patch: improve enforcement performance on legacy browsers (#507)
- Loading branch information
Showing
6 changed files
with
84 additions
and
74 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { RUN_RULE } from 'enforceKeywords'; | ||
import genRuleProxy from 'genRuleProxy'; | ||
import isFunction from 'isFunction'; | ||
import runtimeRules from 'runtimeRules'; | ||
|
||
// Initiates a chain of functions directly from the `enforce` | ||
// function - that's even though we do not have any closure | ||
// there to store that data. | ||
export default function bindLazyRule(ruleName) { | ||
const registeredRules = []; | ||
|
||
const addFn = fnName => (...args) => { | ||
registeredRules.push( | ||
Object.defineProperty( | ||
value => runtimeRules[fnName](value, ...args), | ||
'name', | ||
{ value: fnName } | ||
) | ||
); | ||
|
||
const returnvalue = genRuleProxy({}, addFn); | ||
|
||
return Object.assign(returnvalue, { | ||
[RUN_RULE]: getValue => { | ||
return registeredRules.every(fn => { | ||
// This inversion of control when getting the value is | ||
// required in order to pass the function over to `shape` | ||
// so it can make the decision which args to pass to `optional` | ||
return fn(isFunction(getValue) ? getValue(fn.name) : getValue); | ||
}); | ||
}, | ||
}); | ||
}; | ||
|
||
return addFn(ruleName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import isFunction from 'isFunction'; | ||
import isRule from 'isRule'; | ||
import proxySupported from 'proxySupported'; | ||
import runtimeRules from 'runtimeRules'; | ||
|
||
// Creates a proxy object that has access to all the rules | ||
export default function genRuleProxy(target, output) { | ||
if (proxySupported()) { | ||
return new Proxy(target, { | ||
get: (target, fnName) => { | ||
if (isRule(fnName)) { | ||
return output(fnName); | ||
} | ||
|
||
return target[fnName]; | ||
}, | ||
}); | ||
} else { | ||
/** | ||
* This method is REALLY not recommended as it is slow and iterates over | ||
* all the rules for each direct enforce reference. We only use it as a | ||
* lightweight alternative for the much faster proxy interface | ||
*/ | ||
for (const fnName in runtimeRules) { | ||
if (!isFunction(target[fnName])) { | ||
Object.defineProperties(target, { | ||
[fnName]: { get: () => output(fnName) }, | ||
}); | ||
} | ||
} | ||
return target; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import compounds from 'compounds'; | ||
import rules from 'rules'; | ||
|
||
export default Object.assign(rules(), compounds); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import isFunction from 'isFunction'; | ||
import runtimeRules from 'runtimeRules'; | ||
|
||
const isRule = (rulesObject, name) => { | ||
return rulesObject.hasOwnProperty(name) && isFunction(rulesObject[name]); | ||
}; | ||
const isRule = name => isFunction(runtimeRules[name]); | ||
|
||
export default isRule; |