Skip to content

Commit

Permalink
chore: with mixins +1
Browse files Browse the repository at this point in the history
  • Loading branch information
seankwarren committed Feb 8, 2024
1 parent 3ec7cc7 commit fb388a3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 40 deletions.
47 changes: 24 additions & 23 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import { NamedDefaultableHashedInMemoryEntity } from "@exabyte-io/code.js/dist/e

import lodash from "lodash";

import { Executable } from "./executable";
import { Executable as AdeExecutable } from "./executable";
import { getApplicationConfig, getExecutableConfig } from "./tree";
import { ApplicationConfig, ApplicationData } from "./types";
import { Constructor } from "@exabyte-io/code.js/dist/context";

const Base = NamedDefaultableHashedInMemoryEntity;
type ApplicationBaseEntity = InstanceType<typeof Base>;
abstract class ApplicationBaseEntity extends Base {};

export function ApplicationMixin<
E extends Constructor<AdeExecutable> = Constructor<AdeExecutable>,
T extends Constructor<ApplicationBaseEntity> = Constructor<ApplicationBaseEntity>,
>(superclass: T) {
>(superclass: T, Executable: E) {
return class Application extends superclass {
static Executable = Executable;

Expand All @@ -41,7 +42,7 @@ export function ApplicationMixin<
name: string,
version?: string,
build?: string
}) {
}): Application {
return this.createFromNameVersionBuild(config);
}

Expand All @@ -53,30 +54,30 @@ export function ApplicationMixin<
name: string,
version?: string,
build?: string
}) {
}): Application {
return new Application({ name, version, build });
}

getExecutables() {
return this.executables;
}

getBuilds() {
getBuilds(): string[] {
const data = getAppData(this.prop("name")) as ApplicationData;
const { versions } = data;
const builds = ["Default"];
versions.map((v) => v.build && builds.push(v.build));
return lodash.uniq(builds);
}

getVersions() {
getVersions(): string[] {
const data = getAppData(this.prop("name")) as ApplicationData;
const { versions } = data;
const these: string[] = versions.map((v) => v.version);
return lodash.uniq(these);
}

static getUniqueAvailableNames() {
static getUniqueAvailableNames(): string[] {
return allApplications;
}

Expand All @@ -103,20 +104,20 @@ export function ApplicationMixin<
return [];
}

get summary() {
return this.prop("summary");
get summary(): string {
return this.prop<string>("summary");
}

get version() {
return this.prop("version");
get version(): string {
return this.prop<string>("version");
}

get build() {
return this.prop("build");
get build(): string {
return this.prop<string>("build");
}

get shortName() {
return this.prop("shortName", this.prop("name"));
get shortName(): string {
return this.prop<string>("shortName", this.prop<string>("name"));
}

get executables() {
Expand All @@ -134,21 +135,21 @@ export function ApplicationMixin<
});
}

get hasAdvancedComputeOptions() {
return this.prop("hasAdvancedComputeOptions");
get hasAdvancedComputeOptions(): boolean {
return this.prop<boolean>("hasAdvancedComputeOptions", false);
}

get isLicensed() {
return this.prop("isLicensed");
get isLicensed(): boolean {
return this.prop<boolean>("isLicensed", false);
}

get isUsingMaterial() {
get isUsingMaterial(): boolean {
const materialUsingApplications = ["vasp", "nwchem", "espresso", "exabyteml"];
return materialUsingApplications.includes(this.name);
}
}
}

export const Application = ApplicationMixin(Base);
export const Application = ApplicationMixin(Base, AdeExecutable);

export type Application = InstanceType<typeof Application>;
export type Application = typeof Application;
22 changes: 14 additions & 8 deletions src/executable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,31 @@ import {
RuntimeItemsMixin,
} from "@exabyte-io/code.js/dist/entity";

import { Flavor } from "./flavor";
import { Flavor as AdeFlavor } from "./flavor";
import { FlavorData } from "./types";
import { Constructor } from "@exabyte-io/code.js/dist/context";
import { AnyObject } from "@exabyte-io/code.js/dist/entity/in_memory";

const Base = RuntimeItemsMixin(NamedDefaultableHashedInMemoryEntity);
type ExecutableBaseEntity = InstanceType<typeof Base>;

export function ExecutableMixin<
F extends AdeFlavor = AdeFlavor,
T extends Constructor<ExecutableBaseEntity> = Constructor<ExecutableBaseEntity>,
>(superclass: T) {
return class Executable extends RuntimeItemsMixin(NamedDefaultableHashedInMemoryEntity) {
>(superclass: T, Flavor: F) {
return class Executable extends superclass {
static Flavor = Flavor;

constructor(...config: any[]) {
super(...config);
}

// @ts-ignore
toJSON(exclude) {
return super.toJSON(["flavors"].concat(exclude));
toJSON(exclude?: string[]): AnyObject {
return super.toJSON(["flavors"].concat(exclude ? exclude : []));
}

get flavorsTree() {
get flavorsTree(): Record<string, FlavorData> {
return this.prop<Record<string, FlavorData>>("flavors");
}

Expand All @@ -46,7 +52,7 @@ export function ExecutableMixin<
}

getFlavorByName(name?: string | null) {
return name ? this.getEntityByName(this.flavors, "flavor", name) as Flavor : undefined;
return name ? this.getEntityByName(this.flavors, "flavor", name) : undefined;
}

getFlavorByConfig(config?: {name: string}) {
Expand All @@ -64,6 +70,6 @@ export function ExecutableMixin<
}
}

export const Executable = ExecutableMixin(Base);
export const Executable = ExecutableMixin(Base, AdeFlavor);

export type Executable = InstanceType<typeof Executable>;
15 changes: 8 additions & 7 deletions src/flavor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ import {
import { Template } from "./template";
import { NamedTemplate } from "./types";
import { Constructor } from "@exabyte-io/code.js/dist/context";
import { AnyObject } from "@exabyte-io/code.js/dist/entity/in_memory";

const Base = RuntimeItemsMixin(NamedDefaultableHashedInMemoryEntity);
type FlavorBaseEntity = InstanceType<typeof Base>;

export function FlavorMixin<
T extends Constructor<FlavorBaseEntity> = Constructor<FlavorBaseEntity>,
>(superclass: T) {
return class Flavor extends RuntimeItemsMixin(NamedDefaultableHashedInMemoryEntity) {
get input() {
return class Flavor extends superclass {
get input(): NamedTemplate[] {
return this.prop<NamedTemplate[]>("input", []);
}

get inputAsTemplates() {
get inputAsTemplates(): Template[] {
return this.input.map((input) => {
const templateName = 'name' in input ? input.name : input.templateName;
const template = Template.fromFlavor(
Expand All @@ -33,16 +34,16 @@ export function FlavorMixin<
});
}

getInputAsRenderedTemplates(context: object) {
getInputAsRenderedTemplates(context: object): AnyObject[] {
return this.inputAsTemplates.map((t) => t.getRenderedJSON(context));
}

get disableRenderMaterials() {
return this.prop("isMultiMaterial", false);
get disableRenderMaterials(): boolean {
return this.prop<boolean>("isMultiMaterial", false);
}
}
}

export const Flavor = FlavorMixin(Base);

export type Flavor = InstanceType<typeof Flavor>;
export type Flavor = typeof Flavor;
4 changes: 2 additions & 2 deletions src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import { Constructor, ContextProvider } from "@exabyte-io/code.js/dist/context";
import { TemplateData } from "./types";

const Base = HashedInputArrayMixin(HashedEntityMixin(NamedInMemoryEntity))
type TemplateBaseEntity = InstanceType<typeof Base>;
abstract class TemplateBaseEntity extends Base {};

export function TemplateMixin<
T extends Constructor<TemplateBaseEntity> = Constructor<TemplateBaseEntity>,
>(superclass: T) {
return class Template extends HashedInputArrayMixin(HashedEntityMixin(NamedInMemoryEntity)) {
return class Template extends superclass {
static providerRegistry = ContextProviderRegistry;

get isManuallyChanged() {
Expand Down

0 comments on commit fb388a3

Please sign in to comment.