Search in sources :

Example 1 with UnaryCallback

use of org.apache.sling.scripting.sightly.js.impl.async.UnaryCallback in project sling by apache.

the class UseFunction method use.

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;
            dependencyResolver.resolve(depNames.get(i), new UnaryCallback() {

                @Override
                public void invoke(Object arg) {
                    counter[0]--;
                    dependencies[dependencyPos] = arg;
                    if (counter[0] == 0) {
                        Object result = JsUtils.callFn(callback, cx, scope, thisObj, dependencies);
                        asyncContainer.complete(result);
                    }
                }
            });
        }
    }
    return asyncContainer;
}
Also used : UnaryCallback(org.apache.sling.scripting.sightly.js.impl.async.UnaryCallback) AsyncContainer(org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer) NativeObject(org.mozilla.javascript.NativeObject) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Aggregations

AsyncContainer (org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer)1 UnaryCallback (org.apache.sling.scripting.sightly.js.impl.async.UnaryCallback)1 NativeObject (org.mozilla.javascript.NativeObject)1 ScriptableObject (org.mozilla.javascript.ScriptableObject)1