Created
December 9, 2019 16:34
-
-
Save oslego/d8d8bf0248aa83ccef395d6f3ca48025 to your computer and use it in GitHub Desktop.
StyleSheetArray implementation with Proxy and Reflect
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
// https://github.com/WICG/construct-stylesheets/issues/45#issuecomment-522879888 | |
'use strict'; | |
function isArrayIndex(string) { | |
if (typeof string !== 'string') { | |
return false; | |
} | |
const number = Number(string); | |
return Number.isSafeInteger(number) | |
&& String(number) === string | |
&& number >= 0; | |
} | |
class StyleSheetArray extends Array { | |
#updateStyles() { | |
/* Do usual updates when style list changes */ | |
} | |
constructor(...args) { | |
super(...args); | |
return new Proxy(this, { | |
defineProperty: (target, prop, descriptor) => { | |
try { | |
return Reflect.defineProperty(target, prop, descriptor); | |
} finally { | |
if (prop === 'length' || isArrayIndex(prop)) { | |
this.#updateStyles(); | |
} | |
} | |
}, | |
deleteProperty: (target, prop) => { | |
try { | |
return Reflect.deleteProperty(target, prop); | |
} finally { | |
if (prop === 'length' || isArrayIndex(prop)) { | |
this.#updateStyles(); | |
} | |
} | |
}, | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment