Skip to content
This repository was archived by the owner on Jul 31, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/dynamicControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ client.open({
* then call ready so our controls show up.
* then call loop() to begin our loop.
*/
return client.synchronizeScenes();
return client.synchronizeState();
})
.then(() => client.ready(true))
.then(() => loop());
Expand Down
11 changes: 4 additions & 7 deletions examples/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function createScenes(): Promise<ISceneDataArray> {
sceneID: 'secondScene',
controls: makeControls('second')
};

return client.createScenes({
scenes: [secondScene]
});
Expand All @@ -138,7 +138,7 @@ function createGroups(): Promise<void> {
sceneID: 'default'
}
);

return client
// First update the default group
.updateGroups({
Expand All @@ -165,12 +165,9 @@ client
authToken: process.argv[2],
versionId: parseInt(process.argv[3], 10),
})

// Pull the scenes from the interactive server
.then(() => client.synchronizeScenes())

// Pull the groups from the interactive server
.then(() => client.synchronizeGroups())
// Pull the scenes and groups from the interactive server
.then(() => client.synchronizeState())

// Set the client as ready so that interactive controls show up
.then(() => client.ready(true))
Expand Down
56 changes: 53 additions & 3 deletions src/Client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { expect } from 'chai';
import * as sinon from 'sinon';
import * as WebSocket from 'ws';
import { setWebSocket } from './';
import { Method } from './wire/packets';

import { setWebSocket } from './';
import { Client, ClientType } from './Client';
import { IGroupData, ISceneData } from './state/interfaces';
import { Method } from './wire/packets';

setWebSocket(WebSocket);
const port = process.env.SERVER_PORT || 1339;
Expand Down Expand Up @@ -49,6 +52,7 @@ describe('client', () => {
done();
});
});
after(done => tearDown(done));
});

describe('method handling', () => {
Expand All @@ -61,5 +65,51 @@ describe('client', () => {
});
});

afterEach(done => tearDown(done));
describe('state synchronization', () => {
let executeStub: sinon.SinonStub;
const scenes: ISceneData[] = [{sceneID: 'default', controls: []}];
const groups: IGroupData[] = [{groupID: 'default'}];

beforeEach(() => {
client = createClient();
executeStub = sinon.stub(client, 'execute');
});
afterEach(() => {
executeStub.restore();
});

it('synchronizes scenes', () => {
executeStub.onCall(0).resolves(scenes);
const syncScenesStub = sinon.stub(client.state, 'synchronizeScenes');
return client.synchronizeScenes().then(() => {
expect(syncScenesStub).to.have.been.calledWith(scenes);

syncScenesStub.restore();
});
});

it('synchronizes groups', () => {
executeStub.onCall(0).resolves(groups);
const syncGroupsStub = sinon.stub(client.state, 'synchronizeGroups');
return client.synchronizeGroups().then(() => {
expect(syncGroupsStub).to.have.been.calledWith(groups);

syncGroupsStub.restore();
});
});

it('synchronizes state', () => {
executeStub.withArgs('getGroups', null, false).resolves(groups);
executeStub.withArgs('getScenes', null, false).resolves(scenes);
const syncGroupsStub = sinon.stub(client.state, 'synchronizeGroups');
const syncScenesStub = sinon.stub(client.state, 'synchronizeScenes');
return client.synchronizeState().then(() => {
expect(syncScenesStub).to.have.been.calledWith(scenes);
expect(syncGroupsStub).to.have.been.calledWith(groups);

syncGroupsStub.restore();
syncScenesStub.restore();
});
});
});
});
12 changes: 11 additions & 1 deletion src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class Client extends EventEmitter implements IClient {
this.createSocket(options);
this.socket.connect();
return resolveOn(this, 'open')
.then(() => this);
.then(() => this);
}

/**
Expand Down Expand Up @@ -196,6 +196,16 @@ export class Client extends EventEmitter implements IClient {
.then(res => this.state.synchronizeGroups(res));
}

/**
* Retrieves and hydrates client side stores with state from the server
*/
public synchronizeState(): Promise<[IGroup[], IScene[]]> {
return Promise.all([
this.synchronizeGroups(),
this.synchronizeScenes(),
]);
}

/**
* Gets the time from the server as a unix timestamp in UTC.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/EndpointDiscovery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ describe('endpoint discovery', () => {
});
it('resolves with a list of endpoints', () => {
stub.resolves(servers);
expect(discovery.retrieveEndpoints()).to.eventually.equal(servers);
return expect(discovery.retrieveEndpoints()).to.eventually.equal(servers);
});
it('rejects with a NoInteractiveServersAvailable if the response contains no servers', () => {
stub.resolves([]);
expect(discovery.retrieveEndpoints()).to.be.rejectedWith(NoInteractiveServersAvailable);
return expect(discovery.retrieveEndpoints()).to.be.rejectedWith(NoInteractiveServersAvailable);
});
});
2 changes: 1 addition & 1 deletion src/EndpointDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class EndpointDiscovery {
if (res.length > 0) {
return res;
}
return new NoInteractiveServersAvailable('No Interactive servers are available, please try again.');
throw new NoInteractiveServersAvailable('No Interactive servers are available, please try again.');
});
}
}
2 changes: 1 addition & 1 deletion src/state/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class State extends EventEmitter implements IState {
}

/**
* Syncronize scenes takes a collection of scenes from the server
* Synchronize scenes takes a collection of scenes from the server
* and hydrates the Scene store with them.
*/
public synchronizeScenes(data: ISceneDataArray): IScene[] {
Expand Down