-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ExtractZipServiceWorker, update vite config to build ExtractZipSe…
…rvice to separate file
- Loading branch information
1 parent
8451368
commit 734f286
Showing
6 changed files
with
111 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Worker } from 'node:worker_threads' | ||
import { extractOptions } from './types' | ||
import { EventEmitter } from 'node:events' | ||
import path from 'node:path' | ||
|
||
export class ExtractZipServiceWorker extends EventEmitter { | ||
worker: Worker | ||
initPromise: Promise<void> | ||
#resolveInitPromise: () => void = () => { | ||
console.error('resolve init promise not assigned!') | ||
} | ||
|
||
constructor( | ||
zipFile: string, | ||
destinationPath: string, | ||
options?: extractOptions | ||
) { | ||
super() | ||
this.initPromise = new Promise((res) => { | ||
this.#resolveInitPromise = res | ||
}) | ||
const extractZipServicePath = path.join( | ||
__dirname, | ||
'../preload/ExtractZipService.js' | ||
) | ||
this.worker = new Worker(extractZipServicePath) | ||
|
||
this.worker.on('message', (data) => { | ||
if (data?.initEvent === 'INITIALIZED') { | ||
this.worker.postMessage({ | ||
type: 'INIT', | ||
zipFile, | ||
destinationPath, | ||
options | ||
}) | ||
return | ||
} else if (data?.initEvent === 'ZIP_SERVICE_INSTANTIATED') { | ||
this.#resolveInitPromise() | ||
return | ||
} | ||
const { eventType, args } = data | ||
this.emit(eventType, ...args) | ||
}) | ||
} | ||
|
||
public cancel() { | ||
this.worker.postMessage({ type: 'CANCEL' }) | ||
} | ||
|
||
public pause(): void { | ||
this.worker.postMessage({ type: 'PAUSE' }) | ||
} | ||
|
||
async extract() { | ||
this.worker.postMessage({ type: 'EXTRACT' }) | ||
} | ||
} |
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,5 @@ | ||
export type extractOptions = { | ||
deleteOnEnd?: boolean | ||
} | ||
|
||
export type ExtractZipServiceCommand = 'INIT' | 'CANCEL' | 'PAUSE' | 'EXTRACT' |
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