diff --git a/src/containers/Chat/index.js b/src/containers/Chat/index.js index 0ae9b135..f197b6d0 100644 --- a/src/containers/Chat/index.js +++ b/src/containers/Chat/index.js @@ -20,10 +20,44 @@ import Input from 'components/Input' import './style.scss' const MAX_GET_MEMORY_TIME = 10 * 1000 // in ms +const MAX_APP_PARSE_TIME = 3 * 1000 // in ms const FAILED_TO_GET_MEMORY = 'Could not get memory from webchatMethods.getMemory :' +const FAILED_TO_PARSE = 'Application could not parse the response from backend : ' const WRONG_MEMORY_FORMAT = 'Wrong memory format, expecting : { "memory": , "merge": }' +const getApplicationParse = messages => { + return new Promise(resolve => { + if (!window.webchatMethods || !window.webchatMethods.applicationParse) { + return resolve() + } + // so that we process the message in all cases + setTimeout(resolve, MAX_APP_PARSE_TIME) + try { + const applicationParseResponse = window.webchatMethods.applicationParse(messages) + if (!applicationParseResponse) { + return resolve() + } + if (applicationParseResponse.then && typeof applicationParseResponse.then === 'function') { + // the function returned a Promise + applicationParseResponse + .then(applicationParse => resolve()) + .catch(err => { + console.error(FAILED_TO_PARSE) + console.error(err) + resolve() + }) + } else { + resolve() + } + } catch (err) { + console.error(FAILED_TO_PARSE) + console.error(err) + resolve() + } + }) +} + @connect( state => ({ token: state.conversation.token, @@ -61,6 +95,7 @@ class Chat extends Component { const { messages, show } = nextProps if (messages !== this.state.messages) { + getApplicationParse(messages) this.setState({ messages }, () => { const { getLastMessage } = this.props if (getLastMessage) {