-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
5 changed files
with
112 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { ipcMain } from 'electron'; | ||
import { createReadStream, ReadStream } from 'node:fs'; | ||
|
||
let nextId = 0; | ||
|
||
const streams = new Map<number, ReadStream>(); | ||
|
||
ipcMain.handle('stream-open', (event, filePath: string) => { | ||
const { sender } = event; | ||
const stream = createReadStream(filePath, 'utf8'); | ||
const id = nextId++; | ||
streams.set(id, stream); | ||
|
||
stream.on('data', (chunk: string) => { | ||
sender.send('stream-data', id, chunk); | ||
}); | ||
|
||
stream.on('end', () => { | ||
sender.send('stream-end', id); | ||
}); | ||
|
||
stream.on('error', (error) => { | ||
sender.send('stream-error', id, error); | ||
}); | ||
|
||
return id; | ||
}); | ||
|
||
ipcMain.on('stream-close', (event, id: number) => { | ||
streams.get(id)?.close(); | ||
streams.delete(id); | ||
}); |
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,25 @@ | ||
export type EventListener = (...args: unknown[]) => void; | ||
|
||
export abstract class EventEmitter { | ||
private readonly listeners = new Map<string, Set<EventListener>>(); | ||
|
||
on(event: string, listener: EventListener) { | ||
if (!this.listeners.has(event)) { | ||
this.listeners.set(event, new Set()); | ||
} | ||
this.listeners.get(event).add(listener); | ||
return this; | ||
} | ||
|
||
off(event: string, listener: EventListener) { | ||
this.listeners.get(event)?.delete(listener); | ||
return this; | ||
} | ||
|
||
protected emit(event: string, ...args: unknown[]) { | ||
for (const listener of this.listeners.get(event) ?? []) { | ||
listener(...args); | ||
} | ||
return this; | ||
} | ||
} |
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,45 @@ | ||
import { EventEmitter } from '@/lib/event-emitter'; | ||
|
||
const { ipcRenderer } = window.electron; | ||
|
||
const streams = new Map<number, IpcPushStream>(); | ||
|
||
export interface IpcPushStream { | ||
on(event: 'data', listener: (chunk: string) => void): this; | ||
|
||
on(event: 'end', listener: () => void): this; | ||
|
||
on(event: 'error', listener: (error: Error) => void): this; | ||
} | ||
|
||
export class IpcPushStream extends EventEmitter { | ||
static { | ||
ipcRenderer.on('stream-data', (event, id: number, chunk: string) => { | ||
streams.get(id)?.emit('data', chunk); | ||
}); | ||
|
||
ipcRenderer.on('stream-end', (event, id: number) => { | ||
streams.get(id)?.emit('end'); | ||
streams.delete(id); | ||
}); | ||
|
||
ipcRenderer.on('stream-error', (event, id: number, error: Error) => { | ||
streams.get(id)?.emit('error', error); | ||
streams.delete(id); | ||
}); | ||
} | ||
|
||
private constructor(private readonly id: number) { | ||
super(); | ||
streams.set(id, this); | ||
} | ||
|
||
public static async open(filePath: string) { | ||
return new IpcPushStream(await window.electron.ipcRenderer.invoke('stream-open', filePath)); | ||
} | ||
|
||
public close() { | ||
streams.delete(this.id); | ||
ipcRenderer.send('stream-close', this.id); | ||
} | ||
} |