mics icon indicating copy to clipboard operation
mics copied to clipboard

Mixins included multiple times

Open wkeese opened this issue 4 years ago • 0 comments

Even if an inheritance chain includes a mixin multiple times, mix() should only include it once. It should also follow C3MRO as documented in https://en.wikipedia.org/wiki/C3_linearization.

However, opposite to #61, in some of my tests, a mixin is getting included multiple times. For example, MixinA in the following example is included twice:

import { mix } from "mics/src/index";

// Base class
class SimpleGreeting {
	foo () {
		console.log("SimpleGreeting foo()");
		this.textContent = "Hello world!";
	}
}

// Root mixin A.
const MixinA = mix(SimpleGreeting, Base => class extends Base {
	foo () {
		super.foo();
		console.log("Mixin A foo()");
	}
});

// Mixin B extends root mixin A.
const MixinB = mix(SimpleGreeting, MixinA, Base => class extends Base {
	foo () {
		super.foo();
		console.log("Mixin B foo()");
	}
});

// Mixin C also extends root mixin A.
const MixinC = mix(SimpleGreeting, MixinA, Base => class extends Base {
	foo () {
		super.foo();
		console.log("Mixin C foo()");
	}
});

// ExtendedGreeting extends SimpleGreeting, mixing in B and C
const ExtendedGreeting = mix(SimpleGreeting, MixinB, MixinC, Base => class extends Base {
	foo () {
		super.foo();
		console.log("ExtendedGreeting foo()");
	}
});

// "new" doesn't work here, despite documentation saying that it does
const eg = ExtendedGreeting();

// Prints "Mixin A foo()" twice.
eg.foo();

wkeese avatar Jun 12 '21 06:06 wkeese