Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Persist some data in the simulator across runs.
The contents of the "Saved" folder that's mounted in the Mac via ExtFS are saved in IndexedDB. This us done using a homegrown method that extracts the contents during emulator unloading and uses the idb-keyval wrapper to store to. Emscripten's IDBFS is not an options since it relies on an event loop, which not an option. Communication of the emulator stop is done via a synthetic "stop" event so that we don't need to set up an additional communication path for it.
- Loading branch information
Showing
9 changed files
with
145 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type {EmulatorWorkerDirectorExtraction} from "./emulator-common"; | ||
import * as idbKeyVal from "idb-keyval"; | ||
|
||
export async function persistData( | ||
extraction: EmulatorWorkerDirectorExtraction | ||
) { | ||
await idbKeyVal.set(EXTRACTION_KEY, extraction); | ||
} | ||
|
||
export async function getPersistedData(): Promise< | ||
EmulatorWorkerDirectorExtraction | undefined | ||
> { | ||
const extraction = await idbKeyVal.get<EmulatorWorkerDirectorExtraction>( | ||
EXTRACTION_KEY | ||
); | ||
return extraction; | ||
} | ||
const EXTRACTION_KEY = "extraction"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { | ||
EmulatorWorkerDirectorExtraction, | ||
EmulatorWorkerDirectorExtractionEntry, | ||
} from "./emulator-common"; | ||
|
||
export function restorePersistedData( | ||
dirPath: string, | ||
extraction: EmulatorWorkerDirectorExtraction | ||
) { | ||
function restore( | ||
parentDirPath: string, | ||
entries: EmulatorWorkerDirectorExtractionEntry[] | ||
) { | ||
for (const entry of entries) { | ||
if (Array.isArray(entry.contents)) { | ||
const childDirPath = `${parentDirPath}/${entry.name}`; | ||
FS.mkdir(childDirPath); | ||
restore(childDirPath, entry.contents); | ||
} else { | ||
let entryParentDirPath = parentDirPath; | ||
let entryName = entry.name; | ||
if (entryName === "DInfo") { | ||
// Write the DInfo struct for this folder in the parent's .finf | ||
// directory. | ||
entryName = extraction.name; | ||
entryParentDirPath = `${ | ||
FS.analyzePath(dirPath).parentPath | ||
}/.finf`; | ||
if (!FS.analyzePath(entryParentDirPath).exists) { | ||
FS.mkdir(entryParentDirPath); | ||
} | ||
} | ||
FS.createDataFile( | ||
entryParentDirPath, | ||
entryName, | ||
entry.contents, | ||
true, | ||
true, | ||
true | ||
); | ||
} | ||
} | ||
} | ||
|
||
restore(dirPath, extraction.contents); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters