mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-02 21:16:09 +00:00
fix(deps): mixer correct mix constructors base (#327)
This commit is contained in:
parent
dda74f7882
commit
5b1122de3e
@ -24,10 +24,8 @@ function getNodeDescriptors(c: TypeClass) {
|
|||||||
const result: Record<string, TypedPropertyDescriptor<unknown> | PropertyDescriptor>[] = [];
|
const result: Record<string, TypedPropertyDescriptor<unknown> | PropertyDescriptor>[] = [];
|
||||||
while (proto) {
|
while (proto) {
|
||||||
const descriptors = Object.getOwnPropertyDescriptors(proto);
|
const descriptors = Object.getOwnPropertyDescriptors(proto);
|
||||||
// @ts-expect-error this is not a function in all cases
|
|
||||||
if (descriptors.valueOf.configurable) break;
|
|
||||||
result.push(descriptors);
|
result.push(descriptors);
|
||||||
proto = proto.__proto__;
|
proto = Object.getPrototypeOf(proto);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -44,31 +42,29 @@ function getDescriptors(c: TypeClass) {
|
|||||||
* @returns The mixed class.
|
* @returns The mixed class.
|
||||||
*/
|
*/
|
||||||
export function Mixin<T, C extends TypeClass[]>(...args: C): C[number] & T {
|
export function Mixin<T, C extends TypeClass[]>(...args: C): C[number] & T {
|
||||||
const ignoreOverwriteToString = Object.keys(Object.getOwnPropertyDescriptors(args[0].prototype)).includes('toString');
|
const Base = args[0];
|
||||||
function MixedClass(...constructorArgs: any[]) {
|
|
||||||
for (const i of args) {
|
|
||||||
const descriptors = getDescriptors(i).reverse();
|
|
||||||
for (const j of descriptors) {
|
|
||||||
// @ts-expect-error
|
|
||||||
Object.assign(this, new j.constructor.value(...constructorArgs));
|
|
||||||
|
|
||||||
for (const descriptorK in j) {
|
class MixedClass extends Base {
|
||||||
if (descriptorK === 'constructor') continue;
|
constructor(...constructorArgs: any[]) {
|
||||||
if (descriptorK in MixedClass.prototype && descriptorK !== 'toString') continue;
|
super(...constructorArgs);
|
||||||
const descriptor = j[descriptorK];
|
|
||||||
if (descriptor.value) {
|
for (const mixin of args.slice(1)) {
|
||||||
if (descriptorK === 'toString' && ignoreOverwriteToString) {
|
const descriptors = getDescriptors(mixin).reverse();
|
||||||
MixedClass.prototype[descriptorK] = args[0].prototype.toString;
|
for (const desc of descriptors) {
|
||||||
continue;
|
for (const key in desc) {
|
||||||
|
if (key === 'constructor') continue;
|
||||||
|
if (key in mixin.prototype) continue;
|
||||||
|
const descriptor = desc[key];
|
||||||
|
|
||||||
|
if (descriptor.value) {
|
||||||
|
// @ts-expect-error
|
||||||
|
MixedClass.prototype[key] = descriptor.value;
|
||||||
|
} else if (descriptor.get || descriptor.set) {
|
||||||
|
Object.defineProperty(MixedClass.prototype, key, {
|
||||||
|
get: descriptor.get,
|
||||||
|
set: descriptor.set,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
MixedClass.prototype[descriptorK] = descriptor.value;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (descriptor.get || descriptor.set) {
|
|
||||||
Object.defineProperty(MixedClass.prototype, descriptorK, {
|
|
||||||
get: descriptor.get,
|
|
||||||
set: descriptor.set,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -89,11 +85,9 @@ export const mix =
|
|||||||
(decoratedClass: any) => {
|
(decoratedClass: any) => {
|
||||||
ingredients.unshift(decoratedClass);
|
ingredients.unshift(decoratedClass);
|
||||||
const mixedClass = Mixin(...ingredients);
|
const mixedClass = Mixin(...ingredients);
|
||||||
|
|
||||||
Object.defineProperty(mixedClass, 'name', {
|
Object.defineProperty(mixedClass, 'name', {
|
||||||
value: decoratedClass.name,
|
value: decoratedClass.name,
|
||||||
writable: false,
|
writable: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
return mixedClass as any;
|
return mixedClass as any;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user