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

Commit

Permalink
feat: send a persisted state with Automerge.save(), with history
Browse files Browse the repository at this point in the history
  • Loading branch information
severo committed Jan 24, 2020
1 parent ea928f6 commit 2e25cee
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 20 deletions.
7 changes: 2 additions & 5 deletions src/domain/events/toclient/state.event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ describe('Events', () => {
it("should have 'state' event as name", () => {
expect(StateEvent.eventName).to.equal('state')
})
it('should initialize an object', () => {
it('should initialize a string', () => {
// arrange
const state: object = {
points: [{ x: 1, y: 2 }],
imageSrc: 'lake.png',
}
const state: string = `["~#iL",[]]`

// act
const event = new StateEvent(state)
Expand Down
2 changes: 1 addition & 1 deletion src/domain/events/toclient/state.event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { EventToClient } from './event.to.client'

export class StateEvent {
static eventName: string = EventToClient.State
constructor(public readonly state: object) {}
constructor(public readonly state: string) {}
}
15 changes: 8 additions & 7 deletions src/socket.io/socket.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ioClient from 'socket.io-client'
import io from 'socket.io'
import Automerge from 'automerge'
import { default as chai, expect } from 'chai'
import chaiThings from 'chai-things'
chai.should()
Expand Down Expand Up @@ -103,9 +104,9 @@ describe('Server', () => {
newClient.disconnect()
})

it('should send an empty state to a new user meanwhile the state has not been changed', async () => {
it('should send an empty Automerge state to a new user meanwhile the state has not been changed', async () => {
// arrange
const getStateEvent = (): Promise<object> =>
const getStateEvent = (): Promise<string> =>
new Promise(resolve =>
passiveClient.on(StateEvent.eventName, resolve)
)
Expand All @@ -114,7 +115,7 @@ describe('Server', () => {
const state = await getStateEvent()

// assert
expect(state).to.deep.equal({})
expect(Automerge.load(state)).to.deep.equal({})
})
})

Expand Down Expand Up @@ -330,7 +331,7 @@ describe('Server', () => {
.should.include.something.that.equals(`State updated`)
})

it('should log send the same event to the other clients', async () => {
it('should send the same event to the other clients', async () => {
// arrange
await new Promise(resolve => passiveClient.on('connect', resolve))
const getStateChanges: Promise<UpdateStateEventArgs> = new Promise(
Expand All @@ -348,7 +349,7 @@ describe('Server', () => {
expect(receivedChanges).to.deep.equal(changes)
})

it('should log not send the same event to the sender', async () => {
it('should not send the same event to the sender', async () => {
// arrange
const getStateChanges: Promise<UpdateStateEventArgs> = new Promise(
resolve => client.on(UpdateStateEvent.eventName, resolve)
Expand All @@ -371,7 +372,7 @@ describe('Server', () => {
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> => {
const getStateEvent = (): Promise<string> => {
return new Promise(resolve => {
newClient = ioClient.connect(
socketUrl + '/occupapp-beta',
Expand All @@ -386,7 +387,7 @@ describe('Server', () => {
const state = await getStateEvent()

// assert
expect(state).to.deep.equal(newState)
expect(Automerge.load(state)).to.deep.equal(newState)

// after
newClient.disconnect()
Expand Down
10 changes: 3 additions & 7 deletions src/socket.io/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class Socket {

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

Expand All @@ -193,12 +193,8 @@ class Socket {
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 {
// No need to send the metadata, that could be heavy
// This means that the history is not included
return JSON.parse(JSON.stringify(this.state))
private get savedAutomergeState(): string {
return Automerge.save(this.state)
}

private toException = (error: Error): Exception => {
Expand Down

0 comments on commit 2e25cee

Please sign in to comment.