Skip to content

Commit b2147f0

Browse files
committed
chore: rewrite RuntimeItemsMixin
1 parent 5ef787c commit b2147f0

File tree

7 files changed

+397
-205
lines changed

7 files changed

+397
-205
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import type { NameResultSchema, RuntimeItemSchema } from "@mat3ra/esse/dist/js/types";
2+
import type { Constructor } from "../../utils/types";
3+
import { InMemoryEntity } from "../in_memory";
4+
export declare enum ItemKey {
5+
results = "results",
6+
monitors = "monitors",
7+
preProcessors = "preProcessors",
8+
postProcessors = "postProcessors"
9+
}
10+
export type BaseRuntimeItemsInMemoryEntity = InMemoryEntity & {
11+
_json: {
12+
results?: NameResultSchema[];
13+
monitors?: NameResultSchema[];
14+
preProcessors?: NameResultSchema[];
15+
postProcessors?: NameResultSchema[];
16+
};
17+
defaultResults?: NameResultSchema[];
18+
defaultMonitors?: NameResultSchema[];
19+
defaultPreProcessors?: NameResultSchema[];
20+
defaultPostProcessors?: NameResultSchema[];
21+
};
22+
export declare function runtimeItemsMixin(item: BaseRuntimeItemsInMemoryEntity): {
23+
results: NameResultSchema[];
24+
monitors: NameResultSchema[];
25+
preProcessors: NameResultSchema[];
26+
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;
40+
};
41+
export type RuntimeItemsInMemoryEntity = ReturnType<typeof runtimeItemsMixin>;
42+
export type RuntimeItemsInMemoryEntityConstructor = Constructor<RuntimeItemsInMemoryEntity>;
43+
export default function RuntimeItemsMixin<S extends Constructor<BaseRuntimeItemsInMemoryEntity>>(superclass: S): S & RuntimeItemsInMemoryEntityConstructor;
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.ItemKey = void 0;
4+
exports.runtimeItemsMixin = runtimeItemsMixin;
5+
exports.default = RuntimeItemsMixin;
6+
const object_1 = require("../../utils/object");
7+
var ItemKey;
8+
(function (ItemKey) {
9+
ItemKey["results"] = "results";
10+
ItemKey["monitors"] = "monitors";
11+
ItemKey["preProcessors"] = "preProcessors";
12+
ItemKey["postProcessors"] = "postProcessors";
13+
})(ItemKey || (exports.ItemKey = ItemKey = {}));
14+
/*
15+
* @summary Contains runtime items: results, monitors, pre/postProcessors
16+
* Is meant to work with Entity, InMemoryEntity b/c of `prop` extraction from `_json`.
17+
*/
18+
function runtimeItemsMixin(item) {
19+
const properties = {
20+
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);
27+
},
28+
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);
35+
},
36+
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);
43+
},
44+
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);
101+
},
102+
getResultByName(name) {
103+
return this.results.find((r) => (r === null || r === void 0 ? void 0 : r.name) === name);
104+
},
105+
};
106+
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
107+
return properties;
108+
}
109+
function RuntimeItemsMixin(superclass) {
110+
class RuntimeItemsMixin extends superclass {
111+
}
112+
runtimeItemsMixin(RuntimeItemsMixin.prototype);
113+
return RuntimeItemsMixin;
114+
}

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

Lines changed: 13 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,19 @@
11
import { AnyObject } from "@mat3ra/esse/dist/js/esse/types";
22
import { NameResultSchema, RuntimeItemSchema } from "@mat3ra/esse/dist/js/types";
3+
import type { Constructor } from "src/js/utils/types";
34
import { InMemoryEntityConstructor } from "../in_memory";
4-
export declare enum ItemKey {
5-
results = "results",
6-
monitors = "monitors",
7-
preProcessors = "preProcessors",
8-
postProcessors = "postProcessors"
9-
}
10-
export declare function RuntimeItemsMixin<T extends InMemoryEntityConstructor>(superclass: T): {
11-
new (...args: any[]): {
12-
readonly results: NameResultSchema[];
13-
readonly monitors: NameResultSchema[];
14-
readonly preProcessors: NameResultSchema[];
15-
readonly postProcessors: NameResultSchema[];
16-
readonly defaultResults: NameResultSchema[];
17-
readonly defaultMonitors: NameResultSchema[];
18-
readonly defaultPreProcessors: NameResultSchema[];
19-
readonly defaultPostProcessors: NameResultSchema[];
20-
readonly hashObjectFromRuntimeItems: {
21-
results: NameResultSchema[];
22-
preProcessors: NameResultSchema[];
23-
postProcessors: NameResultSchema[];
24-
};
25-
_json: AnyObject;
26-
prop<T_1 = undefined>(name: string, defaultValue: T_1): T_1;
27-
prop<T_1 = undefined>(name: string): T_1 | undefined;
28-
setProp(name: string, value: unknown): void;
29-
unsetProp(name: string): void;
30-
setProps(json?: AnyObject): /*elided*/ any;
31-
toJSON(exclude?: string[]): AnyObject;
32-
toJSONSafe(exclude?: string[]): AnyObject;
33-
toJSONQuick(exclude?: string[]): AnyObject;
34-
clone(extraContext?: object): /*elided*/ any;
35-
validate(): void;
36-
clean(config: AnyObject): AnyObject;
37-
isValid(): boolean;
38-
readonly cls: string;
39-
getClsName(): string;
40-
getAsEntityReference(byIdOnly: true): {
41-
_id: string;
42-
};
43-
getAsEntityReference(byIdOnly?: false): Required<import("@mat3ra/esse/dist/js/types").EntityReferenceSchema>;
44-
getEntityByName(entities: import("../in_memory").InMemoryEntity[], entity: string, name: string): import("../in_memory").InMemoryEntity;
45-
id: string;
46-
_id: string;
47-
schemaVersion: string;
48-
systemName: string;
49-
readonly slug: string;
50-
readonly isSystemEntity: boolean;
51-
};
52-
} & T;
5+
import RuntimeItemsMixin, { type BaseRuntimeItemsInMemoryEntity, ItemKey } from "./RuntimeItemsMixin";
6+
export { RuntimeItemsMixin, ItemKey };
537
export interface RuntimeItemsUILogicJSON extends AnyObject {
548
results?: NameResultSchema[];
559
monitors?: NameResultSchema[];
5610
preProcessors?: NameResultSchema[];
5711
postProcessors?: NameResultSchema[];
5812
}
59-
export declare function RuntimeItemsUILogicMixin<T extends InMemoryEntityConstructor>(superclass: T): {
13+
export declare function RuntimeItemsUILogicMixin<T extends Constructor<BaseRuntimeItemsInMemoryEntity>>(superclass: T): {
6014
new (...params: any): {
6115
_json: RuntimeItemsUILogicJSON;
62-
getDefaultsByKey(key: ItemKey): NameResultSchema[];
16+
getDefaultsByKey(key: ItemKey): NameResultSchema[] | undefined;
6317
setRuntimeItemsToDefaultValues(): void;
6418
/**
6519
* @summary Must pass config for subclasses to override and use initialization logic
@@ -79,19 +33,6 @@ export declare function RuntimeItemsUILogicMixin<T extends InMemoryEntityConstru
7933
readonly postProcessorNames: string[];
8034
readonly preProcessorNames: string[];
8135
getResultByName(name: string): NameResultSchema | undefined;
82-
readonly results: NameResultSchema[];
83-
readonly monitors: NameResultSchema[];
84-
readonly preProcessors: NameResultSchema[];
85-
readonly postProcessors: NameResultSchema[];
86-
readonly defaultResults: NameResultSchema[];
87-
readonly defaultMonitors: NameResultSchema[];
88-
readonly defaultPreProcessors: NameResultSchema[];
89-
readonly defaultPostProcessors: NameResultSchema[];
90-
readonly hashObjectFromRuntimeItems: {
91-
results: NameResultSchema[];
92-
preProcessors: NameResultSchema[];
93-
postProcessors: NameResultSchema[];
94-
};
9536
prop<T_1 = undefined>(name: string, defaultValue: T_1): T_1;
9637
prop<T_1 = undefined>(name: string): T_1 | undefined;
9738
setProp(name: string, value: unknown): void;
@@ -117,6 +58,14 @@ export declare function RuntimeItemsUILogicMixin<T extends InMemoryEntityConstru
11758
systemName: string;
11859
readonly slug: string;
11960
readonly isSystemEntity: boolean;
61+
defaultResults?: NameResultSchema[];
62+
defaultMonitors?: NameResultSchema[];
63+
defaultPreProcessors?: NameResultSchema[];
64+
defaultPostProcessors?: NameResultSchema[];
65+
results: NameResultSchema[];
66+
monitors: NameResultSchema[];
67+
preProcessors: NameResultSchema[];
68+
postProcessors: NameResultSchema[];
12069
};
12170
} & T;
12271
export declare function RuntimeItemsUIAllowedMixin<T extends InMemoryEntityConstructor>(superclass: T): {

0 commit comments

Comments
 (0)