oden/oden-js/src/promise.rs
John Doty 5be0ffa08f 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
2023-06-28 15:54:13 -07:00

34 lines
854 B
Rust

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]);
}
}