/* * Copyright (c) 2022, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include namespace Web::Fetch::Fetching { JS::NonnullGCPtr PendingResponse::create(JS::VM& vm, JS::NonnullGCPtr request) { return vm.heap().allocate_without_realm(request); } JS::NonnullGCPtr PendingResponse::create(JS::VM& vm, JS::NonnullGCPtr request, JS::NonnullGCPtr response) { return vm.heap().allocate_without_realm(request, response); } PendingResponse::PendingResponse(JS::NonnullGCPtr request, JS::GCPtr response) : m_request(request) , m_response(response) { m_request->add_pending_response({}, *this); } void PendingResponse::visit_edges(JS::Cell::Visitor& visitor) { Base::visit_edges(visitor); visitor.visit(m_request); visitor.visit(m_response); } void PendingResponse::when_loaded(Callback callback) { VERIFY(!m_callback); m_callback = move(callback); if (m_response) run_callback(); } void PendingResponse::resolve(JS::NonnullGCPtr response) { VERIFY(!m_response); m_response = response; if (m_callback) run_callback(); } void PendingResponse::run_callback() { VERIFY(m_callback); VERIFY(m_response); Platform::EventLoopPlugin::the().deferred_invoke([this] { m_callback(*m_response); m_request->remove_pending_response({}, *this); }); } }