forked from Cutehacks/duperagent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise.cpp
168 lines (139 loc) · 4.9 KB
/
promise.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2016 Cutehacks AS. All rights reserved.
// License can be found in the LICENSE file.
#include <QtQml/QQmlEngine>
#include "promise.h"
namespace com { namespace cutehacks { namespace duperagent {
Promise::Promise(QQmlEngine *engine, QJSValue executor) :
QObject(0),
m_engine(engine),
m_state(PENDING)
{
m_self = m_engine->newQObject(this);
m_engine->setObjectOwnership(this, QQmlEngine::JavaScriptOwnership);
m_self.setProperty("catch", m_self.property("katch"));
if (executor.isCallable()) {
executor.call(QJSValueList()
<< m_self.property("fulfill")
<< m_self.property("reject"));
}
}
QJSValue Promise::then(QJSValue onFulfilled, QJSValue onRejected)
{
Promise *next = new Promise(m_engine);
if (m_state == FULFILLED) {
call(onFulfilled, m_value, next);
} else if (m_state == REJECTED) {
call(onRejected, m_value, next);
} else {
Handler h = {
onFulfilled,
onRejected,
next,
};
m_handlers.append(h);
}
return next->self();
}
QJSValue Promise::katch(QJSValue onRejected)
{
return then(QJSValue::UndefinedValue, onRejected);
}
void Promise::fulfill(const QJSValue &value)
{
if (m_state != PENDING)
return;
m_state = FULFILLED;
m_value = value;
while (!m_handlers.empty()) {
Handler h = m_handlers.takeFirst();
call(h.onFulfilled, m_value, h.next);
}
emit settled(m_state, m_value);
}
void Promise::reject(const QJSValue &reason)
{
if (m_state != PENDING)
return;
m_state = REJECTED;
m_value = reason;
while (!m_handlers.empty()) {
Handler h = m_handlers.takeFirst();
call(h.onRejected, m_value, h.next);
}
emit settled(m_state, m_value);
}
void Promise::call(QJSValue fn, const QJSValue& arg, Promise *promise)
{
Q_ASSERT(m_state != PENDING);
if (!fn.isCallable()) {
if (m_state == FULFILLED)
promise->fulfill(!fn.isUndefined() ? fn : arg);
else
promise->reject(!fn.isUndefined() ? fn : arg);
} else {
QJSValue result = fn.call(QJSValueList() << arg);
if (result.isError()) {
promise->reject(result);
} else {
Promise *p = qobject_cast<Promise*>(result.toQObject());
if (p) {
if (p == promise) {
// 2.3.1 If promise and x refer to the same object, reject
// promise with a TypeError as the reason.
promise->reject(m_engine->evaluate("new TypeError();"));
} else {
// 2.3.2 If x is a promise, adopt its state
promise->merge(p);
}
} else if (result.isCallable() || result.isObject()) {
// 2.3.3 Otherwise, if x is an object or function,
// 2.3.3.1 Let then be x.then.
QJSValue then = result.property("then");
if (then.isError()) {
// 2.3.3.2 If retrieving the property x.then results in a
// thrown exception e, reject promise with e as the reason.
promise->reject(then);
} else if (then.isCallable()) {
// 2.3.3.3 If then is a function, call it with x as this,
// first argument resolvePromise, and second argument
// rejectPromise
QJSValue resolvePromise = promise->self().property("fulfill");
QJSValue rejectPromise = promise->self().property("reject");
QJSValue thenResult = then.callWithInstance(result,
QJSValueList() << resolvePromise << rejectPromise);
if (thenResult.isError()) {
promise->reject(thenResult);
}
} else {
// 2.3.3.4 If then is not a function, fulfill promise with x.
promise->fulfill(result);
}
} else {
// 2.3.4 If x is not an object or function, fulfill promise with x.
promise->fulfill(result);
}
}
}
}
void Promise::merge(Promise *other)
{
if (other->m_state != PENDING) {
// 2.3.2.2 If/when x is fulfilled, fulfill promise with the same value.
// 2.3.2.3 If/when x is rejected, reject promise with the same reason.
settle(other->m_state, other->m_value);
} else {
// 2.3.2.1 If x is pending, promise must remain pending until x is
// fulfilled or rejected.
connect(other, SIGNAL(settled(Promise::State, QJSValue)),
this, SLOT(settle(Promise::State, QJSValue)));
}
}
void Promise::settle(Promise::State state, QJSValue value)
{
if (state == FULFILLED) {
fulfill(value);
} else {
reject(value);
}
}
} } }