UNPKG

@mieweb/wikigdrive

Version:

Google Drive to MarkDown synchronization

124 lines (123 loc) 4.58 kB
import { MarkdownTreeProcessor } from '../transform/MarkdownTreeProcessor.js'; import { UserConfigService } from '../google_folder/UserConfigService.js'; import { getContentFileService } from '../transform/utils.js'; export class SocketManager { constructor(engine) { Object.defineProperty(this, "engine", { enumerable: true, configurable: true, writable: true, value: engine }); Object.defineProperty(this, "socketsMap", { enumerable: true, configurable: true, writable: true, value: {} }); Object.defineProperty(this, "fileService", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.engine.subscribe('jobs:changed', (driveId, driveJobs) => { this.onJobsChanged(driveId, driveJobs); }); this.engine.subscribe('toasts:added', (driveId, toast) => { this.onToastsAdded(driveId, toast); }); this.engine.subscribe('changes:changed', (driveId, changes) => { this.onChangesChanged(driveId, changes); }); } async mount(fileService) { this.fileService = fileService; } async addSocketConnection(ws, driveId) { if (!this.socketsMap[driveId]) { this.socketsMap[driveId] = new Set(); } this.socketsMap[driveId].add(ws); ws.send(JSON.stringify({ cmd: 'connected' })); const jobManagerContainer = this.engine.getContainer('job_manager'); const driveJobs = await jobManagerContainer.inspect(driveId); ws.send(JSON.stringify({ cmd: 'jobs:changed', payload: driveJobs })); if (this.engine.hasContainer('watch_changes')) { const watchChangesContainer = this.engine.getContainer('watch_changes'); const changes = await watchChangesContainer.getChanges(driveId); const filteredChanges = await this.getFilteredChanges(driveId, changes); ws.send(JSON.stringify({ cmd: 'changes:changed', payload: filteredChanges })); } ws.addEventListener('close', () => { this.socketsMap[driveId].delete(ws); }); } onToastsAdded(driveId, toast) { if (!this.socketsMap[driveId]) { return; } for (const socket of this.socketsMap[driveId]) { socket.send(JSON.stringify({ cmd: 'toasts:added', payload: toast })); } } onJobsChanged(driveId, driveJobs) { if (!this.socketsMap[driveId]) { return; } for (const socket of this.socketsMap[driveId]) { socket.send(JSON.stringify({ cmd: 'jobs:changed', payload: driveJobs })); } } async getFilteredChanges(driveId, changes) { let filteredChanges = []; const googleFileSystem = await this.fileService.getSubFileService(driveId, ''); const userConfigService = new UserConfigService(googleFileSystem); await userConfigService.load(); const transformedFileSystem = await this.fileService.getSubFileService(driveId + '_transform', ''); const contentFileService = await getContentFileService(transformedFileSystem, userConfigService); const markdownTreeProcessor = new MarkdownTreeProcessor(contentFileService); await markdownTreeProcessor.load(); if (!markdownTreeProcessor.isEmpty()) { for (const change of changes) { const fileId = change.id; const [file, drivePath] = await markdownTreeProcessor.findById(fileId); if (file && drivePath) { if (file.modifiedTime !== change.modifiedTime) { filteredChanges.push(change); } } } } else { filteredChanges = changes; } return filteredChanges; } async onChangesChanged(driveId, changes) { if (!this.socketsMap[driveId]) { return; } const filteredChanges = await this.getFilteredChanges(driveId, changes); for (const socket of this.socketsMap[driveId]) { socket.send(JSON.stringify({ cmd: 'changes:changed', payload: filteredChanges })); } } }