private Object use()

in src/main/java/org/apache/sling/scripting/sightly/js/impl/use/UseFunction.java [82:110]


    private Object use(List<String> depNames, final Function callback, final Context cx, final Scriptable scope) {
        final AsyncContainer asyncContainer = new AsyncContainer();
        if (depNames.isEmpty()) {
            callImmediate(callback, asyncContainer, cx, scope);
        } else {
            final int[] counter = {depNames.size()};
            final Object[] dependencies = new Object[depNames.size()];
            for (int i = 0; i < depNames.size(); i++) {
                final int dependencyPos = i;
                String dependency = depNames.get(i);
                ScriptNameAwareReader dependencyReader = dependencyResolver.resolve(globalBindings, dependency);
                if (dependencyReader == null) {
                    throw new SightlyException("Cannot locate script " + dependency);
                }
                Bindings bindings = new LazyBindings();
                bindings.putAll(globalBindings);
                bindings.put(ScriptEngine.FILENAME, dependencyReader.getScriptName());
                jsEnvironment.runScript(dependencyReader, bindings, Utils.EMPTY_BINDINGS, arg -> {
                    counter[0]--;
                    dependencies[dependencyPos] = arg;
                    if (counter[0] == 0) {
                        Object result = JsUtils.callFn(callback, cx, scope, thisObj, dependencies);
                        asyncContainer.complete(result);
                    }
                });
            }
        }
        return asyncContainer;
    }