| 28 | type EditorBufferStoreEvents = Record<string, unknown[]> |
| 29 | |
| 30 | class EditorBufferStore extends TypedEmitter<EditorBufferStoreEvents> { |
| 31 | editorBufferStorePath: string |
| 32 | bufferStores: Record<string, BufferStoreEntry> | null |
| 33 | serviceName: string |
| 34 | encryptKeys: string[] |
| 35 | writeSequence: number |
| 36 | |
| 37 | constructor(paths: EditorBufferStorePaths) { |
| 38 | super() |
| 39 | |
| 40 | const { editorBufferStorePath } = paths |
| 41 | this.editorBufferStorePath = editorBufferStorePath |
| 42 | // Object of paths to buffer stores. Buffer stores are NOT held in memory |
| 43 | // for performance reasons — they are read from disk when needed and |
| 44 | // written to disk when updated. |
| 45 | this.bufferStores = null |
| 46 | this.serviceName = 'marktext' |
| 47 | this.encryptKeys = [] |
| 48 | this.writeSequence = 0 |
| 49 | |
| 50 | this.init() |
| 51 | } |
| 52 | |
| 53 | init(): void { |
| 54 | if (!fs.existsSync(this.editorBufferStorePath)) { |
| 55 | fs.mkdirSync(this.editorBufferStorePath, { recursive: true }) |
| 56 | } |
| 57 | this._listenForIpcMain() |
| 58 | } |
| 59 | |
| 60 | getAll(): Record<string, BufferStoreEntry> { |
| 61 | return this.getAllBufferStores() |
| 62 | } |
| 63 | |
| 64 | getAllBufferStores(): Record<string, BufferStoreEntry> { |
| 65 | if (!this.bufferStores) { |
| 66 | this.bufferStores = this.findEditorBufferStores(this.editorBufferStorePath) |
| 67 | } |
| 68 | |
| 69 | return this.bufferStores |
| 70 | } |
| 71 | |
| 72 | clearBufferStoresWithAllSaved(): void { |
| 73 | this.bufferStores = this.getAllBufferStores() |
| 74 | |
| 75 | for (const id in this.bufferStores) { |
| 76 | try { |
| 77 | const buffer = this.readBufferStoreFile(this.bufferStores[id].filePath) |
| 78 | const allSaved = buffer.tabs.every((file) => file.isSaved) |
| 79 | if (buffer.tabs.length === 0 || allSaved) { |
| 80 | try { |
| 81 | fs.unlinkSync(this.bufferStores[id].filePath) |
| 82 | } catch (e) { |
| 83 | console.error('Failed to delete buffer store file during clear', e) |
| 84 | } |
| 85 | } |
| 86 | } catch (e) { |
| 87 | console.error('Failed to read buffer store file during clear', e) |
nothing calls this directly
no outgoing calls
no test coverage detected