diff --git a/src/electron/lib/Blix.ts b/src/electron/lib/Blix.ts index bd43fb68..9fd9cb93 100644 --- a/src/electron/lib/Blix.ts +++ b/src/electron/lib/Blix.ts @@ -60,14 +60,8 @@ export class Blix { // this._aiManager = new AiManager(mainWindow); this._projectManager = new ProjectManager(mainWindow); - // Add subscribers - const graphSubscriber = new IPCGraphSubscriber(); - - graphSubscriber.listen = (graphId: UUID, newGraph: UIGraph) => { - mainWindow.apis.graphClientApi.graphChanged(graphId, newGraph); - }; - - this._graphManager.addAllSubscriber(graphSubscriber); + this.initSubscribers(); + mainWindow.apis.utilClientApi.onBlixReady(); // testStuffies(this); @@ -84,6 +78,16 @@ export class Blix { // }, 3000); } + private initSubscribers() { + const graphSubscriber = new IPCGraphSubscriber(); + + graphSubscriber.listen = (graphId: UUID, newGraph: UIGraph) => { + this.mainWindow?.apis.graphClientApi.graphChanged(graphId, newGraph); + }; + + this._graphManager.addAllSubscriber(graphSubscriber); + } + // TODO: Move these to a Utils.ts or something like that sendInformationMessage(message: string) { this._mainWindow.apis.utilClientApi.showToast({ message, type: "info" }); diff --git a/src/electron/lib/api/MainApi.ts b/src/electron/lib/api/MainApi.ts index 37db7542..3f54ebfa 100644 --- a/src/electron/lib/api/MainApi.ts +++ b/src/electron/lib/api/MainApi.ts @@ -31,15 +31,7 @@ export function exposeMainApis(blix: Blix) { // @ts-ignore: no-var-requires global.mainApis = apis as any; - - // Return callback to initialize APIs - // once everything else is ready - return async () => { - // apis.projectApi.init(); - // apis.pluginApi.init(); - apis.graphApi.init(); - // apis.toolboxApi.init(); - }; + return apis; } export type MainApis = ReturnType; diff --git a/src/electron/lib/api/apis/GraphApi.ts b/src/electron/lib/api/apis/GraphApi.ts index c97247aa..034fc202 100644 --- a/src/electron/lib/api/apis/GraphApi.ts +++ b/src/electron/lib/api/apis/GraphApi.ts @@ -9,20 +9,9 @@ import { type NodeSignature } from "@shared/ui/ToolboxTypes"; // Graphs across projects are stored homogeneously and referenced by UUID export class GraphApi implements ElectronMainApi { private readonly _blix: Blix; - private readonly graphSubscriber: IPCGraphSubscriber; constructor(blix: Blix) { this._blix = blix; - this.graphSubscriber = new IPCGraphSubscriber(); - } - - // Called in index.ts after Blix has been initialized - async init() { - // Add IPC subscriber to listen for graph changes and alert the frontend - this._blix.graphManager.addAllSubscriber(this.graphSubscriber); - this.graphSubscriber.listen = (graphId: UUID, newGraph: UIGraph) => { - this._blix.mainWindow?.apis.graphClientApi.graphChanged(graphId, newGraph); - }; } // TODO: Implement these properly diff --git a/src/frontend/lib/Blix.ts b/src/frontend/lib/Blix.ts deleted file mode 100644 index d08c8706..00000000 --- a/src/frontend/lib/Blix.ts +++ /dev/null @@ -1,56 +0,0 @@ -// import { graphMall } from "./stores/GraphStore"; -import { blixStore } from "./stores/BlixStore"; -import { commandStore } from "./stores/CommandStore"; -import { initializeAPIs } from "./api/apiInitializer"; -// import { GraphNode, UIGraph } from "@shared/ui/UIGraph"; -import { toolboxStore } from "./stores/ToolboxStore"; -/** - * Runs on app start. Will initialize the IPC APIs and set - * the initial frontend stores. - */ -export async function init() { - await initializeAPIs(); - - // TODO: This needs to be called on some onBlixReady event - // when the backend Blix + apis have all been fully initialized - // await setInitialStores(); -} - -async function setInitialStores() { - // ===== SET INITIAL STORE VALUES ===== // - - // BLix store - const res = await window.apis.utilApi.getSystemInfo(); - blixStore.set({ systemInfo: res }); - - // Command store - const command = await window.apis.commandApi.getCommands(); - commandStore.refreshStore(command); - - // Toolbox store - const node = await window.apis.toolboxApi.getNodes(); - toolboxStore.refreshStore(node); - - // Graph store - // const allGraphIds = await window.apis.graphApi.getAllGraphUUIDs(); - // console.log("ALL GRAPHS", allGraphIds); - - // for (const graphId of allGraphIds) { - // const graph = await window.apis.graphApi.getGraph(graphId); - // console.log("BACKEND GRAPH", graph.getNodes); - - // TODO: REMOVE; This is just for testing - // const uiGraph = new UIGraph(graphId); - // const node1 = new GraphNode("1"); - // const node2 = new GraphNode("2"); - // const node3 = new GraphNode("a"); - // uiGraph.nodes[node1.uuid] = node1; - // uiGraph.nodes[node2.uuid] = node2; - // uiGraph.nodes[node3.uuid] = node3; - // node1.pos.x = 100; - // node1.pos.y = 100; - - // graphMall.refreshGraph(uiGraph.uuid, uiGraph); - // } - // exportLayout() -} diff --git a/src/frontend/lib/api/apiInitializer.ts b/src/frontend/lib/api/apiInitializer.ts index 5dd5ee71..2bb565e4 100644 --- a/src/frontend/lib/api/apiInitializer.ts +++ b/src/frontend/lib/api/apiInitializer.ts @@ -10,21 +10,16 @@ import type { ToolboxApi } from "@electron/lib/api/apis/ToolboxApi"; // Window APIs import { CommandClientApi } from "./apis/CommandClientApi"; - -// stores -import { blixStore } from "../stores/BlixStore"; -import { commandStore } from "../stores/CommandStore"; import { GraphClientApi } from "./apis/GraphClientApi"; import { ProjectClientApi } from "./apis/ProjectClientApi"; import { UtilClientApi } from "./apis/UtilClientApi"; -import { toolboxStore } from "../stores/ToolboxStore"; import { ToolboxClientApi } from "./apis/ToolboxClientApi"; /** * Initializes the application by exposing the window IPC APIs to the main * process and binding the main process IPC APIs to the window. */ -export async function initializeAPIs() { +export async function initAPIs() { exposeWindowApis(); window.apis = await bindMainApis(); } diff --git a/src/frontend/lib/api/apis/UtilClientApi.ts b/src/frontend/lib/api/apis/UtilClientApi.ts index 4a67017b..c7b01aa7 100644 --- a/src/frontend/lib/api/apis/UtilClientApi.ts +++ b/src/frontend/lib/api/apis/UtilClientApi.ts @@ -1,8 +1,14 @@ import type { ElectronWindowApi } from "electron-affinity/window"; import { toastStore, type ToastOptions } from "../../stores/ToastStore"; +import { blixStore, setInitialStores } from "../../../lib/stores/BlixStore"; export class UtilClientApi implements ElectronWindowApi { showToast(options: Partial) { toastStore.trigger(options); } + + async onBlixReady() { + await setInitialStores(); + blixStore.update((state) => ({ ...state, blixReady: true })); + } } diff --git a/src/frontend/lib/stores/BlixStore.ts b/src/frontend/lib/stores/BlixStore.ts index 8d0fe7c0..80b8ceb2 100644 --- a/src/frontend/lib/stores/BlixStore.ts +++ b/src/frontend/lib/stores/BlixStore.ts @@ -1,6 +1,9 @@ import { writable } from "svelte/store"; +import { commandStore } from "./CommandStore"; +import { toolboxStore } from "./ToolboxStore"; interface BlixStore { + blixReady: boolean; systemInfo: { nodeVersion: string; systemPlatform: string; @@ -12,6 +15,7 @@ interface BlixStore { // Currently used to store some startup config. Can potentially be used to store // some other global state in the future. export const blixStore = writable({ + blixReady: false, systemInfo: { nodeVersion: "", systemPlatform: "", @@ -19,3 +23,40 @@ export const blixStore = writable({ systemVersion: "", }, }); + +export async function setInitialStores() { + // BLix store + const res = await window.apis.utilApi.getSystemInfo(); + blixStore.update((state) => ({ ...state, systemInfo: res })); + + // Command store + const command = await window.apis.commandApi.getCommands(); + commandStore.refreshStore(command); + + // Toolbox store + const node = await window.apis.toolboxApi.getNodes(); + toolboxStore.refreshStore(node); + + // Graph store + // const allGraphIds = await window.apis.graphApi.getAllGraphUUIDs(); + // console.log("ALL GRAPHS", allGraphIds); + + // for (const graphId of allGraphIds) { + // const graph = await window.apis.graphApi.getGraph(graphId); + // console.log("BACKEND GRAPH", graph.getNodes); + + // TODO: REMOVE; This is just for testing + // const uiGraph = new UIGraph(graphId); + // const node1 = new GraphNode("1"); + // const node2 = new GraphNode("2"); + // const node3 = new GraphNode("a"); + // uiGraph.nodes[node1.uuid] = node1; + // uiGraph.nodes[node2.uuid] = node2; + // uiGraph.nodes[node3.uuid] = node3; + // node1.pos.x = 100; + // node1.pos.y = 100; + + // graphMall.refreshGraph(uiGraph.uuid, uiGraph); + // } + // exportLayout() +} diff --git a/src/frontend/ui/base/App.svelte b/src/frontend/ui/base/App.svelte index 155c7025..f186931b 100644 --- a/src/frontend/ui/base/App.svelte +++ b/src/frontend/ui/base/App.svelte @@ -1,39 +1,32 @@ {#if testing} - {#if isLoading} + {#if $blixStore.blixReady} + + {:else}
Loading
- {:else} - {/if} -{:else if isLoading} -
- Loading -
-{:else} +{:else if $blixStore.blixReady}
+{:else} +
+ Loading +
{/if}