gnh1201 / welsonjs

WelsonJS - Build a Windows app on the Windows built-in JavaScript engine
https://catswords.social/@catswords_oss
GNU General Public License v3.0
143 stars 12 forks source link

[lib/pipe-ipc] Improving concurrencies in a multiprocessing #84

Closed gnh1201 closed 9 months ago

gnh1201 commented 11 months ago

Summary

Resolution of issues encountered when requiring concurrencies in a multiprocessing environment

Related to IPC(Inter-Process Communication)

Related to processes

Related issues

gnh1201 commented 11 months ago

Summary of Actual User Case

After applying the improvements, when using the writeFile and readFile function and providing absolutely nothing, not even a blank (''), it would simply wait indefinitely without performing any task.

Code snippets

lib/file.js

function writeFile(FN, content, charset) {
    if (typeof content === "undefined") {
        console.warn("CONTENT has not been passed. Force to empty string.");
        content = '';
    }
    if (typeof charset === "undefined") {
        console.warn("CHARSET has not been passed. Force to UTF-8.");
        charset = PipeIPC.CdoUTF_8;
    }

    var pipe = PipeIPC.connect("volatile");
    pipe.setCharset(charset);
    pipe.startRecorder(FN, PipeIPC.ForWriting);
    pipe.write(content);
    pipe.destroy();
    return true;
}
function readFile(FN, charset) {
    if (typeof charset === "undefined") {
        console.warn("CHARSET has not been passed. Force to UTF-8.");
        charset = PipeIPC.CdoUTF_8;
    }

    var text =  ''; 
    var pipe = PipeIPC.connect("volatile");
    pipe.setCharset(charset);
    pipe.loadFromFile(FN, charset);
    text += pipe.read();
    pipe.destroy();

    return text;
}

In a caller

var filename = "helloworld.txt";
writeFile(filename);   // not correctly but allowed
//writeFile(filename, "helloworld", "utf-8");   // correctly

var text = readFile(filename);   // not correctly but allowed
//readFile(filename, "utf-8");   // correctly
console.log(text);
gnh1201 commented 9 months ago

Done