1.4.0 • Published 10 months ago

@open-wc/dedupe-mixin v1.4.0

Weekly downloads
14,472
License
MIT
Repository
github
Last release
10 months ago

Development >> Dedupe Mixin ||30

Automatically Deduplicate JavaScript Class Mixins

Features

  • Small
  • Fast
  • Typed

Usage

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
    },
);

What is a Mixin?

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.

Let's take for example Logging. Imagine you have 3 Pages

  • Red
  • Green
  • Blue
              +----------+
              |   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:

  • we can't put the logic in Page as Blue should not be logged
  • we can't reuse the logic in Logged 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.

Why is Deduping of Mixins Necessary?

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.

  • no-dedupe "fails" by logging Green two times
  • with-dedupe "succeeds" by logging Green one time as well

You can check the source code for both on github.

Nested examples

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* |
                             +----------+
  • Pages generally only need Logging
  • There is however also more advanced Metrics System which extends Logging
  • Metrics was separately developed for Features
  • When we now want to get the same Metrics on Page Blue we get duplicate logging without consciously applying logging even once (we do class PageBlue extends MetricsMixin(Page) {})
  • Only deduping can help in these scenarios

Ascii Graphics made with AsciiFlow

pnp-modern-search-core@crossfields/puzzle-elements@nymbus/coreweb-wcbriolink-ui-kit@infinitebrahmanuniverse/nolb-_open-@everything-registry/sub-chunk-684@sfdocs-internal/api-type-documentlit-element-contextmultiselect-combo-boxmduiweb-component-definestateful-messaging-systemuno-experimental-runtimedark-theme-utils@kuscamara/context-provider@ithaka/pharos@logo-elements/field-base@logo-elements/list-mixin@logo-elements/logo-themable-mixin@lion/core@lion/ui@pwrs/mixinsds-lena-melo@robrez/scoped-registry-polymer@vaadin/select@vaadin/vaadin-themable-mixin@vaadin/virtual-list@vaadin/item@vaadin/crud@vaadin/custom-field@vaadin/date-picker@vaadin/checkbox@vaadin/icon@vaadin/list-box@vaadin/login@vaadin/overlay@vaadin/progress-bar@vaadin/number-field@vaadin/rich-text-editor@vaadin/radio-group@vaadin/dialog@vaadin/field-base@vaadin/details@vaadin/button@vaadin/text-field@vaadin/tooltip@vaadin/avatar@vaadin/grid@vaadin/grid-pro@vaadin/vaadin-list-mixin@vaadin/combo-box@vaadin/component-base@vaadin/confirm-dialog@vaadin/menu-bar@vaadin/context-menu@vaadin/side-nav@vaadin/split-layout@vaadin/tabs@vaadin/text-area@vaadin/upload@untitled-engineer/scoped-elements@frontend_zz/mixins@frontend_zz/web-component-mixins@frontend_zz/components@scoped-vaadin/button@scoped-vaadin/combo-box@scoped-vaadin/component-base@scoped-vaadin/context-menu@scoped-vaadin/date-picker@scoped-vaadin/dialog@scoped-vaadin/field-base@scoped-vaadin/grid@scoped-vaadin/vaadin-list-mixin@scoped-vaadin/vaadin-themable-mixin@scoped-vaadin/item@scoped-vaadin/login@scoped-vaadin/menu-bar@scoped-vaadin/overlay@scoped-vaadin/progress-bar@scoped-elements/shoelace@scoped-vaadin/a11y-base@unicef-polymer/etools-dropdown@unicef-polymer/etools-unicef@vaadin/a11y-base@tokens-studio/configurator@pnp/modern-search-core@poratta/splash@nymbus/coreweb-components@nathancollins/lego-lib@mdui/sharedapi-console-lwc@aquera/nile@apollo-elements/mixins@api-components/api-type-document@api-components/api-authorization@api-components/api-authorization-method@api-components/api-forms@api-client/amf-store@api-client/graph-project@api-client/ui
1.4.0

10 months ago

1.3.1

2 years ago

1.3.0

4 years ago

1.2.18

4 years ago

1.2.17

4 years ago

2.0.0

4 years ago

1.2.16

4 years ago

1.2.15

4 years ago

1.2.14

4 years ago

1.2.13

4 years ago

1.2.12

4 years ago

1.2.11

4 years ago

1.2.9

4 years ago

1.2.10

4 years ago

1.2.8

4 years ago

1.2.7

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago