Starting to mess with promises
Going to want async IO, I think. And it's a fun detail that I guess I'm in charge of deciding when to run promise completion functions. :D
This commit is contained in:
parent
c1d86676c3
commit
5be0ffa08f
7 changed files with 251 additions and 74 deletions
34
oden-js/src/promise.rs
Normal file
34
oden-js/src/promise.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
use crate::{ContextRef, Value, ValueRef};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Promise {
|
||||
pub object: Value,
|
||||
pub resolve_fn: Value,
|
||||
pub reject_fn: Value,
|
||||
}
|
||||
|
||||
impl Promise {
|
||||
pub(crate) fn new(object: Value, resolve_fn: Value, reject_fn: Value) -> Self {
|
||||
Promise {
|
||||
object,
|
||||
resolve_fn,
|
||||
reject_fn,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dup(&self, ctx: &ContextRef) -> Self {
|
||||
Promise {
|
||||
object: self.object.dup(ctx),
|
||||
resolve_fn: self.resolve_fn.dup(ctx),
|
||||
reject_fn: self.reject_fn.dup(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resolve(self, context: &ContextRef, value: &ValueRef) {
|
||||
let _ = self.resolve_fn.call(context, &[value]);
|
||||
}
|
||||
|
||||
pub fn reject(self, context: &ContextRef, value: &ValueRef) {
|
||||
let _ = self.reject_fn.call(context, &[value]);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue