Skip to content

Commit

Permalink
speed up window re-focus
Browse files Browse the repository at this point in the history
  • Loading branch information
hlorenzi committed Jul 6, 2024
1 parent 064ea27 commit fcb19b4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/data/editors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export interface Global
export const LAYERDEF_ID_MAP = "world"


export const DATE_TOLERANCE_MS = 250


export type Tool =
"move" |
"draw" |
Expand All @@ -73,6 +76,7 @@ export interface EditorDefs extends EditorCommon
{
type: "defs"
defs: Defs.Defs
lastSavedDefsDate: number
lastSavedDefsSerialized: string
lastSavedDefs: Defs.Defs

Expand All @@ -92,10 +96,12 @@ export interface EditorMap extends EditorCommon
{
type: "map"
defs: Defs.Defs
lastSavedDefsDate: number
lastSavedDefsSerialized: string
defsRootPath: string
defsBasePath: string
map: Map.Map
lastSavedMapDate: number
lastSavedMapSerialized: string
lastSavedMap: Map.Map
mapEditor: MapEditor.State
Expand Down Expand Up @@ -324,6 +330,7 @@ export async function openEditorDefs(rootRelativePath: string)
rootPath: rootRelativePath,
basePath: Filesystem.removeLastPathComponent(rootRelativePath),
defs: null!,
lastSavedDefsDate: 0,
lastSavedDefsSerialized: null!,
lastSavedDefs: null!,
history: [],
Expand All @@ -345,6 +352,10 @@ export async function openEditorDefs(rootRelativePath: string)

export async function loadEditorDefs(editor: EditorDefs)
{
const serDefsDate = await Filesystem.readFileLastModified(editor.rootPath)
if (serDefsDate < editor.lastSavedDefsDate + DATE_TOLERANCE_MS)
return true

let serDefsText = await Filesystem.readFileText(editor.rootPath)
if (serDefsText.length === 0)
{
Expand All @@ -355,7 +366,7 @@ export async function loadEditorDefs(editor: EditorDefs)
DefsSerialization.serialize(
Defs.makeNew()))
}

if (serDefsText === editor.lastSavedDefsSerialized)
return true

Expand Down Expand Up @@ -387,6 +398,7 @@ export async function saveEditorDefs(editorIndex: number)
await writable.write(serDefsText)
await writable.close()

editorData.lastSavedDefsDate = Date.now()
editorData.lastSavedDefsSerialized = serDefsText
editorData.lastSavedDefs = editorData.defs

Expand All @@ -412,10 +424,12 @@ export async function openEditorMap(rootRelativePath: string)
rootPath: rootRelativePath,
basePath: Filesystem.removeLastPathComponent(rootRelativePath),
defs: null!,
lastSavedDefsDate: 0,
lastSavedDefsSerialized: null!,
defsRootPath: null!,
defsBasePath: null!,
map: null!,
lastSavedMapDate: 0,
lastSavedMapSerialized: null!,
lastSavedMap: null!,
mapEditor: null!,
Expand Down Expand Up @@ -450,6 +464,12 @@ export async function loadEditorMap(editor: EditorMap)
return false
}

const serDefsDate = await Filesystem.readFileLastModified(defsFile.rootRelativePath)
const serMapDate = await Filesystem.readFileLastModified(editor.rootPath)
if (serDefsDate < editor.lastSavedDefsDate + DATE_TOLERANCE_MS &&
serMapDate < editor.lastSavedMapDate + DATE_TOLERANCE_MS)
return true

const serDefsText = await Filesystem.readFileText(defsFile.rootRelativePath)
const serDefs = DefsSerialization.parse(serDefsText)
const defs = DefsSerialization.deserialize(serDefs)
Expand All @@ -473,10 +493,12 @@ export async function loadEditorMap(editor: EditorMap)
const map = MapSerialization.deserialize(defs, serMap)

editor.defs = defs
editor.lastSavedDefsDate = Date.now()
editor.lastSavedDefsSerialized = serDefsText,
editor.defsRootPath = defsFile.rootRelativePath,
editor.defsBasePath = Filesystem.removeLastPathComponent(defsFile.rootRelativePath),
editor.map = map
editor.lastSavedMapDate = Date.now()
editor.lastSavedMapSerialized = serMapText
editor.lastSavedMap = map

Expand All @@ -501,6 +523,8 @@ export async function saveEditorMap(editorIndex: number)
await writable.write(serMapText)
await writable.close()

editorData.lastSavedDefsDate = Date.now()
editorData.lastSavedMapDate = Date.now()
editorData.lastSavedMapSerialized = serMapText
editorData.lastSavedMap = editorData.map
global.editors.refreshToken.commit()
Expand Down Expand Up @@ -569,7 +593,7 @@ export async function refreshDefsForOpenEditors()

export async function handleExternalFileChanges()
{
await Filesystem.refreshEntries()
//await Filesystem.refreshEntries()

for (let i = 0; i < global.editors.editors.length; i++)
{
Expand Down
8 changes: 8 additions & 0 deletions src/data/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,14 @@ export function resolveRelativePath(
}


export async function readFileLastModified(rootRelativePath: string)
{
const file = await findFile(rootRelativePath)
const fileData = await file.handle.getFile()
return fileData.lastModified
}


export async function readFileText(rootRelativePath: string)
{
const file = await findFile(rootRelativePath)
Expand Down

0 comments on commit fcb19b4

Please sign in to comment.