Skip to content

Commit 539184d

Browse files
committed
chore: rewrite RuntimeItemsMixin
1 parent b2147f0 commit 539184d

File tree

5 files changed

+67
-189
lines changed

5 files changed

+67
-189
lines changed
Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { NameResultSchema, RuntimeItemSchema } from "@mat3ra/esse/dist/js/types";
1+
import type { NameResultSchema } from "@mat3ra/esse/dist/js/types";
22
import type { Constructor } from "../../utils/types";
33
import { InMemoryEntity } from "../in_memory";
44
export declare enum ItemKey {
@@ -19,25 +19,17 @@ export type BaseRuntimeItemsInMemoryEntity = InMemoryEntity & {
1919
defaultPreProcessors?: NameResultSchema[];
2020
defaultPostProcessors?: NameResultSchema[];
2121
};
22-
export declare function runtimeItemsMixin(item: BaseRuntimeItemsInMemoryEntity): {
22+
export declare function runtimeItemsMixin(item: BaseRuntimeItemsInMemoryEntity): void;
23+
export type RuntimeItemsInMemoryEntity = {
2324
results: NameResultSchema[];
2425
monitors: NameResultSchema[];
2526
preProcessors: NameResultSchema[];
2627
postProcessors: NameResultSchema[];
27-
readonly resultNames: string[];
28-
readonly monitorNames: string[];
29-
readonly preProcessorNames: string[];
30-
readonly postProcessorNames: string[];
31-
_addRuntimeItem(key: ItemKey, config: RuntimeItemSchema): void;
32-
_removeRuntimeItem(key: ItemKey, config: RuntimeItemSchema): void;
33-
_removeRuntimeItemByName(key: ItemKey, name: string): void;
34-
_toggleRuntimeItem(key: ItemKey, data: RuntimeItemSchema, isAdding: boolean): void;
35-
toggleResult(data: RuntimeItemSchema, isAdding: boolean): void;
36-
toggleMonitor(data: RuntimeItemSchema, isAdding: boolean): void;
37-
togglePreProcessor(data: RuntimeItemSchema, isAdding: boolean): void;
38-
togglePostProcessor(data: RuntimeItemSchema, isAdding: boolean): void;
39-
getResultByName(name: string): NameResultSchema | undefined;
28+
hashObjectFromRuntimeItems: {
29+
results: NameResultSchema[];
30+
preProcessors: NameResultSchema[];
31+
postProcessors: NameResultSchema[];
32+
};
4033
};
41-
export type RuntimeItemsInMemoryEntity = ReturnType<typeof runtimeItemsMixin>;
4234
export type RuntimeItemsInMemoryEntityConstructor = Constructor<RuntimeItemsInMemoryEntity>;
4335
export default function RuntimeItemsMixin<S extends Constructor<BaseRuntimeItemsInMemoryEntity>>(superclass: S): S & RuntimeItemsInMemoryEntityConstructor;

dist/js/entity/mixins/RuntimeItemsMixin.js

Lines changed: 17 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -16,95 +16,35 @@ var ItemKey;
1616
* Is meant to work with Entity, InMemoryEntity b/c of `prop` extraction from `_json`.
1717
*/
1818
function runtimeItemsMixin(item) {
19+
// @ts-expect-error - this is a hack to get the properties of the item
1920
const properties = {
2021
get results() {
21-
const self = this;
22-
return self.prop("results", self.defaultResults || []).map(object_1.safeMakeObject);
23-
},
24-
set results(array) {
25-
const self = this;
26-
self.setProp("results", array);
22+
var _a;
23+
return this.prop("results", (_a = this.defaultResults) !== null && _a !== void 0 ? _a : []).map(object_1.safeMakeObject);
2724
},
2825
get monitors() {
29-
const self = this;
30-
return self.prop("monitors", self.defaultMonitors || []).map(object_1.safeMakeObject);
31-
},
32-
set monitors(array) {
33-
const self = this;
34-
self.setProp("monitors", array);
26+
var _a;
27+
return this.prop("monitors", (_a = this.defaultMonitors) !== null && _a !== void 0 ? _a : []).map(object_1.safeMakeObject);
3528
},
3629
get preProcessors() {
37-
const self = this;
38-
return self.prop("preProcessors", self.defaultPreProcessors || []).map(object_1.safeMakeObject);
39-
},
40-
set preProcessors(array) {
41-
const self = this;
42-
self.setProp("preProcessors", array);
30+
var _a;
31+
// TODO: safeMakeObject could return null. Should we throw an error here?
32+
return this.prop("preProcessors", (_a = this.defaultPreProcessors) !== null && _a !== void 0 ? _a : []).map(object_1.safeMakeObject);
4333
},
4434
get postProcessors() {
45-
const self = this;
46-
return self
47-
.prop("postProcessors", self.defaultPostProcessors || [])
48-
.map(object_1.safeMakeObject);
49-
},
50-
set postProcessors(array) {
51-
const self = this;
52-
self.setProp("postProcessors", array);
53-
},
54-
get resultNames() {
55-
return this.results.map((r) => r === null || r === void 0 ? void 0 : r.name);
56-
},
57-
get monitorNames() {
58-
return this.monitors.map((r) => r === null || r === void 0 ? void 0 : r.name);
59-
},
60-
get preProcessorNames() {
61-
return this.preProcessors.map((r) => r === null || r === void 0 ? void 0 : r.name);
62-
},
63-
get postProcessorNames() {
64-
return this.postProcessors.map((r) => r === null || r === void 0 ? void 0 : r.name);
65-
},
66-
_addRuntimeItem(key, config) {
67-
const self = this;
68-
const runtimeItems = self._json[key || ItemKey.results];
69-
if (!runtimeItems) {
70-
throw new Error("not found");
71-
}
72-
runtimeItems.push((0, object_1.safeMakeObject)(config));
73-
},
74-
_removeRuntimeItem(key, config) {
75-
const newConfig = (0, object_1.safeMakeObject)(config);
76-
this._removeRuntimeItemByName(key, (newConfig === null || newConfig === void 0 ? void 0 : newConfig.name) || "");
77-
},
78-
_removeRuntimeItemByName(key, name) {
79-
const self = this;
80-
self._json[key] = self._json[key].filter((x) => x.name !== name);
81-
},
82-
_toggleRuntimeItem(key, data, isAdding) {
83-
if (isAdding) {
84-
this._addRuntimeItem(key, data);
85-
}
86-
else {
87-
this._removeRuntimeItem(key, data);
88-
}
89-
},
90-
toggleResult(data, isAdding) {
91-
this._toggleRuntimeItem(ItemKey.results, data, isAdding);
92-
},
93-
toggleMonitor(data, isAdding) {
94-
this._toggleRuntimeItem(ItemKey.monitors, data, isAdding);
95-
},
96-
togglePreProcessor(data, isAdding) {
97-
this._toggleRuntimeItem(ItemKey.preProcessors, data, isAdding);
98-
},
99-
togglePostProcessor(data, isAdding) {
100-
this._toggleRuntimeItem(ItemKey.postProcessors, data, isAdding);
35+
var _a;
36+
// TODO: safeMakeObject could return null. Should we throw an error here?
37+
return this.prop("postProcessors", (_a = this.defaultPostProcessors) !== null && _a !== void 0 ? _a : []).map(object_1.safeMakeObject);
10138
},
102-
getResultByName(name) {
103-
return this.results.find((r) => (r === null || r === void 0 ? void 0 : r.name) === name);
39+
get hashObjectFromRuntimeItems() {
40+
return {
41+
results: this.results,
42+
preProcessors: this.preProcessors,
43+
postProcessors: this.postProcessors,
44+
};
10445
},
10546
};
10647
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
107-
return properties;
10848
}
10949
function RuntimeItemsMixin(superclass) {
11050
class RuntimeItemsMixin extends superclass {

dist/js/entity/mixins/runtime_items.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ export declare function RuntimeItemsUILogicMixin<T extends Constructor<BaseRunti
6666
monitors: NameResultSchema[];
6767
preProcessors: NameResultSchema[];
6868
postProcessors: NameResultSchema[];
69+
hashObjectFromRuntimeItems: {
70+
results: NameResultSchema[];
71+
preProcessors: NameResultSchema[];
72+
postProcessors: NameResultSchema[];
73+
};
6974
};
7075
} & T;
7176
export declare function RuntimeItemsUIAllowedMixin<T extends InMemoryEntityConstructor>(superclass: T): {

dist/js/entity/other.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,11 @@ export declare const NamedDefaultableRepetitionRuntimeItemsImportantSettingsCont
369369
monitors: import("@mat3ra/esse/dist/js/types").NameResultSchema[];
370370
preProcessors: import("@mat3ra/esse/dist/js/types").NameResultSchema[];
371371
postProcessors: import("@mat3ra/esse/dist/js/types").NameResultSchema[];
372+
hashObjectFromRuntimeItems: {
373+
results: import("@mat3ra/esse/dist/js/types").NameResultSchema[];
374+
preProcessors: import("@mat3ra/esse/dist/js/types").NameResultSchema[];
375+
postProcessors: import("@mat3ra/esse/dist/js/types").NameResultSchema[];
376+
};
372377
};
373378
} & {
374379
new (...args: any[]): {

src/js/entity/mixins/RuntimeItemsMixin.ts

Lines changed: 32 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { NameResultSchema, RuntimeItemSchema } from "@mat3ra/esse/dist/js/types";
1+
import type { NameResultSchema } from "@mat3ra/esse/dist/js/types";
22

33
import { safeMakeObject } from "../../utils/object";
44
import type { Constructor } from "../../utils/types";
@@ -29,116 +29,52 @@ export type BaseRuntimeItemsInMemoryEntity = InMemoryEntity & {
2929
* Is meant to work with Entity, InMemoryEntity b/c of `prop` extraction from `_json`.
3030
*/
3131
export function runtimeItemsMixin(item: BaseRuntimeItemsInMemoryEntity) {
32-
const properties = {
33-
get results() {
34-
const self = this as unknown as BaseRuntimeItemsInMemoryEntity;
35-
return self.prop("results", self.defaultResults || []).map(safeMakeObject);
32+
// @ts-expect-error - this is a hack to get the properties of the item
33+
const properties: BaseRuntimeItemsInMemoryEntity & RuntimeItemsInMemoryEntity = {
34+
get results(): NameResultSchema[] {
35+
return this.prop("results", this.defaultResults ?? []).map(safeMakeObject);
3636
},
37-
set results(array: NameResultSchema[]) {
38-
const self = this as unknown as BaseRuntimeItemsInMemoryEntity;
39-
self.setProp("results", array);
40-
},
41-
get monitors(): NameResultSchema[] {
42-
const self = this as unknown as BaseRuntimeItemsInMemoryEntity;
43-
return self.prop("monitors", self.defaultMonitors || []).map(safeMakeObject);
44-
},
45-
set monitors(array: NameResultSchema[]) {
46-
const self = this as unknown as BaseRuntimeItemsInMemoryEntity;
47-
self.setProp("monitors", array);
48-
},
49-
get preProcessors(): NameResultSchema[] {
50-
const self = this as unknown as BaseRuntimeItemsInMemoryEntity;
51-
return self.prop("preProcessors", self.defaultPreProcessors || []).map(safeMakeObject);
52-
},
53-
set preProcessors(array: NameResultSchema[]) {
54-
const self = this as unknown as BaseRuntimeItemsInMemoryEntity;
55-
self.setProp("preProcessors", array);
56-
},
57-
get postProcessors(): NameResultSchema[] {
58-
const self = this as unknown as BaseRuntimeItemsInMemoryEntity;
59-
return self
60-
.prop("postProcessors", self.defaultPostProcessors || [])
61-
.map(safeMakeObject);
62-
},
63-
set postProcessors(array: NameResultSchema[]) {
64-
const self = this as unknown as BaseRuntimeItemsInMemoryEntity;
65-
self.setProp("postProcessors", array);
66-
},
67-
68-
get resultNames() {
69-
return this.results.map((r) => r?.name);
70-
},
71-
72-
get monitorNames() {
73-
return this.monitors.map((r) => r?.name);
74-
},
75-
76-
get preProcessorNames() {
77-
return this.preProcessors.map((r) => r?.name);
78-
},
79-
80-
get postProcessorNames() {
81-
return this.postProcessors.map((r) => r?.name);
82-
},
83-
84-
_addRuntimeItem(key: ItemKey, config: RuntimeItemSchema) {
85-
const self = this as unknown as BaseRuntimeItemsInMemoryEntity;
86-
const runtimeItems = self._json[key || ItemKey.results];
8737

88-
if (!runtimeItems) {
89-
throw new Error("not found");
90-
}
91-
92-
runtimeItems.push(safeMakeObject(config));
38+
get monitors(): NameResultSchema[] {
39+
return this.prop("monitors", this.defaultMonitors ?? []).map(safeMakeObject);
9340
},
9441

95-
_removeRuntimeItem(key: ItemKey, config: RuntimeItemSchema) {
96-
const newConfig = safeMakeObject(config);
97-
this._removeRuntimeItemByName(key, newConfig?.name || "");
42+
get preProcessors(): NameResultSchema[] {
43+
// TODO: safeMakeObject could return null. Should we throw an error here?
44+
return this.prop("preProcessors", this.defaultPreProcessors ?? []).map(safeMakeObject);
9845
},
9946

100-
_removeRuntimeItemByName(key: ItemKey, name: string) {
101-
const self = this as unknown as BaseRuntimeItemsInMemoryEntity;
102-
self._json[key] = (self._json[key] as NameResultSchema[]).filter(
103-
(x) => x.name !== name,
47+
get postProcessors(): NameResultSchema[] {
48+
// TODO: safeMakeObject could return null. Should we throw an error here?
49+
return this.prop("postProcessors", this.defaultPostProcessors ?? []).map(
50+
safeMakeObject,
10451
);
10552
},
10653

107-
_toggleRuntimeItem(key: ItemKey, data: RuntimeItemSchema, isAdding: boolean) {
108-
if (isAdding) {
109-
this._addRuntimeItem(key, data);
110-
} else {
111-
this._removeRuntimeItem(key, data);
112-
}
113-
},
114-
115-
toggleResult(data: RuntimeItemSchema, isAdding: boolean) {
116-
this._toggleRuntimeItem(ItemKey.results, data, isAdding);
117-
},
118-
119-
toggleMonitor(data: RuntimeItemSchema, isAdding: boolean) {
120-
this._toggleRuntimeItem(ItemKey.monitors, data, isAdding);
121-
},
122-
123-
togglePreProcessor(data: RuntimeItemSchema, isAdding: boolean) {
124-
this._toggleRuntimeItem(ItemKey.preProcessors, data, isAdding);
125-
},
126-
127-
togglePostProcessor(data: RuntimeItemSchema, isAdding: boolean) {
128-
this._toggleRuntimeItem(ItemKey.postProcessors, data, isAdding);
129-
},
130-
131-
getResultByName(name: string) {
132-
return this.results.find((r) => r?.name === name);
54+
get hashObjectFromRuntimeItems() {
55+
return {
56+
results: this.results,
57+
preProcessors: this.preProcessors,
58+
postProcessors: this.postProcessors,
59+
};
13360
},
13461
};
13562

13663
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
137-
138-
return properties;
13964
}
14065

141-
export type RuntimeItemsInMemoryEntity = ReturnType<typeof runtimeItemsMixin>;
66+
export type RuntimeItemsInMemoryEntity = {
67+
results: NameResultSchema[];
68+
monitors: NameResultSchema[];
69+
preProcessors: NameResultSchema[];
70+
postProcessors: NameResultSchema[];
71+
hashObjectFromRuntimeItems: {
72+
results: NameResultSchema[];
73+
preProcessors: NameResultSchema[];
74+
postProcessors: NameResultSchema[];
75+
};
76+
};
77+
14278
export type RuntimeItemsInMemoryEntityConstructor = Constructor<RuntimeItemsInMemoryEntity>;
14379

14480
export default function RuntimeItemsMixin<S extends Constructor<BaseRuntimeItemsInMemoryEntity>>(

0 commit comments

Comments
 (0)