Skip to content

Commit

Permalink
refactor(flat-services): support keep reference
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed Aug 22, 2022
1 parent 052fb39 commit e08b874
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
11 changes: 10 additions & 1 deletion packages/flat-pages/src/FilePreviewPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@ export const FilePreviewPage: React.FC<FilePreviewPageProps> = props => {
);

useIsomorphicLayoutEffect(() => {
sp(FlatServices.getInstance().requestService(`file-preview:${fileExt}`)).then(setService);
let previewService: IServiceFilePreview | null = null;
sp(FlatServices.getInstance().requestService(`file-preview:${fileExt}`, false)).then(
service => {
previewService = service;
setService(service);
},
);
return () => {
previewService?.destroy?.();
};
}, []);

useIsomorphicLayoutEffect(() => {
Expand Down
5 changes: 4 additions & 1 deletion packages/flat-services/src/flat-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,16 @@ export class FlatServices {

public async requestService<T extends FlatServiceID>(
name: T,
keepReference = true,
): Promise<FlatServicesCatalog[T] | null> {
let service = this.services.get(name) || null;
if (!service) {
const creator = this.registry.get(name);
if (creator) {
service = creator();
this.services.set(name, service);
if (keepReference) {
this.services.set(name, service);
}
}
}
return service as Promise<FlatServicesCatalog[T] | null>;
Expand Down
37 changes: 14 additions & 23 deletions packages/flat-services/src/providers/provider-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "@netless/flat-server-api";
import type { FlatI18n } from "@netless/flat-i18n";
import type { Toaster } from "../toaster";
import { FlatServiceID, FlatServices, FlatServicesCatalog } from "../flat-services";
import { FlatServiceID, FlatServices } from "../flat-services";
import { getFileExt } from "../services/file/utils";
import { IServiceFile, IServiceFileExtensions } from "../services/file";
import { SideEffectManager } from "side-effect-manager";
Expand Down Expand Up @@ -41,20 +41,22 @@ export class FlatServiceProviderFile implements IServiceFile {
const ext = getFileExt(file.fileName) as IServiceFileExtensions;
const serviceName = `file-insert:${ext}` as const;

const insertService = await this.requestAutoService(serviceName);
const insertService = await this.flatServices.requestService(serviceName, false);
if (!insertService) {
throw new TypeError(`No service provider for inserting file '${file.fileName}'`);
}

const convertStatus = await this.checkConvertStatus(file, ext);
try {
const convertStatus = await this.checkConvertStatus(file, ext);

if (convertStatus === FileConvertStep.Done || convertStatus === FileConvertStep.None) {
try {
if (convertStatus === FileConvertStep.Done || convertStatus === FileConvertStep.None) {
await insertService.insert(file);
} catch (e) {
this.toaster.emit("error", this.flatI18n.t("unable-to-insert-courseware"));
}
} catch (e) {
this.toaster.emit("error", this.flatI18n.t("unable-to-insert-courseware"));
}

await insertService.destroy?.();
}

public async preview(file: CloudFile): Promise<void> {
Expand All @@ -78,9 +80,10 @@ export class FlatServiceProviderFile implements IServiceFile {
file: CloudFile,
ext: IServiceFileExtensions,
): Promise<FileConvertStep> {
let convertStep = file.convertStep;
if (file.convertStep !== FileConvertStep.Done) {
const serviceName = `file-convert:${ext}` as const;
const convertService = await this.requestAutoService(serviceName);
const convertService = await this.flatServices.requestService(serviceName, false);
if (convertService) {
try {
const result = await convertService.queryStatus(file);
Expand Down Expand Up @@ -112,25 +115,13 @@ export class FlatServiceProviderFile implements IServiceFile {
this.flatI18n.t("in-the-process-of-transcoding-tips"),
);
}
return result.status;
convertStep = result.status;
} catch (e) {
console.error(e);
return file.convertStep;
}
convertService.destroy?.();
}
}
return file.convertStep;
}

private async requestAutoService<T extends FlatServiceID>(
serviceName: T,
): Promise<FlatServicesCatalog[T] | null> {
const convertService = await this.flatServices.requestService(serviceName);
this.sideEffect.setTimeout(
() => this.flatServices.shutdownService(serviceName),
5000,
serviceName,
);
return convertService;
return convertStep;
}
}

0 comments on commit e08b874

Please sign in to comment.