use of im.actor.runtime.promise.Promise in project actor-platform by actorapp.
the class UserRouter method onLoadFullUser.
//
// Users changed
//
@Verified
private void onLoadFullUser(int uid) {
if (requestedFullUsers.contains(uid)) {
return;
}
requestedFullUsers.add(uid);
freeze();
users().getValueAsync(uid).flatMap((Function<User, Promise<Tuple2<ResponseLoadFullUsers, User>>>) u -> {
if (!u.isHaveExtension()) {
ArrayList<ApiUserOutPeer> users = new ArrayList<>();
users.add(new ApiUserOutPeer(u.getUid(), u.getAccessHash()));
return api(new RequestLoadFullUsers(users)).map(responseLoadFullUsers -> new Tuple2<>(responseLoadFullUsers, u));
} else {
if (!getUserVM(uid).isInPhoneBook().get()) {
return checkIsInPhoneBook(u).flatMap(new Function<Void, Promise<Tuple2<ResponseLoadFullUsers, User>>>() {
@Override
public Promise<Tuple2<ResponseLoadFullUsers, User>> apply(Void aVoid) {
return Promise.failure(new RuntimeException("Already loaded"));
}
});
} else {
return Promise.failure(new RuntimeException("Already loaded"));
}
}
}).then(r -> {
User upd = r.getT2().updateExt(r.getT1().getFullUsers().get(0));
users().addOrUpdateItem(upd);
}).chain(r -> checkIsInPhoneBook(r.getT2().updateExt(r.getT1().getFullUsers().get(0)))).after((r, e) -> unfreeze());
}
use of im.actor.runtime.promise.Promise in project actor-platform by actorapp.
the class SequenceHandlerActor method onDifferenceUpdate.
private Promise<Void> onDifferenceUpdate(@NotNull List<ApiUser> users, @NotNull List<ApiGroup> groups, @NotNull List<ApiUserOutPeer> userOutPeers, @NotNull List<ApiGroupOutPeer> groupOutPeers, @NotNull List<Update> updates) {
if (updates.size() > 0) {
String command = "Difference updates:";
for (Update u : updates) {
command += "\n| " + u;
}
Log.d(TAG, command);
}
beginUpdates();
// Related Users
Promise<Void> currentPromise = updates().applyRelatedData(users, groups);
// Loading missing peers
currentPromise = currentPromise.chain(v -> updates().loadRequiredPeers(userOutPeers, groupOutPeers));
// Apply Diff
long applyStart = im.actor.runtime.Runtime.getCurrentTime();
currentPromise = currentPromise.chain(v -> processor.applyDifferenceUpdate(updates)).then(v -> {
Log.d(TAG, "Difference applied in " + (im.actor.runtime.Runtime.getCurrentTime() - applyStart) + " ms");
endUpdates();
});
return currentPromise;
}
use of im.actor.runtime.promise.Promise in project actor-platform by actorapp.
the class AndroidHttpProvider method getMethod.
@Override
public Promise<HTTPResponse> getMethod(String url, int startOffset, int size, int totalSize) {
return new Promise<>(resolver -> {
final Request request = new Request.Builder().url(url).addHeader("Range", "bytes=" + startOffset + "-" + (startOffset + size)).build();
Log.d(TAG, "Downloading part: " + request.toString());
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
Log.d(TAG, "Downloading part error: " + request.toString());
e.printStackTrace();
resolver.error(new HTTPError(0));
}
@Override
public void onResponse(Response response) throws IOException {
Log.d(TAG, "Downloading part response: " + request.toString() + " -> " + response.toString());
if (response.code() >= 200 && response.code() < 300) {
resolver.result(new HTTPResponse(response.code(), response.body().bytes()));
} else {
resolver.error(new HTTPError(response.code()));
}
}
});
});
}
use of im.actor.runtime.promise.Promise in project actor-platform by actorapp.
the class AndroidInputFile method read.
@Override
public Promise<FilePart> read(int fileOffset, int len) {
return new Promise<>(resolver -> {
executor.execute(() -> {
try {
byte[] data = new byte[len];
randomAccessFile.seek(fileOffset);
randomAccessFile.read(data, 0, len);
resolver.result(new FilePart(fileOffset, len, data));
} catch (Exception e) {
e.printStackTrace();
resolver.error(e);
}
});
});
}
Aggregations