fix(bodies): MBP should cache "" so that it updates when new body arrives

This commit is contained in:
Ben Gotow 2016-03-09 18:02:21 -08:00
parent 7a31da9fc9
commit 6d7cfa50f2
3 changed files with 28 additions and 20 deletions

View file

@ -86,7 +86,7 @@ describe "MessageItem", ->
return '/fake/path-inline.png' if f.id is file_inline.id return '/fake/path-inline.png' if f.id is file_inline.id
return '/fake/path-downloading.png' if f.id is file_inline_downloading.id return '/fake/path-downloading.png' if f.id is file_inline_downloading.id
return null return null
spyOn(MessageBodyProcessor, 'addToCache').andCallFake -> spyOn(MessageBodyProcessor, '_addToCache').andCallFake ->
@downloads = @downloads =
'file_1_id': download, 'file_1_id': download,

View file

@ -98,7 +98,7 @@ describe "MessageItem", ->
spyOn(FileDownloadStore, 'downloadDataForFiles').andCallFake (ids) -> spyOn(FileDownloadStore, 'downloadDataForFiles').andCallFake (ids) ->
return {'file_1_id': download, 'file_inline_downloading_id': download_inline} return {'file_1_id': download, 'file_inline_downloading_id': download_inline}
spyOn(MessageBodyProcessor, 'addToCache').andCallFake -> spyOn(MessageBodyProcessor, '_addToCache').andCallFake ->
@message = new Message @message = new Message
id: "111" id: "111"

View file

@ -24,7 +24,7 @@ class MessageBodyProcessor {
this._recentlyProcessedA = []; this._recentlyProcessedA = [];
this._recentlyProcessedD = {}; this._recentlyProcessedD = {};
for (const {message, callback} of this._subscriptions) { for (const {message, callback} of this._subscriptions) {
callback(this.process(message)); callback(this.retrieve(message));
} }
} }
@ -51,7 +51,7 @@ class MessageBodyProcessor {
if (subscriptions.length > 0) { if (subscriptions.length > 0) {
const updatedMessage = changedMessage.clone(); const updatedMessage = changedMessage.clone();
updatedMessage.body = updatedMessage.body || subscriptions[0].message.body; updatedMessage.body = updatedMessage.body || subscriptions[0].message.body;
const output = this.process(updatedMessage); const output = this.retrieve(updatedMessage);
for (const subscription of subscriptions) { for (const subscription of subscriptions) {
subscription.callback(output); subscription.callback(output);
subscription.message = updatedMessage; subscription.message = updatedMessage;
@ -59,18 +59,12 @@ class MessageBodyProcessor {
} }
} }
_key(message) {
// It's safe to key off of the message ID alone because we invalidate the
// cache whenever the message is persisted to the database.
return message.id;
}
version() { version() {
return this._version; return this._version;
} }
processAndSubscribe(message, callback) { subscribe(message, callback) {
_.defer(() => callback(this.process(message))); _.defer(() => callback(this.retrieve(message)));
const sub = {message, callback} const sub = {message, callback}
this._subscriptions.push(sub); this._subscriptions.push(sub);
return () => { return () => {
@ -78,16 +72,30 @@ class MessageBodyProcessor {
} }
} }
process(message) { retrieve(message) {
let body = message.body;
if (!_.isString(message.body)) {
return "";
}
const key = this._key(message); const key = this._key(message);
if (this._recentlyProcessedD[key]) { if (this._recentlyProcessedD[key]) {
return this._recentlyProcessedD[key].body; return this._recentlyProcessedD[key].body;
} }
const body = this._process(message);
this._addToCache(key, body);
return body;
}
// Private Methods
_key(message) {
// It's safe to key off of the message ID alone because we invalidate the
// cache whenever the message is persisted to the database.
return message.id;
}
_process(message) {
let body = message.body;
if (!_.isString(body)) {
return "";
}
// Give each extension the message object to process the body, but don't // Give each extension the message object to process the body, but don't
// allow them to modify anything but the body for the time being. // allow them to modify anything but the body for the time being.
@ -132,11 +140,11 @@ class MessageBodyProcessor {
result = MessageUtils.cidRegex.exec(body); result = MessageUtils.cidRegex.exec(body);
} }
this.addToCache(key, body);
return body; return body;
} }
addToCache(key, body) { _addToCache(key, body) {
if (this._recentlyProcessedA.length > 50) { if (this._recentlyProcessedA.length > 50) {
const removed = this._recentlyProcessedA.pop() const removed = this._recentlyProcessedA.pop()
delete this._recentlyProcessedD[removed.key] delete this._recentlyProcessedD[removed.key]