0.2.0 • Published 1 year ago
@yadah/dedupe-mixin v0.2.0
dedupe-mixin
A mixin is a function which returns a class that extends a provided "super class".
(superclass) => class extends superclass {};
The dedupe()
function is a wrapper for mixins to allow a mixin to be used
multiple times without duplicating behaviour.
The typical usage is like:
// CustomMixin.js
import dedupe from "@yadah/dedupe-mixin";
function CustomMixin(superclass) {
return class Custom extends superclass {
fn() {
super.fn();
console.log("inside Custom.fn");
}
};
}
export default CustomMixin |> dedupe(%);
If this mixin is then used multiple times in the some mixin chain, it will only appear once
import CustomMixin from "./CustomMixin.js";
class superclass {
fn() {}
}
class Example extends (superclass |> CustomMixin(%) |> CustomMixin(%)) {}
new Example().fn();
// logs "inside Custom.fn" (once only)
dedupe()
also adds a method to the wrapped mixin function that allows
checking if a class includes the mixin:
CustomMixin.extends(Example);
// true