Skip to content

Commit

Permalink
fix: rename progressiveEvent
Browse files Browse the repository at this point in the history
Signed-off-by: lstocchi <[email protected]>
  • Loading branch information
lstocchi committed Mar 11, 2024
1 parent b30b71d commit d72baf5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 28 deletions.
18 changes: 9 additions & 9 deletions packages/backend/src/managers/modelsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import * as podmanDesktopApi from '@podman-desktop/api';
import { Downloader } from '../utils/downloader';
import type { TaskRegistry } from '../registries/TaskRegistry';
import type { Task } from '@shared/src/models/ITask';
import type { ProgressiveEvent } from '../models/progressiveEvent';
import { isCompletionProgressiveEvent, isExecutingProgressiveEvent } from '../models/progressiveEvent';
import type { BaseEvent } from '../models/baseEvent';
import { isCompletionEvent, isProgressEvent } from '../models/baseEvent';
import { Uploader } from '../utils/uploader';

export class ModelsManager implements Disposable {
Expand Down Expand Up @@ -204,7 +204,7 @@ export class ModelsManager implements Disposable {
// If we have an existing downloader running we subscribe on its events
return new Promise((resolve, reject) => {
const disposable = existingDownloader.onEvent(event => {
if (!isCompletionProgressiveEvent(event)) return;
if (!isCompletionEvent(event)) return;

switch (event.status) {
case 'completed':
Expand All @@ -218,7 +218,7 @@ export class ModelsManager implements Disposable {
});
}

private onDownloadEvent(event: ProgressiveEvent): void {
private onDownloadEvent(event: BaseEvent): void {
// Always use the task registry as source of truth for tasks
const tasks = this.taskRegistry.getTasksByLabels({ 'model-pulling': event.id });
if (tasks.length === 0) {
Expand All @@ -228,10 +228,10 @@ export class ModelsManager implements Disposable {
}

tasks.forEach(task => {
if (isExecutingProgressiveEvent(event)) {
if (isProgressEvent(event)) {
task.state = 'loading';
task.progress = event.value;
} else if (isCompletionProgressiveEvent(event)) {
} else if (isCompletionEvent(event)) {
// status error or canceled
if (event.status === 'error' || event.status === 'canceled') {
task.state = 'error';
Expand Down Expand Up @@ -315,11 +315,11 @@ export class ModelsManager implements Disposable {
});

const uploader = new Uploader(localModelPath);
uploader.onEvent((event: ProgressiveEvent) => {
if (isExecutingProgressiveEvent(event)) {
uploader.onEvent((event: BaseEvent) => {
if (isProgressEvent(event)) {
task.state = 'loading';
task.progress = event.value;
} else if (isCompletionProgressiveEvent(event)) {
} else if (isCompletionEvent(event)) {
// status error or canceled
if (event.status === 'error' || event.status === 'canceled') {
task.state = 'error';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

export interface ProgressiveEvent {
export interface BaseEvent {
id: string;
status: 'error' | 'completed' | 'progress' | 'canceled';
message?: string;
}

export interface CompletionProgressiveEvent extends ProgressiveEvent {
export interface CompletionEvent extends BaseEvent {
status: 'completed' | 'error' | 'canceled';
duration: number;
}

export interface ExecutingProgressiveEvent extends ProgressiveEvent {
export interface ProgressEvent extends BaseEvent {
status: 'progress';
value: number;
}

export const isCompletionProgressiveEvent = (value: unknown): value is CompletionProgressiveEvent => {
export const isCompletionEvent = (value: unknown): value is CompletionEvent => {
return (
!!value &&
typeof value === 'object' &&
Expand All @@ -43,7 +43,7 @@ export const isCompletionProgressiveEvent = (value: unknown): value is Completio
);
};

export const isExecutingProgressiveEvent = (value: unknown): value is ExecutingProgressiveEvent => {
export const isProgressEvent = (value: unknown): value is ProgressEvent => {
return (
!!value && typeof value === 'object' && 'status' in value && value['status'] === 'progress' && 'value' in value
);
Expand Down
14 changes: 5 additions & 9 deletions packages/backend/src/utils/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@ import { getDurationSecondsSince } from './utils';
import { createWriteStream, promises } from 'node:fs';
import https from 'node:https';
import { EventEmitter, type Event } from '@podman-desktop/api';
import type {
CompletionProgressiveEvent,
ExecutingProgressiveEvent,
ProgressiveEvent,
} from '../models/progressiveEvent';
import type { CompletionEvent, ProgressEvent, BaseEvent } from '../models/baseEvent';

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

completed: boolean;
Expand All @@ -55,7 +51,7 @@ export class Downloader {
status: 'completed',
message: `Duration ${durationSeconds}s.`,
duration: durationSeconds,
} as CompletionProgressiveEvent);
} as CompletionEvent);
} catch (err: unknown) {
if (!this.abortSignal?.aborted) {
this._onEvent.fire({
Expand Down Expand Up @@ -133,7 +129,7 @@ export class Downloader {
id: this.requestedIdentifier,
status: 'progress',
value: progressValue,
} as ExecutingProgressiveEvent);
} as ProgressEvent);
}
});
resp.pipe(stream);
Expand Down
10 changes: 5 additions & 5 deletions packages/backend/src/utils/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
import { EventEmitter, type Event } from '@podman-desktop/api';
import { WSLUploader } from './WSLUploader';
import { getDurationSecondsSince } from './utils';
import type { CompletionProgressiveEvent, ProgressiveEvent } from '../models/progressiveEvent';
import type { CompletionEvent, BaseEvent } from '../models/baseEvent';

export interface UploadWorker {
canUpload: () => boolean;
upload: (path: string) => Promise<string>;
}

export class Uploader {
readonly #_onEvent = new EventEmitter<ProgressiveEvent>();
readonly onEvent: Event<ProgressiveEvent> = this.#_onEvent.event;
readonly #_onEvent = new EventEmitter<BaseEvent>();
readonly onEvent: Event<BaseEvent> = this.#_onEvent.event;
readonly #workers: UploadWorker[] = [];

constructor(
Expand All @@ -55,7 +55,7 @@ export class Uploader {
status: 'completed',
message: `Duration ${durationSeconds}s.`,
duration: durationSeconds,
} as CompletionProgressiveEvent);
} as CompletionEvent);
return modelPath;
}
} catch (err) {
Expand All @@ -78,7 +78,7 @@ export class Uploader {
id,
status: 'completed',
message: `Use local model`,
} as CompletionProgressiveEvent);
} as CompletionEvent);

return modelPath;
}
Expand Down

0 comments on commit d72baf5

Please sign in to comment.