-
Notifications
You must be signed in to change notification settings - Fork 75
/
qfappscriptrunnable.cpp
154 lines (125 loc) · 3.77 KB
/
qfappscriptrunnable.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <QtCore>
#include <QUuid>
#include <QFAppDispatcher>
#include "priv/qfappscriptrunnable.h"
#include "priv/qfappscriptdispatcherwrapper.h"
QFAppScriptRunnable::QFAppScriptRunnable(QObject *parent) : QObject(parent)
{
m_engine = 0;
m_next = 0;
m_isSignalCondition = false;
m_isOnceOnly = true;
}
QFAppScriptRunnable::~QFAppScriptRunnable()
{
release();
}
QJSValue QFAppScriptRunnable::script() const
{
return m_script;
}
void QFAppScriptRunnable::setScript(const QJSValue &script)
{
m_script = script;
}
QString QFAppScriptRunnable::type() const
{
return m_type;
}
void QFAppScriptRunnable::setType(const QString &type)
{
m_type = type;
}
bool QFAppScriptRunnable::isOnceOnly() const
{
return m_isOnceOnly;
}
void QFAppScriptRunnable::setIsOnceOnly(bool isOnceOnly)
{
m_isOnceOnly = isOnceOnly;
}
void QFAppScriptRunnable::setEngine(QQmlEngine* engine)
{
m_engine = engine;
}
void QFAppScriptRunnable::release()
{
if (!m_condition.isNull() &&
m_condition.isObject() &&
m_condition.hasProperty("disconnect")) {
QJSValue disconnect = m_condition.property("disconnect");
QJSValueList args;
args << m_callback;
disconnect.callWithInstance(m_condition,args);
}
m_condition = QJSValue();
m_callback = QJSValue();
}
void QFAppScriptRunnable::run(QJSValue message)
{
QJSValueList args;
if (m_isSignalCondition &&
message.hasProperty("length")) {
int count = message.property("length").toInt();
for (int i = 0 ; i < count;i++) {
args << message.property(i);
}
} else {
args << message;
}
QJSValue ret = m_script.call(args);
if (ret.isError()) {
QString message = QString("%1:%2: %3: %4")
.arg(ret.property("fileName").toString())
.arg(ret.property("lineNumber").toString())
.arg(ret.property("name").toString())
.arg(ret.property("message").toString());
qWarning() << message;
}
}
QFAppScriptRunnable *QFAppScriptRunnable::then(QJSValue condition,QJSValue script)
{
QFAppScriptRunnable* runnable = new QFAppScriptRunnable(this);
runnable->setEngine(m_engine.data());
runnable->setCondition(condition);
runnable->setScript(script);
setNext(runnable);
return runnable;
}
QFAppScriptRunnable *QFAppScriptRunnable::next() const
{
return m_next;
}
void QFAppScriptRunnable::setNext(QFAppScriptRunnable *next)
{
m_next = next;
}
void QFAppScriptRunnable::setCondition(QJSValue condition)
{
m_condition = condition;
if (condition.isString()) {
setType(condition.toString());
m_isSignalCondition = false;
} else if (condition.isObject() && condition.hasProperty("connect")) {
Q_ASSERT(!m_engine.isNull());
QString type = QString("QuickFlux.AppScript.%1").arg(QUuid::createUuid().toString());
setType(type);
QString generator = "function(dispatcher) { return function() {dispatcher.dispatch(arguments)}}";
QFAppDispatcher* dispatcher = QFAppDispatcher::instance(m_engine);
QFAppScriptDispatcherWrapper * wrapper = new QFAppScriptDispatcherWrapper();
wrapper->setType(type);
wrapper->setDispatcher(dispatcher);
QJSValue generatorFunc = m_engine->evaluate(generator);
QJSValueList args;
args << m_engine->newQObject(wrapper);
QJSValue callback = generatorFunc.call(args);
args.clear();
args << callback;
QJSValue connect = condition.property("connect");
connect.callWithInstance(condition,args);
m_callback = callback;
m_isSignalCondition = true;
} else {
qWarning() << "AppScript: Invalid condition type";
}
}