Skip to content

Commit

Permalink
feat: adding router state persistence
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 fdc085b commit 3738de3
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 0 deletions.
36 changes: 36 additions & 0 deletions packages/backend/src/registries/RouterRegistry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**********************************************************************
* Copyright (C) 2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import type { RouterState } from '@shared/src/models/IRouterState';

export class RouterRegistry {
private state: RouterState;

constructor() {
this.state = {
url: '/',
};
}

getRouterState(): RouterState {
return this.state;
}

setRouterState(state: RouterState) {
this.state = state;
}
}
10 changes: 10 additions & 0 deletions packages/backend/src/studio-api-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ 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 @@ -39,6 +41,7 @@ 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 @@ -123,4 +126,11 @@ 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: 3 additions & 0 deletions packages/backend/src/studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ 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 @@ -97,6 +98,7 @@ 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 @@ -105,6 +107,7 @@ export class Studio {
taskRegistry,
this.playgroundManager,
this.catalogManager,
routerRegister,
);

await this.catalogManager.loadCatalog();
Expand Down
8 changes: 8 additions & 0 deletions packages/frontend/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ 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 { onMount } from 'svelte';
import { studioClient } from '/@/utils/client';
router.mode.hash();
onMount(async () => {
// Load router state on application startup
const state = await studioClient.getRouterState();
router.goto(state.url);
});
</script>


Expand Down
6 changes: 6 additions & 0 deletions packages/frontend/src/Route.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import { createRouteObject } from 'tinro/dist/tinro_lib';
import type { TinroRouteMeta } from 'tinro';
import { studioClient } from '/@/utils/client';
export let path = '/*';
export let fallback = false;
Expand All @@ -23,6 +24,11 @@ const route = createRouteObject({
onMeta(newMeta: TinroRouteMeta) {
meta = newMeta;
params = meta.params;
// Run fully async
setTimeout(async () => {
await studioClient.saveRouterState({url: newMeta.url});
}, 0)
},
});
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/src/StudioAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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 @@ -29,4 +30,7 @@ export abstract class StudioAPI {
abstract getPlaygroundQueriesState(): Promise<QueryState[]>;

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

abstract saveRouterState(state: RouterState): Promise<void>;
abstract getRouterState(): Promise<RouterState>;
}
21 changes: 21 additions & 0 deletions packages/shared/src/models/IRouterState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**********************************************************************
* Copyright (C) 2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

export interface RouterState {
url: string;
}

0 comments on commit 3738de3

Please sign in to comment.