From ea137eafcd5b9e35747211da31ed2676dc82d8ef Mon Sep 17 00:00:00 2001 From: Abhinav Date: Fri, 26 Aug 2022 14:52:33 +0530 Subject: [PATCH] make watch util and service --- src/api/watch.ts | 26 +++++--------------------- src/services/watch.ts | 11 +++++++++++ src/types/index.ts | 2 +- src/utils/watch.ts | 8 ++++++++ 4 files changed, 25 insertions(+), 22 deletions(-) create mode 100644 src/services/watch.ts create mode 100644 src/utils/watch.ts diff --git a/src/api/watch.ts b/src/api/watch.ts index 04c6bfd42..11b0557e6 100644 --- a/src/api/watch.ts +++ b/src/api/watch.ts @@ -1,24 +1,17 @@ +import { isMappingPresent } from '../utils/watch'; import path from 'path'; -import { watchStore } from '../stores/watch.store'; import { ipcRenderer } from 'electron'; -import { ElectronFile, WatchStoreType } from '../types'; +import { ElectronFile } from '../types'; import { getElectronFile, getFilesFromDir } from '../services/fs'; +import { getWatchMappings, setWatchMappings } from '../services/watch'; export async function addWatchMapping( rootFolderName: string, folderPath: string, uploadStrategy: number ) { - let watchMappings = getWatchMappings(); - if (!watchMappings) { - watchMappings = []; - } - - const watchMapping = watchMappings?.find( - (mapping) => mapping.folderPath === folderPath - ); - - if (watchMapping) { + const watchMappings = getWatchMappings(); + if (isMappingPresent(watchMappings, folderPath)) { return; } @@ -57,15 +50,6 @@ export async function removeWatchMapping(folderPath: string) { setWatchMappings(watchMappings); } -export function getWatchMappings() { - const mappings = watchStore.get('mappings') ?? []; - return mappings; -} - -export function setWatchMappings(watchMappings: WatchStoreType['mappings']) { - watchStore.set('mappings', watchMappings); -} - export async function getAllFilesFromDir(dirPath: string) { const files = await getFilesFromDir(dirPath); const electronFiles = await Promise.all(files.map(getElectronFile)); diff --git a/src/services/watch.ts b/src/services/watch.ts new file mode 100644 index 000000000..47157470f --- /dev/null +++ b/src/services/watch.ts @@ -0,0 +1,11 @@ +import { WatchStoreType } from '../types'; +import { watchStore } from '../stores/watch.store'; + +export function getWatchMappings() { + const mappings = watchStore.get('mappings') ?? []; + return mappings; +} + +export function setWatchMappings(watchMappings: WatchStoreType['mappings']) { + watchStore.set('mappings', watchMappings); +} diff --git a/src/types/index.ts b/src/types/index.ts index ddd85e97c..2b5da36f6 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -25,7 +25,7 @@ type FileMapping = { id: number; }; -interface Mapping { +export interface Mapping { rootFolderName: string; uploadStrategy: number; folderPath: string; diff --git a/src/utils/watch.ts b/src/utils/watch.ts new file mode 100644 index 000000000..d68f3f1a6 --- /dev/null +++ b/src/utils/watch.ts @@ -0,0 +1,8 @@ +import { Mapping } from '../types'; + +export function isMappingPresent(watchMappings: Mapping[], folderPath: string) { + const watchMapping = watchMappings?.find( + (mapping) => mapping.folderPath === folderPath + ); + return !!watchMapping; +}