Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

Commit

Permalink
Clean up terminal connectTo
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 committed Mar 20, 2017
1 parent 3a1b79b commit 09e7806
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 24 deletions.
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -5,10 +5,10 @@
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"dependencies": {
"@phosphor/algorithm": "^0.1.0",
"@phosphor/algorithm": "^0.1.1",
"@phosphor/coreutils": "^0.1.5",
"@phosphor/disposable": "^0.1.0",
"@phosphor/signaling": "^0.1.1",
"@phosphor/disposable": "^0.1.1",
"@phosphor/signaling": "^0.1.2",
"@types/minimist": "^1.2.0",
"@types/text-encoding": "0.0.30",
"minimist": "^1.2.0",
Expand Down
23 changes: 14 additions & 9 deletions src/terminal/default.ts
Expand Up @@ -2,7 +2,7 @@
// Distributed under the terms of the Modified BSD License.

import {
each, map, toArray
ArrayExt, each, map, toArray
} from '@phosphor/algorithm';

import {
Expand Down Expand Up @@ -291,13 +291,10 @@ namespace DefaultTerminalSession {
* If the session was already started via `startNew`, the existing
* session object is used as the fulfillment value.
*
* Otherwise, if `options` are given, we attempt to connect to the existing
* session.
* The promise is fulfilled when the session is ready on the server,
* otherwise the promise is rejected.
* Otherwise, if `options` are given, we resolve the promise after
* confirming that the session exists on the server.
*
* If the session was not already started and no `options` are given,
* the promise is rejected.
* If the session does not exist on the server, the promise is rejected.
*/
export
function connectTo(name: string, options: TerminalSession.IOptions = {}): Promise<TerminalSession.ISession> {
Expand All @@ -309,8 +306,16 @@ namespace DefaultTerminalSession {
if (url in Private.running) {
return Promise.resolve(Private.running[url]);
}
let session = new DefaultTerminalSession(name, options);
return Promise.resolve(session);
return listRunning(options).then(models => {
let index = ArrayExt.findFirstIndex(models, model => {
return model.name === name;
});
if (index !== -1) {
let session = new DefaultTerminalSession(name, options);
return Promise.resolve(session);
}
return Promise.reject<TerminalSession.ISession>('Could not find session');
});
}

/**
Expand Down
9 changes: 3 additions & 6 deletions src/terminal/terminal.ts
Expand Up @@ -130,13 +130,10 @@ namespace TerminalSession {
* If the session was already started via `startNew`, the existing
* session object is used as the fulfillment value.
*
* Otherwise, if `options` are given, we attempt to connect to the existing
* session.
* The promise is fulfilled when the session is ready on the server,
* otherwise the promise is rejected.
* Otherwise, if `options` are given, we resolve the promise after
* confirming that the session exists on the server.
*
* If the session was not already started and no `options` are given,
* the promise is rejected.
* If the session does not exist on the server, the promise is rejected.
*/
export
function connectTo(name: string, options?: IOptions): Promise<ISession> {
Expand Down
31 changes: 25 additions & 6 deletions test/src/terminal/terminal.spec.ts
Expand Up @@ -64,14 +64,33 @@ describe('terminals', () => {

describe('.connectTo', () => {

it('should give back an existing session', (done) => {
TerminalSession.startNew().then(s => {
it('should give back an existing session', () => {
return TerminalSession.startNew().then(s => {
session = s;
return TerminalSession.connectTo(s.name).then(newSession => {
return TerminalSession.connectTo(s.name);
}).then(newSession => {
expect(newSession).to.be(session);
done();
});
}).catch(done);
});
});

it('should give back a session that exists on the server', () => {
tester.onRequest = () => {
tester.respond(200, [{ name: 'foo' }]);
};
return TerminalSession.connectTo('foo').then(s => {
expect(s.name).to.be('foo');
s.dispose();
});
});

it('should reject if the session does not exist on the server', () => {
tester.onRequest = () => {
tester.respond(200, [{ name: 'foo' }]);
};
return TerminalSession.connectTo('bar').then(
() => { throw Error('should not get here'); },
() => undefined
);
});

});
Expand Down

0 comments on commit 09e7806

Please sign in to comment.