Skip to content

Commit b98dc38

Browse files
committed
Adding in more examples for DoneHandle
1 parent 3e9630c commit b98dc38

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

examples/promise/src/main.rs

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ fn log( a: &str ) {
1919
}
2020

2121

22+
fn sleep( ms: u32 ) -> PromiseFuture< Null > {
23+
js!( return new Promise( function ( success, failure ) {
24+
setTimeout( function () {
25+
success( null );
26+
}, @{ms} );
27+
} ); ).try_into().unwrap()
28+
}
29+
30+
2231
fn test_from_thenable() {
2332
let a = Promise::from_thenable( &js!( return Promise.resolve(5); ).try_into().unwrap() );
2433

@@ -233,14 +242,6 @@ fn test_notify() {
233242

234243

235244
fn test_timeout() {
236-
fn sleep( ms: u32 ) -> PromiseFuture< Null > {
237-
js!( return new Promise( function ( success, failure ) {
238-
setTimeout( function () {
239-
success( null );
240-
}, @{ms} );
241-
} ); ).try_into().unwrap()
242-
}
243-
244245
PromiseFuture::spawn(
245246
sleep( 2000 ).inspect( |_| log( "Timeout 1 done!") ).join(
246247
sleep( 2000 ).inspect( |_| log( "Timeout 2 done!" ) ) )
@@ -267,15 +268,41 @@ fn test_callback_drop() {
267268
}
268269
}
269270

270-
let y = Foo( js!(
271-
return setInterval( function () {
272-
console.log( "Callback drop interval" );
273-
}, 1000 );
274-
) );
271+
fn make_interval() -> Foo {
272+
Foo( js!(
273+
return setInterval( function () {
274+
console.log( "Callback drop interval" );
275+
}, 1000 );
276+
) )
277+
}
278+
279+
let y = make_interval();
275280

276281
x.done( move |_result: Result< (), () >| {
277282
let _z = y;
278283
} );
284+
285+
286+
struct Bar;
287+
288+
impl Drop for Bar {
289+
fn drop( &mut self ) {
290+
log( "Bar dropped" );
291+
}
292+
}
293+
294+
let bar = Bar;
295+
296+
PromiseFuture::spawn(
297+
x.to_future::< (), Error >()
298+
.map( move |_| {
299+
let _y = bar;
300+
()
301+
} )
302+
.select( sleep( 5000 ).map( |_| () ) )
303+
.map( |_| () )
304+
.map_err( |( e, _ )| e.print() )
305+
);
279306
}
280307

281308

src/webcore/promise.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use futures::unsync::oneshot::channel;
1010
use super::promise_future::PromiseFuture;
1111

1212

13+
///
1314
#[derive( Debug, Clone )]
1415
pub struct DoneHandle {
1516
callback: Value,
@@ -117,7 +118,7 @@ impl Promise {
117118
B::Error: std::fmt::Debug,
118119
F: FnOnce( Result< A, B > ) + 'static {
119120

120-
let callback = |value: Value, success: bool| {
121+
let callback = move |value: Value, success: bool| {
121122
let value: Result< A, B > = if success {
122123
// TODO figure out a way to avoid the unwrap
123124
let value: A = value.try_into().unwrap();
@@ -154,7 +155,7 @@ impl Promise {
154155

155156
DoneHandle {
156157
callback,
157-
done
158+
done,
158159
}
159160
}
160161

@@ -187,7 +188,7 @@ impl Promise {
187188
Ok( _ ) => {},
188189
Err( _ ) => {},
189190
};
190-
} )
191+
} ),
191192
}
192193
}
193194
}

0 commit comments

Comments
 (0)