Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: preliminary types added to units and unit config builders #47

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/units/assertion.js → src/units/assertion.ts
@@ -1,8 +1,8 @@
import { UNIT_TYPES } from "../enums";
import { BaseUnit } from "./base";
import { AssertionUnitConfig, UNIT_TYPES } from "./types";

export class AssertionUnit extends BaseUnit {
constructor(config) {
export class AssertionUnit extends BaseUnit<AssertionUnitConfig> {
constructor(config: AssertionUnitConfig) {
super({ ...AssertionUnit.getAssertionConfig(), ...config });
}

Expand All @@ -23,7 +23,7 @@ export class AssertionUnit extends BaseUnit {
return this.prop("errorMessage");
}

getHashObject() {
getHashObject(): Partial<AssertionUnitConfig> {
return { statement: this.statement, errorMessage: this.errorMessage };
}
}
8 changes: 4 additions & 4 deletions src/units/assignment.js → src/units/assignment.ts
@@ -1,8 +1,8 @@
import { UNIT_TYPES } from "../enums";
import { BaseUnit } from "./base";
import { AssignmentUnitConfig, UNIT_TYPES } from "./types";

export class AssignmentUnit extends BaseUnit {
constructor(config) {
export class AssignmentUnit extends BaseUnit<AssignmentUnitConfig> {
constructor(config: AssignmentUnitConfig) {
super({ ...AssignmentUnit.getAssignmentConfig(), ...config });
}

Expand All @@ -28,7 +28,7 @@ export class AssignmentUnit extends BaseUnit {
return this.prop("input");
}

getHashObject() {
getHashObject(): Partial<AssignmentUnitConfig> {
return { input: this.input, operand: this.operand, value: this.value };
}
}
21 changes: 14 additions & 7 deletions src/units/base.js → src/units/base.ts
Expand Up @@ -6,13 +6,19 @@ import { getUUID } from "@exabyte-io/code.js/dist/utils";
import lodash from "lodash";
import { mix } from "mixwith";

import { UNIT_STATUSES } from "../enums";
import { UNIT_STATUSES, UnitConfig } from "./types";

// eslint-disable-next-line max-len
export class BaseUnit extends mix(
export class BaseUnit<T extends UnitConfig> extends mix(
NamedDefaultableRepetitionRuntimeItemsImportantSettingsContextAndRenderHashedInMemoryEntity,
).with(TaggableMixin) {
constructor(config) {
// TODO investigate why this is needed here instead of inherited from the mixin
public prop: <K extends keyof T>(key: K, defaultValue?: T[K]) => T[K];
public setProp: <K extends keyof T>(key: K, value: T[K]) => void;
private repetition: number;
private hashObjectFromRuntimeItems: Partial<UnitConfig>;

constructor(config: UnitConfig) {
super({
...config,
status: config.status || UNIT_STATUSES.idle,
Expand All @@ -22,7 +28,7 @@ export class BaseUnit extends mix(
});
}

static generateFlowChartId() {
static generateFlowChartId(): string {
return getUUID();
}

Expand Down Expand Up @@ -54,11 +60,11 @@ export class BaseUnit extends mix(
this.setProp("status", s);
}

get lastStatusUpdate() {
get lastStatusUpdate(): UnitConfig["statusTrack"][0] {
const statusTrack = this.prop("statusTrack", []).filter(
(s) => (s.repetition || 0) === this.repetition,
);
const sortedStatusTrack = lodash.sortBy(statusTrack || [], (x) => x.trackedAt);
const sortedStatusTrack = lodash.sortBy(statusTrack || [], (x) => x.trackedAt); // TODO: check if this is the right way to sort with TS
return sortedStatusTrack[sortedStatusTrack.length - 1];
}

Expand All @@ -70,7 +76,7 @@ export class BaseUnit extends mix(
return this.prop("isDraft", false);
}

getHashObject() {
getHashObject(): Partial<UnitConfig> {
return { ...this.hashObjectFromRuntimeItems, type: this.type };
}

Expand All @@ -91,4 +97,5 @@ export class BaseUnit extends mix(
};
return super.clone(flowchartIDOverrideConfigAsExtraContext);
}

}
@@ -1,24 +1,31 @@
import { UNIT_TYPES } from "../../enums";
import { AssertionUnitConfig, UNIT_TYPES } from "../types";
import { UnitConfigBuilder } from "./UnitConfigBuilder";

export class AssertionUnitConfigBuilder extends UnitConfigBuilder {
constructor(name, statement, errorMessage) {
export class AssertionUnitConfigBuilder extends UnitConfigBuilder<AssertionUnitConfig> {
private _statement: string;
private _errorMessage: string;

constructor(
name: AssertionUnitConfig["name"],
statement: AssertionUnitConfig["statement"],
errorMessage: AssertionUnitConfig["errorMessage"]
){
super({ name, type: UNIT_TYPES.assertion });
this._statement = statement;
this._errorMessage = errorMessage;
}

statement(str) {
statement(str: string): this {
this._statement = str;
return this;
}

errorMessage(str) {
errorMessage(str: string): this {
this._errorMessage = str;
return this;
}

build() {
build(): AssertionUnitConfig {
return {
...super.build(),
statement: this._statement,
Expand Down
36 changes: 0 additions & 36 deletions src/units/builders/AssignmentUnitConfigBuilder.js

This file was deleted.

44 changes: 44 additions & 0 deletions src/units/builders/AssignmentUnitConfigBuilder.ts
@@ -0,0 +1,44 @@
import { AssignmentUnitConfig, UNIT_TYPES, UnitInput } from "../types";
import { UnitConfigBuilder } from "./UnitConfigBuilder";

export class AssignmentUnitConfigBuilder extends UnitConfigBuilder<AssignmentUnitConfig> {
private _operand: string;
private _value: string;
private _input: UnitInput;

constructor(
name: AssignmentUnitConfig["name"],
operand: AssignmentUnitConfig["operand"],
value: AssignmentUnitConfig["value"],
input: AssignmentUnitConfig["input"] = []
) {
super({ name, type: UNIT_TYPES.assignment });
this._operand = operand;
this._value = value;
this._input = input;
}

input(arr: UnitInput): this {
this._input = arr;
return this;
}

variableName(str: string): this {
this._operand = str;
return this;
}

variableValue(str: string): this {
this._value = str;
return this;
}

build() {
return {
...super.build(),
input: this._input,
operand: this._operand,
value: this._value,
};
}
}
@@ -1,16 +1,28 @@
import { Application, Executable, Flavor } from "@exabyte-io/ade.js";

import { UNIT_TYPES } from "../../enums";
import { UnitConfigBuilder } from "./UnitConfigBuilder";
import { ExecutionUnitConfig, UNIT_TYPES } from "../types";

export class ExecutionUnitConfigBuilder extends UnitConfigBuilder {
static Application = Application;
export class ExecutionUnitConfigBuilder extends UnitConfigBuilder<ExecutionUnitConfig> {

static Executable = Executable;
private flavor: ExecutionUnitConfig["flavor"];
private executable: ExecutionUnitConfig["executable"];
private application: ExecutionUnitConfig["application"];
private _results: ExecutionUnitConfig["flavor"]["results"];
private _monitors: ExecutionUnitConfig["flavor"]["monitors"];
private _preProcessors: ExecutionUnitConfig["flavor"]["preProcessors"];
private _postProcessors: ExecutionUnitConfig["flavor"]["postProcessors"];

static Application = Application;
static Executable = Executable;
static Flavor = Flavor;

constructor(name, application, execName, flavorName, flowchartId) {
constructor(
name : ExecutionUnitConfig["name"],
application: ExecutionUnitConfig["application"],
execName: ExecutionUnitConfig["executable"]["name"],
flavorName: ExecutionUnitConfig["flavor"]["name"],
flowchartId: ExecutionUnitConfig["flowchartId"]
) {
super({ name, type: UNIT_TYPES.execution, flowchartId });

try {
Expand All @@ -27,13 +39,16 @@ export class ExecutionUnitConfigBuilder extends UnitConfigBuilder {
this._postProcessors = this.flavor.postProcessors;
}

initialize(application, execName, flavorName) {
initialize(
application: ExecutionUnitConfig["application"],
execName: ExecutionUnitConfig["executable"]["name"],
flavorName: ExecutionUnitConfig["flavor"]["name"]) {
this.application = application;
this.executable = this.constructor.Executable.create({
this.executable = ExecutionUnitConfigBuilder.Executable.create({
name: execName,
application: this.application,
});
this.flavor = this.constructor.Flavor.create({
this.flavor = ExecutionUnitConfigBuilder.Flavor.create({
name: flavorName,
executable: this.executable,
});
Expand Down
@@ -1,8 +1,18 @@
import { UNIT_TYPES } from "../../enums";
import { IOUnitConfig, IOUnitSubTypes, UNIT_TYPES } from "../types";
import { UnitConfigBuilder } from "./UnitConfigBuilder";

export class IOUnitConfigBuilder extends UnitConfigBuilder {
constructor(name, endpointName, endpointOptions) {
export class IOUnitConfigBuilder extends UnitConfigBuilder<IOUnitConfig> {
private _endpointName: string;
private _endpointOptions: string;
private _variableName: string;
private _subtype: IOUnitSubTypes;
private _source: string;

constructor(
name: IOUnitConfig["name"],
endpointName: string,
endpointOptions: string,
) {
super({ name, type: UNIT_TYPES.io });
this._endpointName = endpointName;
this._endpointOptions = endpointOptions;
Expand All @@ -11,32 +21,32 @@ export class IOUnitConfigBuilder extends UnitConfigBuilder {
this._source = "api";
}

endpointName(str) {
endpointName(str: string): this {
this._endpointName = str;
return this;
}

endpointOptions(options) {
endpointOptions(options: string): this {
this._endpointOptions = options;
return this;
}

variableName(str) {
variableName(str: string): this {
this._variableName = str;
return this;
}

subtype(str) {
subtype(str: IOUnitSubTypes): this {
this._subtype = str;
return this;
}

source(str) {
source(str: string): this {
this._source = str;
return this;
}

build() {
build(): IOUnitConfig {
return {
...super.build(),
subtype: this._subtype,
Expand Down