Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
@open-wc/dedupe-mixin
Advanced tools
@open-wc/dedupe-mixin is a utility for creating mixins in JavaScript that ensures the mixin is only applied once, even if it is included multiple times in the inheritance chain. This is particularly useful in web component development where mixins are often used to add reusable functionality to components.
DedupeMixin
The DedupeMixin function ensures that the mixin is only applied once, even if it is included multiple times in the inheritance chain. In this example, the mixin logs a message when it is applied.
const dedupeMixin = (superclass) => class extends superclass {
constructor() {
super();
console.log('DedupeMixin applied');
}
};
class BaseClass {}
class MixedClass extends dedupeMixin(BaseClass) {}
const instance = new MixedClass();
mixin-decorator is a package that provides a decorator-based approach to applying mixins in JavaScript. Unlike @open-wc/dedupe-mixin, it uses decorators to apply mixins, which can be more intuitive for developers familiar with the decorator pattern.
mixin is a simple utility for creating mixins in JavaScript. It does not include deduplication functionality like @open-wc/dedupe-mixin, but it provides a straightforward way to apply mixins to classes.
mixwith is a comprehensive mixin library that includes features like deduplication, similar to @open-wc/dedupe-mixin. It provides a more extensive set of utilities for working with mixins, making it a good alternative for more complex use cases.
Automatically Deduplicate JavaScript Class Mixins
Part of Open Web Components: guides, tools and libraries for modern web development and web components
Apply it to each mixin in the chain to make sure they are not applied more than once to the final class.
import { dedupeMixin } from '@open-wc/dedupe-mixin';
export const MyMixin = dedupeMixin(
superclass =>
class MyMixin extends superclass {
// your mixin code goes here
},
);
A mixin is an abstract subclass; i.e. a subclass definition that may be applied to different superclasses to create a related family of modified classes.
- Gilad Bracha and William Cook, Mixin-based Inheritance
Let's take for example Logging. Imagine you have 3 Pages
+----------+
| Page |
+----------+
| | |
+----------+ | +-----------+
| | |
+---------+ +-----------+ +----------+
| PageRed | | PageGreen | | PageBlue |
+----+----+ +-----------+ +----------+
class Page {}
class PageRed extends Page {}
class PageGreen extends Page {}
class PageBlue extends Page {}
Now we want to log whenever someone goes on Page Red. To archive that we extend Page Red and make a Logged Page Red.
+----------+
| Page |
+-+--+--+--+
| | |
+----------+ | +-----------+
| | |
+----+----+ +-----+-----+ +-----+----+
| PageRed | | PageGreen | | PageBlue |
+----+----+ +-----------+ +----------+
|
+----+----+
| Logged |
| PageRed |
+---------+
class Page {}
class PageRed extends Page {}
class PageGreen extends Page {}
class PageBlue extends Page {}
class LoggedPagRed extends PageRed {}
If we want to start logging for PageGreen we have an issue:
Page
as Blue should not be loggedLogged PageGreen
as we can not extend from 2 source (even if we could it would mean conflicting info in Red and Green)What we can do is put it in an "external" place and write it so it can be "mixed in".
+----------+ +----------+
| Page | | Logging* |
+-+--+--+--+ +----------+
| | |
+----------+ | +-----------+
| | |
+-----+----+ +-----+-----+ +-----+----+
| PageRed | | PageGreen | | PageBlue |
| with | | with | +----------+
| Logging* | | Logging* |
+----------+ +-----------+
// defining the Mixin
export const LoggingMixin = superclass =>
class LoggingMixin extends superclass {
// logging logic
};
class Page {}
// applying a Mixin
class PageRed extends LoggingMixin(Page) {}
class PageGreen extends LoggingMixin(Page) {}
class PageBlue extends Page {}
With that approach we can extract logic into a separate code pieces we can use where needed.
For a more in depth technical explanation please read Real Mixins with JavaScript Classes.
We now want all logging to the Red, Green, and Blue pages. Easy enough - as we can now apply the LoggingMixin on the Page itself.
+----------+ +----------+
| Page | | Logging* |
| with | +----------+
| Logging* |
+-+--+--+--+
| | |
+----------+ | +-----------+
| | |
+-----+----+ +-----+-----+ +-----+----+
| PageRed | | PageGreen | | PageBlue |
+----------+ | with | +----------+
| Logging* |
+-----------+
However, Team Green were eager to launch, so they already applied LoggingMixin
to their Page class. When we apply it to the base Page
class, Mixin is now applied twice 😱
Suddenly, the Green page will print each log twice - not what we originally had in mind.
What we need to do is make sure that each Mixin is attached only once even if we try to apply it multiple times.
Generally the more generic a mixin is, the higher the chance becomes that is gets applied more than once. As a mixin author you can't control how it is used, and can't always predict it. So as a safety measure it is always recommended to create deduping mixins.
import { dedupeMixin } from '@open-wc/dedupe-mixin';
export const MyMixin = dedupeMixin(
superclass =>
class MyMixin extends superclass {
// your mixin code goes here
},
);
You can see exactly this situation in the demo.
By applying dedupeMixin to the mixin function, before we export it, we can be sure that our mixin class will only take effect once, even if it is mixed in to multiple base classes in the inheritance chain.
You can check the source code for both on github.
You may think that the above example is too simple and can be solved by aligning on when to do changes. However in most real live scenarios the situation is much more complicated 🙈 Mixins can be extended and just because you import a class it does not meant that this class has some Mixins pre applied.
Consider this example:
+----------+ +----------+ +----------+
| Page | | Logging* | | Feature |
| with | +----+-----+ | with |
| Logging* | | | Metrics* |
+-+--+--+--+ +----+-----+ +----+--+--+
| | | | Metrics* | | |
+----------+ | +-----------+ +----------+ | +------
| | | |
+-----+----+ +-----+-----+ +-----+----+ +------+-------+
| PageRed | | PageGreen | | PageBlue | | WaterFeature |
+----------+ +-----------+ | with | +--------------+
| Metrics* |
+----------+
class PageBlue extends MetricsMixin(Page) {}
)Ascii Graphics made with AsciiFlow
FAQs
Dedupe JavaScript Class Mixins
The npm package @open-wc/dedupe-mixin receives a total of 110,972 weekly downloads. As such, @open-wc/dedupe-mixin popularity was classified as popular.
We found that @open-wc/dedupe-mixin demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.