Skip to content

Commit

Permalink
feat(android): have Ti.Database.DB.executeAllAsync return a Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
sgtcoolguy committed Jan 26, 2021
1 parent e22e26c commit 41e83be
Showing 1 changed file with 37 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public void run()
}
});
} catch (InterruptedException e) {
// Ignore...
promise.reject(e);
}
});
}
Expand Down Expand Up @@ -347,36 +347,47 @@ public Object[] executeAll(final String[] queries) throws BatchQueryException
* @param callback Result callback for query execution.
*/
@Kroll.method
public void executeAllAsync(final String[] queries, final KrollFunction callback)
public KrollPromise<Object[]> executeAllAsync(final String[] queries,
@Kroll.argument(optional = true) final KrollFunction callback)
{
// Validate `queries` and `callback` parameters.
if (queries == null || queries.length == 0 || callback == null) {
throw new InvalidParameterException("'query' and 'callback' parameters are required");
// Validate `queries` parameter
if (queries == null || queries.length == 0) {
throw new InvalidParameterException("'query' parameter is required");
}

executingQueue.set(true);
try {
queue.put(new Runnable() {
@Override
public void run()
{
Throwable error = null;
List<TiResultSetProxy> results = new ArrayList<>(queries.length);
for (int index = 0; index < queries.length; index++) {
try {
final TiResultSetProxy result = executeSQL(queries[index], null);
results.add(result);
} catch (Throwable t) {
error = new BatchQueryException(t, index, null);
break;
final KrollObject callbackThisObject = getKrollObject();
return KrollPromise.create((promise) -> {
executingQueue.set(true);
try {
queue.put(new Runnable() {
@Override
public void run()
{
Throwable error = null;
List<TiResultSetProxy> results = new ArrayList<>(queries.length);
for (int index = 0; index < queries.length; index++) {
try {
final TiResultSetProxy result = executeSQL(queries[index], null);
results.add(result);
} catch (Throwable t) {
error = new BatchQueryException(t, index, results);
break;
}
}
if (callback != null) {
callback.callAsync(callbackThisObject, new Object[] { error, results.toArray() });
}
if (error != null) {
promise.reject(error);
} else {
promise.resolve(results.toArray());
}
}
callback.callAsync(getKrollObject(), new Object[] { error, results.toArray() });
}
});
} catch (InterruptedException e) {
// Ignore...
}
});
} catch (InterruptedException e) {
promise.reject(e);
}
});
}

/**
Expand Down

0 comments on commit 41e83be

Please sign in to comment.