MCPcopy Create free account
hub / github.com/Snapchat/Valdi / operator()

Method operator()

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

Source from the content-addressed store, hash-verified

208 auto future = promise->get_future();
209
210 // The JS callback may need locks this thread holds (e.g. the ViewNodeTree lock when the call
211 // originates from the scroll path); suspend them while parked so the JS thread can make
212 // progress instead of stalling until the deadline. Same pattern as
213 // DeferredViewTransaction::flush before dispatchSync.
214 DropAllTrackedLocks dropAllTrackedLocks;
215
216 taskScheduler->dispatchOnJsThreadAsync(
217 getContext(),
218 getDispatchAttribution(),
219 [self = strongSmallRef(this), parameters = captureParameters(parameters, parametersSize), promise](
220 auto& jsEntry) {
221 MainThreadBatchAllowScope mainThreadBatchAllowScope;
222 auto result = self->doJsCall(jsEntry, parameters.data(), parameters.size(), nullptr, false);
223 promise->set_value(Result(std::move(result)));
224 });
225
226 if (std::future_status::ready == future.wait_until(deadline)) {
227 return future.get();
228 }
229
230 return Error("JS function timeout");
231}
232
233// State shared between the thread parked on a deadline-bounded call and the JS task that runs it.
234// Exactly one side wins the Pending transition: the JS task (Completed, the waiter reads the
235// result) or the waiter (TimedOut, the task is now overdue and reports back when it runs).
236struct DeadlineCall {
237 enum class State : uint8_t { Pending, Completed, TimedOut };
238
239 std::promise<Result<Value>> promise;
240 std::atomic<State> state{State::Pending};
241 std::chrono::steady_clock::time_point dispatchedAt = std::chrono::steady_clock::now();
242};
243
244Result<Value> ValueFunctionWithJSValue::dispatchAndWaitOnJsThreadWithCircuitBreaker(
245 const Ref<JavaScriptTaskScheduler>& taskScheduler,
246 const std::chrono::steady_clock::time_point& deadline,
247 const Value* parameters,
248 size_t parametersSize,
249 SyncCallTimeoutPolicy timeoutPolicy) {
250 auto skipIfTimedOut = timeoutPolicy == SyncCallTimeoutPolicy::SkipIfTimedOut;
251
252 if (taskScheduler->hasOverdueDeadlineCalls()) {
253 // An earlier bounded call is still queued behind whatever the JS thread is stuck on, so this
254 // one would only burn its full deadline too. Waiting per call is what lets a burst of input
255 // hold the main thread for seconds while each individual wait stays within budget.
256 taskScheduler->onDeadlineCallFailedFast();
257 if (!skipIfTimedOut) {
258 taskScheduler->dispatchOnJsThreadAsync(
259 getContext(),
260 getDispatchAttribution(),
261 [self = strongSmallRef(this),
262 parameters = captureParameters(parameters, parametersSize)](auto& jsEntry) {
263 MainThreadBatchAllowScope mainThreadBatchAllowScope;
264 self->doJsCall(jsEntry, parameters.data(), parameters.size(), nullptr, true);
265 });
266 }
267 return Error("JS function skipped: JS thread has an overdue sync call");

Callers

nothing calls this directly

Calls 15

doJsCallMethod · 0.95
strongSmallRefFunction · 0.85
captureParametersFunction · 0.85
getLoggerMethod · 0.80
getFlagsMethod · 0.80
toStringMethod · 0.65
loadMethod · 0.65
getContextFunction · 0.50
to_stringFunction · 0.50
lockMethod · 0.45

Tested by

no test coverage detected