summaryrefslogtreecommitdiff
path: root/platform/javascript/native/utils.js
diff options
context:
space:
mode:
authorFabio Alessandrelli <fabio.alessandrelli@gmail.com>2020-09-18 17:25:05 +0200
committerFabio Alessandrelli <fabio.alessandrelli@gmail.com>2020-09-23 09:51:06 +0200
commitdccd71c7a36fc31f6cc19e0f7f587fb0860c973a (patch)
tree6edeac4fc717836e661dd9a965cf6280693a981b /platform/javascript/native/utils.js
parent53f04aa1b93d67c62206eb8ee1182ce5f4934123 (diff)
JS synchronous start, better persistent FS sync.
The engine now expects to emscripten FS to be setup and sync-ed before main is called. This is exposed via `Module["initFS"]` which also allows to setup multiple persistence paths (internal use only for now). Additionally, FS syncing is done **once** for every loop if at least one file in a persistent path was open for writing and closed, and if the FS is not syncing already. This should potentially fix issues reported by users where "autosave" would not work on the web (never calling `syncfs` because of too many writes).
Diffstat (limited to 'platform/javascript/native/utils.js')
-rw-r--r--platform/javascript/native/utils.js34
1 files changed, 33 insertions, 1 deletions
diff --git a/platform/javascript/native/utils.js b/platform/javascript/native/utils.js
index 18f041fd63..0b3698fd86 100644
--- a/platform/javascript/native/utils.js
+++ b/platform/javascript/native/utils.js
@@ -28,6 +28,38 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
+Module['initFS'] = function(persistentPaths) {
+ FS.mkdir('/userfs');
+ FS.mount(IDBFS, {}, '/userfs');
+
+ function createRecursive(dir) {
+ try {
+ FS.stat(dir);
+ } catch (e) {
+ if (e.errno !== ERRNO_CODES.ENOENT) {
+ throw e;
+ }
+ FS.mkdirTree(dir);
+ }
+ }
+
+ persistentPaths.forEach(function(path) {
+ createRecursive(path);
+ FS.mount(IDBFS, {}, path);
+ });
+ return new Promise(function(resolve, reject) {
+ FS.syncfs(true, function(err) {
+ if (err) {
+ Module.idbfs = false;
+ console.log("IndexedDB not available: " + err.message);
+ } else {
+ Module.idbfs = true;
+ }
+ resolve(err);
+ });
+ });
+};
+
Module['copyToFS'] = function(path, buffer) {
var p = path.lastIndexOf("/");
var dir = "/";
@@ -37,7 +69,7 @@ Module['copyToFS'] = function(path, buffer) {
try {
FS.stat(dir);
} catch (e) {
- if (e.errno !== ERRNO_CODES.ENOENT) { // 'ENOENT', see https://github.com/emscripten-core/emscripten/blob/master/system/lib/libc/musl/arch/emscripten/bits/errno.h
+ if (e.errno !== ERRNO_CODES.ENOENT) {
throw e;
}
FS.mkdirTree(dir);