Skip to content

Commit

Permalink
Convert entryPointStore to composition API + TS
Browse files Browse the repository at this point in the history
  • Loading branch information
davelopez committed Jan 25, 2024
1 parent fb41f4f commit 742a46d
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 75 deletions.
74 changes: 0 additions & 74 deletions client/src/stores/entryPointStore.js

This file was deleted.

2 changes: 1 addition & 1 deletion client/src/stores/entryPointStore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("stores/EntryPointStore", () => {
expect(store.entryPoints.length).toBe(2);
});
it("stops polling", async () => {
expect(store.pollTimeout >= 0).toBeTruthy();
expect(store.pollTimeout !== undefined).toBeTruthy();
store.stopPollingEntryPoints();
expect(store.pollTimeout === undefined).toBeTruthy();
});
Expand Down
103 changes: 103 additions & 0 deletions client/src/stores/entryPointStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import axios from "axios";
import isEqual from "lodash.isequal";
import { defineStore } from "pinia";
import { computed, ref } from "vue";

import { getAppRoot } from "@/onload/loadConfig";
import { rethrowSimple } from "@/utils/simple-error";

// TODO: replace with the corresponding autogenerated model when ready
interface EntryPoint {
model_class: "InteractiveToolEntryPoint";
id: string;
job_id: string;
name: string;
active: boolean;
created_time: string;
modified_time: string;
output_datasets_ids: string[];
target?: string;
}

export const useEntryPointStore = defineStore("entryPointStore", () => {
const pollTimeout = ref<NodeJS.Timeout | undefined>(undefined);
const entryPoints = ref<EntryPoint[]>([]);

const entryPointsForJob = computed(() => {
return (jobId: string) => entryPoints.value.filter((entryPoint) => entryPoint["job_id"] === jobId);
});

const entryPointsForHda = computed(() => {
return (hdaId: string) =>
entryPoints.value.filter((entryPoint) => entryPoint["output_datasets_ids"].includes(hdaId));
});

async function ensurePollingEntryPoints() {
await fetchEntryPoints();
pollTimeout.value = setTimeout(() => {
ensurePollingEntryPoints();
}, 10000);
}

function stopPollingEntryPoints() {
clearTimeout(pollTimeout.value);
pollTimeout.value = undefined;
}

async function fetchEntryPoints() {
const url = `${getAppRoot()}api/entry_points`;
const params = { running: true };
try {
const response = await axios.get(url, { params: params });
updateEntryPoints(response.data);
} catch (e) {
rethrowSimple(e);
}
}

function updateEntryPoints(data: EntryPoint[]) {
let hasChanged = entryPoints.value.length !== data.length ? true : false;
if (entryPoints.value.length === 0) {
entryPoints.value = data;
} else {
const newEntryPoints = [];
for (const ep of data) {
const olderEntryPoint = entryPoints.value.filter((item) => item.id === ep.id)[0];
if (!hasChanged && !isEqual(olderEntryPoint, ep)) {
hasChanged = true;
}
if (olderEntryPoint) {
newEntryPoints.push(mergeEntryPoints(olderEntryPoint, ep));
}
}
if (hasChanged) {
entryPoints.value = newEntryPoints;
}
}
}

function mergeEntryPoints(original: EntryPoint, updated: EntryPoint) {
return { ...original, ...updated };
}

function removeEntryPoint(toolId: string) {
const index = entryPoints.value.findIndex((ep) => {
return ep.id === toolId ? true : false;
});
if (index >= 0) {
entryPoints.value.splice(index, 1);
}
}

return {
entryPoints,
entryPointsForJob,
entryPointsForHda,
fetchEntryPoints,
updateEntryPoints,
removeEntryPoint,
pollTimeout,
ensurePollingEntryPoints,
stopPollingEntryPoints,
};
});

0 comments on commit 742a46d

Please sign in to comment.