8
8
9
9
import android .content .Context ;
10
10
import android .os .AsyncTask ;
11
- import android .os .Handler ;
12
- import android .os .Looper ;
13
11
import android .widget .Toast ;
14
12
import androidx .annotation .Nullable ;
15
13
import com .facebook .common .logging .FLog ;
16
14
import com .facebook .infer .annotation .Assertions ;
17
15
import com .facebook .react .R ;
18
- import com .facebook .react .bridge .UiThreadUtil ;
19
16
import com .facebook .react .common .ReactConstants ;
20
- import com .facebook .react .common .network .OkHttpCallUtil ;
21
17
import com .facebook .react .devsupport .interfaces .DevBundleDownloadListener ;
22
18
import com .facebook .react .devsupport .interfaces .PackagerStatusCallback ;
23
19
import com .facebook .react .devsupport .interfaces .StackFrame ;
38
34
import java .util .concurrent .TimeUnit ;
39
35
import okhttp3 .Call ;
40
36
import okhttp3 .Callback ;
41
- import okhttp3 .ConnectionPool ;
42
37
import okhttp3 .MediaType ;
43
38
import okhttp3 .OkHttpClient ;
44
39
import okhttp3 .Request ;
@@ -68,8 +63,6 @@ public class DevServerHelper {
68
63
69
64
private static final String PACKAGER_OK_STATUS = "packager-status:running" ;
70
65
71
- private static final int LONG_POLL_KEEP_ALIVE_DURATION_MS = 2 * 60 * 1000 ; // 2 mins
72
- private static final int LONG_POLL_FAILURE_DELAY_MS = 5000 ;
73
66
private static final int HTTP_CONNECT_TIMEOUT_MS = 5000 ;
74
67
75
68
private static final String DEBUGGER_MSG_DISABLE = "{ \" id\" :1,\" method\" :\" Debugger.disable\" }" ;
@@ -117,15 +110,11 @@ public String typeID() {
117
110
118
111
private final DevInternalSettings mSettings ;
119
112
private final OkHttpClient mClient ;
120
- private final Handler mRestartOnChangePollingHandler ;
121
113
private final BundleDownloader mBundleDownloader ;
122
114
private final String mPackageName ;
123
115
124
- private boolean mOnChangePollingEnabled ;
125
116
private @ Nullable JSPackagerClient mPackagerClient ;
126
117
private @ Nullable InspectorPackagerConnection mInspectorPackagerConnection ;
127
- private @ Nullable OkHttpClient mOnChangePollingClient ;
128
- private @ Nullable OnServerContentChangeListener mOnServerContentChangeListener ;
129
118
private InspectorPackagerConnection .BundleStatusProvider mBundlerStatusProvider ;
130
119
131
120
public DevServerHelper (
@@ -142,7 +131,6 @@ public DevServerHelper(
142
131
.build ();
143
132
mBundleDownloader = new BundleDownloader (mClient );
144
133
145
- mRestartOnChangePollingHandler = new Handler (Looper .getMainLooper ());
146
134
mPackageName = packageName ;
147
135
}
148
136
@@ -524,90 +512,6 @@ private static String createPackagerStatusURL(String host) {
524
512
return String .format (Locale .US , "http://%s/status" , host );
525
513
}
526
514
527
- public void stopPollingOnChangeEndpoint () {
528
- mOnChangePollingEnabled = false ;
529
- mRestartOnChangePollingHandler .removeCallbacksAndMessages (null );
530
- if (mOnChangePollingClient != null ) {
531
- OkHttpCallUtil .cancelTag (mOnChangePollingClient , this );
532
- mOnChangePollingClient = null ;
533
- }
534
- mOnServerContentChangeListener = null ;
535
- }
536
-
537
- public void startPollingOnChangeEndpoint (
538
- OnServerContentChangeListener onServerContentChangeListener ) {
539
- if (mOnChangePollingEnabled ) {
540
- // polling already enabled
541
- return ;
542
- }
543
- mOnChangePollingEnabled = true ;
544
- mOnServerContentChangeListener = onServerContentChangeListener ;
545
- mOnChangePollingClient =
546
- new OkHttpClient .Builder ()
547
- .connectionPool (
548
- new ConnectionPool (1 , LONG_POLL_KEEP_ALIVE_DURATION_MS , TimeUnit .MILLISECONDS ))
549
- .connectTimeout (HTTP_CONNECT_TIMEOUT_MS , TimeUnit .MILLISECONDS )
550
- .build ();
551
- enqueueOnChangeEndpointLongPolling ();
552
- }
553
-
554
- private void handleOnChangePollingResponse (boolean didServerContentChanged ) {
555
- if (mOnChangePollingEnabled ) {
556
- if (didServerContentChanged ) {
557
- UiThreadUtil .runOnUiThread (
558
- new Runnable () {
559
- @ Override
560
- public void run () {
561
- if (mOnServerContentChangeListener != null ) {
562
- mOnServerContentChangeListener .onServerContentChanged ();
563
- }
564
- }
565
- });
566
- }
567
- enqueueOnChangeEndpointLongPolling ();
568
- }
569
- }
570
-
571
- private void enqueueOnChangeEndpointLongPolling () {
572
- Request request = new Request .Builder ().url (createOnChangeEndpointUrl ()).tag (this ).build ();
573
- Assertions .assertNotNull (mOnChangePollingClient )
574
- .newCall (request )
575
- .enqueue (
576
- new Callback () {
577
- @ Override
578
- public void onFailure (Call call , IOException e ) {
579
- if (mOnChangePollingEnabled ) {
580
- // this runnable is used by onchange endpoint poller to delay subsequent requests
581
- // in case
582
- // of a failure, so that we don't flood network queue with frequent requests in
583
- // case when
584
- // dev server is down
585
- FLog .d (ReactConstants .TAG , "Error while requesting /onchange endpoint" , e );
586
- mRestartOnChangePollingHandler .postDelayed (
587
- new Runnable () {
588
- @ Override
589
- public void run () {
590
- handleOnChangePollingResponse (false );
591
- }
592
- },
593
- LONG_POLL_FAILURE_DELAY_MS );
594
- }
595
- }
596
-
597
- @ Override
598
- public void onResponse (Call call , Response response ) throws IOException {
599
- handleOnChangePollingResponse (response .code () == 205 );
600
- }
601
- });
602
- }
603
-
604
- private String createOnChangeEndpointUrl () {
605
- return String .format (
606
- Locale .US ,
607
- "http://%s/onchange" ,
608
- mSettings .getPackagerConnectionSettings ().getDebugServerHost ());
609
- }
610
-
611
515
private String createLaunchJSDevtoolsCommandUrl () {
612
516
return String .format (
613
517
Locale .US ,
0 commit comments