mirror of
https://github.com/wassname/template.git
synced 2026-07-08 08:32:43 +08:00
39 lines
1021 B
JavaScript
39 lines
1021 B
JavaScript
export const Mutating = (superclass) => {
|
|
return class extends superclass {
|
|
|
|
constructor() {
|
|
super();
|
|
|
|
// set up mutation observer
|
|
const options = {childList: true, characterData: true, subtree: true};
|
|
const observer = new MutationObserver( () => {
|
|
observer.disconnect();
|
|
this.renderIfPossible();
|
|
observer.observe(this, options);
|
|
});
|
|
|
|
// ...and listen for changes
|
|
observer.observe(this, options);
|
|
}
|
|
|
|
connectedCallback() {
|
|
super.connectedCallback();
|
|
|
|
this.renderIfPossible();
|
|
}
|
|
|
|
// potential TODO: check if this is enough for all our usecases
|
|
// maybe provide a custom function to tell if we have enough information to render
|
|
renderIfPossible() {
|
|
if (this.textContent && this.root) {
|
|
this.renderContent();
|
|
}
|
|
}
|
|
|
|
renderContent() {
|
|
console.error(`Your class ${this.constructor.name} must provide a custom renderContent() method!` );
|
|
}
|
|
|
|
}; // end class
|
|
}; // end mixin function
|