Skip to content

Commit

Permalink
Parse and render messages synchronously. (zulip#248)
Browse files Browse the repository at this point in the history
This simplifies (and maybe speeds up) `MessageContainer`.
  • Loading branch information
neerajwahi authored Jan 27, 2017
1 parent 2991bc4 commit 2b568a7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 28 deletions.
20 changes: 1 addition & 19 deletions src/message/MessageContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,13 @@ import MessageFull from './MessageFull';
import MessageBrief from './MessageBrief';

export default class MessageContainer extends React.PureComponent {

state = {
message: null,
};

constructor(props) {
super(props);
this.renderMessage();
}

async renderMessage() {
const { message, auth } = this.props;
this.setState({
message: await renderHtml(message, auth),
});
}

render() {
const { avatarUrl, timestamp, twentyFourHourTime, fromName, fromEmail, isBrief } = this.props;
const { message } = this.state;
const MessageComponent = isBrief ? MessageBrief : MessageFull;

return (
<MessageComponent
message={message}
message={renderHtml(this.props.message, this.props.auth)}
avatarUrl={avatarUrl}
fromName={fromName}
fromEmail={fromEmail}
Expand Down
18 changes: 9 additions & 9 deletions src/message/renderHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ const parseDom = (dom, auth, baseStyle, onPress) => {
});
};

export const renderHtml = (html: string, auth) =>
new Promise((resolve, reject) => {
const handler = new htmlparser.DomHandler((err, dom) => {
if (err) reject(err);
resolve(parseDom(dom, auth, [styles.base]));
});
const parser = new htmlparser.Parser(handler);
parser.write(html.replace(/\n|\r/g, ''));
parser.done();
export const renderHtml = (html: string, auth) => {
let result = null;
const handler = new htmlparser.DomHandler((err, dom) => {
if (!err) result = parseDom(dom, auth, [styles.base]);
});
const parser = new htmlparser.Parser(handler);
parser.write(html.replace(/\n|\r/g, ''));
parser.done();
return result;
};

0 comments on commit 2b568a7

Please sign in to comment.