Skip to content

Commit

Permalink
fix(wasm): Avoid multiple FS.syncfs causing emscripten warning
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Feb 1, 2022
1 parent 4f08e8a commit ea33cf2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/Uno.UI/WasmScripts/Uno.UI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,7 @@ declare namespace Uno.Storage {
declare namespace Windows.Storage {
class StorageFolder {
private static _isInit;
private static _isSynchronizing;
private static dispatchStorageInitialized;
/**
* Determine if IndexDB is available, some browsers and modes disable it.
Expand Down
22 changes: 11 additions & 11 deletions src/Uno.UI/WasmScripts/Uno.UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -3667,12 +3667,7 @@ var Windows;
// Ensure to sync pseudo file system on unload (and periodically for safety)
if (!this._isInit) {
// Request an initial sync to populate the file system
FS.syncfs(true, err => {
if (err) {
console.error(`Error synchronizing filesystem from IndexDB: ${err} (errno: ${err.errno})`);
}
StorageFolder.onStorageInitialized();
});
StorageFolder.synchronizeFileSystem();
window.addEventListener("beforeunload", this.synchronizeFileSystem);
setInterval(this.synchronizeFileSystem, 10000);
this._isInit = true;
Expand All @@ -3689,14 +3684,19 @@ var Windows;
* Synchronize the IDBFS memory cache back to IndexDB
* */
static synchronizeFileSystem() {
FS.syncfs(err => {
if (err) {
console.error(`Error synchronizing filesystem from IndexDB: ${err} (errno: ${err.errno})`);
}
});
if (!StorageFolder._isSynchronizing) {
StorageFolder._isSynchronizing = true;
FS.syncfs(err => {
StorageFolder._isSynchronizing = false;
if (err) {
console.error(`Error synchronizing filesystem from IndexDB: ${err} (errno: ${err.errno})`);
}
});
}
}
}
StorageFolder._isInit = false;
StorageFolder._isSynchronizing = false;
Storage.StorageFolder = StorageFolder;
})(Storage = Windows.Storage || (Windows.Storage = {}));
})(Windows || (Windows = {}));
Expand Down
41 changes: 24 additions & 17 deletions src/Uno.UI/ts/Windows/Storage/StorageFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace Windows.Storage {

export class StorageFolder {
private static _isInit = false;
private static _isInitialized = false;
private static _isSynchronizing = false;
private static dispatchStorageInitialized: () => number;

/**
Expand Down Expand Up @@ -62,20 +63,14 @@ namespace Windows.Storage {
FS.mount(IDBFS, {}, path);

// Ensure to sync pseudo file system on unload (and periodically for safety)
if (!this._isInit) {
if (!this._isInitialized) {
// Request an initial sync to populate the file system
StorageFolder.synchronizeFileSystem(() => StorageFolder.onStorageInitialized());

// Request an initial sync to populate the file system
FS.syncfs(true, err => {
if (err) {
console.error(`Error synchronizing filesystem from IndexDB: ${err} (errno: ${err.errno})`);
}
StorageFolder.onStorageInitialized();
});

window.addEventListener("beforeunload", this.synchronizeFileSystem);
window.addEventListener("beforeunload", () => this.synchronizeFileSystem());
setInterval(this.synchronizeFileSystem, 10000);

this._isInit = true;
this._isInitialized = true;
}
}

Expand All @@ -91,11 +86,23 @@ namespace Windows.Storage {
/**
* Synchronize the IDBFS memory cache back to IndexDB
* */
private static synchronizeFileSystem(): void {
FS.syncfs(err => {
if (err) {
console.error(`Error synchronizing filesystem from IndexDB: ${err} (errno: ${err.errno})`);
}});
private static synchronizeFileSystem(onSynchronized: Function = null): void {

if (!StorageFolder._isSynchronizing) {
StorageFolder._isSynchronizing = true;

FS.syncfs(err => {
StorageFolder._isSynchronizing = false;

if (onSynchronized) {
onSynchronized();
}

if (err) {
console.error(`Error synchronizing filesystem from IndexDB: ${err} (errno: ${err.errno})`);
}
});
}
}
}
}

0 comments on commit ea33cf2

Please sign in to comment.