Skip to content

Commit

Permalink
Log timed out requests for content
Browse files Browse the repository at this point in the history
This renames the interruption callback and logs when the latch times out
prior to a response from the editor.
  • Loading branch information
mkevins committed Nov 2, 2021
1 parent 1180e79 commit bc2ab62
Showing 1 changed file with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.reactnative.maskedview.RNCMaskedViewPackage;

import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.AppLog.T;
import org.wordpress.mobile.ReactNativeAztec.ReactAztecPackage;
import org.wordpress.mobile.ReactNativeGutenbergBridge.BuildConfig;
import org.wordpress.mobile.ReactNativeGutenbergBridge.GutenbergBridgeJS2Parent;
Expand Down Expand Up @@ -855,20 +856,24 @@ private void updateContent(String title, String content) {
}
}

public interface OnGetContentTimeout {
public interface OnGetContentInterrupted {
void onGetContentTimeout(InterruptedException ie);
}

public synchronized CharSequence getContent(CharSequence originalContent, OnGetContentTimeout onGetContentTimeout) {
public synchronized CharSequence getContent(CharSequence originalContent,
OnGetContentInterrupted onGetContentInterrupted) {
if (mReactContext != null) {
mGetContentCountDownLatch = new CountDownLatch(1);

mRnReactNativeGutenbergBridgePackage.getRNReactNativeGutenbergBridgeModule().getHtmlFromJS();

try {
mGetContentCountDownLatch.await(10, TimeUnit.SECONDS);
boolean success = mGetContentCountDownLatch.await(10, TimeUnit.SECONDS);
if (!success) {
AppLog.e(T.EDITOR, "Timeout reached before response from requestGetHtml");
}
} catch (InterruptedException ie) {
onGetContentTimeout.onGetContentTimeout(ie);
onGetContentInterrupted.onGetContentTimeout(ie);
}

return mContentChanged ? (mContentHtml == null ? "" : mContentHtml) : originalContent;
Expand All @@ -879,16 +884,19 @@ public synchronized CharSequence getContent(CharSequence originalContent, OnGetC
return originalContent;
}

public synchronized CharSequence getTitle(OnGetContentTimeout onGetContentTimeout) {
public synchronized CharSequence getTitle(OnGetContentInterrupted onGetContentInterrupted) {
if (mReactContext != null) {
mGetContentCountDownLatch = new CountDownLatch(1);

mRnReactNativeGutenbergBridgePackage.getRNReactNativeGutenbergBridgeModule().getHtmlFromJS();

try {
mGetContentCountDownLatch.await(10, TimeUnit.SECONDS);
boolean success = mGetContentCountDownLatch.await(10, TimeUnit.SECONDS);
if (!success) {
AppLog.e(T.EDITOR, "Timeout reached before response from requestGetHtml");
}
} catch (InterruptedException ie) {
onGetContentTimeout.onGetContentTimeout(ie);
onGetContentInterrupted.onGetContentTimeout(ie);
}

return mTitle == null ? "" : mTitle;
Expand All @@ -905,22 +913,24 @@ public synchronized CharSequence getTitle(OnGetContentTimeout onGetContentTimeou
* same event anyway, and also share the same mechanism to suspend execution until a response is received (or a
* timeout is reached).
* @param originalContent fallback content to return in case the timeout is reached, or the thread is interrupted
* @param onGetContentTimeout callback to invoke if thread is interrupted before the timeout
* @param onGetContentInterrupted callback to invoke if thread is interrupted before the timeout
* @return
*/
public synchronized Pair<CharSequence, CharSequence> getTitleAndContent(CharSequence originalContent,
OnGetContentTimeout onGetContentTimeout) {
OnGetContentInterrupted onGetContentInterrupted) {
if (mReactContext != null) {
mGetContentCountDownLatch = new CountDownLatch(1);

mRnReactNativeGutenbergBridgePackage.getRNReactNativeGutenbergBridgeModule().getHtmlFromJS();

try {
// TODO: we should consider logging when await returns false
mGetContentCountDownLatch.await(10, TimeUnit.SECONDS);
boolean success = mGetContentCountDownLatch.await(10, TimeUnit.SECONDS);
if (!success) {
AppLog.e(T.EDITOR, "Timeout reached before response from requestGetHtml");
}
} catch (InterruptedException ie) {
// TODO: this should be either renamed, or invoked when the await returns false.

This comment has been minimized.

Copy link
@mkevins

mkevins Nov 2, 2021

Author Contributor

Forgot to remove this comment.. will remove before merging.

onGetContentTimeout.onGetContentTimeout(ie);
onGetContentInterrupted.onGetContentTimeout(ie);
}

return new Pair<>(
Expand Down Expand Up @@ -949,7 +959,10 @@ public boolean triggerGetContentInfo(OnContentInfoReceivedListener onContentInfo
mRnReactNativeGutenbergBridgePackage.getRNReactNativeGutenbergBridgeModule().getHtmlFromJS();

try {
mGetContentCountDownLatch.await(5, TimeUnit.SECONDS);
boolean success = mGetContentCountDownLatch.await(5, TimeUnit.SECONDS);
if (!success) {
AppLog.e(T.EDITOR, "Timeout reached before response from requestGetHtml");
}
if (mContentInfo == null) {
onContentInfoReceivedListener.onContentInfoFailed();
} else {
Expand Down

0 comments on commit bc2ab62

Please sign in to comment.