use of php.runtime.invoke.RunnableInvoker in project jphp by jphp-compiler.
the class WrapPromise method resolve.
@Signature
public static WrapPromise resolve(Environment env, Memory result) {
if (result.instanceOf(WrapPromise.class)) {
WrapPromise promise = new WrapPromise(env);
promise.__construct(env, new RunnableInvoker(env, new Callback<Memory, Memory[]>() {
@Override
public Memory call(Memory[] args) {
try {
return ObjectMemory.valueOf(promise.then(env, Invoker.create(env, args[0]), Invoker.create(env, args[1])));
} catch (Exception e) {
env.forwardThrow(e);
return Memory.NULL;
}
}
}));
return promise;
} else {
return new WrapPromise(env, new Callback<Memory, Memory[]>() {
@Override
public Memory call(Memory[] args) {
// resolve.
return Invoker.create(env, args[0]).callAny(result);
}
});
}
}
use of php.runtime.invoke.RunnableInvoker in project jphp by jphp-compiler.
the class WrapPromise method all.
@Signature
public static WrapPromise all(Environment env, ForeachIterator promises) {
Map<String, WrapPromise> list = new LinkedHashMap<>();
while (promises.next()) {
Memory value = promises.getValue();
if (value.instanceOf(WrapPromise.class)) {
list.put(promises.getStringKey(), value.toObject(WrapPromise.class));
}
}
if (list.isEmpty()) {
return resolve(env, Memory.NULL);
}
int max = list.size();
ArrayMemory result = ArrayMemory.createListed(max);
for (String key : list.keySet()) {
result.put(key, Memory.NULL);
}
return new WrapPromise(env, new Callback<Memory, Memory[]>() {
@Override
public Memory call(Memory[] args) {
final boolean[] done = { false };
Invoker resolve = Invoker.create(env, args[0]);
Invoker reject = Invoker.create(env, args[1]);
final int[] count = { 0 };
for (Map.Entry<String, WrapPromise> entry : list.entrySet()) {
WrapPromise promise = entry.getValue();
String key = entry.getKey();
try {
promise.then(env, new RunnableInvoker(env, new Callback<Memory, Memory[]>() {
@Override
public Memory call(Memory[] args) {
if (!done[0]) {
result.put(key, args[0]);
count[0]++;
if (max == count[0]) {
done[0] = true;
resolve.callNoThrow(result);
}
}
return Memory.NULL;
}
}), new RunnableInvoker(env, new Callback<Memory, Memory[]>() {
@Override
public Memory call(Memory[] memories) {
if (!done[0]) {
done[0] = true;
reject.callNoThrow(args[0]);
}
return Memory.NULL;
}
}));
} catch (Exception e) {
env.forwardThrow(e);
}
}
return Memory.NULL;
}
});
}
Aggregations