File indexing completed on 2024-05-05 04:41:15

0001 // SPDX-FileCopyrightText: 2021 Carson Black <uhhadd@gmail.com>
0002 //
0003 // SPDX-License-Identifier: MIT
0004 
0005 #include <QDebug>
0006 #include "futurebase.h"
0007 
0008 namespace Croutons
0009 {
0010 
0011 class FutureListener : public QObject
0012 {
0013     Q_OBJECT
0014 
0015     Q_PROPERTY(QJSValue value READ value NOTIFY valueChanged)
0016 
0017 public:
0018     QJSValue _value;
0019     QJSValue value() const {
0020         return _value;
0021     }
0022     Q_SIGNAL void valueChanged();
0023 };
0024 
0025 QJSValue FutureBase::valueOr(const QJSValue& it)
0026 {
0027     auto eng = it.engine();
0028     auto fn = eng->evaluate("(function(foo) { return Qt.binding(() => foo.value ) })");
0029 
0030     if (d->valueOrObject != nullptr) {
0031         return fn.call({eng->newQObject(d->valueOrObject)});
0032     }
0033 
0034     auto lis = new FutureListener;
0035     d->valueOrObject = lis;
0036     if (d->settled && d->succeeded) {
0037         lis->_value = eng->toScriptValue(d->result);
0038         return fn.call({eng->newQObject(d->valueOrObject)});
0039     }
0040 
0041     lis->_value = it;
0042     d->valueOr = [lis, eng](QVariant it) {
0043         lis->_value = eng->toScriptValue(it);
0044         Q_EMIT lis->valueChanged();
0045     };
0046     return fn.call({eng->newQObject(d->valueOrObject)});
0047 }
0048 
0049 }; // namespace Croutons
0050 
0051 #include "futurebase_p.moc"