Repository: nomic-ai/gpt4all
File: gpt4all-chat/src/chatmodel.h (C/C++)

Code before:
        QHash<int, QByteArray> roles;
        roles[IdRole] = "id";
        roles[NameRole] = "name";
        roles[ValueRole] = "value";
        roles[NewResponseRole] = "newResponse";
        roles[CurrentResponseRole] = "currentResponse";
        roles[StoppedRole] = "stopped";
        roles[ThumbsUpStateRole] = "thumbsUpState";
        roles[ThumbsDownStateRole] = "thumbsDownState";
        roles[SourcesRole] = "sources";
        roles[ConsolidatedSourcesRole] = "consolidatedSources";
        return roles;
    }

    void appendPrompt(const QString &name, const QString &value)
    {
        ChatItem item;
        item.name = name;
        item.value = value;
        m_mutex.lock();
        const int count = m_chatItems.count();
        m_mutex.unlock();
        beginInsertRows(QModelIndex(), count, count);
        m_mutex.lock();
        m_chatItems.append(item);
        m_mutex.unlock();
        endInsertRows();
        emit countChanged();
    }

    void appendResponse(const QString &name)
    {
        m_mutex.lock();
        const int count = m_chatItems.count();
        m_mutex.unlock();
        ChatItem item;
        item.id = count; // This is only relevant for responses
        item.name = name;
        item.currentResponse = true;
        beginInsertRows(QModelIndex(), count, count);
        m_mutex.lock();
        m_chatItems.append(item);
        m_mutex.unlock();
        endInsertRows();
        emit countChanged();
    }

    Q_INVOKABLE void clear()
    {
        {
            QMutexLocker locker(&m_mutex);

Review comment (suggestion):
QList::append is not noexcept (and can't be due to alloc). Should use a scoped lock like above:
```suggestion
        {
            QMutexLocker locker(&m_mutex);
            m_chatItems.append(item);
        }
```

Code after:
    void appendPrompt(const QString &name, const QString &value)
    {
        ChatItem item;
        item.name = name;
        item.value = value;
        m_mutex.lock();
        const int count = m_chatItems.count();
        m_mutex.unlock();
        beginInsertRows(QModelIndex(), count, count);
        {
            QMutexLocker locker(&m_mutex);
            m_chatItems.append(item);
        }
        endInsertRows();
        emit countChanged();
    }

    void appendResponse(const QString &name)
    {
        m_mutex.lock();
        const int count = m_chatItems.count();
        m_mutex.unlock();
        ChatItem item;
        item.id = count; // This is only relevant for responses
        item.name = name;
        item.currentResponse = true;
        beginInsertRows(QModelIndex(), count, count);
        {
            QMutexLocker locker(&m_mutex);
            m_chatItems.append(item);
        }
        endInsertRows();
        emit countChanged();
    }

    Q_INVOKABLE void clear()
    {
        {
            QMutexLocker locker(&m_mutex);
            if (m_chatItems.isEmpty()) return;
        }

        beginResetModel();
        {
            QMutexLocker locker(&m_mutex);
            m_chatItems.clear();
        }
        endResetModel();
        emit countChanged();
    }