@@ -5,6 +5,7 @@ import minimatch = require("minimatch");
5
5
import * as path from "path" ;
6
6
import * as util from "util" ;
7
7
import * as moment from "moment" ;
8
+ import * as fiberBootstrap from "../fiber-bootstrap" ;
8
9
let gaze = require ( "gaze" ) ;
9
10
10
11
interface IProjectFileInfo {
@@ -13,39 +14,68 @@ interface IProjectFileInfo {
13
14
shouldIncludeFile : boolean ;
14
15
}
15
16
17
+ // https://github.com/Microsoft/TypeScript/blob/master/src/compiler/tsc.ts#L487-L489
18
+ const SYNC_WAIT_THRESHOLD = 250 ; //milliseconds
19
+
16
20
class SyncBatch {
17
21
private timer : NodeJS . Timer = null ;
18
22
private syncQueue : string [ ] = [ ] ;
23
+ private syncInProgress : boolean = false ;
19
24
20
25
constructor (
21
26
private $logger : ILogger ,
22
27
private $dispatcher : IFutureDispatcher ,
23
- private done : ( filesToSync : Array < string > ) => void ) {
24
- }
28
+ private syncData : ILiveSyncData ,
29
+ private done : ( ) => IFuture < void > ) {
30
+ }
31
+
32
+ private get filesToSync ( ) : string [ ] {
33
+ let filteredFiles = this . syncQueue . filter ( syncFilePath => ! UsbLiveSyncServiceBase . isFileExcluded ( path . relative ( this . syncData . projectFilesPath , syncFilePath ) , this . syncData . excludedProjectDirsAndFiles , this . syncData . projectFilesPath ) ) ;
34
+ return filteredFiles ;
35
+ }
25
36
26
- public addFile ( filePath : string ) : void {
27
- if ( this . timer ) {
28
- clearTimeout ( this . timer ) ;
37
+ public get syncPending ( ) {
38
+ return this . syncQueue . length > 0 ;
39
+ }
40
+
41
+ public reset ( ) : void {
42
+ this . syncQueue = [ ] ;
43
+ }
44
+
45
+ public syncFiles ( syncAction : ( filesToSync : string [ ] ) => IFuture < void > ) : IFuture < void > {
46
+ return ( ( ) => {
47
+ if ( this . filesToSync . length > 0 ) {
48
+ syncAction ( this . filesToSync ) . wait ( ) ;
49
+ this . reset ( ) ;
29
50
}
51
+ } ) . future < void > ( ) ( ) ;
52
+ }
30
53
31
- this . syncQueue . push ( filePath ) ;
54
+ public addFile ( filePath : string ) : void {
55
+ if ( this . timer ) {
56
+ clearTimeout ( this . timer ) ;
57
+ this . timer = null ;
58
+ }
32
59
60
+ this . syncQueue . push ( filePath ) ;
61
+
62
+ if ( ! this . syncInProgress ) {
33
63
this . timer = setTimeout ( ( ) => {
34
- let filesToSync = this . syncQueue ;
35
- if ( filesToSync . length > 0 ) {
36
- this . syncQueue = [ ] ;
37
- this . $logger . trace ( "Syncing %s" , filesToSync . join ( ", " ) ) ;
38
- this . $dispatcher . dispatch ( ( ) => {
39
- return ( ( ) => this . done ( filesToSync ) ) . future < void > ( ) ( ) ;
64
+ if ( this . syncQueue . length > 0 ) {
65
+ this . $logger . trace ( "Syncing %s" , this . syncQueue . join ( ", " ) ) ;
66
+ fiberBootstrap . run ( ( ) => {
67
+ try {
68
+ this . syncInProgress = true ;
69
+ this . done ( ) . wait ( ) ;
70
+ } finally {
71
+ this . syncInProgress = false ;
72
+ }
40
73
} ) ;
41
74
}
42
75
this . timer = null ;
43
- } , 250 ) ; // https://github.com/Microsoft/TypeScript/blob/master/src/compiler/tsc.ts#L487-L489
44
- }
45
-
46
- public get syncPending ( ) {
47
- return this . syncQueue . length > 0 ;
76
+ } , SYNC_WAIT_THRESHOLD ) ;
48
77
}
78
+ }
49
79
}
50
80
51
81
export class UsbLiveSyncServiceBase implements IUsbLiveSyncServiceBase {
@@ -96,7 +126,7 @@ export class UsbLiveSyncServiceBase implements IUsbLiveSyncServiceBase {
96
126
}
97
127
98
128
let projectFiles = this . $fs . enumerateFilesInDirectorySync ( data . projectFilesPath ,
99
- ( filePath , stat ) => ! this . isFileExcluded ( path . relative ( data . projectFilesPath , filePath ) , data . excludedProjectDirsAndFiles , data . projectFilesPath ) ,
129
+ ( filePath , stat ) => ! UsbLiveSyncServiceBase . isFileExcluded ( path . relative ( data . projectFilesPath , filePath ) , data . excludedProjectDirsAndFiles , data . projectFilesPath ) ,
100
130
{ enumerateDirectories : true }
101
131
) ;
102
132
@@ -111,32 +141,30 @@ export class UsbLiveSyncServiceBase implements IUsbLiveSyncServiceBase {
111
141
gaze ( "**/*" , { cwd : data . watchGlob } , function ( err : any , watcher : any ) {
112
142
this . on ( 'all' , ( event : string , filePath : string ) => {
113
143
if ( event === "added" || event === "changed" ) {
114
- if ( ! that . isFileExcluded ( filePath , data . excludedProjectDirsAndFiles , data . projectFilesPath ) ) {
115
- that . $dispatcher . dispatch ( ( ) => ( ( ) => {
116
- let fileHash = that . $fs . getFsStats ( filePath ) . wait ( ) . isFile ( ) ? that . $fs . getFileShasum ( filePath ) . wait ( ) : "" ;
117
- if ( fileHash === that . fileHashes [ filePath ] ) {
118
- that . $logger . trace ( `Skipping livesync for ${ filePath } file with ${ fileHash } hash.` ) ;
119
- return ;
120
- }
121
-
122
- that . $logger . trace ( `Adding ${ filePath } file with ${ fileHash } hash.` ) ;
123
- that . fileHashes [ filePath ] = fileHash ;
124
-
125
- let canExecuteFastLiveSync = data . canExecuteFastLiveSync && data . canExecuteFastLiveSync ( filePath ) ;
126
-
127
- if ( synciOSSimulator && ! canExecuteFastLiveSync ) {
128
- that . batchSimulatorLiveSync ( data , filePath ) ;
129
- }
130
-
131
- if ( ( ! that . $options . emulator || data . platform . toLowerCase ( ) === "android" ) && ! canExecuteFastLiveSync ) {
132
- that . batchLiveSync ( data , filePath ) ;
133
- }
134
-
135
- if ( canExecuteFastLiveSync ) {
136
- data . fastLiveSync ( filePath ) ;
137
- }
138
- } ) . future < void > ( ) ( ) ) ;
139
- }
144
+ that . $dispatcher . dispatch ( ( ) => ( ( ) => {
145
+ let fileHash = that . $fs . getFsStats ( filePath ) . wait ( ) . isFile ( ) ? that . $fs . getFileShasum ( filePath ) . wait ( ) : "" ;
146
+ if ( fileHash === that . fileHashes [ filePath ] ) {
147
+ that . $logger . trace ( `Skipping livesync for ${ filePath } file with ${ fileHash } hash.` ) ;
148
+ return ;
149
+ }
150
+
151
+ that . $logger . trace ( `Adding ${ filePath } file with ${ fileHash } hash.` ) ;
152
+ that . fileHashes [ filePath ] = fileHash ;
153
+
154
+ let canExecuteFastLiveSync = data . canExecuteFastLiveSync && data . canExecuteFastLiveSync ( filePath ) ;
155
+
156
+ if ( synciOSSimulator && ! canExecuteFastLiveSync ) {
157
+ that . batchSimulatorLiveSync ( data , filePath ) ;
158
+ }
159
+
160
+ if ( ( ! that . $options . emulator || data . platform . toLowerCase ( ) === "android" ) && ! canExecuteFastLiveSync ) {
161
+ that . batchLiveSync ( data , filePath ) ;
162
+ }
163
+
164
+ if ( canExecuteFastLiveSync ) {
165
+ data . fastLiveSync ( filePath ) ;
166
+ }
167
+ } ) . future < void > ( ) ( ) ) ;
140
168
}
141
169
142
170
if ( event === "deleted" ) {
@@ -265,41 +293,62 @@ export class UsbLiveSyncServiceBase implements IUsbLiveSyncServiceBase {
265
293
private batch : SyncBatch = null ;
266
294
267
295
private batchLiveSync ( data : ILiveSyncData , filePath : string ) : void {
296
+ this . $dispatcher . dispatch ( ( ) => ( ( ) => {
268
297
if ( ! this . batch || ! this . batch . syncPending ) {
269
- this . batch = new SyncBatch (
270
- this . $logger , this . $dispatcher , ( filesToSync ) => {
271
- this . preparePlatformForSync ( data . platform ) ;
272
- this . syncCore ( data , filesToSync , true ) . wait ( ) ;
273
- }
274
- ) ;
298
+ this . batch = new SyncBatch ( this . $logger , this . $dispatcher , data , ( ) => {
299
+ return ( ( ) => {
300
+ this . preparePlatformForSync ( data . platform ) . wait ( ) ;
301
+
302
+ setTimeout ( ( ) => {
303
+ fiberBootstrap . run ( ( ) => {
304
+ this . batch . syncFiles ( filesToSync => this . syncCore ( data , filesToSync , true ) ) . wait ( ) ;
305
+ } ) ;
306
+ } , SYNC_WAIT_THRESHOLD ) ;
307
+ } ) . future < void > ( ) ( ) ;
308
+ } ) ;
275
309
}
276
310
277
- this . $dispatcher . dispatch ( ( ) => ( ( ) => {
278
311
let fileToSync = data . beforeBatchLiveSyncAction ? data . beforeBatchLiveSyncAction ( filePath ) . wait ( ) : filePath ;
279
- if ( fileToSync ) {
312
+ if ( fileToSync ) {
280
313
this . batch . addFile ( fileToSync ) ;
281
314
}
282
315
} ) . future < void > ( ) ( ) ) ;
283
316
}
284
317
285
318
private batchSimulatorLiveSync ( data : ILiveSyncData , filePath : string ) : void {
319
+ this . $dispatcher . dispatch ( ( ) => ( ( ) => {
286
320
if ( ! this . batch || ! this . batch . syncPending ) {
287
- this . batch = new SyncBatch (
288
- this . $logger , this . $dispatcher , ( filesToSync ) => {
289
- this . preparePlatformForSync ( data . platform ) ;
290
- this . $iOSEmulatorServices . syncFiles ( data . appIdentifier , data . projectFilesPath , filesToSync , data . notRunningiOSSimulatorAction , data . getApplicationPathForiOSSimulatorAction , data . iOSSimulatorRelativeToProjectBasePathAction ) ;
291
- }
292
- ) ;
321
+ this . batch = new SyncBatch ( this . $logger , this . $dispatcher , data , ( ) => {
322
+ return ( ( ) => {
323
+ this . preparePlatformForSync ( data . platform ) . wait ( ) ;
324
+
325
+ setTimeout ( ( ) => {
326
+ fiberBootstrap . run ( ( ) => {
327
+ this . batch . syncFiles ( filesToSync =>
328
+ this . $iOSEmulatorServices . syncFiles ( data . appIdentifier ,
329
+ data . projectFilesPath ,
330
+ filesToSync ,
331
+ data . notRunningiOSSimulatorAction ,
332
+ data . getApplicationPathForiOSSimulatorAction ,
333
+ data . iOSSimulatorRelativeToProjectBasePathAction ) ) . wait ( ) ;
334
+ } ) ;
335
+ } , SYNC_WAIT_THRESHOLD ) ;
336
+ } ) . future < void > ( ) ( ) ;
337
+ } ) ;
293
338
}
294
339
295
- this . batch . addFile ( filePath ) ;
296
- }
340
+ let fileToSync = data . beforeBatchLiveSyncAction ? data . beforeBatchLiveSyncAction ( filePath ) . wait ( ) : filePath ;
341
+ this . batch . addFile ( fileToSync ) ;
297
342
298
- protected preparePlatformForSync ( platform : string ) {
299
- //Overridden in platform-specific services.
300
- }
343
+ } ) . future < void > ( ) ( ) ) ;
344
+ }
345
+
346
+ protected preparePlatformForSync ( platform : string ) : IFuture < void > {
347
+ return null ;
348
+ //Overridden in platform-specific services.
349
+ }
301
350
302
- private isFileExcluded ( path : string , exclusionList : string [ ] , projectDir : string ) : boolean {
351
+ public static isFileExcluded ( path : string , exclusionList : string [ ] , projectDir : string ) : boolean {
303
352
return ! ! _ . find ( exclusionList , ( pattern ) => minimatch ( path , pattern , { nocase : true } ) ) ;
304
353
}
305
354
0 commit comments