Skip to content

Commit

Permalink
Implement bundle sync status
Browse files Browse the repository at this point in the history
Reviewed By: pakoito

Differential Revision: D6807480

fbshipit-source-id: d71f2cecd882c47e79bb71dfb9d03d3597fa4068
  • Loading branch information
rayshih authored and facebook-github-bot committed Feb 1, 2018
1 parent 9f57ded commit 88980f2
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 8 deletions.
3 changes: 2 additions & 1 deletion React/DevSupport/RCTInspectorDevServerHelper.h
Expand Up @@ -5,12 +5,13 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>


#import <React/RCTDefines.h> #import <React/RCTDefines.h>
#import <React/RCTInspectorPackagerConnection.h>


#if RCT_DEV #if RCT_DEV


@interface RCTInspectorDevServerHelper : NSObject @interface RCTInspectorDevServerHelper : NSObject


+ (void)connectWithBundleURL:(NSURL *)bundleURL; + (RCTInspectorPackagerConnection *)connectWithBundleURL:(NSURL *)bundleURL;
+ (void)disableDebugger; + (void)disableDebugger;
+ (void)attachDebugger:(NSString *)owner + (void)attachDebugger:(NSString *)owner
withBundleURL:(NSURL *)bundleURL withBundleURL:(NSURL *)bundleURL
Expand Down
4 changes: 3 additions & 1 deletion React/DevSupport/RCTInspectorDevServerHelper.mm
Expand Up @@ -104,7 +104,7 @@ + (void)disableDebugger
sendEventToAllConnections(kDebuggerMsgDisable); sendEventToAllConnections(kDebuggerMsgDisable);
} }


+ (void)connectWithBundleURL:(NSURL *)bundleURL + (RCTInspectorPackagerConnection *)connectWithBundleURL:(NSURL *)bundleURL
{ {
NSURL *inspectorURL = getInspectorDeviceUrl(bundleURL); NSURL *inspectorURL = getInspectorDeviceUrl(bundleURL);


Expand All @@ -122,6 +122,8 @@ + (void)connectWithBundleURL:(NSURL *)bundleURL
socketConnections[key] = connection; socketConnections[key] = connection;
[connection connect]; [connection connect];
} }

return connection;
} }


@end @end
Expand Down
10 changes: 9 additions & 1 deletion React/Inspector/RCTInspectorPackagerConnection.h
Expand Up @@ -2,15 +2,23 @@


#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import <React/RCTDefines.h> #import <React/RCTDefines.h>
#import <React/RCTInspector.h>


#if RCT_DEV #if RCT_DEV


@interface RCTBundleStatus : NSObject
@property (atomic, assign) BOOL isLastBundleDownloadSuccess;
@property (atomic, assign) NSTimeInterval bundleUpdateTimestamp;
@end

typedef RCTBundleStatus *(^RCTBundleStatusProvider)(void);

@interface RCTInspectorPackagerConnection : NSObject @interface RCTInspectorPackagerConnection : NSObject
- (instancetype)initWithURL:(NSURL *)url; - (instancetype)initWithURL:(NSURL *)url;

- (void)connect; - (void)connect;
- (void)closeQuietly; - (void)closeQuietly;
- (void)sendEventToAllConnections:(NSString *)event; - (void)sendEventToAllConnections:(NSString *)event;
- (void)setBundleStatusProvider:(RCTBundleStatusProvider)bundleStatusProvider;
@end @end


@interface RCTInspectorRemoteConnection : NSObject @interface RCTInspectorRemoteConnection : NSObject
Expand Down
21 changes: 21 additions & 0 deletions React/Inspector/RCTInspectorPackagerConnection.m
Expand Up @@ -14,13 +14,17 @@


const int RECONNECT_DELAY_MS = 2000; const int RECONNECT_DELAY_MS = 2000;


@implementation RCTBundleStatus
@end

@interface RCTInspectorPackagerConnection () <RCTSRWebSocketDelegate> { @interface RCTInspectorPackagerConnection () <RCTSRWebSocketDelegate> {
NSURL *_url; NSURL *_url;
NSMutableDictionary<NSString *, RCTInspectorLocalConnection *> *_inspectorConnections; NSMutableDictionary<NSString *, RCTInspectorLocalConnection *> *_inspectorConnections;
RCTSRWebSocket *_webSocket; RCTSRWebSocket *_webSocket;
dispatch_queue_t _jsQueue; dispatch_queue_t _jsQueue;
BOOL _closed; BOOL _closed;
BOOL _suppressConnectionErrors; BOOL _suppressConnectionErrors;
RCTBundleStatusProvider _bundleStatusProvider;
} }
@end @end


Expand Down Expand Up @@ -51,6 +55,11 @@ - (instancetype)initWithURL:(NSURL *)url
return self; return self;
} }


- (void)setBundleStatusProvider:(RCTBundleStatusProvider)bundleStatusProvider
{
_bundleStatusProvider = bundleStatusProvider;
}

- (void)handleProxyMessage:(NSDictionary<NSString *, id> *)message - (void)handleProxyMessage:(NSDictionary<NSString *, id> *)message
{ {
NSString *event = message[@"event"]; NSString *event = message[@"event"];
Expand Down Expand Up @@ -135,11 +144,23 @@ - (NSArray *)pages
{ {
NSArray<RCTInspectorPage *> *pages = [RCTInspector pages]; NSArray<RCTInspectorPage *> *pages = [RCTInspector pages];
NSMutableArray *array = [NSMutableArray arrayWithCapacity:pages.count]; NSMutableArray *array = [NSMutableArray arrayWithCapacity:pages.count];

RCTBundleStatusProvider statusProvider = _bundleStatusProvider;
RCTBundleStatus *bundleStatus = statusProvider == nil
? nil
: statusProvider();

for (RCTInspectorPage *page in pages) { for (RCTInspectorPage *page in pages) {
NSDictionary *jsonPage = @{ NSDictionary *jsonPage = @{
@"id": [@(page.id) stringValue], @"id": [@(page.id) stringValue],
@"title": page.title, @"title": page.title,
@"app": [[NSBundle mainBundle] bundleIdentifier], @"app": [[NSBundle mainBundle] bundleIdentifier],
@"isLastBundleDownloadSuccess": bundleStatus == nil
? [NSNull null]
: @(bundleStatus.isLastBundleDownloadSuccess),
@"bundleUpdateTimestamp": bundleStatus == nil
? [NSNull null]
: @((long)bundleStatus.bundleUpdateTimestamp * 1000),
}; };
[array addObject:jsonPage]; [array addObject:jsonPage];
} }
Expand Down
Expand Up @@ -116,9 +116,15 @@ public interface SymbolicationListener {
private @Nullable InspectorPackagerConnection mInspectorPackagerConnection; private @Nullable InspectorPackagerConnection mInspectorPackagerConnection;
private @Nullable OkHttpClient mOnChangePollingClient; private @Nullable OkHttpClient mOnChangePollingClient;
private @Nullable OnServerContentChangeListener mOnServerContentChangeListener; private @Nullable OnServerContentChangeListener mOnServerContentChangeListener;
private InspectorPackagerConnection.BundleStatusProvider mBundlerStatusProvider;


public DevServerHelper(DevInternalSettings settings, String packageName) { public DevServerHelper(
DevInternalSettings settings,
String packageName,
InspectorPackagerConnection.BundleStatusProvider bundleStatusProvider
) {
mSettings = settings; mSettings = settings;
mBundlerStatusProvider = bundleStatusProvider;
mClient = new OkHttpClient.Builder() mClient = new OkHttpClient.Builder()
.connectTimeout(HTTP_CONNECT_TIMEOUT_MS, TimeUnit.MILLISECONDS) .connectTimeout(HTTP_CONNECT_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.readTimeout(0, TimeUnit.MILLISECONDS) .readTimeout(0, TimeUnit.MILLISECONDS)
Expand Down Expand Up @@ -212,7 +218,11 @@ public void openInspectorConnection() {
new AsyncTask<Void, Void, Void>() { new AsyncTask<Void, Void, Void>() {
@Override @Override
protected Void doInBackground(Void... params) { protected Void doInBackground(Void... params) {
mInspectorPackagerConnection = new InspectorPackagerConnection(getInspectorDeviceUrl(), mPackageName); mInspectorPackagerConnection = new InspectorPackagerConnection(
getInspectorDeviceUrl(),
mPackageName,
mBundlerStatusProvider
);
mInspectorPackagerConnection.connect(); mInspectorPackagerConnection.connect();
return null; return null;
} }
Expand Down
Expand Up @@ -47,6 +47,7 @@
import com.facebook.react.common.ShakeDetector; import com.facebook.react.common.ShakeDetector;
import com.facebook.react.common.futures.SimpleSettableFuture; import com.facebook.react.common.futures.SimpleSettableFuture;
import com.facebook.react.devsupport.DevServerHelper.PackagerCommandListener; import com.facebook.react.devsupport.DevServerHelper.PackagerCommandListener;
import com.facebook.react.devsupport.InspectorPackagerConnection;
import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener; import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener;
import com.facebook.react.devsupport.interfaces.DevOptionHandler; import com.facebook.react.devsupport.interfaces.DevOptionHandler;
import com.facebook.react.devsupport.interfaces.DevSupportManager; import com.facebook.react.devsupport.interfaces.DevSupportManager;
Expand Down Expand Up @@ -156,6 +157,8 @@ private enum ErrorType {
private @Nullable DevBundleDownloadListener mBundleDownloadListener; private @Nullable DevBundleDownloadListener mBundleDownloadListener;
private @Nullable List<ErrorCustomizer> mErrorCustomizers; private @Nullable List<ErrorCustomizer> mErrorCustomizers;


private InspectorPackagerConnection.BundleStatus mBundleStatus;

private static class JscProfileTask extends AsyncTask<String, Void, Void> { private static class JscProfileTask extends AsyncTask<String, Void, Void> {
private static final MediaType JSON = private static final MediaType JSON =
MediaType.parse("application/json; charset=utf-8"); MediaType.parse("application/json; charset=utf-8");
Expand Down Expand Up @@ -217,7 +220,17 @@ public DevSupportManagerImpl(
mApplicationContext = applicationContext; mApplicationContext = applicationContext;
mJSAppBundleName = packagerPathForJSBundleName; mJSAppBundleName = packagerPathForJSBundleName;
mDevSettings = new DevInternalSettings(applicationContext, this); mDevSettings = new DevInternalSettings(applicationContext, this);
mDevServerHelper = new DevServerHelper(mDevSettings, mApplicationContext.getPackageName()); mBundleStatus = new InspectorPackagerConnection.BundleStatus();
mDevServerHelper = new DevServerHelper(
mDevSettings,
mApplicationContext.getPackageName(),
new InspectorPackagerConnection.BundleStatusProvider() {
@Override
public InspectorPackagerConnection.BundleStatus getBundleStatus() {
return mBundleStatus;
}
}
);
mBundleDownloadListener = devBundleDownloadListener; mBundleDownloadListener = devBundleDownloadListener;


// Prepare shake gesture detector (will be started/stopped from #reload) // Prepare shake gesture detector (will be started/stopped from #reload)
Expand Down Expand Up @@ -1032,6 +1045,10 @@ public void reloadJSFromServer(final String bundleURL) {
public void onSuccess() { public void onSuccess() {
mDevLoadingViewController.hide(); mDevLoadingViewController.hide();
mDevLoadingViewVisible = false; mDevLoadingViewVisible = false;
synchronized (DevSupportManagerImpl.this) {
mBundleStatus.isLastDownloadSucess = true;
mBundleStatus.updateTimestamp = System.currentTimeMillis();
}
if (mBundleDownloadListener != null) { if (mBundleDownloadListener != null) {
mBundleDownloadListener.onSuccess(); mBundleDownloadListener.onSuccess();
} }
Expand All @@ -1057,6 +1074,9 @@ public void onProgress(@Nullable final String status, @Nullable final Integer do
public void onFailure(final Exception cause) { public void onFailure(final Exception cause) {
mDevLoadingViewController.hide(); mDevLoadingViewController.hide();
mDevLoadingViewVisible = false; mDevLoadingViewVisible = false;
synchronized (DevSupportManagerImpl.this) {
mBundleStatus.isLastDownloadSucess = false;
}
if (mBundleDownloadListener != null) { if (mBundleDownloadListener != null) {
mBundleDownloadListener.onFailure(cause); mBundleDownloadListener.onFailure(cause);
} }
Expand Down
Expand Up @@ -8,7 +8,6 @@
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;


import android.os.AsyncTask; import android.os.AsyncTask;
Expand All @@ -33,11 +32,17 @@ public class InspectorPackagerConnection {
private final Connection mConnection; private final Connection mConnection;
private final Map<String, Inspector.LocalConnection> mInspectorConnections; private final Map<String, Inspector.LocalConnection> mInspectorConnections;
private final String mPackageName; private final String mPackageName;
private BundleStatusProvider mBundleStatusProvider;


public InspectorPackagerConnection(String url, String packageName) { public InspectorPackagerConnection(
String url,
String packageName,
BundleStatusProvider bundleStatusProvider
) {
mConnection = new Connection(url); mConnection = new Connection(url);
mInspectorConnections = new HashMap<>(); mInspectorConnections = new HashMap<>();
mPackageName = packageName; mPackageName = packageName;
mBundleStatusProvider = bundleStatusProvider;
} }


public void connect() { public void connect() {
Expand Down Expand Up @@ -143,11 +148,14 @@ private void handleWrappedEvent(JSONObject payload) throws JSONException {
private JSONArray getPages() throws JSONException { private JSONArray getPages() throws JSONException {
List<Inspector.Page> pages = Inspector.getPages(); List<Inspector.Page> pages = Inspector.getPages();
JSONArray array = new JSONArray(); JSONArray array = new JSONArray();
BundleStatus bundleStatus = mBundleStatusProvider.getBundleStatus();
for (Inspector.Page page : pages) { for (Inspector.Page page : pages) {
JSONObject jsonPage = new JSONObject(); JSONObject jsonPage = new JSONObject();
jsonPage.put("id", String.valueOf(page.getId())); jsonPage.put("id", String.valueOf(page.getId()));
jsonPage.put("title", page.getTitle()); jsonPage.put("title", page.getTitle());
jsonPage.put("app", mPackageName); jsonPage.put("app", mPackageName);
jsonPage.put("isLastBundleDownloadSuccess", bundleStatus.isLastDownloadSucess);
jsonPage.put("bundleUpdateTimestamp", bundleStatus.updateTimestamp);
array.put(jsonPage); array.put(jsonPage);
} }
return array; return array;
Expand Down Expand Up @@ -306,4 +314,25 @@ private void closeWebSocketQuietly() {
} }
} }
} }

static public class BundleStatus {
public Boolean isLastDownloadSucess;
public long updateTimestamp = -1;

public BundleStatus(
Boolean isLastDownloadSucess,
long updateTimestamp
) {
this.isLastDownloadSucess = isLastDownloadSucess;
this.updateTimestamp = updateTimestamp;
}

public BundleStatus() {
this(false, -1);
}
}

public interface BundleStatusProvider {
public BundleStatus getBundleStatus();
}
} }

0 comments on commit 88980f2

Please sign in to comment.