/*
 * The MIT License
 *
 * Copyright 2020 Intuit Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.intuit.karate.runtime;

import com.intuit.karate.graal.JsValue;
import java.util.function.Consumer;
import java.util.function.Function;
import org.graalvm.polyglot.Value;

/**
 *
 * @author pthomas3
 */
public class ScenarioListener implements Consumer, Function, Runnable {
    
    private final ScenarioEngine parent;
    private final ScenarioEngine child;
    private final CharSequence source;
    
    private Value function;
    
    public ScenarioListener(ScenarioEngine parent, Value function) {
        this.parent = parent;
        this.child = parent.child();
        source = function.getSourceLocation().getCharacters();
    }
    
    private Value get() {
        if (function != null) {
            return function;
        }
        ScenarioEngine.set(child);
        child.init();
        function = child.attachSource(source);
        return function;
    }
    
    @Override
    public void accept(Object arg) {
        get().executeVoid(arg);
    }
    
    @Override
    public Object apply(Object arg) {
        Value result = get().execute(arg);
        return new JsValue(result).getValue();
    }
    
    @Override
    public void run() {
        get().executeVoid();
    }
    
}

@@ -1309,16 +1309,73 @@
 t.Value;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0A/**%0A *
@@ -1471,20 +1471,107 @@
 nable %7B%0A
+%0A
     
+private static final Logger logger = LoggerFactory.getLogger(ScenarioListener.class);%0A
 %0A    pri
@@ -1687,45 +1687,8 @@
 ce;%0A
-    %0A    private Value function;%0A    
 %0A   
@@ -1741,24 +1741,21 @@
 , Value 
-function
+value
 ) %7B%0A    
@@ -1834,24 +1834,21 @@
 ource = 
-function
+value
 .getSour
@@ -1877,30 +1877,109 @@
 ters();%0A    
-%7D%0A
     
+logger.debug(%22init listener for: %7B%7D%22, value);%0A    %7D%0A%0A    private Value function;%0A
 %0A    private
@@ -2076,54 +2076,197 @@
     
-ScenarioEngine.set(child);%0A        child.init(
+logger.debug(%22thread init%22);%0A        ScenarioEngine.set(child);%0A        logger.debug(%22before listener init%22);%0A        child.init();%0A        logger.debug(%22after listener init, before attach%22
 );%0A 
@@ -2307,24 +2307,62 @@
 ce(source);%0A
+        logger.debug(%22after attach%22);%0A
         retu
@@ -2372,36 +2372,32 @@
 function;%0A    %7D%0A
-    
 %0A    @Override%0A 
@@ -2462,36 +2462,32 @@
 oid(arg);%0A    %7D%0A
-    
 %0A    @Override%0A 
@@ -2619,20 +2619,16 @@
 ;%0A    %7D%0A
-    
 %0A    @Ov
@@ -2693,15 +2693,11 @@
 ;%0A    %7D%0A
-    
 %0A%7D%0A

add some logging to see wtf is graal js thread ci problem