Skip to content

Commit

Permalink
unread [nfc]: Factor out deleteFromListMap.
Browse files Browse the repository at this point in the history
Should be helpful when converting over the other parts of the
unread state.
  • Loading branch information
gnprice committed Jan 30, 2021
1 parent 7147d16 commit fa405c1
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/unread/unreadModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,28 @@ function deleteFromList<V>(
return list.filterNot(id => toDeleteSet.has(id));
}

const emptyList = Immutable.List();

/**
* Remove the given values, given where to find them; and prune.
*
* That is, for each entry in `toDelete`, we apply `deleteFromList` with the
* given list to the corresponding entry in `state`. When the resulting
* list is empty, we prune that entry entirely.
*/
function deleteFromListMap<K, V>(
state: Immutable.Map<K, Immutable.List<V>>,
toDelete: Immutable.Map<K, Immutable.List<V>>,
): Immutable.Map<K, Immutable.List<V>> {
// prettier-ignore
return state.withMutations(mut => {
toDelete.forEach((msgIds, key) => {
updateAndPrune(mut, emptyList, key, list =>
list && deleteFromList(list, msgIds));
});
});
}

/**
* Delete the given messages from the unreads state.
*
Expand Down Expand Up @@ -176,18 +198,11 @@ function deleteMessages(
});

const emptyMap = Immutable.Map();
const emptyList = Immutable.List();
// prettier-ignore
return state.withMutations(stateMut => {
byConversation.forEach((byTopic, streamId) => {
updateAndPrune(stateMut, emptyMap, streamId, perStream =>
perStream && perStream.withMutations(perStreamMut => {
byTopic.forEach((msgIds, topic) => {
updateAndPrune(perStreamMut, emptyList, topic, perTopic =>
perTopic && deleteFromList(perTopic, msgIds),
);
});
}),
perStream && deleteFromListMap(perStream, byTopic),
);
});
});
Expand Down

0 comments on commit fa405c1

Please sign in to comment.