|
1 |
| -import type { NameResultSchema, RuntimeItemSchema } from "@mat3ra/esse/dist/js/types"; |
| 1 | +import type { NameResultSchema } from "@mat3ra/esse/dist/js/types"; |
2 | 2 |
|
3 | 3 | import { safeMakeObject } from "../../utils/object";
|
4 | 4 | import type { Constructor } from "../../utils/types";
|
@@ -29,116 +29,52 @@ export type BaseRuntimeItemsInMemoryEntity = InMemoryEntity & {
|
29 | 29 | * Is meant to work with Entity, InMemoryEntity b/c of `prop` extraction from `_json`.
|
30 | 30 | */
|
31 | 31 | 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); |
36 | 36 | },
|
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]; |
87 | 37 |
|
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); |
93 | 40 | },
|
94 | 41 |
|
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); |
98 | 45 | },
|
99 | 46 |
|
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, |
104 | 51 | );
|
105 | 52 | },
|
106 | 53 |
|
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 | + }; |
133 | 60 | },
|
134 | 61 | };
|
135 | 62 |
|
136 | 63 | Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
|
137 |
| - |
138 |
| - return properties; |
139 | 64 | }
|
140 | 65 |
|
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 | + |
142 | 78 | export type RuntimeItemsInMemoryEntityConstructor = Constructor<RuntimeItemsInMemoryEntity>;
|
143 | 79 |
|
144 | 80 | export default function RuntimeItemsMixin<S extends Constructor<BaseRuntimeItemsInMemoryEntity>>(
|
|
0 commit comments