Skip to content

Commit

Permalink
Remove the "auto" preparing of mixin
Browse files Browse the repository at this point in the history
The attempt to automatically apply the Mixin decorator failed, because it would not change the output reference / const variable that defined, yielding unexpected results.
  • Loading branch information
aedart committed Feb 7, 2024
1 parent f91e407 commit cba16ca
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions packages/support/src/mixins/mix.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import type { MixinFunction } from "@aedart/contracts/support/mixins";
import { Mixin } from './decorators';

/**
* Mix target class with one or more Abstract subclasses ("Mixins")
* Mix target class with one or more abstract subclasses ("Mixins")
*
* **Note**: _Method is intended to be used as a decorator!_
*
* @example:
* ```ts
* const BoxMixin = <T extends AbstractConstructor>(superclass: T) => class extends superclass {
* // ...not shown...
* }
*
* @mix(BoxMixin)
* class A {}
* ```
*
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/extends#mix-ins
* @see https://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/
* @see https://justinfagnani.com/2016/01/07/enhancing-mixins-with-decorator-functions/
*
* @param {...MixinFunction} mixins
*
Expand Down Expand Up @@ -46,29 +56,15 @@ export function mix(...mixins: MixinFunction[])
return superclass;
}

// Prepare the mixin and apply it...
return prepare(mixin)(superclass);
// Apply the mixin...
return mixin(superclass);
}, parent);

// Finally, change target to inherit from the "superclass" and return it.
return mergeClasses(target, superclass);
};
}

/**
* Prepares the given mixin, before it is applied on a superclass
*
* @param {MixinFunction} mixin
*
* @returns {MixinFunction}
*/
function prepare(mixin: MixinFunction): MixinFunction
{
// TODO: This SHOULD perhaps allow for customisation?

return Mixin(mixin);
}

/**
* Sets the prototype of given target to that of given superclass
*
Expand Down

0 comments on commit cba16ca

Please sign in to comment.