File: fetchworker.cpp

package info (click to toggle)
qt6-declarative 6.9.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 310,088 kB
  • sloc: cpp: 779,045; javascript: 514,338; xml: 10,981; python: 2,806; ansic: 2,253; java: 954; sh: 262; makefile: 41; php: 27
file content (46 lines) | stat: -rw-r--r-- 1,605 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include "fetchworker.h"

#include <QThread>

static constexpr int s_fetchBlockSize{50};
static constexpr int s_maximumListSize{s_fetchBlockSize * 10};
static constexpr int s_sleepIterations{20};
static constexpr std::chrono::milliseconds s_sleepInterval{100};

FetchWorker::FetchWorker(QObject *parent)
    : QObject{parent}
{}

void FetchWorker::fetchDataBlock()
{
    if (m_existingItemsCount < s_maximumListSize) {
        QList<DataBlock> itemsToSend = slowDataConstruction(m_existingItemsCount);
        m_existingItemsCount += itemsToSend.size();
        emit dataFetched(itemsToSend);
    }
    if (m_existingItemsCount >= s_maximumListSize)
        emit noMoreToFetch();
}

QList<FetchWorker::DataBlock> FetchWorker::slowDataConstruction(int fromIndex)
{
    // Block for two seconds to mimic slow data source, while allowing interruption in case of application close
    for (int iterations = s_sleepIterations;
         iterations > 0 && !QThread::currentThread()->isInterruptionRequested();
         --iterations) {
        QThread::sleep(s_sleepInterval);
    }
    QList<DataBlock> returnValues;
    returnValues.reserve(s_fetchBlockSize);
    int number = fromIndex;
    for (int blocks = 0; blocks < s_fetchBlockSize; ++blocks) {
        ++number;
        returnValues.append({QStringLiteral("Contact %1 name").arg(number),
                             QStringLiteral("Contact %1 telephone").arg(number),
                             number});
    }
    return returnValues;
}