Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ const setupBackendServer = async (appPath: string, backendRootPath: string, user

surfBackendManager.start()
await surfBackendManager.waitForStart()
surfBackendManager.initWatcher()

initializeSFFSMain()
}
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/ipcHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { handleDragStart } from './drag'
import {
BrowserType,
ElectronAppInfo,
ResourceFileChange,
RightSidebarTab,
SFFSResource,
UserSettings
Expand Down Expand Up @@ -716,5 +717,15 @@ export const ipcSenders = {
}

IPC_EVENTS_MAIN.updateViewBounds.sendToWebContents(window.webContents, { viewId, bounds })
},

resourceFileChange(data: ResourceFileChange) {
const window = getMainWindow()
if (!window) {
log.error('Main window not found')
return
}

IPC_EVENTS_MAIN.resourceFileChange.sendToWebContents(window.webContents, data)
}
}
19 changes: 18 additions & 1 deletion app/src/main/sffs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,26 @@ export class SFFSMain {

const stringified = JSON.stringify(resource)

const result = this.sffs.js__store_update_resource(stringified)
const result = await this.sffs.js__store_update_resource(stringified)
return result
}

async deleteResource(id: string) {
console.debug('deleting resource with id', id)
await this.sffs.js__store_remove_resources([id])
}

async getResourceByPath(path: string): Promise<SFFSResource | null> {
console.log('getting resource by path', path)
const dataString = await this.sffs.js__store_get_resource_by_path(path)

const composite = optimisticParseJSON<SFFSRawCompositeResource>(dataString)
if (!composite) {
return null
}

return SFFSMain.convertCompositeResourceToResource(composite)
}
}

let sffsMain: SFFSMain | null = null
Expand Down
39 changes: 38 additions & 1 deletion app/src/main/surfBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { isWindows } from '@deta/utils'
import { spawn, type ChildProcess, execSync } from 'child_process'

import EventEmitter from 'events'
import { basename } from 'path'
import path, { basename } from 'path'
import { FileWatcher } from './watcher'
import { app } from 'electron'
import { ipcSenders } from './ipcHandlers'

export class SurfBackendServerManager extends EventEmitter {
private process: ChildProcess | null = null
Expand All @@ -17,6 +20,8 @@ export class SurfBackendServerManager extends EventEmitter {
private startResolve: (() => void) | null = null
private startReject: ((reason: Error) => void) | null = null

private watcher: FileWatcher | null = null

constructor(
private readonly serverPath: string,
private readonly args: string[],
Expand All @@ -29,6 +34,34 @@ export class SurfBackendServerManager extends EventEmitter {
return this.lastKnownHealth
}

initWatcher() {
const USER_DATA_PATH = app.getPath('userData')
const BACKEND_ROOT_PATH = path.join(USER_DATA_PATH, 'sffs_backend')
const BACKEND_RESOURCES_PATH = path.join(BACKEND_ROOT_PATH, 'resources')

this.watcher = new FileWatcher(BACKEND_RESOURCES_PATH)

this.watcher
.on('create', ({ filename, path }) => {
ipcSenders.resourceFileChange({
type: 'create',
data: { newName: filename, newPath: path }
})
})
.on('delete', ({ filename, path }) => {
ipcSenders.resourceFileChange({
type: 'delete',
data: { oldName: filename, oldPath: path }
})
})
.on('rename', ({ oldName, newName, oldPath, newPath }) => {
ipcSenders.resourceFileChange({
type: 'rename',
data: { oldName, newName, oldPath, newPath }
})
})
}

start(): void {
if (this.process) {
this.emit('warn', 'surf backend server is already running')
Expand Down Expand Up @@ -67,6 +100,10 @@ export class SurfBackendServerManager extends EventEmitter {
return
}

if (this.watcher) {
this.watcher.close()
}

this.isShuttingDown = true
this.process.kill()
this.process = null
Expand Down
Loading