From 7ddb0b726f5d8c7dc5f4e3bd821f2fec5f145f2f Mon Sep 17 00:00:00 2001 From: chee Date: Thu, 14 Sep 2017 03:22:51 +0100 Subject: [PATCH] use a single `type` to indicate message type --- api/constants.js | 1 + client/src/constants.js | 1 + client/src/containers/Toad.js | 51 ++++++++++++++++++++++------------- shared/constants.js | 9 +++++++ 4 files changed, 43 insertions(+), 19 deletions(-) create mode 120000 api/constants.js create mode 120000 client/src/constants.js create mode 100644 shared/constants.js diff --git a/api/constants.js b/api/constants.js new file mode 120000 index 0000000..2ce8572 --- /dev/null +++ b/api/constants.js @@ -0,0 +1 @@ +../shared/constants.js \ No newline at end of file diff --git a/client/src/constants.js b/client/src/constants.js new file mode 120000 index 0000000..ae64a04 --- /dev/null +++ b/client/src/constants.js @@ -0,0 +1 @@ +../../shared/constants.js \ No newline at end of file diff --git a/client/src/containers/Toad.js b/client/src/containers/Toad.js index ffb1d10..18db02d 100644 --- a/client/src/containers/Toad.js +++ b/client/src/containers/Toad.js @@ -5,17 +5,18 @@ 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 } @@ -23,7 +24,7 @@ function response (sha, picture) { function reload () { return { - reload: true + type: RELOAD } } @@ -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: } } diff --git a/shared/constants.js b/shared/constants.js new file mode 100644 index 0000000..201a70d --- /dev/null +++ b/shared/constants.js @@ -0,0 +1,9 @@ +const REQUEST = '@action/request' +const RESPONSE = '@action/response' +const RELOAD = '@action/reload' + +module.exports = { + REQUEST, + RESPONSE, + RELOAD +}