Skip to content

Commit e35cd3b

Browse files
authored
Merge pull request #293 from XpressAI/fahreza/cache-fetch-component
✨ / 🐛 Cache Fetch Component List
2 parents 45a8f36 + f59b36c commit e35cd3b

File tree

4 files changed

+48
-37
lines changed

4 files changed

+48
-37
lines changed

src/context-menu/ComponentsPanel.tsx

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import ComponentList from '../tray_library/Component';
2-
31
import React, { useEffect, useState } from 'react';
4-
52
import styled from '@emotion/styled';
6-
73
import { JupyterFrontEnd } from '@jupyterlab/application';
84

95
import {
@@ -17,6 +13,7 @@ import {
1713
import { DefaultLinkModel, DiagramEngine } from '@projectstorm/react-diagrams';
1814
import { TrayPanel } from './TrayPanel';
1915
import { TrayItemPanel } from './TrayItemPanel';
16+
import { ComponentList } from '../tray_library/Component';
2017

2118
export const Body = styled.div`
2219
display: flex;

src/tray_library/AdvanceComponentLib.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CustomNodeModel } from "../components/node/CustomNodeModel";
2-
import ComponentList from "./Component";
2+
import { ComponentList } from "./Component";
33

44
interface AdvancedComponentLibraryProps {
55
model: any;

src/tray_library/Component.tsx

+33-29
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
11
import { showDialog, Dialog } from '@jupyterlab/apputils';
2-
32
import { requestAPI } from "../server/handler";
43
import React from 'react';
54

6-
async function get_all_components_method() {
5+
let componentsCache = {
6+
data: null
7+
};
8+
9+
async function fetchComponents() {
10+
console.log("Fetching all components... this might take a while.")
11+
try {
12+
const componentsResponse = await requestAPI<any>('components/');
13+
const components = componentsResponse["components"];
14+
const error_msg = componentsResponse["error_msg"];
715

8-
try {
9-
// Trigger the load library config on refresh
10-
await requestAPI('library/reload_config', { method: 'POST', headers: { 'Content-Type': 'application/json' } });
11-
12-
// Proceed with the GET request to fetch components
13-
const componentsResponse = await requestAPI<any>('components/');
14-
const components = componentsResponse["components"];
15-
const error_msg = componentsResponse["error_msg"];
16-
17-
if (error_msg) {
18-
showDialog({
19-
title: 'Parse Component Failed',
20-
body: (
21-
<pre>{error_msg}</pre>
22-
),
23-
buttons: [Dialog.warnButton({ label: 'OK' })]
24-
});
25-
}
26-
27-
return components;
28-
} catch (error) {
29-
console.error('Failed to get components or trigger library reload', error);
30-
// Handle the error appropriately in your application
16+
if (error_msg) {
17+
showDialog({
18+
title: 'Parse Component Failed',
19+
body: (
20+
<pre>{error_msg}</pre>
21+
),
22+
buttons: [Dialog.warnButton({ label: 'OK' })]
23+
});
3124
}
25+
console.log("Fetch complete.")
26+
return components;
27+
} catch (error) {
28+
console.error('Failed to get components', error);
3229
}
33-
30+
}
31+
32+
export async function ComponentList() {
3433

35-
export default async function ComponentList() {
36-
let component_list_result: string[] = await get_all_components_method();
34+
if (!componentsCache.data) {
35+
componentsCache.data = await fetchComponents();
36+
}
37+
38+
return componentsCache.data;
39+
}
3740

38-
return component_list_result;
41+
export async function refreshComponentListCache() {
42+
componentsCache.data = await fetchComponents();
3943
}

src/tray_library/Sidebar.tsx

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import ComponentList from './Component';
2-
import React, { useEffect, useState, useRef } from 'react';
1+
import { ComponentList, refreshComponentListCache } from './Component';
2+
import React, { useEffect, useState } from 'react';
33
import styled from '@emotion/styled';
44
import { TrayItemWidget } from './TrayItemWidget';
55
import { TrayWidget } from './TrayWidget';
66
import { JupyterFrontEnd } from '@jupyterlab/application';
7+
import { requestAPI } from '../server/handler';
78

89
import {
910
Accordion,
@@ -101,7 +102,15 @@ export default function Sidebar(props: SidebarProps) {
101102
}
102103

103104
const fetchComponentList = async () => {
104-
// get the component list
105+
106+
try {
107+
// Trigger the load library config on refresh
108+
await requestAPI('library/reload_config', { method: 'POST', headers: { 'Content-Type': 'application/json' } });
109+
} catch (error){
110+
console.error('Failed to reload config: ', error);
111+
}
112+
113+
// get the component list
105114
const component_list = await ComponentList();
106115

107116
// get the header from the components
@@ -126,6 +135,7 @@ export default function Sidebar(props: SidebarProps) {
126135
}, [category, componentList]);
127136

128137
function handleRefreshOnClick() {
138+
refreshComponentListCache()
129139
fetchComponentList();
130140
}
131141

0 commit comments

Comments
 (0)