Problem with WorkerScript and list model of a ListView



  • Edit: Did not know how to format the code?!

    I have a WorkerScript which gets content from a AJAX-Call and print the processed content into a list model of the ListView. The ListView is like a file browser where you can click on a folder and get the content of the clicked folder. The content is read blockwise:

    <code>

    WorkerScript.onMessage = function(message) {

    if(message.method === "searchContainerEntries") {

            var ip = message.ip;
            var session = message.session;
            var debug = true;
    
            if(message.start === 0) {
                message.model.clear();
                //message.model.sync();
            }
            xhr = new XMLHttpRequest;
    
            xhr.open("POST", "http://"+ip+":8080", true);
            xhr.onreadystatechange  = function() {
    
                if (xhr.readyState == XMLHttpRequest.DONE) {
    
                    var response = xhr.responseText;
                    var objectArray = parseJSONString(response, "$.result[*]")
                    var mediaType = "";
    
                    for(var i=0;i<objectArray.length;i++) {
                        var mediaPath = objectArray[i].mediaPath;
                        var name = objectArray[i].name;
                        if(objectArray[i].mediaType === "directory") {
                            mediaType = "folder";
                        } else if(objectArray[i].mediaType === "item-picture") {
                            mediaType = "picture";
                        } else if(objectArray[i].mediaType === "item-music") {
                            mediaType = "music";
                        } else if(objectArray[i].mediaType === "item-movie") {
                            mediaType = "movie";
                        } else if(objectArray[i].mediaType === "directory-parent") {
                           mediaType = "back";
                        }
    
                        if(mediaType !== "back") {
    
                               message.model.append({"fileType" : mediaType, "name" : name, "iconSource" : "", "mediaPathItem" : mediaPath});
                                
                                //WorkerScript.sendMessage({"listViewInsert" : 'true', "fileType" : mediaType, "name" : name, "iconSource" : ", "mediaPathItem" : mediaPath});
                                message.model.sync();
                        }
                    }
                    // Send message back that we are ready to get further elements of current path
                    if(message.max !== 400) {
                        console.log("Sending next request from Worker Script in same path...")
                        WorkerScript.sendMessage({currentMediaPath: message.mediaPath});
                    }
                }
            }
            xhr.send('{"id":6,"jsonrpc":"2.0","method":"mediaNav_getMediaListContent","params":{"max":'+message.max+',"mediaPath":"'+message.mediaPath+'","sessionId":'+session+',"start":'+message.start+'}}');
    

    }
    </code>

    So, the WorkerScript gets the first 8 elements and send a message back that we can process the next 400 elements:

    WorkerScript {

    id: myWorker
    
    source: "js/searchdevices.js"
    
    // Messages sent FROM WorkerScript
    onMessage: {
           // We got first element block and can read next elements
            myWorker.sendMessage({'max': 400,  'ip' : sessionRefresh.ip, 'session' : sessionRefresh.session,
                                     'mediaPath' : messageObject.currentMediaPath, 'model' : listView1.model,
                                     'method' : 'searchContainerEntries', 'start' : 8});
        }
    }
    

    However, this is working good so far. No GUI blocking and processed elements where appended in list view model

    But there is a problem:

    • Like I wrote the list view acts as a file browser. Clicking on a folder will invoke the WorkerScript which reads the content of the folder blockwise.
    • **BUT NOW ** a user did not wait until the next read block is processed for the clicked folder and click quickly on the next folder. Now the next read content from the parent folder is wrongly appened to the clicked folder.

    Question

    How to fix this? I have no possibility to stop the invoked funtion in the WorkerScript, right? Furthermore I have no possibility to check the appended items against a path within the WorkerScript because I cannot access properties of qml elements.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.