Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/events/defineEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ export const Mixin = (Super = HTMLElement) => class WithEvents extends Super {
// FIXME these won't apply if we're not using NudeElement somewhere in the inheritance chain
static mixins = [PropsMixin(Super)];

constructor () {
super();
this.init();
}

init () {
this.constructor.init();

Expand Down
5 changes: 0 additions & 5 deletions src/form-associated.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ export function appliesTo (Class) {
}

export const Mixin = (Super = HTMLElement, { internalsProp = "_internals", configProp = "formAssociated" } = {}) => class FormAssociated extends Super {
constructor () {
super();
this.init();
}

init () {
this.constructor[init]();

Expand Down
8 changes: 0 additions & 8 deletions src/hooks/with.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ export function appliesTo (Class) {
export const Mixin = (Super = HTMLElement) => class WithHooks extends Super {
static hooks = new Hooks(super.hooks || {});

constructor () {
super();

this.init?.();
}

init () {
super.init?.();

const Self = this.constructor;

if (Self.hooks && !(Self.hooks instanceof Hooks)) {
Expand Down
35 changes: 29 additions & 6 deletions src/mixins/apply.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
import { copyProperties } from "../util/copy-properties.js";
import { getSuper } from "../util/super.js";

export function applyMixins (Class = this, mixins = Class.mixins) {
if (Object.hasOwn(Class, "mixinsActive") || !mixins?.length) {
return;
}

// Determine applicable mixins first. Mixins without `appliesTo()` are always applicable.
const applicable = mixins.filter(Mixin => Mixin.appliesTo?.(Class) ?? true);
if (!applicable.length) {
return;
}

Class.mixinsActive = [];

for (let Mixin of mixins) {
if (Mixin.appliesTo && !Mixin.appliesTo(Class)) {
// Not applicable to this class
continue;
// Phase 1: create stubs for all prototype methods from all applicable mixins,
// so that they can be used as the base function that mixins can extend.
// In that case, none of the mixins' functions is used as the base function to add side effects to.
for (const Mixin of applicable) {
for (const property of Object.getOwnPropertyNames(Mixin.prototype)) {
if (property === "constructor" || Object.hasOwn(Class.prototype, property)) {
continue;
}

const descriptor = Object.getOwnPropertyDescriptor(Mixin.prototype, property);
if (typeof descriptor.value !== "function") {
continue;
}

// Only create a stub if the class doesn't already have its own implementation
Class.prototype[property] = function (...args) {
getSuper(this, property)?.call(this, ...args);
};
}
}

// Phase 2: apply all mixins
for (let Mixin of applicable) {
applyMixin(Class, Mixin);
}
}
Expand All @@ -24,10 +48,9 @@ export function applyMixin (Class, Mixin, force = false) {
return;
}

copyProperties(Class, Mixin, {recursive: true, prototypes: true});
copyProperties(Class, Mixin, { recursive: true, prototypes: true });

if (!alreadyApplied) {
Class.mixinsActive.push(Mixin);
}
}

5 changes: 0 additions & 5 deletions src/props/defineProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import { defineLazyProperties } from "../util/lazy.js";
const { initialized, propsDef } = getSymbols;

export const Mixin = (Super = HTMLElement) => class WithProps extends Super {
constructor () {
super();
this.init();
}

init () {
this.constructor.init();

Expand Down
5 changes: 0 additions & 5 deletions src/slots/defineSlots.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ import { assignToSlot } from "./util.js";
let mutationObserver;

export const Mixin = (Super = HTMLElement) => class DefineSlots extends Super {
constructor () {
super();
this.init();
}

init () {
if (!this.shadowRoot) {
return;
Expand Down
5 changes: 0 additions & 5 deletions src/styles/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ export function appliesTo (Class) {
}

export const Mixin = (Super = HTMLElement) => class GlobalStyles extends Super {
constructor () {
super();
this.init();
}

async [render] () {
let Self = this.constructor;

Expand Down
5 changes: 0 additions & 5 deletions src/styles/shadow.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ export function appliesTo (Class) {
}

export const Mixin = (Super = HTMLElement) => class ShadowStyles extends Super {
constructor () {
super();
this.init();
}

init () {
if (!this.shadowRoot) {
return;
Expand Down