mirror of
https://github.com/servo/servo.git
synced 2025-08-08 15:05:35 +01:00
implement wait for all promise
Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com>
This commit is contained in:
parent
8c064e0aa6
commit
6b7cf15c48
1 changed files with 55 additions and 15 deletions
|
@ -410,11 +410,11 @@ impl FromJSValConvertibleRc for Promise {
|
||||||
#[derive(Clone, JSTraceable, MallocSizeOf)]
|
#[derive(Clone, JSTraceable, MallocSizeOf)]
|
||||||
/// The fulfillment handler for the list of promises in
|
/// The fulfillment handler for the list of promises in
|
||||||
/// <https://webidl.spec.whatwg.org/#wait-for-all>.
|
/// <https://webidl.spec.whatwg.org/#wait-for-all>.
|
||||||
struct WaitAllFulfillmentHandler {
|
struct WaitForAllFulfillmentHandler {
|
||||||
/// The steps to call when all promises are resolved.
|
/// The steps to call when all promises are resolved.
|
||||||
#[ignore_malloc_size_of = "Rc is hard"]
|
#[ignore_malloc_size_of = "Rc is hard"]
|
||||||
#[no_trace]
|
#[no_trace]
|
||||||
success_steps: Rc<dyn Fn(&[HandleValue])>,
|
success_steps: Rc<dyn Fn(Vec<HandleValue>)>,
|
||||||
|
|
||||||
/// The results of the promises.
|
/// The results of the promises.
|
||||||
#[ignore_malloc_size_of = "Rc is hard"]
|
#[ignore_malloc_size_of = "Rc is hard"]
|
||||||
|
@ -428,9 +428,9 @@ struct WaitAllFulfillmentHandler {
|
||||||
fulfilled_count: Rc<RefCell<usize>>,
|
fulfilled_count: Rc<RefCell<usize>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Callback for WaitAllFulfillmentHandler {
|
impl Callback for WaitForAllFulfillmentHandler {
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
fn callback(&self, cx: SafeJSContext, v: HandleValue, _realm: InRealm, _can_gc: CanGc) {
|
fn callback(&self, _cx: SafeJSContext, v: HandleValue, _realm: InRealm, _can_gc: CanGc) {
|
||||||
// Let fulfillmentHandler be the following steps given arg:
|
// Let fulfillmentHandler be the following steps given arg:
|
||||||
|
|
||||||
let equals_total = {
|
let equals_total = {
|
||||||
|
@ -456,7 +456,7 @@ impl Callback for WaitAllFulfillmentHandler {
|
||||||
.map(|val| HandleValue::from_raw(val.handle()))
|
.map(|val| HandleValue::from_raw(val.handle()))
|
||||||
.collect()
|
.collect()
|
||||||
};
|
};
|
||||||
(self.success_steps)(&result_handles);
|
(self.success_steps)(result_handles);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -464,7 +464,7 @@ impl Callback for WaitAllFulfillmentHandler {
|
||||||
#[derive(Clone, JSTraceable, MallocSizeOf)]
|
#[derive(Clone, JSTraceable, MallocSizeOf)]
|
||||||
/// The rejection handler for the list of promises in
|
/// The rejection handler for the list of promises in
|
||||||
/// <https://webidl.spec.whatwg.org/#wait-for-all>.
|
/// <https://webidl.spec.whatwg.org/#wait-for-all>.
|
||||||
struct WaitAllRejectionHandler {
|
struct WaitForAllRejectionHandler {
|
||||||
/// The steps to call if any promise rejects.
|
/// The steps to call if any promise rejects.
|
||||||
#[ignore_malloc_size_of = "Rc is hard"]
|
#[ignore_malloc_size_of = "Rc is hard"]
|
||||||
#[no_trace]
|
#[no_trace]
|
||||||
|
@ -474,8 +474,8 @@ struct WaitAllRejectionHandler {
|
||||||
rejected: Cell<bool>,
|
rejected: Cell<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Callback for WaitAllRejectionHandler {
|
impl Callback for WaitForAllRejectionHandler {
|
||||||
fn callback(&self, cx: SafeJSContext, v: HandleValue, _realm: InRealm, _can_gc: CanGc) {
|
fn callback(&self, _cx: SafeJSContext, v: HandleValue, _realm: InRealm, _can_gc: CanGc) {
|
||||||
// Let rejectionHandlerSteps be the following steps given arg:
|
// Let rejectionHandlerSteps be the following steps given arg:
|
||||||
|
|
||||||
if self.rejected.replace(true) {
|
if self.rejected.replace(true) {
|
||||||
|
@ -494,7 +494,7 @@ pub(crate) fn wait_for_all(
|
||||||
cx: SafeJSContext,
|
cx: SafeJSContext,
|
||||||
global: &GlobalScope,
|
global: &GlobalScope,
|
||||||
promises: Vec<Rc<Promise>>,
|
promises: Vec<Rc<Promise>>,
|
||||||
success_steps: Rc<dyn Fn(&[HandleValue])>,
|
success_steps: Rc<dyn Fn(Vec<HandleValue>)>,
|
||||||
failure_steps: Rc<dyn Fn(HandleValue)>,
|
failure_steps: Rc<dyn Fn(HandleValue)>,
|
||||||
realm: InRealm,
|
realm: InRealm,
|
||||||
can_gc: CanGc,
|
can_gc: CanGc,
|
||||||
|
@ -506,11 +506,11 @@ pub(crate) fn wait_for_all(
|
||||||
// Note: done below when constructing a rejection handler.
|
// Note: done below when constructing a rejection handler.
|
||||||
|
|
||||||
// Let rejectionHandlerSteps be the following steps given arg:
|
// Let rejectionHandlerSteps be the following steps given arg:
|
||||||
// Note: implemented with the `WaitAllRejectionHandler`.
|
// Note: implemented with the `WaitForAllRejectionHandler`.
|
||||||
|
|
||||||
// Let rejectionHandler be CreateBuiltinFunction(rejectionHandlerSteps, « »):
|
// Let rejectionHandler be CreateBuiltinFunction(rejectionHandlerSteps, « »):
|
||||||
// Note: done as part of attaching the `WaitAllRejectionHandler` as native rejection handler.
|
// Note: done as part of attaching the `WaitForAllRejectionHandler` as native rejection handler.
|
||||||
let rejection_handler = WaitAllRejectionHandler {
|
let rejection_handler = WaitForAllRejectionHandler {
|
||||||
failure_steps,
|
failure_steps,
|
||||||
rejected: Default::default(),
|
rejected: Default::default(),
|
||||||
};
|
};
|
||||||
|
@ -544,7 +544,7 @@ pub(crate) fn wait_for_all(
|
||||||
// Note: done with `enumerate` above.
|
// Note: done with `enumerate` above.
|
||||||
|
|
||||||
// Let fulfillmentHandler be the following steps given arg:
|
// Let fulfillmentHandler be the following steps given arg:
|
||||||
// Note: implemented with the `WaitAllFulFillmentHandler`.
|
// Note: implemented with the `WaitForAllFulFillmentHandler`.
|
||||||
|
|
||||||
// Let fulfillmentHandler be CreateBuiltinFunction(fulfillmentHandler, « »):
|
// Let fulfillmentHandler be CreateBuiltinFunction(fulfillmentHandler, « »):
|
||||||
// Note: passed below to avoid the need to root it.
|
// Note: passed below to avoid the need to root it.
|
||||||
|
@ -552,7 +552,7 @@ pub(crate) fn wait_for_all(
|
||||||
// Perform PerformPromiseThen(promise, fulfillmentHandler, rejectionHandler).
|
// Perform PerformPromiseThen(promise, fulfillmentHandler, rejectionHandler).
|
||||||
let handler = PromiseNativeHandler::new(
|
let handler = PromiseNativeHandler::new(
|
||||||
global,
|
global,
|
||||||
Some(Box::new(WaitAllFulfillmentHandler {
|
Some(Box::new(WaitForAllFulfillmentHandler {
|
||||||
success_steps: success_steps.clone(),
|
success_steps: success_steps.clone(),
|
||||||
result,
|
result,
|
||||||
promise_index,
|
promise_index,
|
||||||
|
@ -567,3 +567,43 @@ pub(crate) fn wait_for_all(
|
||||||
// Note: done above with `enumerate`.
|
// Note: done above with `enumerate`.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <https://webidl.spec.whatwg.org/#waiting-for-all-promise>
|
||||||
|
pub(crate) fn wait_for_all_promise(
|
||||||
|
cx: SafeJSContext,
|
||||||
|
global: &GlobalScope,
|
||||||
|
promises: Vec<Rc<Promise>>,
|
||||||
|
realm: InRealm,
|
||||||
|
can_gc: CanGc,
|
||||||
|
) -> Rc<Promise> {
|
||||||
|
// Let promise be a new promise of type Promise<sequence<T>> in realm.
|
||||||
|
let promise = Promise::new(global, can_gc);
|
||||||
|
let success_promise = promise.clone();
|
||||||
|
let failure_promise = promise.clone();
|
||||||
|
|
||||||
|
// Let successSteps be the following steps, given results:
|
||||||
|
let success_steps = Rc::new(move |results: Vec<HandleValue>| {
|
||||||
|
// Resolve promise with results.
|
||||||
|
success_promise.resolve_native(&results, can_gc);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Let failureSteps be the following steps, given reason:
|
||||||
|
let failure_steps = Rc::new(move |reason: HandleValue| {
|
||||||
|
// Reject promise with reason.
|
||||||
|
failure_promise.reject_native(&reason, can_gc);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Wait for all with promises, given successSteps and failureSteps.
|
||||||
|
wait_for_all(
|
||||||
|
cx,
|
||||||
|
global,
|
||||||
|
promises,
|
||||||
|
success_steps,
|
||||||
|
failure_steps,
|
||||||
|
realm,
|
||||||
|
can_gc,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Return promise.
|
||||||
|
promise
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue