Skip to content

Commit 497e9b1

Browse files
committed
chore: mixins cleanups
1 parent fed9fc4 commit 497e9b1

File tree

9 files changed

+100
-116
lines changed

9 files changed

+100
-116
lines changed

dist/js/entity/set.d.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { InMemoryEntity } from "./in_memory";
2-
declare const InMemoryEntitySet_base: typeof InMemoryEntity & (new (...args: any[]) => {
2+
declare const InMemoryEntitySet_base: typeof InMemoryEntity & import("./set/InMemoryEntitySetBaseMixin").InMemoryEntitySetBaseConstructor & (new (...args: any[]) => {
33
inSet: import("./set/InMemoryEntityInSetMixin").InSet[];
44
}) & (new (...args: any[]) => {
55
getInSetFilteredByCls(cls: string): {
@@ -18,13 +18,5 @@ declare const InMemoryEntitySet_base: typeof InMemoryEntity & (new (...args: any
1818
} | undefined;
1919
}) & import("./set/InMemoryEntitySetMixin").InMemoryEntitySetConstructor;
2020
export declare class InMemoryEntitySet extends InMemoryEntitySet_base {
21-
get isEntitySet(): boolean | undefined;
22-
get entitySetType(): string | undefined;
23-
get entityCls(): string | undefined;
24-
get cls(): string;
25-
toJSONForInclusionInEntity(): {
26-
_id: string;
27-
type: string;
28-
};
2921
}
3022
export {};

dist/js/entity/set.js

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
11
"use strict";
2+
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
return (mod && mod.__esModule) ? mod : { "default": mod };
4+
};
25
Object.defineProperty(exports, "__esModule", { value: true });
36
exports.InMemoryEntitySet = void 0;
47
const in_memory_1 = require("./in_memory");
8+
const InMemoryEntitySetBaseMixin_1 = __importDefault(require("./set/InMemoryEntitySetBaseMixin"));
59
const mixins_1 = require("./set/mixins");
6-
class InMemoryEntitySet extends (0, mixins_1.InMemoryEntitySetMixin)((0, mixins_1.InMemoryEntityInSetMixin)(in_memory_1.InMemoryEntity)) {
7-
get isEntitySet() {
8-
return this.prop("isEntitySet", false);
9-
}
10-
get entitySetType() {
11-
return this.prop("entitySetType");
12-
}
13-
get entityCls() {
14-
return this.prop("entityCls");
15-
}
16-
get cls() {
17-
return this.entityCls || super.cls;
18-
}
19-
toJSONForInclusionInEntity() {
20-
const { _id, type } = this.toJSON();
21-
return { _id, type };
22-
}
10+
class InMemoryEntitySet extends (0, mixins_1.InMemoryEntitySetMixin)((0, mixins_1.InMemoryEntityInSetMixin)((0, InMemoryEntitySetBaseMixin_1.default)(in_memory_1.InMemoryEntity))) {
2311
}
2412
exports.InMemoryEntitySet = InMemoryEntitySet;

dist/js/entity/set/InMemoryEntityInSetMixin.d.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import type { Constructor } from "../../utils/types";
33
import { type InMemoryEntity } from "../in_memory";
44
export type SystemInSet = Required<SystemInSetSchema>;
55
export type InSet = SystemInSet["inSet"][0];
6-
declare function entityInSetPropsMixin<E extends InMemoryEntity>(item: E): {
6+
declare function schemaMixin<E extends InMemoryEntity>(item: E): {
77
inSet: InSet[];
88
};
9-
declare function entityInSetMethodsMixin<E extends InMemoryEntity>(item: E & InMemoryEntityInSetProps): {
9+
declare function propertiesMixin<E extends InMemoryEntity>(item: E & InMemoryEntityInSetSchema): {
1010
getInSetFilteredByCls(cls: string): {
1111
_id: string;
1212
cls?: string;
@@ -22,13 +22,13 @@ declare function entityInSetMethodsMixin<E extends InMemoryEntity>(item: E & InM
2222
index?: number;
2323
} | undefined;
2424
};
25-
export declare function entityInSetMixin<E extends InMemoryEntity>(item: E): void;
26-
type InMemoryEntityInSetProps = ReturnType<typeof entityInSetPropsMixin>;
27-
type InMemoryEntityInSetPropsConstructor = Constructor<InMemoryEntityInSetProps>;
28-
type InMemoryEntityInSetMethods = ReturnType<typeof entityInSetMethodsMixin>;
29-
type InMemoryEntityInSetMethodsConstructor = Constructor<InMemoryEntityInSetMethods>;
30-
export type InMemoryEntityInSet = InMemoryEntityInSetProps & InMemoryEntityInSetMethods;
31-
export type InMemoryEntityInSetConstructor = InMemoryEntityInSetPropsConstructor & InMemoryEntityInSetMethodsConstructor;
25+
export declare function inMemoryEntityInSetMixin<E extends InMemoryEntity>(item: E): void;
26+
type InMemoryEntityInSetSchema = ReturnType<typeof schemaMixin>;
27+
type InMemoryEntityInSetSchemaConstructor = Constructor<InMemoryEntityInSetSchema>;
28+
type InMemoryEntityInSetProperties = ReturnType<typeof propertiesMixin>;
29+
type InMemoryEntityInSetPropertiesConstructor = Constructor<InMemoryEntityInSetProperties>;
30+
export type InMemoryEntityInSet = InMemoryEntityInSetSchema & InMemoryEntityInSetProperties;
31+
export type InMemoryEntityInSetConstructor = InMemoryEntityInSetSchemaConstructor & InMemoryEntityInSetPropertiesConstructor;
3232
type Base = Constructor<InMemoryEntity>;
3333
export default function InMemoryEntityInSetMixin<S extends Base = Base>(superclass: S): S & InMemoryEntityInSetConstructor;
3434
export {};

dist/js/entity/set/InMemoryEntityInSetMixin.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
exports.entityInSetMixin = entityInSetMixin;
3+
exports.inMemoryEntityInSetMixin = inMemoryEntityInSetMixin;
44
exports.default = InMemoryEntityInSetMixin;
5-
function entityInSetPropsMixin(item) {
6-
const properties = {
5+
function schemaMixin(item) {
6+
const schema = {
77
get inSet() {
88
return item.prop("inSet", []);
99
},
1010
set inSet(inSet) {
1111
item.setProp("inSet", inSet);
1212
},
1313
};
14-
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
15-
return properties;
14+
Object.defineProperties(item, Object.getOwnPropertyDescriptors(schema));
15+
return schema;
1616
}
17-
function entityInSetMethodsMixin(item) {
18-
const methods = {
17+
function propertiesMixin(item) {
18+
const properties = {
1919
getInSetFilteredByCls(cls) {
2020
return item.inSet.filter((ref) => ref.cls === cls);
2121
},
@@ -25,18 +25,18 @@ function entityInSetMethodsMixin(item) {
2525
return item.inSet.find((item) => item._id && !item.cls);
2626
},
2727
};
28-
Object.defineProperties(item, Object.getOwnPropertyDescriptors(methods));
29-
return methods;
28+
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
29+
return properties;
3030
}
31-
function entityInSetMixin(item) {
32-
entityInSetPropsMixin(item);
33-
entityInSetMethodsMixin(item);
31+
function inMemoryEntityInSetMixin(item) {
32+
schemaMixin(item);
33+
propertiesMixin(item);
3434
}
3535
function InMemoryEntityInSetMixin(superclass) {
3636
class InMemoryEntityInSetMixin extends superclass {
3737
constructor(...args) {
3838
super(...args);
39-
entityInSetMixin(this);
39+
inMemoryEntityInSetMixin(this);
4040
}
4141
}
4242
return InMemoryEntityInSetMixin;

dist/js/entity/set/InMemoryEntitySetBaseMixin.d.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
import { SystemInSetSchema } from "@mat3ra/esse/dist/js/types";
1+
import { type EntitySetSchema, SystemInSetSchema } from "@mat3ra/esse/dist/js/types";
22
import type { Constructor } from "../../utils/types";
33
import { type InMemoryEntity } from "../in_memory";
44
export type SystemInSet = Required<SystemInSetSchema>;
55
export type InSet = SystemInSet["inSet"][0];
6-
export declare function inMemoryEntitySetBaseMixin<T extends InMemoryEntity>(item: T): {
7-
readonly isEntitySet: boolean | undefined;
8-
readonly entitySetType: string | undefined;
9-
readonly entityCls: string | undefined;
6+
declare function schemaMixin<E extends InMemoryEntity>(item: E): {
7+
readonly isEntitySet: boolean;
8+
readonly entitySetType: undefined;
9+
readonly entityCls: undefined;
10+
};
11+
declare function methodsMixin<E extends InMemoryEntity>(item: E & EntitySetSchema): {
1012
readonly cls: string;
1113
toJSONForInclusionInEntity(): {
1214
_id: string;
1315
type: string;
1416
};
1517
};
16-
export type InMemoryEntitySetBase = ReturnType<typeof inMemoryEntitySetBaseMixin>;
18+
export declare function inMemoryEntitySetBaseMixin<T extends InMemoryEntity>(item: T): void;
19+
export type InMemoryEntitySetBase = ReturnType<typeof schemaMixin> & ReturnType<typeof methodsMixin>;
1720
export type InMemoryEntitySetBaseConstructor = Constructor<InMemoryEntitySetBase>;
1821
type Base = Constructor<InMemoryEntity>;
1922
export default function InMemoryEntitySetBaseMixin<S extends Base = Base>(superclass: S): S & InMemoryEntitySetBaseConstructor;

dist/js/entity/set/InMemoryEntitySetBaseMixin.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
Object.defineProperty(exports, "__esModule", { value: true });
33
exports.inMemoryEntitySetBaseMixin = inMemoryEntitySetBaseMixin;
44
exports.default = InMemoryEntitySetBaseMixin;
5-
function inMemoryEntitySetBaseMixin(item) {
6-
const originalCls = item.cls;
7-
const properties = {
5+
function schemaMixin(item) {
6+
const schema = {
87
get isEntitySet() {
98
return item.prop("isEntitySet", false);
109
},
@@ -14,16 +13,27 @@ function inMemoryEntitySetBaseMixin(item) {
1413
get entityCls() {
1514
return item.prop("entityCls");
1615
},
16+
};
17+
Object.defineProperties(item, Object.getOwnPropertyDescriptors(schema));
18+
return schema;
19+
}
20+
function methodsMixin(item) {
21+
const originalCls = item.cls;
22+
const methods = {
1723
get cls() {
18-
return this.entityCls || originalCls;
24+
return item.entityCls || originalCls;
1925
},
2026
toJSONForInclusionInEntity() {
2127
const { _id, type } = item.toJSON();
2228
return { _id, type };
2329
},
2430
};
25-
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
26-
return properties;
31+
Object.defineProperties(item, Object.getOwnPropertyDescriptors(methods));
32+
return methods;
33+
}
34+
function inMemoryEntitySetBaseMixin(item) {
35+
schemaMixin(item);
36+
methodsMixin(item);
2737
}
2838
function InMemoryEntitySetBaseMixin(superclass) {
2939
class InMemoryEntitySetBaseMixin extends superclass {

src/js/entity/set.ts

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,7 @@
1-
import { EntitySetSchema } from "@mat3ra/esse/dist/js/types";
2-
31
import { InMemoryEntity } from "./in_memory";
2+
import InMemoryEntitySetBaseMixin from "./set/InMemoryEntitySetBaseMixin";
43
import { InMemoryEntityInSetMixin, InMemoryEntitySetMixin } from "./set/mixins";
54

65
export class InMemoryEntitySet extends InMemoryEntitySetMixin(
7-
InMemoryEntityInSetMixin(InMemoryEntity),
8-
) {
9-
get isEntitySet() {
10-
return this.prop<EntitySetSchema["isEntitySet"]>("isEntitySet", false);
11-
}
12-
13-
get entitySetType() {
14-
return this.prop<EntitySetSchema["entitySetType"]>("entitySetType");
15-
}
16-
17-
get entityCls() {
18-
return this.prop<EntitySetSchema["entityCls"]>("entityCls");
19-
}
20-
21-
get cls() {
22-
return this.entityCls || super.cls;
23-
}
24-
25-
toJSONForInclusionInEntity() {
26-
const { _id, type } = this.toJSON() as { _id: string; type: string };
27-
return { _id, type };
28-
}
29-
}
6+
InMemoryEntityInSetMixin(InMemoryEntitySetBaseMixin(InMemoryEntity)),
7+
) {}

src/js/entity/set/InMemoryEntityInSetMixin.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { type InMemoryEntity } from "../in_memory";
77
export type SystemInSet = Required<SystemInSetSchema>;
88
export type InSet = SystemInSet["inSet"][0];
99

10-
function entityInSetPropsMixin<E extends InMemoryEntity>(item: E) {
11-
const properties = {
10+
function schemaMixin<E extends InMemoryEntity>(item: E) {
11+
const schema = {
1212
get inSet() {
1313
return item.prop<InSet[]>("inSet", []);
1414
},
@@ -18,13 +18,13 @@ function entityInSetPropsMixin<E extends InMemoryEntity>(item: E) {
1818
},
1919
} satisfies SystemInSetSchema;
2020

21-
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
21+
Object.defineProperties(item, Object.getOwnPropertyDescriptors(schema));
2222

23-
return properties;
23+
return schema;
2424
}
2525

26-
function entityInSetMethodsMixin<E extends InMemoryEntity>(item: E & InMemoryEntityInSetProps) {
27-
const methods = {
26+
function propertiesMixin<E extends InMemoryEntity>(item: E & InMemoryEntityInSetSchema) {
27+
const properties = {
2828
getInSetFilteredByCls(cls: string) {
2929
return item.inSet.filter((ref) => ref.cls === cls);
3030
},
@@ -36,32 +36,32 @@ function entityInSetMethodsMixin<E extends InMemoryEntity>(item: E & InMemoryEnt
3636
},
3737
};
3838

39-
Object.defineProperties(item, Object.getOwnPropertyDescriptors(methods));
39+
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
4040

41-
return methods;
41+
return properties;
4242
}
4343

44-
export function entityInSetMixin<E extends InMemoryEntity>(item: E) {
45-
entityInSetPropsMixin(item);
46-
entityInSetMethodsMixin(item as E & InMemoryEntityInSetProps);
44+
export function inMemoryEntityInSetMixin<E extends InMemoryEntity>(item: E) {
45+
schemaMixin(item);
46+
propertiesMixin(item as E & InMemoryEntityInSetSchema);
4747
}
4848

49-
type InMemoryEntityInSetProps = ReturnType<typeof entityInSetPropsMixin>;
50-
type InMemoryEntityInSetPropsConstructor = Constructor<InMemoryEntityInSetProps>;
51-
type InMemoryEntityInSetMethods = ReturnType<typeof entityInSetMethodsMixin>;
52-
type InMemoryEntityInSetMethodsConstructor = Constructor<InMemoryEntityInSetMethods>;
49+
type InMemoryEntityInSetSchema = ReturnType<typeof schemaMixin>;
50+
type InMemoryEntityInSetSchemaConstructor = Constructor<InMemoryEntityInSetSchema>;
51+
type InMemoryEntityInSetProperties = ReturnType<typeof propertiesMixin>;
52+
type InMemoryEntityInSetPropertiesConstructor = Constructor<InMemoryEntityInSetProperties>;
5353

54-
export type InMemoryEntityInSet = InMemoryEntityInSetProps & InMemoryEntityInSetMethods;
55-
export type InMemoryEntityInSetConstructor = InMemoryEntityInSetPropsConstructor &
56-
InMemoryEntityInSetMethodsConstructor;
54+
export type InMemoryEntityInSet = InMemoryEntityInSetSchema & InMemoryEntityInSetProperties;
55+
export type InMemoryEntityInSetConstructor = InMemoryEntityInSetSchemaConstructor &
56+
InMemoryEntityInSetPropertiesConstructor;
5757

5858
type Base = Constructor<InMemoryEntity>;
5959

6060
export default function InMemoryEntityInSetMixin<S extends Base = Base>(superclass: S) {
6161
class InMemoryEntityInSetMixin extends superclass {
6262
constructor(...args: any[]) {
6363
super(...args);
64-
entityInSetMixin(this);
64+
inMemoryEntityInSetMixin(this);
6565
}
6666
}
6767

src/js/entity/set/InMemoryEntitySetBaseMixin.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,51 @@ import { type InMemoryEntity } from "../in_memory";
77
export type SystemInSet = Required<SystemInSetSchema>;
88
export type InSet = SystemInSet["inSet"][0];
99

10-
export function inMemoryEntitySetBaseMixin<T extends InMemoryEntity>(item: T) {
11-
const originalCls = item.cls;
12-
13-
const properties = {
10+
function schemaMixin<E extends InMemoryEntity>(item: E) {
11+
const schema = {
1412
get isEntitySet() {
15-
return item.prop<EntitySetSchema["isEntitySet"]>("isEntitySet", false);
13+
return item.prop("isEntitySet", false);
1614
},
1715

1816
get entitySetType() {
19-
return item.prop<EntitySetSchema["entitySetType"]>("entitySetType");
17+
return item.prop("entitySetType");
2018
},
2119

2220
get entityCls() {
23-
return item.prop<EntitySetSchema["entityCls"]>("entityCls");
21+
return item.prop("entityCls");
2422
},
23+
} satisfies EntitySetSchema;
24+
25+
Object.defineProperties(item, Object.getOwnPropertyDescriptors(schema));
26+
27+
return schema;
28+
}
2529

30+
function methodsMixin<E extends InMemoryEntity>(item: E & EntitySetSchema) {
31+
const originalCls = item.cls;
32+
33+
const methods = {
2634
get cls() {
27-
return this.entityCls || originalCls;
35+
return item.entityCls || originalCls;
2836
},
29-
3037
toJSONForInclusionInEntity() {
3138
const { _id, type } = item.toJSON() as { _id: string; type: string };
3239
return { _id, type };
3340
},
3441
};
3542

36-
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
43+
Object.defineProperties(item, Object.getOwnPropertyDescriptors(methods));
3744

38-
return properties;
45+
return methods;
46+
}
47+
48+
export function inMemoryEntitySetBaseMixin<T extends InMemoryEntity>(item: T) {
49+
schemaMixin(item);
50+
methodsMixin(item as T & EntitySetSchema);
3951
}
4052

41-
export type InMemoryEntitySetBase = ReturnType<typeof inMemoryEntitySetBaseMixin>;
53+
export type InMemoryEntitySetBase = ReturnType<typeof schemaMixin> &
54+
ReturnType<typeof methodsMixin>;
4255
export type InMemoryEntitySetBaseConstructor = Constructor<InMemoryEntitySetBase>;
4356

4457
type Base = Constructor<InMemoryEntity>;

0 commit comments

Comments
 (0)