-
-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows `thread_local` contexts to have owned `HostHooks` and `JobQueues`. It changes the following: - Creates a new `MaybeShared` struct that can hold either a reference or an `Rc`. - Changes the `job_queue` and `host_hooks` parameters of `Context` to use `MaybeShared`. This PR also allows us to make `SimpleJobQueue` the default promise runner, which I think it's pretty cool :) cc @lastmjs
- Loading branch information
Showing
14 changed files
with
147 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,20 @@ | ||
use crate::{context::ContextBuilder, job::SimpleJobQueue, run_test_actions_with, TestAction}; | ||
use crate::{run_test_actions, TestAction}; | ||
use indoc::indoc; | ||
|
||
#[test] | ||
fn promise() { | ||
let queue = SimpleJobQueue::new(); | ||
let context = &mut ContextBuilder::new().job_queue(&queue).build().unwrap(); | ||
run_test_actions_with( | ||
[ | ||
TestAction::run(indoc! {r#" | ||
run_test_actions([ | ||
TestAction::run(indoc! {r#" | ||
let count = 0; | ||
const promise = new Promise((resolve, reject) => { | ||
count += 1; | ||
resolve(undefined); | ||
}).then((_) => (count += 1)); | ||
count += 1; | ||
"#}), | ||
TestAction::assert_eq("count", 2), | ||
#[allow(clippy::redundant_closure_for_method_calls)] | ||
TestAction::inspect_context(|ctx| ctx.run_jobs()), | ||
TestAction::assert_eq("count", 3), | ||
], | ||
context, | ||
); | ||
TestAction::assert_eq("count", 2), | ||
#[allow(clippy::redundant_closure_for_method_calls)] | ||
TestAction::inspect_context(|ctx| ctx.run_jobs()), | ||
TestAction::assert_eq("count", 3), | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use std::{ops::Deref, rc::Rc}; | ||
|
||
/// A [`Cow`][std::borrow::Cow]-like pointer where the `Owned` variant is an [`Rc`]. | ||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] | ||
pub enum MaybeShared<'a, T: ?Sized> { | ||
/// Borrowed data. | ||
Borrowed(&'a T), | ||
/// `Rc` shared data. | ||
Shared(Rc<T>), | ||
} | ||
|
||
impl<T: ?Sized> Clone for MaybeShared<'_, T> { | ||
fn clone(&self) -> Self { | ||
match self { | ||
Self::Borrowed(b) => Self::Borrowed(b), | ||
Self::Shared(sh) => Self::Shared(sh.clone()), | ||
} | ||
} | ||
} | ||
|
||
impl<T: ?Sized> Deref for MaybeShared<'_, T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
match self { | ||
MaybeShared::Borrowed(b) => b, | ||
MaybeShared::Shared(sh) => sh, | ||
} | ||
} | ||
} | ||
|
||
impl<'a, T: ?Sized> From<&'a T> for MaybeShared<'a, T> { | ||
fn from(value: &'a T) -> Self { | ||
Self::Borrowed(value) | ||
} | ||
} | ||
|
||
impl<T: ?Sized> From<Rc<T>> for MaybeShared<'static, T> { | ||
fn from(value: Rc<T>) -> Self { | ||
Self::Shared(value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.