From 26079d118865874bf4b8185e7858e54d81df4163 Mon Sep 17 00:00:00 2001 From: Misode Date: Wed, 6 Nov 2024 06:53:05 +0100 Subject: [PATCH] Sort overlay file systems based on prefix length --- src/app/services/FileSystem.ts | 17 ++++++++++------- src/app/services/Spyglass.ts | 6 ++++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/app/services/FileSystem.ts b/src/app/services/FileSystem.ts index d7bf6659..1a09966a 100644 --- a/src/app/services/FileSystem.ts +++ b/src/app/services/FileSystem.ts @@ -46,19 +46,22 @@ export class MixedFileSystem implements core.ExternalFileSystem { constructor( public readonly base: core.ExternalFileSystem, - public readonly overlays: Map = new Map(), - ) {} + public readonly overlays: { prefix: string, fs: core.ExternalFileSystem }[] = [], + ) { + this.overlays.sort((a, b) => b.prefix.length - a.prefix.length) + } - async setOverlay(prefix: string, fileSystem: core.ExternalFileSystem) { - this.overlays.set(prefix, fileSystem) + async setOverlay(prefix: string, fs: core.ExternalFileSystem) { + this.overlays.push({ prefix, fs }) + this.overlays.sort((a, b) => b.prefix.length - a.prefix.length) if (this.watcher) { - await this.watcher.withOverlay(prefix, fileSystem) + await this.watcher.withOverlay(prefix, fs) } return this } getFileSystem(location: core.FsLocation) { - for (const [prefix, fs] of this.overlays.entries()) { + for (const { prefix, fs } of this.overlays) { if (location.toString().startsWith(prefix)) { return fs } @@ -105,7 +108,7 @@ class MixedWatcher extends BrowserEventEmitter implements core.FsWatcher { super() Promise.all([ this.initWatcher(fs.base, locations, options), - ...[...fs.overlays.values()].map(overlay => this.initWatcher(overlay, locations, options)), + ...fs.overlays.map(overlay => this.initWatcher(overlay.fs, locations, options)), ]).then(() => { this.emit('ready') }) diff --git a/src/app/services/Spyglass.ts b/src/app/services/Spyglass.ts index 7919b7d9..9424926e 100644 --- a/src/app/services/Spyglass.ts +++ b/src/app/services/Spyglass.ts @@ -17,7 +17,7 @@ import siteConfig from '../Config.js' import { computeIfAbsent, genPath, message } from '../Utils.js' import type { VersionMeta } from './DataFetcher.js' import { fetchBlockStates, fetchRegistries, fetchVanillaMcdoc, fetchVersions, getVersionChecksum } from './DataFetcher.js' -import { IndexedDbFileSystem, MixedFileSystem } from './FileSystem.js' +import { IndexedDbFileSystem, MemoryFileSystem, MixedFileSystem } from './FileSystem.js' import type { VersionId } from './Versions.js' const builtinMcdoc = ` @@ -46,7 +46,9 @@ interface ClientDocument { } export class SpyglassClient { - public readonly fs = new MixedFileSystem(new IndexedDbFileSystem()) + public readonly fs = new MixedFileSystem(new IndexedDbFileSystem(), [ + { prefix: 'file:///project/mcdoc/', fs: new MemoryFileSystem() }, + ]) public readonly externals: core.Externals = { ...BrowserExternals, archive: {