Promises, promises

This commit is contained in:
John Doty 2023-06-29 10:10:40 -07:00
parent 17fdee51e6
commit a2dafeea12
6 changed files with 303 additions and 102 deletions

View file

@ -1,11 +1,9 @@
use oden_js::{
module::loader::{ModuleLoader, ModuleSource},
Context, ContextRef, Promise, Result, Runtime, Value, ValueResult,
Context, ContextRef, Result, Runtime, Value,
};
use std::collections::HashMap;
use std::ffi::OsStr;
use std::path::Path;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::mpsc::{channel, Receiver};
pub mod graphics;
@ -15,6 +13,7 @@ mod typescript;
use typescript::transpile_to_javascript;
pub mod assets;
pub mod io;
struct Loader {}
@ -39,21 +38,6 @@ impl ModuleLoader for Loader {
}
}
#[derive(Eq, PartialEq, Hash, Debug, Clone, Copy)]
pub struct PromiseHandle(u64);
impl PromiseHandle {
pub fn new() -> Self {
static NEXT_ID: AtomicU64 = AtomicU64::new(0);
PromiseHandle(NEXT_ID.fetch_add(1, Ordering::SeqCst))
}
}
pub enum ScriptEvent {
AddPromise(PromiseHandle, Promise),
CompletePromise(PromiseHandle, Box<dyn FnOnce(&ContextRef) -> ValueResult>),
}
pub struct ScriptContext {
context: Context,
init: Value,
@ -63,9 +47,6 @@ pub struct ScriptContext {
gfx: graphics::GraphicsAPI,
_assets: assets::AssetsAPI,
gfx_receive: Receiver<graphics::GraphicsCommand>,
script_receive: Receiver<ScriptEvent>,
promises: HashMap<PromiseHandle, Promise>,
}
impl ScriptContext {
@ -78,12 +59,12 @@ impl ScriptContext {
context.add_intrinsic_operators();
let (gfx_send, gfx_receive) = channel();
let (script_send, script_receive) = channel();
let gfx = graphics::GraphicsAPI::define(&context, gfx_send.clone())
.expect("Graphics module should load without error");
let assets = assets::AssetsAPI::define(&context, gfx_send.clone())
.expect("Assets module should load without error");
let _io = io::IoAPI::define(&context).expect("IO module should load without error");
let module = context
.import_module("./src/main.ts", "")
@ -110,9 +91,6 @@ impl ScriptContext {
gfx_receive,
_assets: assets,
script_receive,
promises: HashMap::new(),
}
}
@ -127,31 +105,8 @@ impl ScriptContext {
}
pub fn update(&mut self) {
// Handle any promises that have completed before calling update.
while let Ok(event) = self.script_receive.try_recv() {
match event {
// TODO: Capture debugging information.
ScriptEvent::AddPromise(handle, promise) => {
self.promises.insert(handle, promise);
}
ScriptEvent::CompletePromise(handle, value_producer) => {
if let Some(promise) = self.promises.remove(&handle) {
let result = value_producer(&self.context);
match result {
Ok(v) => {
promise.resolve(&self.context, &v);
}
Err(e) => {
let error = e.to_js_error(&self.context);
promise.reject(&self.context, &error);
}
}
}
}
}
}
// Tell the runtime to process all pending "jobs".
// Tell the runtime to process all pending "jobs". This includes
// promise completions.
self.context
.process_all_jobs()
.expect("Error processing async jobs");