【问题标题】:Chrome App: Cannot retrieve file load statusChrome 应用程序:无法检索文件加载状态
【发布时间】:2015-02-12 18:39:28
【问题描述】:

我的 Chrome 应用程序有一个函数要求另一个函数加载文件,检查该函数是否设置了一个表示成功的标志 (External.curFile.lodd),然后尝试处理它。我的问题是我第一次调用该函数时没有设置标志,但是当我第二次调用它时,标志已经设置了。

我感觉这与 Chrome 文件函数是异步的有关,所以我在文件加载时让第一个函数空闲了一段时间。第一次加载永远不会成功,无论我等待多长时间,但第二次加载总是成功!

调用函数:

function load_by_lines_from_cur_dir( fileName, context ){ // determine the 'meaning' of a file line by line, return last 'meaning', otherwise 'null' 

    var curLineMeaning = null;
    var lastLineValid = true;

    External.read_file_in_load_path(fileName); // 'External' load 'fileName' and reads lines, REPLacement does not see this file

    // This is a dirty workaround that accounts for the fact that 'DirectoryEntry.getFile' is asynchronous, thus pre-parsing checks fail intil loaded
    var counter = 0, maxLoops = 10;
    nuClock();
    do{ 
        sleep(500); 
        counter++; 
        preDebug.innerText += '\r\nLoop:' + counter + " , " + time_since_last();

    }while( !External.curFile.lodd && (counter < maxLoops) ); //idle and check if file loaded, 5000ms max

    preDebug.innerText += '\r\nLoaded?:' + External.curFile.lodd;
    preDebug.innerText += '\r\nLines?:' +  External.curFile.lins;

    if( External.curFile.lodd ){ // The last load operating was successful, attempt to parse and interpret each line
        // parse and interpret lines, storing each meaning in 'curLineMeaning', until last line is reached
        while(!External.curFile.rEOF){ 
            curLineMeaning = meaning( s( External.readln_from_current_file() ), context); 
            preDebug.innerText += '\r\nNext Line?: ' + External.curFile.lnnm;
            preDebug.innerText += '\r\nEOF?: ' + External.curFile.rEOF;
        }
    } // else, return 'null'
    return curLineMeaning; // return the result of the last form
}

调用如下:

External.read_file_in_load_path = function(nameStr){ // Read the lines of 'nameStr' into 'External.curFile.lins'
    External.curPath.objt.getFile( // call 'DirectoryEntry.getFile' to fetch a file in that directory 
        nameStr,
        {create: false},
        function(fileEntry){ // action to perform on the fetched file, success
            External.curFile.name = nameStr; // store the file name for later use
            External.curFile.objt = fileEntry; // store the 'FileEntry' for later use

            External.curFile.objt.file( function(file){ // Returns 'File' object associated with selected file. Use this to read the file's content.
                var reader = new FileReader();
                reader.onload = function(e){
                    External.curFile.lodd = true; // File load success
                };
                reader.onloadend = function(e){
                    //var contents = e.target.result;
                    // URL, split string into lines: http://stackoverflow.com/questions/12371970/read-text-file-using-filereader
                    External.curFile.lins = e.target.result.split('\n'); // split the string result into individual lines
                };
                reader.readAsText(file);
                External.curFile.lnnm = 0; // Set current line to 0 for the newly-loaded file
                External.curFile.rEOF = false; // Reset EOF flag
                // let's try a message instead of a flag ...
                /*chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
                    console.log(response.farewell);
                });*/
            } );
        },
        function(e){ External.curFile.lodd = false; } // There was an error
    );
};

这个应用程序是 Scheme 的方言。应用知道源文件是否已加载,这一点很重要。

【问题讨论】:

    标签: javascript asynchronous file-io google-chrome-extension google-chrome-app


    【解决方案1】:

    我没有通读您的所有代码,但是您不能启动异步活动然后忙于等待它完成,因为 JavaScript 是单线程的。无论发生什么,在脚本完成当前处理之前,异步函数都不会执行。换句话说,异步并不意味着并发。

    一般来说,如果要在异步任务 B 完成后执行任务 A,则应该从 B 的完成回调中执行 A。这是直接、安全的方法。任何为了获得更好的响应能力或简化代码的捷径,都会有依赖性或竞争条件问题,并且需要大量的时间来解决问题。即便如此,也很难证明代码在所有情况下都能在所有平台上正确运行。

    【讨论】:

    • 我将加载文件的评估移到文件加载过程的完成回调中,它在第一次尝试时工作。不幸的是,这意味着我不能做 lispy 的事情并返回加载文件中最后一条语句的结果作为调用上述加载代码的加载语句的结果。
    • 任何了解 LISP 的人都知道太多,无法使用 JavaScript。 ;-)
    猜你喜欢
    • 2012-11-04
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多