|
1 | 1 | import { showDialog, Dialog } from '@jupyterlab/apputils';
|
2 |
| - |
3 | 2 | import { requestAPI } from "../server/handler";
|
4 | 3 | import React from 'react';
|
5 | 4 |
|
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"]; |
7 | 15 |
|
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 | + }); |
31 | 24 | }
|
| 25 | + console.log("Fetch complete.") |
| 26 | + return components; |
| 27 | + } catch (error) { |
| 28 | + console.error('Failed to get components', error); |
32 | 29 | }
|
33 |
| - |
| 30 | +} |
| 31 | + |
| 32 | +export async function ComponentList() { |
34 | 33 |
|
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 | +} |
37 | 40 |
|
38 |
| - return component_list_result; |
| 41 | +export async function refreshComponentListCache() { |
| 42 | + componentsCache.data = await fetchComponents(); |
39 | 43 | }
|
0 commit comments