@@ -296,6 +296,12 @@ define(function LiveDevelopment(require, exports, module) {
296
296
_liveDocument . close ( ) ;
297
297
_liveDocument = undefined ;
298
298
}
299
+
300
+ if ( _serverProvider ) {
301
+ // Remove any "request" listeners that were added previously
302
+ $ ( _serverProvider ) . off ( ".livedev" ) ;
303
+ }
304
+
299
305
if ( _relatedDocuments ) {
300
306
_relatedDocuments . forEach ( function ( liveDoc ) {
301
307
liveDoc . close ( ) ;
@@ -315,13 +321,43 @@ define(function LiveDevelopment(require, exports, module) {
315
321
}
316
322
}
317
323
318
- /** Open a live document
324
+ /**
325
+ * @private
326
+ * Open a live document
319
327
* @param {Document } source document to open
320
- * @return {jQuery.Promise } A promise that is resolved once the live
321
- * document is open, and is never explicitly rejected.
322
328
*/
323
329
function _openDocument ( doc , editor ) {
330
+ _closeDocument ( ) ;
331
+ _liveDocument = _createDocument ( doc , editor ) ;
324
332
333
+ // Enable instrumentation
334
+ if ( _liveDocument . setInstrumentationEnabled ) {
335
+ var enableInstrumentation = false ;
336
+
337
+ if ( _serverProvider && _serverProvider . setRequestFilterPaths ) {
338
+ enableInstrumentation = true ;
339
+
340
+ _serverProvider . setRequestFilterPaths (
341
+ [ "/" + encodeURI ( ProjectManager . makeProjectRelativeIfPossible ( doc . file . fullPath ) ) ]
342
+ ) ;
343
+
344
+ // Send custom HTTP response for the current live document
345
+ $ ( _serverProvider ) . on ( "request.livedev" , function ( event , request ) {
346
+ // response can be null in which case the StaticServerDomain reverts to simple file serving.
347
+ var response = _liveDocument . getResponseData ? _liveDocument . getResponseData ( ) : null ;
348
+ request . send ( response ) ;
349
+ } ) ;
350
+ }
351
+
352
+ _liveDocument . setInstrumentationEnabled ( enableInstrumentation ) ;
353
+ }
354
+ }
355
+
356
+ /**
357
+ * @private
358
+ * Populate array of related documents reported by the browser agent(s)
359
+ */
360
+ function _getRelatedDocuments ( ) {
325
361
function createLiveStylesheet ( url ) {
326
362
var stylesheetDeferred = $ . Deferred ( ) ;
327
363
@@ -348,9 +384,6 @@ define(function LiveDevelopment(require, exports, module) {
348
384
return stylesheetDeferred . promise ( ) ;
349
385
}
350
386
351
- _closeDocument ( ) ;
352
- _liveDocument = _createDocument ( doc , editor ) ;
353
-
354
387
// Gather related CSS documents.
355
388
// FUTURE: Gather related JS documents as well.
356
389
_relatedDocuments = [ ] ;
@@ -453,13 +486,12 @@ define(function LiveDevelopment(require, exports, module) {
453
486
return ;
454
487
}
455
488
456
- var editor = EditorManager . getCurrentFullEditor ( ) ,
457
- status = STATUS_ACTIVE ;
489
+ var status = STATUS_ACTIVE ;
458
490
459
491
// Note: the following promise is never explicitly rejected, so there
460
- // is no failure handler. If _openDocument is changed so that rejection
492
+ // is no failure handler. If _getRelatedDocuments is changed so that rejection
461
493
// is possible, failure should be managed accordingly.
462
- _openDocument ( doc , editor )
494
+ _getRelatedDocuments ( )
463
495
. done ( function ( ) {
464
496
if ( doc . isDirty && _classForDocument ( doc ) !== CSSDocument ) {
465
497
status = STATUS_OUT_OF_SYNC ;
@@ -572,23 +604,7 @@ define(function LiveDevelopment(require, exports, module) {
572
604
function doLaunchAfterServerReady ( ) {
573
605
_setStatus ( STATUS_CONNECTING ) ;
574
606
575
- if ( _serverProvider ) {
576
- // Install a request filter for the current document. In the future,
577
- // we need to install filters for *all* files that need to be instrumented.
578
- HTMLInstrumentation . scanDocument ( doc ) ;
579
- _serverProvider . setRequestFilterPaths (
580
- [ "/" + ProjectManager . makeProjectRelativeIfPossible ( doc . file . fullPath ) ]
581
- ) ;
582
-
583
- // Remove any "request" listeners that were added previously
584
- $ ( _serverProvider ) . off ( ".livedev" ) ;
585
-
586
- $ ( _serverProvider ) . on ( "request.livedev" , function ( event , request ) {
587
- var html = HTMLInstrumentation . generateInstrumentedHTML ( doc ) ;
588
-
589
- request . send ( { body : html } ) ;
590
- } ) ;
591
- }
607
+ _openDocument ( doc , EditorManager . getCurrentFullEditor ( ) ) ;
592
608
593
609
Inspector . connectToURL ( launcherUrl ) . done ( result . resolve ) . fail ( function onConnectFail ( err ) {
594
610
if ( err === "CANCEL" ) {
@@ -669,7 +685,6 @@ define(function LiveDevelopment(require, exports, module) {
669
685
670
686
if ( ! doc || ! doc . root ) {
671
687
showWrongDocError ( ) ;
672
-
673
688
} else {
674
689
_serverProvider = LiveDevServerManager . getProvider ( doc . file . fullPath ) ;
675
690
@@ -841,9 +856,9 @@ define(function LiveDevelopment(require, exports, module) {
841
856
if ( Inspector . connected ( ) ) {
842
857
hideHighlight ( ) ;
843
858
if ( agents . network && agents . network . wasURLRequested ( doc . url ) ) {
844
- _closeDocument ( ) ;
845
- var editor = EditorManager . getCurrentFullEditor ( ) ;
846
- promise = _openDocument ( doc , editor ) ;
859
+ _openDocument ( doc , EditorManager . getCurrentFullEditor ( ) ) ;
860
+
861
+ promise = _getRelatedDocuments ( ) ;
847
862
} else {
848
863
if ( exports . config . experimental || _isHtmlFileExt ( doc . extension ) ) {
849
864
promise = close ( ) . done ( open ) ;
@@ -895,15 +910,10 @@ define(function LiveDevelopment(require, exports, module) {
895
910
896
911
/**
897
912
* Determines whether we can serve local file.
898
- *
899
- * @param {String } localPath
900
- * A local path to file being served.
901
- *
902
- * @return {Boolean }
903
- * true for yes, otherwise false.
913
+ * @param {String } localPath A local path to file being served.
914
+ * @return {Boolean } true for yes, otherwise false.
904
915
*/
905
916
UserServerProvider . prototype . canServe = function ( localPath ) {
906
-
907
917
var baseUrl = ProjectManager . getBaseUrl ( ) ;
908
918
if ( ! baseUrl ) {
909
919
return false ;
@@ -918,9 +928,7 @@ define(function LiveDevelopment(require, exports, module) {
918
928
919
929
/**
920
930
* Returns a base url for current project.
921
- *
922
- * @return {String }
923
- * Base url for current project.
931
+ * @return {String } Base url for current project.
924
932
*/
925
933
UserServerProvider . prototype . getBaseUrl = function ( ) {
926
934
return ProjectManager . getBaseUrl ( ) ;
0 commit comments