-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86ff329
commit 3fe2658
Showing
14 changed files
with
153 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { invoke } from '@tauri-apps/api' | ||
|
||
export const ScannerModule = { | ||
startScanner: async (paths: string[]): Promise<void> => { | ||
return await invoke<void>('start_scanner', { paths }) | ||
}, | ||
|
||
stopScanner: async (): Promise<void> => { | ||
return await invoke<void>('stop_scanner') | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,22 @@ | ||
import type { Core } from '../types' | ||
|
||
export namespace Scanner { | ||
export interface State { | ||
currently_scanned_file_path: string | undefined | ||
module_status: Core.ModuleStatus | ||
current_path: string | null | ||
progress: number | null | ||
step: ScannerStatusStep | ||
} | ||
|
||
export enum ScannerStatusStep { | ||
/** Counting the files to scan. */ | ||
Counting = 'Counting', | ||
/** Default step (= waiting for a new job). */ | ||
Idle = 'Idle', | ||
/** Listing the files to scan. */ | ||
Listing = 'Listing', | ||
/** Scanning the files. */ | ||
Running = 'Running', | ||
/** Starting (= has called `clamscan` CLI command). */ | ||
Starting = 'Starting', | ||
/** Stopping (= has called `clamscan` CLI command). */ | ||
Stopping = 'Stopping', | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,50 @@ | ||
import { listen } from '@tauri-apps/api/event' | ||
import { listen, type Event } from '@tauri-apps/api/event' | ||
import type { Scanner } from '../../core/Scanner/types' | ||
import { invoke } from '@tauri-apps/api' | ||
import type { CoreStateListener, CoreStateStore, CoreStateStoreKey } from './types' | ||
|
||
/** | ||
* Listen to all Core states changes and keep track of them. | ||
*/ | ||
export class CoreStateHub { | ||
#scannerState: Scanner.State | undefined | ||
#store: CoreStateStore | ||
|
||
get scannerState() { | ||
return this.#scannerState | ||
constructor() { | ||
this.#store = { | ||
scanner: { | ||
listeners: [], | ||
state: undefined, | ||
}, | ||
} | ||
|
||
this.#init() | ||
} | ||
|
||
get store() { | ||
return this.#store | ||
} | ||
|
||
addListener<K extends CoreStateStoreKey>(key: K, callback: CoreStateListener<K>) { | ||
this.#store[key].listeners.push(callback) | ||
} | ||
|
||
removeListener<K extends CoreStateStoreKey>(key: K, callback: CoreStateListener<K>) { | ||
this.#store[key].listeners = this.#store[key].listeners.filter(listener => listener !== callback) | ||
} | ||
|
||
#init() { | ||
listen<Scanner.State>('scanner:state', this.#initScannerState.bind(this)) | ||
|
||
invoke('get_scanner_state') | ||
} | ||
|
||
init() { | ||
listen<Scanner.State>('scanner:state', event => { | ||
this.#scannerState = event.payload | ||
}) | ||
#initScannerState(event: Event<Scanner.State>) { | ||
this.#store.scanner.state = event.payload | ||
|
||
for (const listener of this.#store.scanner.listeners) { | ||
listener(event.payload) | ||
} | ||
} | ||
} | ||
|
||
export const coreStateHub = new CoreStateHub() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { Scanner } from '@core/Scanner/types' | ||
|
||
export interface CoreStateStore { | ||
scanner: { | ||
listeners: Array<(nextState: Scanner.State) => void> | ||
state: Scanner.State | undefined | ||
} | ||
} | ||
|
||
export type CoreStateStoreKey = keyof CoreStateStore | ||
|
||
export type CoreStateListener<K extends CoreStateStoreKey> = (nextState: CoreStateStore[K]['state']) => void |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,16 @@ | ||
import { useEffect, useRef } from 'react' | ||
import { CoreStateHub } from '.' | ||
import { useEffect, useState } from 'react' | ||
import { coreStateHub } from '.' | ||
import type { CoreStateStore, CoreStateStoreKey } from './types' | ||
|
||
/** | ||
* Hook to initialize the CoreStateHub once the Core is ready. | ||
*/ | ||
export function useCoreStateHub(isCoreReady: boolean): void { | ||
const coreStateHubRef = useRef<CoreStateHub | undefined>(undefined) | ||
export function useCoreStateHub<K extends CoreStateStoreKey>(key: K): CoreStateStore[K]['state'] { | ||
const [state, setState] = useState<CoreStateStore[K]['state']>(undefined) | ||
|
||
useEffect(() => { | ||
if (!isCoreReady || coreStateHubRef.current) { | ||
return | ||
} | ||
coreStateHub.addListener<K>(key, setState) | ||
}, [key]) | ||
|
||
coreStateHubRef.current = new CoreStateHub() | ||
coreStateHubRef.current.init() | ||
}, [isCoreReady]) | ||
return state | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters