Skip to content

Commit

Permalink
use a single type to indicate message type
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed Sep 14, 2017
1 parent ec228dd commit 7ddb0b7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
1 change: 1 addition & 0 deletions api/constants.js
1 change: 1 addition & 0 deletions client/src/constants.js
51 changes: 32 additions & 19 deletions client/src/containers/Toad.js
Expand Up @@ -5,25 +5,26 @@ const {getList, addPicture, getPeers} = require('../api')
const {loadPicture, checkPicture, storePicture} = require('../store')
const Stream = require('../components/Stream')
const Camera = require('../components/Camera')
const {RESPONSE, REQUEST, RELOAD} = require('../constants')

function request (sha) {
return {
request: true,
type: REQUEST,
sha
}
}

function response (sha, picture) {
return {
response: true,
type: RESPONSE,
sha,
picture
}
}

function reload () {
return {
reload: true
type: RELOAD
}
}

Expand Down Expand Up @@ -75,23 +76,35 @@ class Toad extends Component {
}

handleData (connection, data) {
if (data.request) {
loadPicture(data.sha).then(picture => {
picture && connection.send(response(data.sha, picture))
})
} else if (data.response) {
const pictureValid = checkPicture(data.picture, data.sha)
if (!pictureValid) {
return console.error('invalid picture received')
console.log(data, data.type)
switch (data.type) {
case REQUEST: {
return loadPicture(data.sha)
.then(picture =>
picture && connection.send(response(data.sha, picture))
)
}
const index = this.state.list.indexOf(data.sha)
this.setState(state => {
state.pictures[index] = data.picture
storePicture(data.picture)
})
} else if (data.reload) {
getPeers().then(peers =>
this.setState({peers}))
case RESPONSE: {
const pictureValid = checkPicture(data.picture, data.sha)

if (!pictureValid) {
return console.error('invalid picture received')
}

const index = this.state.list.indexOf(data.sha)

return this.setState(state => {
state.pictures[index] = data.picture
storePicture(data.picture)
})
}
case RELOAD: {
return getPeers()
.then(peers =>
this.setState({peers})
)
}
default:
}
}

Expand Down
9 changes: 9 additions & 0 deletions shared/constants.js
@@ -0,0 +1,9 @@
const REQUEST = '@action/request'
const RESPONSE = '@action/response'
const RELOAD = '@action/reload'

module.exports = {
REQUEST,
RESPONSE,
RELOAD
}

0 comments on commit 7ddb0b7

Please sign in to comment.