@@ -27,10 +27,13 @@ export class Tasks {
2727 */
2828 async getAll ( ) : Promise < Task [ ] > {
2929 if ( ! this . cachedTasks ) {
30- const tasks = await this . agentClient . tasks . getTasks ( ) ;
30+ const [ tasks , ports ] = await Promise . all ( [
31+ this . agentClient . tasks . getTasks ( ) ,
32+ this . agentClient . ports . getPorts ( ) ,
33+ ] ) ;
3134
3235 this . cachedTasks = Object . values ( tasks . tasks ) . map (
33- ( task ) => new Task ( this . agentClient , task )
36+ ( task ) => new Task ( this . agentClient , task , ports )
3437 ) ;
3538 }
3639
@@ -78,59 +81,82 @@ export class Task {
7881 return Boolean ( this . data . runAtStart ) ;
7982 }
8083 get ports ( ) {
84+ const configuredPort = this . data . preview ?. port ;
85+
86+ // If we have configured a specific port, we only return that port. This is used where
87+ // the environment is not able to assign port automatically (e.g. Next JS)
88+ if ( configuredPort ) {
89+ return this . _ports . filter ( ( port ) => port . port === configuredPort ) ;
90+ }
91+
92+ // Otherwise, we return the ports assigned to the task
8193 return this . data . ports ;
8294 }
8395 get status ( ) {
8496 return this . shell ?. status || "IDLE" ;
8597 }
8698 constructor (
8799 private agentClient : IAgentClient ,
88- private data : protocol . task . TaskDTO
100+ private data : protocol . task . TaskDTO ,
101+ private _ports : protocol . port . Port [ ]
89102 ) {
90- agentClient . tasks . onTaskUpdate ( async ( task ) => {
91- if ( task . id !== this . id ) {
92- return ;
93- }
94-
95- const lastStatus = this . status ;
96- const lastShellId = this . shell ?. shellId ;
97-
98- this . data = task ;
99-
100- if ( lastStatus !== this . status ) {
101- this . onStatusChangeEmitter . fire ( this . status ) ;
102- }
103-
104- if (
105- this . openedShell &&
106- task . shell &&
107- task . shell . shellId !== lastShellId
108- ) {
109- const openedShell = await this . agentClient . shells . open (
110- task . shell . shellId ,
111- this . openedShell . dimensions
112- ) ;
113-
114- this . openedShell = {
115- shellId : openedShell . shellId ,
116- output : openedShell . buffer ,
117- dimensions : this . openedShell . dimensions ,
118- } ;
119-
120- this . onOutputEmitter . fire ( "\x1B[2J\x1B[3J\x1B[1;1H" ) ;
121- openedShell . buffer . forEach ( ( out ) => this . onOutputEmitter . fire ( out ) ) ;
122- }
123- } ) ;
124-
125- this . agentClient . shells . onShellOut ( ( { shellId, out } ) => {
126- if ( ! this . shell || this . shell . shellId !== shellId || ! this . openedShell ) {
127- return ;
128- }
103+ this . disposable . addDisposable (
104+ agentClient . ports . onPortsUpdated ( ( ports ) => {
105+ this . _ports = ports ;
106+ } )
107+ ) ;
108+ this . disposable . addDisposable (
109+ agentClient . tasks . onTaskUpdate ( async ( task ) => {
110+ if ( task . id !== this . id ) {
111+ return ;
112+ }
113+
114+ const lastStatus = this . status ;
115+ const lastShellId = this . shell ?. shellId ;
116+
117+ this . data = task ;
118+
119+ if ( lastStatus !== this . status ) {
120+ this . onStatusChangeEmitter . fire ( this . status ) ;
121+ }
122+
123+ if (
124+ this . openedShell &&
125+ task . shell &&
126+ task . shell . shellId !== lastShellId
127+ ) {
128+ const openedShell = await this . agentClient . shells . open (
129+ task . shell . shellId ,
130+ this . openedShell . dimensions
131+ ) ;
132+
133+ this . openedShell = {
134+ shellId : openedShell . shellId ,
135+ output : openedShell . buffer ,
136+ dimensions : this . openedShell . dimensions ,
137+ } ;
138+
139+ this . onOutputEmitter . fire ( "\x1B[2J\x1B[3J\x1B[1;1H" ) ;
140+ openedShell . buffer . forEach ( ( out ) => this . onOutputEmitter . fire ( out ) ) ;
141+ }
142+ } )
143+ ) ;
129144
130- // Update output for shell
131- this . openedShell . output . push ( out ) ;
132- this . onOutputEmitter . fire ( out ) ;
133- } ) ;
145+ this . disposable . addDisposable (
146+ this . agentClient . shells . onShellOut ( ( { shellId, out } ) => {
147+ if (
148+ ! this . shell ||
149+ this . shell . shellId !== shellId ||
150+ ! this . openedShell
151+ ) {
152+ return ;
153+ }
154+
155+ // Update output for shell
156+ this . openedShell . output . push ( out ) ;
157+ this . onOutputEmitter . fire ( out ) ;
158+ } )
159+ ) ;
134160 }
135161 async open ( dimensions = DEFAULT_SHELL_SIZE ) {
136162 if ( ! this . shell ) {
@@ -169,6 +195,7 @@ export class Task {
169195 resolve ( task . ports [ 0 ] ) ;
170196 }
171197 } ) ;
198+ this . disposable . addDisposable ( disposer ) ;
172199 } ) ,
173200 new Promise < protocol . port . Port > ( ( resolve , reject ) => {
174201 setTimeout ( ( ) => {
@@ -191,4 +218,7 @@ export class Task {
191218 await this . agentClient . tasks . stopTask ( this . id ) ;
192219 }
193220 }
221+ dispose ( ) {
222+ this . disposable . dispose ( ) ;
223+ }
194224}
0 commit comments