MCPcopy Create free account
hub / github.com/Snapchat/Valdi / callPromise

Method callPromise

valdi/src/valdi/runtime/JavaScript/ValueFunctionWithJSValue.cpp:281–313  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

279}
280
281Value ValueFunctionWithJSValue::callPromise(const Ref<JavaScriptTaskScheduler>& taskScheduler,
282 const ValueFunctionCallContext& callContext) {
283 auto promise = makeShared<ResolvablePromise>();
284
285 taskScheduler->dispatchOnJsThreadAsync(
286 getContext(),
287 [self = strongSmallRef(this), promise, parameters = captureParameters(callContext)](auto& jsEntry) {
288 Value result = self->doJsCall(jsEntry, parameters.data(), parameters.size(), nullptr, false);
289 if (!jsEntry.exceptionTracker) {
290 promise->fulfill(jsEntry.exceptionTracker.extractError());
291 return;
292 }
293
294 // When an interruptible callback is skipped (Valdi context destroyed), doJsCall returns
295 // undefined. Fulfilling the promise with undefined would cause native unmarshalling to
296 // fail (e.g. expected NativeSnapDoc). Reject the promise so the caller can handle it.
297 if (result.getType() == ValueType::Undefined && self->_ignoreIfValdiContextIsDestroyed &&
298 self->getContext() != nullptr && self->getContext()->isDestroyed()) {
299 promise->fulfill(Result<Value>(Error("Valdi context destroyed")));
300 return;
301 }
302
303 auto nestedPromise = result.getTypedRef<Promise>();
304 if (nestedPromise != nullptr) {
305 // Flatten one level of promise, since we already returned a promise to the caller
306 promise->fulfillWithPromiseResult(nestedPromise);
307 } else {
308 promise->fulfill(result);
309 }
310 });
311
312 return Value(promise);
313}
314
315std::string_view ValueFunctionWithJSValue::getFunctionType() const {
316 return "JSFunction";

Callers

nothing calls this directly

Calls 15

doJsCallMethod · 0.95
strongSmallRefFunction · 0.85
captureParametersFunction · 0.85
extractErrorMethod · 0.80
getContextFunction · 0.50
ErrorInterface · 0.50
ValueClass · 0.50
dataMethod · 0.45
sizeMethod · 0.45
fulfillMethod · 0.45

Tested by

no test coverage detected