upsert method
The action for the upsert.
Implementation
@action
void upsert(ModelBaseMessage msg) {
if (!_map.containsKey(msg.id)) {
// For new messages, add to order list
_order.add(msg.id);
}
_map[msg.id] = msg;
_messageStatusMap[msg.id] = msg.status;
// Always resort to maintain correct order
// This handles both new insertions and updates
_order.sort((a, b) {
final seqA = _map[a]?.sequence ?? 0;
final seqB = _map[b]?.sequence ?? 0;
return seqB.compareTo(seqA); // Descending order (newer messages first)
});
}