Skip to content

Commit

Permalink
fix: Add protection to ensure that message listeners from one convers…
Browse files Browse the repository at this point in the history
…ation don't affect a different one.
  • Loading branch information
Standard8 committed Mar 27, 2024
1 parent fbafab3 commit 665abb1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
15 changes: 11 additions & 4 deletions addon/content/reducer/reducerConversation.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ let messageEnricher = () => {
return (_messageEnricher = new MessageEnricher());
};

function removeListeners() {
async function removeListeners() {
if (currentQueryListener) {
browser.convGloda.queryConversationMessages.removeListener(
await browser.convGloda.queryConversationMessages.removeListener(
currentQueryListener,
currentQueryListenerArgs
);
Expand Down Expand Up @@ -77,7 +77,7 @@ export const conversationActions = {
return async (dispatch, getState) => {
let loadingStartedTime = Date.now();

removeListeners();
await removeListeners();

let currentId = getState().conversation.currentId + 1;
await dispatch(conversationActions.setConversationId({ currentId }));
Expand All @@ -98,6 +98,12 @@ export const conversationActions = {
);

currentQueryListener = (event) => {
if (event.conversationId != currentId) {
console.warn(
"Conversation Query Listener called after being removed"
);
return;
}
if (event.initial) {
dispatch(
conversationActions.displayConversationMsgs({
Expand Down Expand Up @@ -130,7 +136,8 @@ export const conversationActions = {

browser.convGloda.queryConversationMessages.addListener(
currentQueryListener,
currentQueryListenerArgs
currentQueryListenerArgs,
currentId
);
};
},
Expand Down
34 changes: 24 additions & 10 deletions addon/experiment-api/glodaApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ var convGloda = class extends ExtensionCommon.ExtensionAPI {
queryConversationMessages: new ExtensionCommon.EventManager({
context,
name: "convContacts.queryConversationMessages",
register(fire, msgIds) {
let status = { stopQuery: false };
register(fire, msgIds, conversationId) {
let status = { stopQuery: false, conversationId };

let msgHdrs = msgIds.map((id) =>
context.extension.messageManager.get(id)
Expand Down Expand Up @@ -109,10 +109,11 @@ function getGlodaMessages(msgHdrs) {
*
*/
class GlodaListener {
constructor(msgHdrs, intermediateResults, fire, context) {
constructor({ msgHdrs, intermediateResults, status, fire, context }) {
this.initialQueryComplete = false;
this.msgHdrs = msgHdrs;
this.intermediateResults = intermediateResults;
this.status = status;
this.fire = fire;
this.context = context;
}
Expand All @@ -132,7 +133,10 @@ class GlodaListener {
}
}
if (messages.length) {
this.fire.async({ added: messages });
this.fire.async({
added: messages,
conversationId: this.status.conversationId,
});
}
}
onItemsModified(items) {
Expand All @@ -148,7 +152,10 @@ class GlodaListener {
}
}
if (messages.length) {
this.fire.async({ modified: messages });
this.fire.async({
modified: messages,
conversationId: this.status.conversationId,
});
}
}
onItemsRemoved(items) {
Expand All @@ -168,7 +175,10 @@ class GlodaListener {
msgIds.push(message.id);
}
}
this.fire.async({ removed: msgIds });
this.fire.async({
removed: msgIds,
conversationId: this.status.conversationId,
});
}
onQueryCompleted(collection) {
if (this.initialQueryComplete) {
Expand Down Expand Up @@ -216,7 +226,10 @@ class GlodaListener {
// since we would still need to load the headers to inject into the gloda
// query and for the basic details.

this.fire.async({ initial: messages });
this.fire.async({
initial: messages,
conversationId: this.status.conversationId,
});
}

/**
Expand Down Expand Up @@ -301,12 +314,13 @@ function getAndObserveConversationThread(
fire,
context
) {
let glodaListener = new GlodaListener(
let glodaListener = new GlodaListener({
msgHdrs,
intermediateResults,
status,
fire,
context
);
context,
});

let query = intermediateResults[0].conversation.getMessagesCollection(
glodaListener,
Expand Down
5 changes: 5 additions & 0 deletions addon/experiment-api/glodaSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
"items": {
"type": "number"
}
},
{
"name": "conversationId",
"type": "number",
"description": "The conversation id for the query."
}
]
}
Expand Down

0 comments on commit 665abb1

Please sign in to comment.