Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Temporary Manager Chunksize and Callback #2432

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/packages/core/temporary-file/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './temporary-file.repository.js';
export * from './components/temporary-file-badge.element.js';
export * from './temporary-file-manager.class.js';
export * from './types.js';
77 changes: 42 additions & 35 deletions src/packages/core/temporary-file/temporary-file-manager.class.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
import { UmbTemporaryFileRepository } from './temporary-file.repository.js';
import {
TemporaryFileStatus,
type UmbQueueHandlerCallback,
type UmbTemporaryFileModel,
type UmbUploadOptions,
} from './types.js';
import { UmbArrayState } from '@umbraco-cms/backoffice/observable-api';
import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';

///export type TemporaryFileStatus = 'success' | 'waiting' | 'error';

export enum TemporaryFileStatus {
SUCCESS = 'success',
WAITING = 'waiting',
ERROR = 'error',
}

export interface UmbTemporaryFileModel {
file: File;
temporaryUnique: string;
status?: TemporaryFileStatus;
}

export class UmbTemporaryFileManager<
UploadableItem extends UmbTemporaryFileModel = UmbTemporaryFileModel,
> extends UmbControllerBase {
export class UmbTemporaryFileManager<UploadableItem extends UmbTemporaryFileModel> extends UmbControllerBase {
readonly #temporaryFileRepository = new UmbTemporaryFileRepository(this._host);

readonly #queue = new UmbArrayState<UploadableItem>([], (item) => item.temporaryUnique);
public readonly queue = this.#queue.asObservable();

async uploadOne(uploadableItem: UploadableItem): Promise<UploadableItem> {
async uploadOne(uploadableItem: UploadableItem, options?: UmbUploadOptions<UploadableItem>): Promise<UploadableItem> {
this.#queue.setValue([]);

const item: UploadableItem = {
Expand All @@ -33,15 +23,18 @@ export class UmbTemporaryFileManager<
};

this.#queue.appendOne(item);
return (await this.#handleQueue())[0];
return (await this.#handleQueue({ ...options, chunkSize: 1 }))[0];
}

async upload(queueItems: Array<UploadableItem>): Promise<Array<UploadableItem>> {
async upload(
queueItems: Array<UploadableItem>,
options?: UmbUploadOptions<UploadableItem>,
): Promise<Array<UploadableItem>> {
this.#queue.setValue([]);

const items = queueItems.map((item): UploadableItem => ({ status: TemporaryFileStatus.WAITING, ...item }));
this.#queue.append(items);
return this.#handleQueue();
return this.#handleQueue({ ...options });
}

removeOne(unique: string) {
Expand All @@ -52,30 +45,44 @@ export class UmbTemporaryFileManager<
this.#queue.remove(uniques);
}

async #handleQueue() {
async #handleQueue(options?: UmbUploadOptions<UploadableItem>): Promise<Array<UploadableItem>> {
const filesCompleted: Array<UploadableItem> = [];
const queue = this.#queue.getValue();

if (!queue.length) return filesCompleted;

for (const item of queue) {
if (!item.temporaryUnique) throw new Error(`Unique is missing for item ${item}`);
const handler: UmbQueueHandlerCallback<UploadableItem> = async (item) => {
const completedUpload = await this.#handleUpload(item);
filesCompleted.push(completedUpload);

const { error } = await this.#temporaryFileRepository.upload(item.temporaryUnique, item.file);
//await new Promise((resolve) => setTimeout(resolve, (Math.random() + 0.5) * 1000)); // simulate small delay so that the upload badge is properly shown
if (options?.callback) await options.callback(completedUpload);
};

let status: TemporaryFileStatus;
if (error) {
status = TemporaryFileStatus.ERROR;
this.#queue.updateOne(item.temporaryUnique, { ...item, status });
} else {
status = TemporaryFileStatus.SUCCESS;
this.#queue.updateOne(item.temporaryUnique, { ...item, status });
}
const chunkSize = options?.chunkSize ?? 5;
const chunks = Math.ceil(queue.length / chunkSize);

filesCompleted.push({ ...item, status });
for (let i = 0; i < chunks; i++) {
const chunk = queue.slice(i * chunkSize, i * chunkSize + chunkSize);
await Promise.all(chunk.map(handler));
}

return filesCompleted;
}

async #handleUpload(item: UploadableItem) {
if (!item.temporaryUnique) throw new Error(`Unique is missing for item ${item}`);

const { error } = await this.#temporaryFileRepository.upload(item.temporaryUnique, item.file);

let status: TemporaryFileStatus;
if (error) {
status = TemporaryFileStatus.ERROR;
this.#queue.updateOne(item.temporaryUnique, { ...item, status });
} else {
status = TemporaryFileStatus.SUCCESS;
this.#queue.updateOne(item.temporaryUnique, { ...item, status });
}

return { ...item, status };
}
}
18 changes: 18 additions & 0 deletions src/packages/core/temporary-file/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export enum TemporaryFileStatus {
SUCCESS = 'success',
WAITING = 'waiting',
ERROR = 'error',
}

export interface UmbTemporaryFileModel {
file: File;
temporaryUnique: string;
status?: TemporaryFileStatus;
}

export type UmbQueueHandlerCallback<TItem extends UmbTemporaryFileModel> = (item: TItem) => Promise<void>;

export type UmbUploadOptions<TItem extends UmbTemporaryFileModel> = {
chunkSize?: number;
callback?: UmbQueueHandlerCallback<TItem>;
};
Loading