-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathWKTJsiPromiseWrapper.h
More file actions
204 lines (163 loc) · 6.05 KB
/
WKTJsiPromiseWrapper.h
File metadata and controls
204 lines (163 loc) · 6.05 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#pragma once
#include <jsi/jsi.h>
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include "WKTJsiHostObject.h"
#include "WKTJsiWorklet.h"
#include "WKTJsiWrapper.h"
namespace RNWorklet {
namespace jsi = facebook::jsi;
class JsiWrapper;
static const char *ThenPropName = "then";
static const char *CatchPropName = "catch";
class JsiPromiseWrapper;
struct PromiseQueueItem {
std::shared_ptr<JsiPromiseWrapper> controlledPromise;
jsi::HostFunctionType fulfilledFn;
jsi::HostFunctionType catchFn;
};
struct FinallyQueueItem {
std::shared_ptr<JsiPromiseWrapper> controlledPromise;
jsi::HostFunctionType sideEffectFn;
};
class PromiseParameter {
public:
virtual void resolve(jsi::Runtime &runtime, const jsi::Value &val) = 0;
virtual void reject(jsi::Runtime &runtime, const jsi::Value &reason) = 0;
};
using PromiseResolveFunction =
std::function<void(jsi::Runtime &runtime, const jsi::Value &val)>;
using PromiseRejectFunction =
std::function<void(jsi::Runtime &runtime, const jsi::Value &reason)>;
using PromiseComputationFunction = std::function<void(
jsi::Runtime &runtime, std::shared_ptr<PromiseParameter> promise)>;
/**
Wraps a Promise so that it can be shared between multiple runtimes as arguments
or return values.
*/
class JsiPromiseWrapper
: public JsiHostObject,
public JsiWrapper,
public PromiseParameter,
public std::enable_shared_from_this<JsiPromiseWrapper> {
public:
static std::shared_ptr<JsiPromiseWrapper>
createPromiseWrapper(jsi::Runtime &runtime,
PromiseComputationFunction computation);
JsiPromiseWrapper(jsi::Runtime &runtime, const jsi::Value &value,
JsiWrapper *parent)
: JsiWrapper(runtime, value, parent) {
_counter = ++Counter;
setType(JsiWrapperType::Promise);
// printf("promise: CTOR JsiPromiseWrapper %zu\n", _counter);
}
JsiPromiseWrapper(jsi::Runtime &runtime, JsiWrapper *parent)
: JsiWrapper(runtime, jsi::Value::undefined(), parent) {
setType(JsiWrapperType::Promise);
_counter = ++Counter;
// printf("promise: CTOR JsiPromiseWrapper %zu\n", _counter);
}
explicit JsiPromiseWrapper(jsi::Runtime &runtime);
~JsiPromiseWrapper() { JsiHostObject::dispose(); }
/**
Overridden dispose - release our array resources!
*/
void release_wrapped_resources() override {
if (_reason != nullptr) {
_reason->release_wrapped_resources();
}
if (_value != nullptr) {
_value->release_wrapped_resources();
}
}
/**
Returns true if the object is a thenable object - ie. an object with a then
function. Which is basically what a promise is.
*/
static bool isThenable(jsi::Runtime &runtime, jsi::Object &obj);
/**
Returns true if the object is a thenable object - ie. an object with a then
function. Which is basically what a promise is.
*/
static bool isThenable(jsi::Runtime &runtime, jsi::Value &value);
JSI_HOST_FUNCTION(then) {
auto thenFn = count > 0 ? &arguments[0] : nullptr;
auto catchFn = count > 1 ? &arguments[1] : nullptr;
return then(runtime, thisValue, thenFn, catchFn);
}
JSI_HOST_FUNCTION(_catch) {
auto catchFn = count > 0 ? &arguments[0] : nullptr;
return then(runtime, thisValue, nullptr, catchFn);
}
JSI_HOST_FUNCTION(finally) {
auto sideEffectsFn = count > 0 ? &arguments[0] : nullptr;
return finally(runtime, thisValue, sideEffectsFn);
}
JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC_NAMED(JsiPromiseWrapper, _catch,
"catch"),
JSI_EXPORT_FUNC(JsiPromiseWrapper, then),
JSI_EXPORT_FUNC(JsiPromiseWrapper, finally))
/**
Creates a wrapped promise that is resolved with the given value
*/
static std::shared_ptr<JsiPromiseWrapper>
resolve(jsi::Runtime &runtime, std::shared_ptr<JsiWrapper> value);
/**
Creates a wrapped promise that is rejected with the given reason
*/
static std::shared_ptr<JsiPromiseWrapper>
reject(jsi::Runtime &runtime, std::shared_ptr<JsiWrapper> reason);
void onFulfilled(jsi::Runtime &runtime, const jsi::Value &val);
void onRejected(jsi::Runtime &runtime, const jsi::Value &reason);
static size_t Counter;
size_t _counter;
void resolve(jsi::Runtime &runtime, const jsi::Value &value) override {
onFulfilled(runtime, value);
}
void reject(jsi::Runtime &runtime, const jsi::Value &reason) override {
onRejected(runtime, reason);
}
protected:
void dispose(bool disposed) override {
if (!disposed) {
release_wrapped_resources();
}
}
jsi::Value then(jsi::Runtime &runtime, const jsi::Value &thisValue,
const jsi::Value *thenFn, const jsi::Value *catchFn);
std::shared_ptr<JsiPromiseWrapper> then(jsi::Runtime &runtime,
std::shared_ptr<JsiWrapper> thisValue,
const jsi::HostFunctionType &thenFn,
const jsi::HostFunctionType &catchFn);
// TODO: Same pattern as for then!
jsi::Value finally(jsi::Runtime &runtime, const jsi::Value &thisValue,
const jsi::Value *sideEffectFn);
/**
Basically makes the promise wrapper readonly (which it should be)
*/
bool canUpdateValue(jsi::Runtime &runtime, const jsi::Value &value) override {
return false;
}
/*
Converts to a promise
*/
jsi::Value getValue(jsi::Runtime &runtime) override;
/**
Converts from a promise
*/
void setValue(jsi::Runtime &runtime, const jsi::Value &value) override;
private:
void runComputation(jsi::Runtime &runtime,
PromiseComputationFunction computation);
typedef enum { Pending = 0, Fulfilled = 1, Rejected = 2 } PromiseState;
void propagateFulfilled(jsi::Runtime &runtime);
void propagateRejected(jsi::Runtime &runtime);
PromiseState _state = PromiseState::Pending;
std::shared_ptr<JsiWrapper> _value;
std::shared_ptr<JsiWrapper> _reason;
std::vector<PromiseQueueItem> _thenQueue;
std::vector<FinallyQueueItem> _finallyQueue;
};
} // namespace RNWorklet