sieukrem / jn-npp-plugin

Plugin for Notepad++ allowing you to automate some tasks using JavaScript
https://github.com/sieukrem/jn-npp-plugin/wiki
110 stars 24 forks source link

onbeforeclose asynchronous #46

Closed kevn-xyz closed 7 years ago

kevn-xyz commented 7 years ago

Does onbeforeclose get triggered asynchronously from IDialog.close()? If I insert an alert after close() then everything runs fine. Is there anyway I can force it to be synchronous if it is in fact async?

sieukrem commented 7 years ago

It is synchronous, due to be able to cancel the close action! Why do you think it is async? Give me an example!

kevn-xyz commented 7 years ago
if (openDialoags.files.indexOf(currentFile) != -1) { //Removes old dialog if relinting the same file
    var objIndex = openDialogs.files.indexOf(currentFile);
    openDialogs.dialogObjs[objIndex].close();
    openDialogs.dialogObjs[objIndex].hide();
}
...
alert("step") // as long as this is here everything runs fine.
...
dockable = {
    name: "ESLint",
    docking: globalCCSettings.so.dialogPos,
    onbeforeclose: (function(oDindex) {
        return function() {
        openDialogs.dialogObjs.splice(oDindex, 1);
        openDialogs.files.splice(oDindex, 1);
        return true;
        };
    })(openDialogs.files.length)
};
...
var dialCfg = new Dialog.Grid({});
...
openDialogs.dialogObjs.push(new Dialog(dialCfg));
openDialogs.files.push(currentFile);

Without the alert("step") the new dockable seems to be created before the old dockables onbeforeclose has run and cleaned itself from the openDialogs arrays (ie. its oDindex is 1 higher than it should be).

Im assuming (openDialoags.files.indexOf(currentFile) != 1) == true

sieukrem commented 7 years ago

You are right, the method dialog.close() is asynchronous. I don't know any more, what was the descision to do it so. For the moment you can move code after .close() into onclose handler

kevn-xyz commented 7 years ago

I could have but then I would have had to added quite a few flags and if statements. I just reworked the code using IDs for the array elements. Thanks for your time