Ameliorate race condition with web import.

This commit is contained in:
Brad Nelson
2022-11-19 22:37:29 -08:00
parent 80fb47f87b
commit 595ed9e829
2 changed files with 33 additions and 13 deletions

View File

@ -747,6 +747,22 @@ JSWORD: getItem { dst dst_limit key key_len session -- n }
return data.length; return data.length;
~ ~
JSWORD: removeItem { key key_len session }
if (session) {
sessionStorage.removeItem(context.GetRawString(u8, key, key_len));
} else {
localStorage.removeItem(context.GetRawString(u8, key, key_len));
}
~
JSWORD: clearItems { session }
if (session) {
sessionStorage.clear();
} else {
localStorage.clear();
}
~
JSWORD: getKey { key key_limit index session -- n } JSWORD: getKey { key key_limit index session -- n }
if (session) { if (session) {
var data = sessionStorage.key(index); var data = sessionStorage.key(index);
@ -818,18 +834,17 @@ if (!globalObj.write) {
filepick.style.display = 'none'; filepick.style.display = 'none';
document.body.appendChild(filepick); document.body.appendChild(filepick);
context.handleImport = function() { context.handleImport = function() {
document.body.onfocus = null; document.body.onblur = function() {
context.filepick_filename = null; document.body.onfocus = function() {
context.filepick_result = 0;
};
context.handleImport = function() {
document.body.onfocus = null; document.body.onfocus = null;
setTimeout(function() { setTimeout(function() {
if (filepick.files.length === 0) { if (filepick.files.length === 0) {
context.filepick_result = 0; context.filepick_result = 0;
context.filepick_filename = null; context.filepick_filename = null;
} }
}, 100); }, 500);
};
};
}; };
filepick.onchange = function(event) { filepick.onchange = function(event) {
if (event.target.files.length > 0) { if (event.target.files.length > 0) {
@ -838,6 +853,7 @@ if (!globalObj.write) {
var data = context.GetRawString( var data = context.GetRawString(
new Uint8Array(e.target.result), 0, e.target.result.byteLength); new Uint8Array(e.target.result), 0, e.target.result.byteLength);
try { try {
if (context.filepick_filename === null) { throw 'fail'; }
localStorage.setItem(context.filepick_filename, data); localStorage.setItem(context.filepick_filename, data);
context.filepick_result = -1; context.filepick_result = -1;
context.filepick_filename = null; context.filepick_filename = null;
@ -860,7 +876,7 @@ if (!globalObj.write) {
JSWORD: upload-start { filename n } JSWORD: upload-start { filename n }
context.filepick_filename = context.GetRawString(u8, filename, n); context.filepick_filename = context.GetRawString(u8, filename, n);
document.body.onfocus = context.handleImport; context.handleImport();
context.filepick.click(); context.filepick.click();
~ ~

View File

@ -33,9 +33,13 @@ web definitions
0 0 a n 0 getItem { len } 0 0 a n 0 getItem { len }
here { buf } len allot here { buf } len allot
buf len a n 0 getItem len = assert buf len a n 0 getItem len = assert
a n 0 removeItem
buf len evaluate buf len evaluate
; ;
: ls 0 keyCount 0 do pad 80 i 0 getKey pad swap type cr loop ;
: rm bl parse 0 removeItem ;
: import s" _temp.fs" 2dup upload-file include-file ; : import s" _temp.fs" 2dup upload-file include-file ;
: yielding begin 50 ms yield again ; : yielding begin 50 ms yield again ;