@open-wc/dedupe-mixin
Advanced tools
Comparing version 1.2.18 to 1.3.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [1.3.0](https://github.com/open-wc/open-wc/compare/@open-wc/dedupe-mixin@1.2.18...@open-wc/dedupe-mixin@1.3.0) (2020-08-05) | ||
### Features | ||
* **dedupe-mixin:** improve performance by simplifying the code ([#1708](https://github.com/open-wc/open-wc/issues/1708)) ([0ac7fff](https://github.com/open-wc/open-wc/commit/0ac7fffe26fffb5bcd8dcf457b185774efb2d395)) | ||
## [1.2.18](https://github.com/open-wc/open-wc/compare/@open-wc/dedupe-mixin@1.2.17...@open-wc/dedupe-mixin@1.2.18) (2020-07-08) | ||
@@ -8,0 +19,0 @@ |
{ | ||
"name": "@open-wc/dedupe-mixin", | ||
"version": "1.2.18", | ||
"version": "1.3.0", | ||
"publishConfig": { | ||
@@ -38,3 +38,3 @@ "access": "public" | ||
"sideEffects": false, | ||
"gitHead": "72c70d66fcc65b6395442a177a1a5220d261e8bf" | ||
"gitHead": "ac212af3eef63c8cdb442a75e63428d9634309d9" | ||
} |
const appliedClassMixins = new WeakMap(); | ||
function getPrototypeChain(obj) { | ||
const chain = []; | ||
let proto = obj; | ||
while (proto) { | ||
chain.push(proto); | ||
proto = Object.getPrototypeOf(proto); | ||
} | ||
return chain; | ||
} | ||
function wasApplied(mixin, superClass) { | ||
const classes = getPrototypeChain(superClass); | ||
for (const klass of classes) { | ||
/** Vefify if the Mixin was previously applyed | ||
* @private | ||
* @param {function} mixin Mixin being applyed | ||
* @param {object} superClass Class receiving the new mixin | ||
* @returns {boolean} | ||
*/ | ||
function wasMixinPreviouslyApplied(mixin, superClass) { | ||
let klass = superClass; | ||
while (klass) { | ||
if (appliedClassMixins.get(klass) === mixin) { | ||
return true; | ||
} | ||
klass = Object.getPrototypeOf(klass); | ||
} | ||
@@ -23,5 +20,10 @@ return false; | ||
/** Apply each mixin in the chain to make sure they are not applied more than once to the final class. | ||
* @export | ||
* @param {function} mixin Mixin to be applyed | ||
* @returns {object} Mixed class with mixin applied | ||
*/ | ||
export function dedupeMixin(mixin) { | ||
return superClass => { | ||
if (wasApplied(mixin, superClass)) { | ||
if (wasMixinPreviouslyApplied(mixin, superClass)) { | ||
return superClass; | ||
@@ -28,0 +30,0 @@ } |
16048
35