Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
feat: added a waitFor parameter to SocketClient#connect() to resolve …
Browse files Browse the repository at this point in the history
…connection issue
  • Loading branch information
tannerntannern committed Nov 14, 2018
1 parent 095f85a commit f666770
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 38 deletions.
29 changes: 12 additions & 17 deletions src/client/socket-client.ts
Expand Up @@ -81,33 +81,28 @@ export abstract class SocketClient<API extends SocketInterface> {
}

/**
* Attempts to connect to a SocketServer. Returns a Promise for when the process completes or fails.
* Attempts to connect to a SocketServer.
*/
public connect(url: string = '', options?: SocketIOClient.ConnectOpts): Promise<any> {
public connect<WaitFor extends string | null>(url: string = '', waitFor: WaitFor = null, options?: SocketIOClient.ConnectOpts): WaitFor extends string ? Promise<any> : void {
if (!options) options = {};
Object.assign(options, {
autoConnect: false
});

return new Promise<any>((resolve, reject) => {
this.socket = socketio(url, options);
this.attachSocketHandlers();
this.socket.io.open((err?) => {
if (err) reject(err);
else resolve();
});
});
this.socket = socketio(url, options);
this.attachSocketHandlers();
this.socket.open();

if (typeof waitFor === 'string')
return <any>this.blockEvent(waitFor);
}

/**
* Disconnects from the SocketServer, if there was a connection.
*/
public disconnect(): Promise<any> {
return new Promise<any>((resolve, reject) => {
if (this.socket) this.socket.close();
this.socket = null;
resolve();
});
public disconnect() {
if (this.socket) this.socket.close();
this.socket = null;
}

/**
Expand All @@ -120,7 +115,7 @@ export abstract class SocketClient<API extends SocketInterface> {
/**
* Gives the ability to block and wait for an event. Usage: `await client.blockEvent('some-event');`
*/
public blockEvent<Event extends keyof API['client']>(event: Event): Promise<any> {
public blockEvent<Event extends string>(event: Event): Promise<any> {
return new Promise((resolve, reject) => {
if (!this.waiters[<string>event])
this.waiters[<string>event] = [];
Expand Down
41 changes: 21 additions & 20 deletions test/integration/Socket.test.ts
Expand Up @@ -17,43 +17,44 @@ describe('SocketServer + SocketClient', function(){

describe('Connecting/disconnecting a client to/from the server', function(){
it('should throw an error if the client connects to a bad address', function(){
expect(async () => { await c.connect('http://localhost:3001') }).to.throw;
expect(async () => { await c.connect('http://localhost:3001', 'connected') }).to.throw;
});

it('should not throw an error if the client connects to the correct address', function(){
expect(async () => { await c.connect('http://localhost:3000') }).to.not.throw;
expect(async () => { await c.connect('http://localhost:3000', 'connected') }).to.not.throw;
});

it('should give proper results for isConnected()', async function(){
expect(c.isConnected()).to.be.false;

await c.connect('http://localhost:3000/');
// expect(c.isConnected()).to.be.true;
await c.connect('http://localhost:3000', 'connected');
expect(c.isConnected()).to.be.true;

await c.disconnect();
c.disconnect('disconnected');
expect(c.isConnected()).to.be.false;
});

it('should properly handle disconnects', async function(){
await c.connect('http://localhost:3000');
await c.connect('http://localhost:3000', 'connected');
expect(c.socket).to.not.equal(null);

await c.disconnect();
c.disconnect();
expect(c.socket).to.equal(null);
});
});

// describe('Transmitting and receiving data', function(){
// it('should be able to accept changes from the server', async function(){
// await c.connect('http://localhost:3000');
//
// console.log('sending event...');
// s.emit(s.io.nsps['/'], 'reset-data', {newKey: 'new value'});
// console.log('awaiting event...');
// await c.blockEvent('reset-data');
// console.log('received event...');
//
// await c.disconnect();
// });
// });
describe('Transmitting and receiving data', function(){
it('should be able to accept changes from the server', async function(){
await c.connect('http://localhost:3000', 'connected');

s.emit(s.io.nsps['/'], 'reset-data', {newKey: 'new value'});
await c.blockEvent('reset-data');

expect(c.data).to.deep.equal({newKey: 'new value'});

await c.disconnect();
});

// TODO: ...
});
});
2 changes: 1 addition & 1 deletion test/shared/socket.ts
Expand Up @@ -47,7 +47,7 @@ export class Client extends SocketClient<API> {

protected socketHandlers: SocketHandlers<API, "client", ClientCtx<API>> = {
'connected': (id: string) => {
console.log(id, 'has connected!');
// console.log(id, 'has connected!');
},
'patch-data': (key: string, value: string) => {
this.data[key] = value;
Expand Down

0 comments on commit f666770

Please sign in to comment.