最終更新日 2025-04-28

jsmodeの「非同期マクロ」同士の順番制御

非同期マクロ同士の順番のコントロール

基本はPromise の then と同じような考え方

test1.mac
jsmode "JScript\\" + "func1_" + currentmacrofilename;

js {

    hidemaru.setJsCompleteNotify = function(label, notifyName) {
        setstaticvariable(label, notifyName, 2);
    }

    // ---------------------------------------------------------
    debuginfo(2);
    
    hidemaru.setJsCompleteNotify("HmTestMacro1", "");
    
    function func1() {
        console.log("func1");
        hidemaru.setJsCompleteNotify("HmTestMacro1", "end");
    }

    hidemaru.setTimeout(func1, 3000);
	
}
test2.mac
// execmacroなどで、test1.mac に続けて test2.mac が呼ばれているものとする。
// ファイル2がexecmacroなどされてるとする
jsmode "JScript\\" + "func2_" + currentmacrofilename;

js {

    hidemaru.waitJsCompleteNotify = function(label, notifyName, onCompleteCallBack, option) {
            
        var tickHandler = null;
        function tick() {
            var currentStatus = getstaticvariable(label, 2);
            
            if (currentStatus == notifyName) {
                hidemaru.clearTimeout(tickHandler);
                if (typeof(onCompleteCallBack) == "function") {
                    onCompleteCallBack();
                }
            }
            else {
                tickHandler = hidemaru.setTimeout(tick, 100);
            }
        }
        
        tickHandler = hidemaru.setTimeout(tick, 0);
        
        if (option && option.timeout) {
            hidemaru.setTimeout( function() { hidemaru.clearTimeout(tickHandler); }, option.timeout );
        }
    }

    // ---------------------------------------------------------

    debuginfo(2);

    function mainAsync() {
        console.log("待ってたよ");
    }
    
    hidemaru.waitJsCompleteNotify("HmTestMacro1", "end", {timeout:10000});

}