Avoid intermediate objects while parsing TextTiles' bodies.

This commit is contained in:
Danila Fedorin 2021-07-12 12:47:22 -07:00
parent 9304ca330e
commit c261b9fb23
2 changed files with 20 additions and 12 deletions

View file

@ -28,22 +28,27 @@ export class BaseTextTile extends BaseMessageTile {
return "message"; return "message";
} }
_parseBody(body) { _parseBody(body, _) {
return stringAsBody(body.string); return stringAsBody(body.string);
} }
_getBodyFormat() {
return "plain";
}
get body() { get body() {
const body = this._getBody(); const body = this._getBody();
const format = this._getBodyFormat();
// body.string is a string, so we can check for difference by just // body.string is a string, so we can check for difference by just
// doing an equality check // doing an equality check
// Even if the body hasn't changed, but the format has, we need // Even if the body hasn't changed, but the format has, we need
// to re-fill our cache. // to re-fill our cache.
if (!this._messageBody || this._messageBody.sourceString !== body.string || this._messageFormat !== body.format) { if (!this._messageBody || this._messageBody.sourceString !== body || this._messageFormat !== format) {
// body with markup is an array of parts, // body with markup is an array of parts,
// so we should not recreate it for the same body string, // so we should not recreate it for the same body string,
// or else the equality check in the binding will always fail. // or else the equality check in the binding will always fail.
// So cache it here. // So cache it here.
this._messageBody = this._parseBody(body); this._messageBody = this._parseBody(body, format);
this._messageFormat = body.format; this._messageFormat = body.format;
} }
return this._messageBody; return this._messageBody;

View file

@ -19,7 +19,7 @@ import {parsePlainBody} from "../MessageBody.js";
import {parseHTMLBody} from "../deserialize.js"; import {parseHTMLBody} from "../deserialize.js";
export class TextTile extends BaseTextTile { export class TextTile extends BaseTextTile {
_getContentString(key, format, fallback = null) { _getContentString(key, fallback = null) {
const content = this._getContent(); const content = this._getContent();
let val = content?.[key] || fallback; let val = content?.[key] || fallback;
if (!val && val !== "") { // empty string is falsy, but OK here. if (!val && val !== "") { // empty string is falsy, but OK here.
@ -28,27 +28,30 @@ export class TextTile extends BaseTextTile {
if (content.msgtype === "m.emote") { if (content.msgtype === "m.emote") {
val = `* ${this.displayName} ${body}`; val = `* ${this.displayName} ${body}`;
} }
return { string: val, format }; return val;
} }
_getPlainBody() { _getPlainBody() {
return this._getContentString("body", "plain", ""); return this._getContentString("body", "");
} }
_getFormattedBody() { _getFormattedBody() {
return this._getContentString("formatted_body", "html"); return this._getContentString("formatted_body");
} }
_getBody() { _getBody() {
return this._getFormattedBody() || this._getPlainBody(); return this._getFormattedBody() || this._getPlainBody();
} }
_parseBody(body) { _getBodyFormat() {
const string = body.string; return this._getContent()?.["formatted_body"] ? "html" : "plain"
if (body.format === "html") { }
return parseHTMLBody({ mediaRepository: this._mediaRepository, platform: this.platform }, string);
_parseBody(body, format) {
if (format === "html") {
return parseHTMLBody({ mediaRepository: this._mediaRepository, platform: this.platform }, body);
} else { } else {
return parsePlainBody(string); return parsePlainBody(body);
} }
} }
} }