Skip to content

Commit

Permalink
keep tool search worker in toolStore to prevent multiple bundle fet…
Browse files Browse the repository at this point in the history
…ches
  • Loading branch information
ahmedhamidawan committed Mar 27, 2024
1 parent f719222 commit 0d75267
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
24 changes: 13 additions & 11 deletions client/src/components/Panels/Common/ToolSearch.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script setup lang="ts">
import { BAlert } from "bootstrap-vue";
import { storeToRefs } from "pinia";
import { computed, type ComputedRef, onMounted, onUnmounted, type PropType, type Ref, ref, watch } from "vue";
import { computed, type ComputedRef, onMounted, type PropType, watch } from "vue";
import { useRouter } from "vue-router/composables";
import { type Tool, type ToolSection, useToolStore } from "@/stores/toolStore";
Expand All @@ -12,6 +13,7 @@ import { type ToolSearchKeys } from "../utilities";
import DelayedInput from "@/components/Common/DelayedInput.vue";
import FilterMenu from "@/components/Common/FilterMenu.vue";
import LoadingSpan from "@/components/LoadingSpan.vue";
const router = useRouter();
Expand Down Expand Up @@ -67,8 +69,6 @@ const emit = defineEmits<{
(e: "onQuery", query: string): void;
}>();
const searchWorker: Ref<Worker | undefined> = ref(undefined);
const localFilterText = computed({
get: () => {
return props.query !== null ? props.query : "";
Expand Down Expand Up @@ -112,16 +112,19 @@ const ToolFilters: ComputedRef<Filtering<string>> = computed(() => new Filtering
const { currentFavorites } = storeToRefs(useUserStore());
const toolStore = useToolStore();
const { searchWorker } = storeToRefs(toolStore);
const sectionNames = toolStore.sectionDatalist("default").map((option: { value: string; text: string }) => option.text);
const ontologyList = toolStore
.sectionDatalist("ontology:edam_topics")
.concat(toolStore.sectionDatalist("ontology:edam_operations"));
onMounted(() => {
searchWorker.value = new Worker(new URL("../toolSearch.worker.js", import.meta.url));
searchWorker.value.onmessage = ({ data }) => {
// initialize worker
if (!searchWorker.value) {
searchWorker.value = new Worker(new URL("components/Panels/toolSearch.worker.js", import.meta.url));
}
searchWorker.value!.onmessage = ({ data }) => {
const { type, payload, sectioned, query, closestTerm } = data;
if (type === "searchToolsByKeysResult" && query === props.query) {
emit("onResults", payload, sectioned, closestTerm);
Expand All @@ -133,10 +136,6 @@ onMounted(() => {
};
});
onUnmounted(() => {
searchWorker.value?.terminate();
});
watch(
() => currentFavorites.value.tools,
() => {
Expand Down Expand Up @@ -178,7 +177,7 @@ function onAdvancedSearch(filters: any) {
</script>

<template>
<div>
<div v-if="searchWorker">
<FilterMenu
v-if="props.enableAdvanced"
:class="!propShowAdvanced && 'mb-3'"
Expand Down Expand Up @@ -245,4 +244,7 @@ function onAdvancedSearch(filters: any) {
:placeholder="placeholder"
@change="checkQuery" />
</div>
<BAlert v-else class="mb-3" variant="info" show>
<LoadingSpan message="Loading Tool Search" />
</BAlert>
</template>
6 changes: 5 additions & 1 deletion client/src/components/Panels/ToolPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ const emit = defineEmits<{
const arePanelsFetched = ref(false);
const toolStore = useToolStore();
const { currentPanelView, defaultPanelView, isPanelPopulated, loading, panel, panelViews } = storeToRefs(toolStore);
const { currentPanelView, defaultPanelView, isPanelPopulated, loading, panel, panelViews, currentPanel } =
storeToRefs(toolStore);
const loadingView = ref<string | undefined>(undefined);
const query = ref("");
Expand Down Expand Up @@ -192,6 +193,9 @@ function onInsertWorkflowSteps(workflowId: string, workflowStepCount: number | u
</b-badge>
</div>
</div>
<b-alert v-else-if="currentPanel" class="m-2" variant="info" show>
<LoadingSpan message="Loading Toolbox" />
</b-alert>
</template>

<style lang="scss" scoped>
Expand Down
8 changes: 8 additions & 0 deletions client/src/stores/toolStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ export const useToolStore = defineStore("toolStore", () => {
const panelViews = ref<Record<string, PanelView>>({});
const loading = ref(false);

const searchWorker = ref<Worker | undefined>(undefined);
// TODO: Where do we terminate the worker?
// onScopeDispose(() => {
// searchWorker.value?.terminate();
// searchWorker.value = undefined;
// });

const getToolForId = computed(() => {
return (toolId: string) => toolsById.value[toolId];
});
Expand Down Expand Up @@ -278,6 +285,7 @@ export const useToolStore = defineStore("toolStore", () => {
currentPanel,
isPanelPopulated,
sectionDatalist,
searchWorker,
fetchToolForId,
fetchTools,
fetchPanelViews,
Expand Down

0 comments on commit 0d75267

Please sign in to comment.