Skip to content

Commit b4150bc

Browse files
author
jizhuozhi.george
committed
Add Promise support for http callout
fix ci Signed-off-by: jizhuozhi.george <[email protected]>
1 parent a1e4f9e commit b4150bc

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

examples/http_parallel_call/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ proxy_wasm::main! {{
2525
proxy_wasm::set_http_context(|_, _| -> Box<dyn HttpContext> { Box::new(HttpParallelCall::default()) });
2626
}}
2727

28+
type OnHttpResponseArgs = (u32, usize, usize, usize);
29+
2830
#[derive(Default)]
2931
struct HttpParallelCall {
30-
m: HashMap<u32, Rc<Promise<(u32, usize, usize, usize)>>>,
32+
m: HashMap<u32, Rc<Promise<OnHttpResponseArgs>>>,
3133
}
3234

3335
impl HttpContext for HttpParallelCall {
@@ -70,10 +72,10 @@ impl HttpContext for HttpParallelCall {
7072
Promise::all_of(vec![
7173
promise1
7274
.then(|(_, _, _body_size, _)| get_http_call_response_body_string(0, _body_size))
73-
.then(|body| body.unwrap_or_else(|| "".to_string())),
75+
.then(|body| body.unwrap_or_default()),
7476
promise2
7577
.then(|(_, _, _body_size, _)| get_http_call_response_body_string(0, _body_size))
76-
.then(|body| body.unwrap_or_else(|| "".to_string())),
78+
.then(|body| body.unwrap_or_default()),
7779
])
7880
.then(|results| {
7981
send_http_response(

src/promise.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ enum PromiseState<T> {
2121
Rejected(String),
2222
}
2323

24+
type ThenCallbackRef<T> = RefCell<Option<Box<dyn FnOnce(T)>>>;
25+
type CatchCallbackRef = RefCell<Option<Box<dyn FnOnce(String)>>>;
26+
2427
pub struct Promise<T> {
2528
state: RefCell<PromiseState<T>>,
26-
then_callback: RefCell<Option<Box<dyn FnOnce(T)>>>,
27-
catch_callback: RefCell<Option<Box<dyn FnOnce(String)>>>,
29+
then_callback: ThenCallbackRef<T>,
30+
catch_callback: CatchCallbackRef,
2831
}
2932

3033
impl<T> Promise<T>
@@ -202,7 +205,7 @@ mod tests {
202205
});
203206

204207
promise.fulfill(42);
205-
assert_eq!(true, touched.take())
208+
assert!(touched.take())
206209
}
207210

208211
#[test]
@@ -217,7 +220,7 @@ mod tests {
217220
});
218221

219222
promise.reject("Error".to_string());
220-
assert_eq!(true, touched.take())
223+
assert!(touched.take())
221224
}
222225

223226
#[test]
@@ -237,7 +240,7 @@ mod tests {
237240
});
238241

239242
promise.fulfill(10);
240-
assert_eq!(true, touched.take())
243+
assert!(touched.take())
241244
}
242245

243246
#[test]
@@ -264,7 +267,7 @@ mod tests {
264267
panic!("Should not reach here");
265268
});
266269

267-
assert_eq!(true, touched.take())
270+
assert!(touched.take())
268271
}
269272

270273
#[test]
@@ -289,7 +292,7 @@ mod tests {
289292
*touched_clone.borrow_mut() = true;
290293
});
291294

292-
assert_eq!(true, touched.take())
295+
assert!(touched.take())
293296
}
294297

295298
#[test]
@@ -314,7 +317,7 @@ mod tests {
314317
*touched_clone.borrow_mut() = true;
315318
});
316319

317-
assert_eq!(true, touched.take())
320+
assert!(touched.take())
318321
}
319322

320323
#[test]
@@ -333,6 +336,6 @@ mod tests {
333336
panic!("Should not reach here");
334337
});
335338

336-
assert_eq!(true, touched.take())
339+
assert!(touched.take())
337340
}
338341
}

0 commit comments

Comments
 (0)