Skip to content

Commit f1c1ac1

Browse files
committed
update: improve InMemoryEntityInSetMixin
1 parent 55dea8d commit f1c1ac1

File tree

3 files changed

+28
-71
lines changed

3 files changed

+28
-71
lines changed

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

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,12 @@ 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 inMemoryEntityInSetMixin<E extends InMemoryEntity>(item: E): {
7-
getInSetFilteredByCls(cls: string): {
8-
_id: string;
9-
cls?: string;
10-
slug?: string;
11-
type?: string;
12-
index?: number;
13-
}[];
14-
parentEntitySetReference: {
15-
_id: string;
16-
cls?: string;
17-
slug?: string;
18-
type?: string;
19-
index?: number;
20-
} | undefined;
21-
inSet: InSet[];
6+
export declare function inMemoryEntityInSetMixin<E extends InMemoryEntity>(item: E): void;
7+
export type InSetPropertiesInMemoryEntity = {
8+
getInSetFilteredByCls: (cls: string) => InSet[];
9+
parentEntitySetReference: InSet | undefined;
2210
};
23-
export type InMemoryEntityInSet = ReturnType<typeof inMemoryEntityInSetMixin>;
11+
export type InMemoryEntityInSet = SystemInSet & InSetPropertiesInMemoryEntity;
2412
export type InMemoryEntityInSetConstructor = Constructor<InMemoryEntityInSet>;
2513
type Base = Constructor<InMemoryEntity>;
2614
export default function InMemoryEntityInSetMixin<S extends Base = Base>(superclass: S): S & InMemoryEntityInSetConstructor;

dist/js/entity/set/InMemoryEntityInSetMixin.js

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,29 @@
22
Object.defineProperty(exports, "__esModule", { value: true });
33
exports.inMemoryEntityInSetMixin = inMemoryEntityInSetMixin;
44
exports.default = InMemoryEntityInSetMixin;
5-
function schemaMixin(item) {
6-
const schema = {
5+
function inMemoryEntityInSetMixin(item) {
6+
// @ts-expect-error
7+
const properties = {
78
get inSet() {
8-
return item.prop("inSet", []);
9+
return this.prop("inSet", []);
910
},
1011
set inSet(inSet) {
11-
item.setProp("inSet", inSet);
12+
this.setProp("inSet", inSet);
1213
},
13-
};
14-
Object.defineProperties(item, Object.getOwnPropertyDescriptors(schema));
15-
return schema;
16-
}
17-
function propertiesMixin(item) {
18-
const properties = {
1914
getInSetFilteredByCls(cls) {
20-
return item.inSet.filter((ref) => ref.cls === cls);
15+
return this.inSet.filter((ref) => ref.cls === cls);
2116
},
2217
// finds a parent entity set of the same cls (hence `cls` field is absent)
2318
// NOTE: assumes that only one entry of this kind is present => gets the first one
2419
get parentEntitySetReference() {
25-
return item.inSet.find((item) => item._id && !item.cls);
20+
return this.inSet.find((inSetItem) => inSetItem._id && !inSetItem.cls);
2621
},
2722
};
2823
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
29-
return properties;
30-
}
31-
function inMemoryEntityInSetMixin(item) {
32-
return {
33-
...schemaMixin(item),
34-
...propertiesMixin(item),
35-
};
3624
}
3725
function InMemoryEntityInSetMixin(superclass) {
3826
class InMemoryEntityInSetMixin extends superclass {
39-
constructor(...args) {
40-
super(...args);
41-
inMemoryEntityInSetMixin(this);
42-
}
4327
}
28+
inMemoryEntityInSetMixin(InMemoryEntityInSetMixin.prototype);
4429
return InMemoryEntityInSetMixin;
4530
}

src/js/entity/set/InMemoryEntityInSetMixin.ts

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

10-
type InMemoryEntityInSetSchema = ReturnType<typeof schemaMixin>;
11-
12-
function schemaMixin<E extends InMemoryEntity>(item: E) {
13-
const schema = {
10+
export function inMemoryEntityInSetMixin<E extends InMemoryEntity>(item: E) {
11+
// @ts-expect-error
12+
const properties: SystemInSet & InSetPropertiesInMemoryEntity & E = {
1413
get inSet() {
15-
return item.prop<InSet[]>("inSet", []);
14+
return this.prop<InSet[]>("inSet", []);
1615
},
1716

1817
set inSet(inSet: InSet[]) {
19-
item.setProp("inSet", inSet);
18+
this.setProp("inSet", inSet);
2019
},
21-
} satisfies SystemInSetSchema;
22-
23-
Object.defineProperties(item, Object.getOwnPropertyDescriptors(schema));
24-
25-
return schema;
26-
}
2720

28-
function propertiesMixin<E extends InMemoryEntity>(item: E & InMemoryEntityInSetSchema) {
29-
const properties = {
3021
getInSetFilteredByCls(cls: string) {
31-
return item.inSet.filter((ref) => ref.cls === cls);
22+
return this.inSet.filter((ref) => ref.cls === cls);
3223
},
3324

3425
// finds a parent entity set of the same cls (hence `cls` field is absent)
3526
// NOTE: assumes that only one entry of this kind is present => gets the first one
3627
get parentEntitySetReference() {
37-
return item.inSet.find((item) => item._id && !item.cls);
28+
return this.inSet.find((inSetItem) => inSetItem._id && !inSetItem.cls);
3829
},
3930
};
4031

4132
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
42-
43-
return properties;
4433
}
4534

46-
export function inMemoryEntityInSetMixin<E extends InMemoryEntity>(item: E) {
47-
return {
48-
...schemaMixin(item),
49-
...propertiesMixin(item as E & InMemoryEntityInSetSchema),
50-
};
51-
}
35+
export type InSetPropertiesInMemoryEntity = {
36+
getInSetFilteredByCls: (cls: string) => InSet[];
37+
parentEntitySetReference: InSet | undefined;
38+
};
5239

53-
export type InMemoryEntityInSet = ReturnType<typeof inMemoryEntityInSetMixin>;
40+
export type InMemoryEntityInSet = SystemInSet & InSetPropertiesInMemoryEntity;
5441
export type InMemoryEntityInSetConstructor = Constructor<InMemoryEntityInSet>;
5542

5643
type Base = Constructor<InMemoryEntity>;
5744

5845
export default function InMemoryEntityInSetMixin<S extends Base = Base>(superclass: S) {
59-
class InMemoryEntityInSetMixin extends superclass {
60-
constructor(...args: any[]) {
61-
super(...args);
62-
inMemoryEntityInSetMixin(this);
63-
}
64-
}
46+
class InMemoryEntityInSetMixin extends superclass {}
47+
48+
inMemoryEntityInSetMixin(InMemoryEntityInSetMixin.prototype);
6549

6650
return InMemoryEntityInSetMixin as S & InMemoryEntityInSetConstructor;
6751
}

0 commit comments

Comments
 (0)