Skip to content

Commit

Permalink
fix: prettier&linter
Browse files Browse the repository at this point in the history
Signed-off-by: axel7083 <[email protected]>
  • Loading branch information
axel7083 committed Feb 15, 2024
1 parent aff3e6c commit 9fc6b53
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 54 deletions.
2 changes: 1 addition & 1 deletion packages/backend/src/managers/applicationManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ describe('pullApplication', () => {
},
} as CatalogManager,
telemetryLogger,
new TaskRegistry({postMessage: vi.fn().mockResolvedValue(undefined)} as unknown as Webview),
new TaskRegistry({ postMessage: vi.fn().mockResolvedValue(undefined) } as unknown as Webview),
);
manager = new ApplicationManager(
'/home/user/aistudio',
Expand Down
29 changes: 11 additions & 18 deletions packages/backend/src/managers/modelsManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import { ModelsManager } from './modelsManager';
import type { TelemetryLogger, Webview } from '@podman-desktop/api';
import type { CatalogManager } from './catalogManager';
import type { ModelInfo } from '@shared/src/models/IModelInfo';
import { RecipeStatusUtils } from '../utils/recipeStatusUtils';
import { RecipeStatusRegistry } from '../registries/RecipeStatusRegistry';
import * as utils from '../utils/utils';
import { TaskRegistry } from '../registries/TaskRegistry';

Expand Down Expand Up @@ -70,7 +68,7 @@ const telemetryLogger = {

beforeEach(() => {
vi.resetAllMocks();
taskRegistry = new TaskRegistry({postMessage: vi.fn().mockResolvedValue(undefined)} as unknown as Webview);
taskRegistry = new TaskRegistry({ postMessage: vi.fn().mockResolvedValue(undefined) } as unknown as Webview);
});

const dirent = [
Expand Down Expand Up @@ -413,13 +411,11 @@ describe('downloadModel', () => {
vi.spyOn(manager, 'isModelOnDisk').mockReturnValue(false);
vi.spyOn(utils, 'getDurationSecondsSince').mockReturnValue(99);
const setMock = vi.spyOn(taskRegistry, 'set');
await manager.downloadModel(
{
id: 'id',
url: 'url',
name: 'name',
} as ModelInfo,
);
await manager.downloadModel({
id: 'id',
url: 'url',
name: 'name',
} as ModelInfo);
expect(setMock).toHaveBeenLastCalledWith({
id: 'id',
name: 'Downloading model name',
Expand All @@ -444,13 +440,11 @@ describe('downloadModel', () => {
const setMock = vi.spyOn(taskRegistry, 'set');
vi.spyOn(manager, 'isModelOnDisk').mockReturnValue(true);
const getLocalModelPathMock = vi.spyOn(manager, 'getLocalModelPath').mockReturnValue('');
await manager.downloadModel(
{
id: 'id',
url: 'url',
name: 'name',
} as ModelInfo,
);
await manager.downloadModel({
id: 'id',
url: 'url',
name: 'name',
} as ModelInfo);
expect(getLocalModelPathMock).toBeCalledWith('id');
expect(setMock).toHaveBeenLastCalledWith({
id: 'id',
Expand All @@ -462,4 +456,3 @@ describe('downloadModel', () => {
});
});
});

4 changes: 2 additions & 2 deletions packages/backend/src/managers/modelsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ export class ModelsManager {
if (isProgressEvent(event)) {
task.state = 'loading';
task.progress = event.value;
} else if(isCompletionEvent(event)) {
} else if (isCompletionEvent(event)) {
// status error or canceled
if(event.status === 'error' || event.status === 'canceled') {
if (event.status === 'error' || event.status === 'canceled') {
task.state = 'error';
task.progress = undefined;
task.error = event.message;
Expand Down
14 changes: 8 additions & 6 deletions packages/backend/src/registries/RecipeStatusRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ export class RecipeStatusRegistry {
}

private notify() {
this.webview.postMessage({
id: MSG_NEW_RECIPE_STATE,
body: this.statuses,
}).catch((err: unknown) => {
console.error('error notifying recipe statuses', err);
});
this.webview
.postMessage({
id: MSG_NEW_RECIPE_STATE,
body: this.statuses,
})
.catch((err: unknown) => {
console.error('error notifying recipe statuses', err);
});
}
}
17 changes: 9 additions & 8 deletions packages/backend/src/registries/TaskRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ export class TaskRegistry {
constructor(private webview: Webview) {}

get(id: string): Task | undefined {
if(this.tasks.has(id))
return this.tasks.get(id);
if (this.tasks.has(id)) return this.tasks.get(id);
return undefined;
}

Expand All @@ -46,11 +45,13 @@ export class TaskRegistry {
}

private notify() {
this.webview.postMessage({
id: MSG_TASKS_UPDATE,
body: this.getTasks(),
}).catch((err: unknown) => {
console.error('error notifying tasks', err);
});
this.webview
.postMessage({
id: MSG_TASKS_UPDATE,
body: this.getTasks(),
})
.catch((err: unknown) => {
console.error('error notifying tasks', err);
});
}
}
8 changes: 7 additions & 1 deletion packages/backend/src/studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,13 @@ export class Studio {
);
// Create catalog manager, responsible for loading the catalog files and watching for changes
this.catalogManager = new CatalogManager(appUserDirectory, this.#panel.webview);
this.modelsManager = new ModelsManager(appUserDirectory, this.#panel.webview, this.catalogManager, this.telemetry, taskRegistry);
this.modelsManager = new ModelsManager(
appUserDirectory,
this.#panel.webview,
this.catalogManager,
this.telemetry,
taskRegistry,
);
const applicationManager = new ApplicationManager(
appUserDirectory,
gitManager,
Expand Down
44 changes: 26 additions & 18 deletions packages/backend/src/utils/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,46 @@ import https from 'node:https';
import { EventEmitter, type Event } from '@podman-desktop/api';

export interface DownloadEvent {
status: 'error' | 'completed' | 'progress' | 'canceled',
message?: string
status: 'error' | 'completed' | 'progress' | 'canceled';
message?: string;
}

export interface CompletionEvent extends DownloadEvent {
status: 'completed' | 'error' | 'canceled',
duration: number,
status: 'completed' | 'error' | 'canceled';
duration: number;
}

export interface ProgressEvent extends DownloadEvent {
status: 'progress',
value: number,
status: 'progress';
value: number;
}

export const isCompletionEvent = (value: unknown): value is CompletionEvent => {
return !!value && typeof value === 'object' && 'status' in value && typeof value['status'] === 'string' && ['canceled', 'completed', 'error'].includes(value['status']) && 'duration' in value;
return (
!!value &&
typeof value === 'object' &&
'status' in value &&
typeof value['status'] === 'string' &&
['canceled', 'completed', 'error'].includes(value['status']) &&
'duration' in value
);
};

export const isProgressEvent = (value: unknown): value is ProgressEvent => {
return !!value && typeof value === 'object' && 'status' in value && value['status'] === 'progress' && 'value' in value;
return (
!!value && typeof value === 'object' && 'status' in value && value['status'] === 'progress' && 'value' in value
);
};

export class Downloader {
private readonly _onEvent = new EventEmitter<DownloadEvent>();
readonly onEvent: Event<DownloadEvent> = this._onEvent.event;

constructor(private url: string, private target: string, private abortSignal?: AbortSignal) {}
constructor(
private url: string,
private target: string,
private abortSignal?: AbortSignal,
) {}

async perform() {
const startTime = performance.now();
Expand All @@ -44,7 +57,7 @@ export class Downloader {
duration: durationSeconds,
} as CompletionEvent);
} catch (err: unknown) {
if(!this.abortSignal?.aborted) {
if (!this.abortSignal?.aborted) {
this._onEvent.fire({
status: 'error',
message: `Something went wrong: ${String(err)}.`,
Expand All @@ -58,11 +71,9 @@ export class Downloader {
}
}

private download(
url: string,
): Promise<void> {
private download(url: string): Promise<void> {
return new Promise((resolve, reject) => {
const callback = (result: {ok: boolean, error?: string}) => {
const callback = (result: { ok: boolean; error?: string }) => {
if (result.ok) {
resolve();
} else {
Expand All @@ -73,10 +84,7 @@ export class Downloader {
});
}

private followRedirects(
url: string,
callback: (message: {ok?: boolean, error?: string}) => void,
) {
private followRedirects(url: string, callback: (message: { ok?: boolean; error?: string }) => void) {
const file = fs.createWriteStream(this.target);
let totalFileSize = 0;
let progress = 0;
Expand Down

0 comments on commit 9fc6b53

Please sign in to comment.