Skip to content

Commit

Permalink
fix: global importing fix. Better handle of multiple sequential opera…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
farfromrefug committed Dec 19, 2024
1 parent babbf1f commit c09f900
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 140 deletions.
2 changes: 1 addition & 1 deletion app/components/PacksList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@
showLoading('loading');
DEV_LOG && console.log('importContentFromFiles', files);
await importService.importContentFromFiles(
files.map((f) => getRealPath(f)),
files.map((f) => ({ filePath: getRealPath(f) })),
folder?.id
);
}
Expand Down
26 changes: 15 additions & 11 deletions app/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,18 +402,22 @@ export async function downloadStories(story: RemoteContent, folderId?: number) {
DEV_LOG && console.log('downloaded', story.download, File.exists(file.path), file.size);
if (File.exists(file.path) && file.size > 0) {
// do it on a background thread
importService.importContentFromFile({
filePath: file.path,
id: name,
extraData: {
age: story.age,
title: story.title,
description: story.description,
createdDate: dayjs(story.created_at).valueOf(),
modifiedDate: dayjs(story.updated_at).valueOf()
},
importService.importContentFromFiles(
[
{
filePath: file.path,
id: name,
extraData: {
age: story.age,
title: story.title,
description: story.description,
createdDate: dayjs(story.created_at).valueOf(),
modifiedDate: dayjs(story.updated_at).valueOf()
}
}
],
folderId
});
);
}
} catch (error) {
showError(error);
Expand Down
41 changes: 23 additions & 18 deletions app/services/importservice.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { setWorkerContextValue } from '@akylas/nativescript-app-utils';
import { lc } from '@nativescript-community/l';
import { EventData, Observable } from '@nativescript/core';
import { time } from '@nativescript/core/profiling';
import { EventData } from '@nativescript-community/ui-image';
import { Observable } from '@nativescript/core';
import { Pack } from '~/models/Pack';
import { EVENT_IMPORT_STATE, EVENT_STATE } from '~/utils/constants';
import { Optional } from '@nativescript/core/utils/typescript-utils';
import { CustomError } from '@shared/utils/error';
import { showError } from '@shared/utils/showError';
import { Pack } from '~/models/Pack';
import { EVENT_IMPORT_STATE, EVENT_STATE } from '~/utils/constants';
import { hideSnackMessage, showSnackMessage } from '~/utils/ui';
import ImportWorker, { ImportStateEventData, WorkerEventType } from '~/workers/ImportWorker';
import { documentsService } from './documents';
import { lc } from '@nativescript-community/l';
import { hideSnackMessage, showSnackMessage } from '~/utils/ui';
import { getWorkerContextValue, loadImageSync, setWorkerContextValue } from '@akylas/nativescript-app-utils';

export interface ImportEnabledEventData extends EventData {
export interface ImportEnabledEventData extends Optional<EventData<Observable>, 'object'> {
enabled: boolean;
}

Expand Down Expand Up @@ -152,6 +152,7 @@ export class ImportService extends Observable {
}
}
onImportState(event: ImportStateEventData) {
this.importRunning = event.state === 'running';
DEV_LOG && console.log('SyncService', 'onImportState', event.type, event.state, event.showSnack);
if (event.showSnack === false) {
return;
Expand All @@ -178,28 +179,32 @@ export class ImportService extends Observable {
this.on(EVENT_IMPORT_STATE, this.onImportState);
}
}

importRunning = false;
async stop() {
this.off(EVENT_IMPORT_STATE, this.onImportState);
// if (this.syncRunning) {
// // if sync is running wait for it to be finished
// await new Promise((resolve) => this.once(EVENT_SYNC_STATE, resolve));
// }
if (this.importRunning) {
// if sync is running wait for it to be finished
await new Promise((resolve) => this.once(EVENT_IMPORT_STATE, resolve));
}
this.worker?.stop();
this.worker = null;
// this.services.forEach((service) => service.stop());
}
async updateContentFromDataFolder({ showSnack = true }: { showSnack?: boolean } = {}) {
this.ensureWorker();
await this.sendMessageToWorker('import_data', { showSnack }, undefined, undefined, false, 0, { db: documentsService.db.db.db });
}
async importContentFromFiles(files: string[], folderId?: number) {
async importContentFromFiles(files: { filePath: string; id?: string; extraData?: Partial<Pack> }[], folderId?: number) {
DEV_LOG && console.log('importContentFromFiles', files, folderId);
this.ensureWorker();
await this.sendMessageToWorker('import_from_files', { files, folderId }, undefined, undefined, false, 0, { db: documentsService.db.db.db });
}
async importContentFromFile(data: { filePath: string; id?: string; extraData?; folderId?: number }) {
DEV_LOG && console.log('importContentFromFile', JSON.stringify(data));
this.ensureWorker();
await this.sendMessageToWorker('import_from_file', data, undefined, undefined, false, 0, { db: documentsService.db.db.db });
}
// async importContentFromFile(data: { filePath: string; id?: string; extraData?; folderId?: number }) {
// DEV_LOG && console.log('importContentFromFile', JSON.stringify(data));
// this.ensureWorker();
// await this.sendMessageToWorker('import_from_file', data, undefined, undefined, false, 0, { db: documentsService.db.db.db });
// }
async deletePacks(packs: Pack[]) {
const data = packs.map((s) => ({ id: s.id, folders: s.folders }));
DEV_LOG && console.log('deleteDocuments', JSON.stringify(data));
Expand Down
16 changes: 10 additions & 6 deletions app/utils/ui/index.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,17 +437,21 @@ export async function showFullscreenPlayer() {

export async function playPack(pack: Pack, showFullscreen = true) {
const storyHandler = getBGServiceInstance().storyHandler;
await storyHandler.playPack({ pack });
if (showFullscreen && storyHandler.playingPack) {
showFullscreenPlayer();
if (storyHandler) {
await storyHandler.playPack({ pack });
if (showFullscreen && storyHandler.playingPack) {
showFullscreenPlayer();
}
}
}

export async function playStory(story: Story, showFullscreen = true, updatePlaylist = true) {
const storyHandler = getBGServiceInstance().storyHandler;
await storyHandler.playStory({ story, updatePlaylist });
if (showFullscreen && storyHandler.playingStory) {
showFullscreenPlayer();
if (storyHandler) {
await storyHandler.playStory({ story, updatePlaylist });
if (showFullscreen && storyHandler.playingStory) {
showFullscreenPlayer();
}
}
}

Expand Down
Loading

0 comments on commit c09f900

Please sign in to comment.