1+ /*
2+ * Copyright (c) 2013 Bernhard Sirlinger. All rights reserved.
3+ *
4+ * Permission is hereby granted, free of charge, to any person obtaining a
5+ * copy of this software and associated documentation files (the "Software"),
6+ * to deal in the Software without restriction, including without limitation
7+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+ * and/or sell copies of the Software, and to permit persons to whom the
9+ * Software is furnished to do so, subject to the following conditions:
10+ *
11+ * The above copyright notice and this permission notice shall be included in
12+ * all copies or substantial portions of the Software.
13+ *
14+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20+ * DEALINGS IN THE SOFTWARE.
21+ *
22+ */
23+
24+
25+ /*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4,
26+ maxerr: 50, browser: true */
27+ /*global $, define, brackets */
28+
29+ define ( function ( require , exports , module ) {
30+ "use strict" ;
31+
32+ var AppInit = brackets . getModule ( "utils/AppInit" ) ,
33+ Commands = brackets . getModule ( "command/Commands" ) ,
34+ CommandManager = brackets . getModule ( "command/CommandManager" ) ,
35+ ExtensionUtils = brackets . getModule ( "utils/ExtensionUtils" ) ,
36+ FileUtils = brackets . getModule ( "file/FileUtils" ) ,
37+ LiveDevServerManager = brackets . getModule ( "LiveDevelopment/LiveDevServerManager" ) ,
38+ NodeConnection = brackets . getModule ( "utils/NodeConnection" ) ,
39+ ProjectManager = brackets . getModule ( "project/ProjectManager" ) ;
40+
41+ /**
42+ * @const
43+ * Amount of time to wait before automatically rejecting the connection
44+ * deferred. If we hit this timeout, we'll never have a node connection
45+ * for the static server in this run of Brackets.
46+ */
47+ var NODE_CONNECTION_TIMEOUT = 5000 ; // 5 seconds
48+
49+ /**
50+ * @private
51+ * @type {jQuery.Deferred.<NodeConnection> }
52+ * A deferred which is resolved with a NodeConnection or rejected if
53+ * we are unable to connect to Node.
54+ */
55+ var _nodeConnectionDeferred = new $ . Deferred ( ) ;
56+
57+ /**
58+ * @private
59+ * @type {NodeConnection }
60+ */
61+ var _nodeConnection = new NodeConnection ( ) ;
62+
63+ function initExtension ( ) {
64+ var connectionTimeout = setTimeout ( function ( ) {
65+ console . error ( "[FileWatcher] Timed out while trying to connect to node" ) ;
66+ _nodeConnectionDeferred . reject ( ) ;
67+ } , NODE_CONNECTION_TIMEOUT ) ;
68+
69+ _nodeConnection . connect ( true ) . then ( function ( ) {
70+ _nodeConnection . loadDomains (
71+ [ ExtensionUtils . getModulePath ( module , "node/FileWatcherDomain" ) ] ,
72+ true
73+ ) . done (
74+ function ( ) {
75+ $ ( _nodeConnection ) . on ( "fileWatcher.fileSystemChange" , function ( event , orgEvent , type ) {
76+ if ( orgEvent !== "change" ) {
77+ CommandManager . execute ( Commands . FILE_REFRESH ) ;
78+ }
79+ } ) ;
80+
81+ clearTimeout ( connectionTimeout ) ;
82+
83+ _nodeConnectionDeferred . resolveWith ( null , [ _nodeConnection ] ) ;
84+ } ) . fail (
85+ function ( ) { // Failed to connect
86+ console . error ( "[FileWatcher] Failed to connect to node" , arguments ) ;
87+ _nodeConnectionDeferred . reject ( ) ;
88+ }
89+ ) ;
90+ } ) ;
91+
92+ return _nodeConnectionDeferred . promise ( ) ;
93+ }
94+
95+ AppInit . htmlReady ( function ( ) {
96+ _nodeConnectionDeferred . done ( function ( nodeConnection ) {
97+ //nodeConnection.fileWatcher.startWatching(ProjectManager.getProjectRoot().fullPath, 1000);
98+ $ ( ProjectManager ) . on ( "projectOpen" , function ( ) {
99+ _nodeConnection . domains . fileWatcher . startWatching ( ProjectManager . getProjectRoot ( ) . fullPath ) ;
100+ } ) ;
101+ } ) ;
102+ } ) ;
103+
104+ exports . initExtension = initExtension ;
105+ } ) ;
0 commit comments