Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@oslego
Created December 9, 2019 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oslego/d8d8bf0248aa83ccef395d6f3ca48025 to your computer and use it in GitHub Desktop.
Save oslego/d8d8bf0248aa83ccef395d6f3ca48025 to your computer and use it in GitHub Desktop.
StyleSheetArray implementation with Proxy and Reflect
// 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