-
Notifications
You must be signed in to change notification settings - Fork 42
[AIT-98] feat: realtime edits and deletes #1183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+816
−94
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
189 changes: 120 additions & 69 deletions
189
lib/src/main/java/io/ably/lib/realtime/ChannelBase.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| package io.ably.lib.types; | ||
|
|
||
| import io.ably.lib.http.HttpCore; | ||
| import io.ably.lib.util.Serialisation; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.msgpack.core.MessageFormat; | ||
| import org.msgpack.core.MessagePacker; | ||
| import org.msgpack.core.MessageUnpacker; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Contains the result of a publish operation. | ||
| */ | ||
| public class PublishResult { | ||
|
|
||
| private static final String SERIALS = "serials"; | ||
|
|
||
| /** | ||
| * An array of message serials corresponding 1:1 to the messages that were published. | ||
| * A serial may be null if the message was discarded due to a configured conflation rule. | ||
| */ | ||
| public final @Nullable String[] serials; | ||
|
|
||
| public PublishResult(@Nullable String[] serials) { | ||
| this.serials = serials; | ||
| } | ||
|
|
||
| private static PublishResult readFromJson(byte[] packed) throws MessageDecodeException { | ||
| return Serialisation.gson.fromJson(new String(packed), PublishResult.class); | ||
| } | ||
|
|
||
| private static PublishResult readMsgpack(byte[] packed) throws AblyException { | ||
| try { | ||
| MessageUnpacker unpacker = Serialisation.msgpackUnpackerConfig.newUnpacker(packed); | ||
| return readMsgpack(unpacker); | ||
| } catch (IOException ioe) { | ||
| throw AblyException.fromThrowable(ioe); | ||
| } | ||
| } | ||
|
|
||
| private static PublishResult readMsgpack(MessageUnpacker unpacker) throws IOException { | ||
| int fieldCount = unpacker.unpackMapHeader(); | ||
| for (int i = 0; i < fieldCount; i++) { | ||
| String fieldName = unpacker.unpackString(); | ||
| MessageFormat fieldFormat = unpacker.getNextFormat(); | ||
| if (fieldFormat.equals(MessageFormat.NIL)) { | ||
| unpacker.unpackNil(); | ||
| continue; | ||
| } | ||
|
|
||
| if (fieldName.equals(SERIALS)) { | ||
| int count = unpacker.unpackArrayHeader(); | ||
| String[] serials = new String[count]; | ||
| for (int j = 0; j < count; j++) { | ||
| if (unpacker.getNextFormat().equals(MessageFormat.NIL)) { | ||
| unpacker.unpackNil(); | ||
| serials[j] = null; | ||
| } else { | ||
| serials[j] = unpacker.unpackString(); | ||
| } | ||
| } | ||
| return new PublishResult(serials); | ||
| } else { | ||
| unpacker.skipValue(); | ||
| } | ||
| } | ||
| return new PublishResult(new String[]{}); | ||
| } | ||
|
|
||
| static void writeMsgpackArray(PublishResult[] results, MessagePacker packer) { | ||
| try { | ||
| int count = results.length; | ||
| packer.packArrayHeader(count); | ||
| for (PublishResult result : results) { | ||
| if (result != null) { | ||
| result.writeMsgpack(packer); | ||
| } else { | ||
| packer.packNil(); | ||
| } | ||
| } | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e.getMessage(), e); | ||
| } | ||
| } | ||
|
|
||
| static PublishResult[] readMsgpackArray(MessageUnpacker unpacker) throws IOException { | ||
| int count = unpacker.unpackArrayHeader(); | ||
| PublishResult[] results = new PublishResult[count]; | ||
| for (int i = 0; i < count; i++) { | ||
| results[i] = readMsgpack(unpacker); | ||
| } | ||
| return results; | ||
| } | ||
|
|
||
| public static HttpCore.BodyHandler<String> getBodyHandler() { | ||
| return new PublishResultBodyHandler(); | ||
| } | ||
|
|
||
| private void writeMsgpack(MessagePacker packer) throws IOException { | ||
| int fieldCount = 0; | ||
| if (serials != null) ++fieldCount; | ||
| packer.packMapHeader(fieldCount); | ||
| if (serials != null) { | ||
| packer.packString(SERIALS); | ||
| packer.packArrayHeader(serials.length); | ||
| for (String serial : serials) { | ||
| if (serial == null) { | ||
| packer.packNil(); | ||
| } else { | ||
| packer.packString(serial); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static class PublishResultBodyHandler implements HttpCore.BodyHandler<String> { | ||
|
|
||
| @Override | ||
| public String[] handleResponseBody(String contentType, byte[] body) throws AblyException { | ||
| try { | ||
| PublishResult publishResult = null; | ||
| if ("application/json".equals(contentType)) | ||
| publishResult = readFromJson(body); | ||
| else if ("application/x-msgpack".equals(contentType)) | ||
| publishResult = readMsgpack(body); | ||
| return publishResult != null ? publishResult.serials : new String[]{}; | ||
ttypic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (MessageDecodeException e) { | ||
| throw AblyException.fromThrowable(e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package io.ably.lib.util; | ||
|
|
||
| import io.ably.lib.realtime.CompletionListener; | ||
| import io.ably.lib.types.Callback; | ||
| import io.ably.lib.types.ErrorInfo; | ||
| import io.ably.lib.types.PublishResult; | ||
| import io.ably.lib.types.UpdateDeleteResult; | ||
|
|
||
| public class Listeners { | ||
|
|
||
| public static <T> Callback<T> fromCompletionListener(CompletionListener listener) { | ||
| return new CompletionListenerWrapper<T>(listener); | ||
| } | ||
|
|
||
| public static Callback<PublishResult> toPublishResultListener(Callback<UpdateDeleteResult> listener) { | ||
| return new UpdateResultToPublishAdapter(listener); | ||
| } | ||
|
|
||
| public static <T> CompletionListener unwrap(Callback<T> listener) { | ||
| if (listener instanceof CompletionListenerWrapper) { | ||
| return ((CompletionListenerWrapper<T>)listener).listener; | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private static class CompletionListenerWrapper<T> implements Callback<T> { | ||
| private final CompletionListener listener; | ||
|
|
||
| private CompletionListenerWrapper(CompletionListener listener) { | ||
| this.listener = listener; | ||
| } | ||
|
|
||
| @Override | ||
| public void onSuccess(T result) { | ||
| if (listener != null) { | ||
| listener.onSuccess(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onError(ErrorInfo reason) { | ||
| if (listener != null) { | ||
| listener.onError(reason); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static class UpdateResultToPublishAdapter implements Callback<PublishResult> { | ||
| private final Callback<UpdateDeleteResult> listener; | ||
|
|
||
| private UpdateResultToPublishAdapter(Callback<UpdateDeleteResult> listener) { | ||
| this.listener = listener; | ||
| } | ||
|
|
||
| @Override | ||
| public void onSuccess(PublishResult result) { | ||
| if (listener != null) { | ||
| String serial = result != null && result.serials != null && result.serials.length > 0 | ||
| ? result.serials[0] : null; | ||
| listener.onSuccess(new UpdateDeleteResult(serial)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onError(ErrorInfo reason) { | ||
| if (listener != null) { | ||
| listener.onError(reason); | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.