Skip to content

Commit 4236545

Browse files
committed
chore: context mixins
1 parent 539184d commit 4236545

18 files changed

+566
-370
lines changed

dist/js/context/JobContextMixin.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { JobSchema } from "@mat3ra/esse/dist/js/types";
2+
import type { Constructor } from "../utils/types";
3+
import type { ContextProvider } from "./provider";
4+
export type JobContextMixinType = {
5+
isEdited?: boolean;
6+
job: JobSchema;
7+
_job: JobSchema;
8+
initJobContextMixin: () => void;
9+
};
10+
export declare function jobContextMixin(item: ContextProvider): void;
11+
export declare function JobContextMixin<T extends Constructor<ContextProvider>>(superclass: T): T & Constructor<JobContextMixinType>;

dist/js/context/JobContextMixin.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.jobContextMixin = jobContextMixin;
4+
exports.JobContextMixin = JobContextMixin;
5+
const defaultJob = {
6+
workflow: {
7+
subworkflows: [],
8+
units: [],
9+
},
10+
status: "pre-submission",
11+
compute: {
12+
queue: "D",
13+
nodes: 1,
14+
ppn: 1,
15+
timeLimit: "3600",
16+
},
17+
_project: {
18+
_id: "",
19+
},
20+
};
21+
function jobContextMixin(item) {
22+
const properties = {
23+
isEdited: false,
24+
_job: defaultJob,
25+
get job() {
26+
return this._job;
27+
},
28+
initJobContextMixin() {
29+
const config = this.config;
30+
this._job = (config.context && config.context.job) || defaultJob;
31+
this.isEdited = false; // we always get the `defaultData` (recalculated from scratch, not persistent)
32+
},
33+
};
34+
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
35+
}
36+
function JobContextMixin(superclass) {
37+
jobContextMixin(superclass.prototype);
38+
return superclass;
39+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { MaterialSchema } from "@mat3ra/esse/dist/js/types";
2+
import type { InMemoryEntity } from "../entity/in_memory";
3+
import type { Constructor } from "../utils/types";
4+
import type { ContextProvider } from "./provider";
5+
type Material = InMemoryEntity & MaterialSchema & {
6+
hash: string;
7+
};
8+
export type MaterialContextMixinType = {
9+
isEditedIsSetToFalseOnMaterialUpdate?: boolean;
10+
updateMaterialHash: () => void;
11+
isMaterialCreatedDefault: boolean;
12+
isMaterialUpdated: boolean;
13+
material: Material;
14+
extraData?: {
15+
materialHash: string;
16+
};
17+
};
18+
export declare function materialContextMixin(item: ContextProvider & {
19+
_material: Material;
20+
}): void;
21+
export declare function MaterialContextMixin<T extends Constructor<ContextProvider & {
22+
_material: Material;
23+
}>>(superclass: T): T & Constructor<MaterialContextMixinType>;
24+
export {};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.materialContextMixin = materialContextMixin;
4+
exports.MaterialContextMixin = MaterialContextMixin;
5+
function materialContextMixin(item) {
6+
const properties = {
7+
updateMaterialHash() {
8+
if (this.isEditedIsSetToFalseOnMaterialUpdate)
9+
this.isEdited = false;
10+
this.extraData = { materialHash: this.material.hash };
11+
},
12+
// Workaround: Material.createDefault() used to initiate workflow reducer and hence here too
13+
// does not have an id. Here we catch when such material is used and avoid resetting isEdited
14+
get isMaterialCreatedDefault() {
15+
return !this.material.id;
16+
},
17+
get isMaterialUpdated() {
18+
return Boolean(this.extraData && this.extraData.materialHash !== this.material.hash);
19+
},
20+
get material() {
21+
return this._material;
22+
},
23+
};
24+
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
25+
}
26+
function MaterialContextMixin(superclass) {
27+
materialContextMixin(superclass.prototype);
28+
return superclass;
29+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { MaterialSchema } from "@mat3ra/esse/dist/js/types";
2+
import type { InMemoryEntity } from "../entity/in_memory";
3+
import type { Constructor } from "../utils/types";
4+
import type { ContextProvider } from "./provider";
5+
type Material = InMemoryEntity & MaterialSchema & {
6+
hash: string;
7+
};
8+
type MaterialsContextMixinType = {
9+
materials: Material[];
10+
initMaterialsContextMixin: () => void;
11+
};
12+
export declare function materialsContextMixin(item: ContextProvider & {
13+
_materials: Material[];
14+
}): void;
15+
export declare function MaterialsContextMixin<T extends Constructor<ContextProvider & {
16+
_material: Material;
17+
}>>(superclass: T): T & Constructor<MaterialsContextMixinType>;
18+
export {};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.materialsContextMixin = materialsContextMixin;
4+
exports.MaterialsContextMixin = MaterialsContextMixin;
5+
function materialsContextMixin(item) {
6+
const properties = {
7+
get materials() {
8+
return this._materials;
9+
},
10+
initMaterialsContextMixin() {
11+
var _a;
12+
// @ts-ignore
13+
const materials = (_a = this.config.context) === null || _a === void 0 ? void 0 : _a.materials;
14+
// @ts-ignore
15+
if (!this.constructor.Material) {
16+
throw Error("MaterialsContextMixin: Material is undefined");
17+
}
18+
this._materials =
19+
materials && materials.length
20+
? materials
21+
: // @ts-ignore
22+
[this.constructor.Material.createDefault()];
23+
},
24+
};
25+
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
26+
}
27+
function MaterialsContextMixin(superclass) {
28+
materialsContextMixin(superclass.prototype);
29+
return superclass;
30+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { Constructor } from "../utils/types";
2+
import type { ContextProvider } from "./provider";
3+
export type MethodDataContextMixinType = {
4+
isEdited?: boolean;
5+
methodDataHash?: string;
6+
extraData?: {
7+
methodDataHash?: string;
8+
};
9+
methodData: any;
10+
_methodData: any;
11+
isMethodDataUpdated: boolean;
12+
_initMethodDataHash: () => void;
13+
initMethodDataContextMixin: () => void;
14+
};
15+
export declare function methodDataContextMixin(item: ContextProvider): void;
16+
export declare function MethodDataContextMixin<T extends Constructor<ContextProvider>>(superclass: T): T & Constructor<MethodDataContextMixinType>;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"use strict";
2+
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
return (mod && mod.__esModule) ? mod : { "default": mod };
4+
};
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
exports.methodDataContextMixin = methodDataContextMixin;
7+
exports.MethodDataContextMixin = MethodDataContextMixin;
8+
const crypto_js_1 = __importDefault(require("crypto-js"));
9+
function methodDataContextMixin(item) {
10+
const properties = {
11+
isEdited: false,
12+
_methodData: {},
13+
get methodData() {
14+
return this._methodData;
15+
},
16+
get isMethodDataUpdated() {
17+
return Boolean(this.extraData && this.extraData.methodDataHash !== this.methodDataHash);
18+
},
19+
initMethodDataContextMixin() {
20+
const config = this.config;
21+
this._methodData = (config.context && config.context.methodData) || {};
22+
this.isEdited = Boolean(config.isEdited);
23+
},
24+
/* @summary Replace the logic in constructor with this in order to enable passing `methodDataHash` between
25+
* subsequent initializations of the derived class. Not used at present and kept for the record.
26+
*/
27+
_initMethodDataHash() {
28+
this.methodDataHash = crypto_js_1.default.MD5(JSON.stringify(this.methodData)).toString();
29+
this.extraData = { methodDataHash: this.methodDataHash };
30+
if (!this._methodData) {
31+
this._methodData = {};
32+
this.isEdited = false;
33+
// Commented out to reduce effect on performance. Uncomment for debugging purposes.
34+
// TODO: remove on next refactoring or convert to log
35+
// console.warn("MethodDataContextMixin: methodData is undefined or null");
36+
}
37+
else if (this.isMethodDataUpdated) {
38+
this.isEdited = false;
39+
}
40+
else {
41+
// @ts-ignore
42+
// eslint-disable-next-line no-undef
43+
this.isEdited = this.config.isEdited;
44+
}
45+
},
46+
};
47+
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
48+
}
49+
function MethodDataContextMixin(superclass) {
50+
methodDataContextMixin(superclass.prototype);
51+
return superclass;
52+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { WorkflowSchema } from "@mat3ra/esse/dist/js/types";
2+
import type { Constructor } from "../utils/types";
3+
import type { ContextProvider } from "./provider";
4+
export type WorkflowContextMixinType = {
5+
isEdited?: boolean;
6+
workflow: WorkflowSchema;
7+
_workflow: WorkflowSchema;
8+
initWorkflowContextMixin: () => void;
9+
};
10+
export declare function workflowContextMixin(item: ContextProvider): void;
11+
export declare function WorkflowContextMixin<T extends Constructor<ContextProvider>>(superclass: T): T & Constructor<WorkflowContextMixinType>;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.workflowContextMixin = workflowContextMixin;
4+
exports.WorkflowContextMixin = WorkflowContextMixin;
5+
const defaultWorkflow = {
6+
subworkflows: [],
7+
units: [],
8+
};
9+
function workflowContextMixin(item) {
10+
const properties = {
11+
isEdited: false,
12+
_workflow: defaultWorkflow,
13+
get workflow() {
14+
return this._workflow;
15+
},
16+
initWorkflowContextMixin() {
17+
const config = this.config;
18+
this._workflow = (config.context && config.context.workflow) || defaultWorkflow;
19+
this.isEdited = false; // we always get the `defaultData` (recalculated from scratch, not persistent)
20+
},
21+
};
22+
Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
23+
}
24+
function WorkflowContextMixin(superclass) {
25+
workflowContextMixin(superclass.prototype);
26+
return superclass;
27+
}

0 commit comments

Comments
 (0)