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

Commit

Permalink
fix: send a state update to all clients but the sender
Browse files Browse the repository at this point in the history
The state was not sent to the other clients.
  • Loading branch information
severo committed Jan 23, 2020
1 parent 851647e commit ea928f6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
40 changes: 39 additions & 1 deletion src/socket.io/socket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,45 @@ describe('Server', () => {
.should.include.something.that.equals(`State updated`)
})

it('should send the current state to a new client, after the state had been updated', async () => {
it('should log send the same event to the other clients', async () => {
// arrange
await new Promise(resolve => passiveClient.on('connect', resolve))
const getStateChanges: Promise<UpdateStateEventArgs> = new Promise(
resolve => passiveClient.on(UpdateStateEvent.eventName, resolve)
)

// act
const [receivedChanges] = await Promise.all([
getStateChanges,
updateState(changes),
])

// assert
expect(receivedChanges).to.not.be.undefined
expect(receivedChanges).to.deep.equal(changes)
})

it('should log not send the same event to the sender', async () => {
// arrange
const getStateChanges: Promise<UpdateStateEventArgs> = new Promise(
resolve => client.on(UpdateStateEvent.eventName, resolve)
)
const waitFor100ms = new Promise(resolve =>
setTimeout(() => resolve('Nothing received'), 100)
)

// act
const [receivedChanges] = await Promise.all([
Promise.race([getStateChanges, waitFor100ms]),

updateState(changes),
])

// assert
expect(receivedChanges).to.equal('Nothing received')
})

it('should send the persisted state to a new client, after the state had been updated', async () => {
// arrange
let newClient: SocketIOClient.Socket
const getStateEvent = (): Promise<object> => {
Expand Down
14 changes: 13 additions & 1 deletion src/socket.io/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ class Socket {
// client that will connect
this.state = Automerge.applyChanges(this.state, data)

// TODO: Send to the other users in the namespace
// Send to the other users in the namespace
this.emitUpdateStateToOthers(socket, data)

this.log.info('State updated')
ack({ updated: true })
Expand Down Expand Up @@ -166,6 +167,7 @@ class Socket {
// }

private emitUsersListToAll() {
// TODO: add log?
const usersListEvent = new UsersListEvent(this.exportedUsers)
this.io
.of('/occupapp-beta')
Expand All @@ -177,10 +179,20 @@ class Socket {
}

private emitStateToUser(socket: SocketIO.Socket) {
// TODO: add log?
const stateEvent = new StateEvent(this.stateAsJson)
socket.emit(StateEvent.eventName, stateEvent.state)
}

private emitUpdateStateToOthers(
socket: SocketIO.Socket,
data: UpdateStateEventArgs
) {
// TODO: add log?
const updateStateEvent = new UpdateStateEvent(data)
socket.broadcast.emit(UpdateStateEvent.eventName, updateStateEvent.data)
}

// TODO: maybe the return type should be "any" as for the JOSN.parse function
// https://github.com/MazeChaZer/TypeScript/blob/master/src/lib/es5.d.ts#L966
private get stateAsJson(): object {
Expand Down

0 comments on commit ea928f6

Please sign in to comment.