Skip to content

Commit

Permalink
fix: revert + using state management
Browse files Browse the repository at this point in the history
Signed-off-by: axel7083 <[email protected]>
  • Loading branch information
axel7083 committed Jan 22, 2024
1 parent 306f0d3 commit e11da66
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 67 deletions.
36 changes: 0 additions & 36 deletions packages/backend/src/registries/RouterRegistry.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/backend/src/studio-api-impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import type { Webview } from '@podman-desktop/api';

import * as fs from 'node:fs';
import { CatalogManager } from './managers/catalogManager';
import type { RouterRegistry } from './registries/RouterRegistry';

vi.mock('./ai.json', () => {
return {
Expand Down Expand Up @@ -79,7 +78,6 @@ beforeEach(async () => {
{} as unknown as TaskRegistry,
{} as unknown as PlayGroundManager,
catalogManager,
{} as unknown as RouterRegistry,
);
vi.resetAllMocks();
vi.mock('node:fs');
Expand Down
10 changes: 0 additions & 10 deletions packages/backend/src/studio-api-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import * as path from 'node:path';
import type { CatalogManager } from './managers/catalogManager';
import type { Catalog } from '@shared/src/models/ICatalog';
import type { PlaygroundState } from '@shared/src/models/IPlaygroundState';
import type { RouterState } from '@shared/src/models/IRouterState';
import type { RouterRegistry } from './registries/RouterRegistry';

export class StudioApiImpl implements StudioAPI {
constructor(
Expand All @@ -41,7 +39,6 @@ export class StudioApiImpl implements StudioAPI {
private taskRegistry: TaskRegistry,
private playgroundManager: PlayGroundManager,
private catalogManager: CatalogManager,
private routerRegistry: RouterRegistry,
) {}

async ping(): Promise<string> {
Expand Down Expand Up @@ -126,11 +123,4 @@ export class StudioApiImpl implements StudioAPI {
async getCatalog(): Promise<Catalog> {
return this.catalogManager.getCatalog();
}

async saveRouterState(state: RouterState): Promise<void> {
this.routerRegistry.setRouterState(state);
}
async getRouterState(): Promise<RouterState> {
return this.routerRegistry.getRouterState();
}
}
3 changes: 0 additions & 3 deletions packages/backend/src/studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import * as fs from 'node:fs';
import { TaskRegistry } from './registries/TaskRegistry';
import { PlayGroundManager } from './managers/playground';
import { CatalogManager } from './managers/catalogManager';
import { RouterRegistry } from './registries/RouterRegistry';

export class Studio {
readonly #extensionContext: ExtensionContext;
Expand Down Expand Up @@ -98,7 +97,6 @@ export class Studio {
this.playgroundManager = new PlayGroundManager(this.#panel.webview);
// Create catalog manager, responsible for loading the catalog files and watching for changes
this.catalogManager = new CatalogManager(applicationManager.appUserDirectory, this.#panel.webview);
const routerRegister = new RouterRegistry();

// Creating StudioApiImpl
this.studioApi = new StudioApiImpl(
Expand All @@ -107,7 +105,6 @@ export class Studio {
taskRegistry,
this.playgroundManager,
this.catalogManager,
routerRegister,
);

await this.catalogManager.loadCatalog();
Expand Down
13 changes: 8 additions & 5 deletions packages/frontend/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,24 @@ import Preferences from '/@/pages/Preferences.svelte';
import Registries from '/@/pages/Registries.svelte';
import Models from '/@/pages/Models.svelte';
import Recipe from '/@/pages/Recipe.svelte';
import Model from './pages/Model.svelte';
import Model from './pages/Model.svelte';
import { onMount } from 'svelte';
import { studioClient } from '/@/utils/client';
import { getRouterState } from '/@/utils/client';
router.mode.hash();
onMount(async () => {
let isMounted = false;
onMount(() => {
// Load router state on application startup
const state = await studioClient.getRouterState();
const state = getRouterState();
router.goto(state.url);
isMounted = true;
});
</script>


<Route path="/*" breadcrumb="Home" let:meta>
<Route path="/*" breadcrumb="Home" isAppMounted="{isMounted}" let:meta>
<main class="flex flex-col w-screen h-screen overflow-hidden bg-charcoal-700">
<div class="flex flex-row w-full h-full overflow-hidden">
<Navigation meta="{meta}"/>
Expand Down
11 changes: 6 additions & 5 deletions packages/frontend/src/Route.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<script lang="ts">
import { createRouteObject } from 'tinro/dist/tinro_lib';
import type { TinroRouteMeta } from 'tinro';
import { studioClient } from '/@/utils/client';
import { saveRouterState, studioClient } from '/@/utils/client';
export let path = '/*';
export let fallback = false;
export let redirect = false;
export let firstmatch = false;
export let breadcrumb: string | undefined = undefined;
export let isAppMounted: boolean = false;
let showContent = false;
let params: Record<string, string> = {};
let meta: TinroRouteMeta = {} as TinroRouteMeta;
Expand All @@ -25,10 +27,9 @@ const route = createRouteObject({
meta = newMeta;
params = meta.params;
// Run fully async
setTimeout(async () => {
await studioClient.saveRouterState({url: newMeta.url});
}, 0)
if(isAppMounted) {
saveRouterState({url: newMeta.url});
}
},
});
Expand Down
2 changes: 0 additions & 2 deletions packages/frontend/src/pages/Model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ vi.mock('../utils/client', async () => {
return {
studioClient: {
getCatalog: mocks.getCatalogMock,
saveRouterState: vi.fn(),
getRouterState: vi.fn().mockResolvedValue({ url: '/' }),
},
rpcBrowser: {
subscribe: () => {
Expand Down
15 changes: 15 additions & 0 deletions packages/frontend/src/utils/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import type { StudioAPI } from '@shared/src/StudioAPI';
import { RpcBrowser } from '@shared/src/messages/MessageProxy';
import type { RouterState } from '@shared/src/models/IRouterState';

export const RECENT_CATEGORY_ID = 'recent-category';
const podmanDesktopApi = acquirePodmanDesktopApi();
export const rpcBrowser: RpcBrowser = new RpcBrowser(window, podmanDesktopApi);

export const studioClient: StudioAPI = rpcBrowser.getProxy<StudioAPI>();

export const saveRouterState = (state: RouterState) => {
podmanDesktopApi.setState(state);
};

const isRouterState = (value: unknown): value is RouterState => {
return typeof value === 'object' && !!value && 'url' in value;
};

export const getRouterState = (): RouterState => {
const state = podmanDesktopApi.getState();
if (isRouterState(state)) return state;
return { url: '/' };
};
4 changes: 0 additions & 4 deletions packages/shared/src/StudioAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { Task } from './models/ITask';
import type { QueryState } from './models/IPlaygroundQueryState';
import type { Catalog } from './models/ICatalog';
import type { PlaygroundState } from './models/IPlaygroundState';
import type { RouterState } from './models/IRouterState';

export abstract class StudioAPI {
abstract ping(): Promise<string>;
Expand All @@ -30,7 +29,4 @@ export abstract class StudioAPI {
abstract getPlaygroundQueriesState(): Promise<QueryState[]>;

abstract getPlaygroundsState(): Promise<PlaygroundState[]>;

abstract saveRouterState(state: RouterState): Promise<void>;
abstract getRouterState(): Promise<RouterState>;
}

0 comments on commit e11da66

Please sign in to comment.