Skip to content

Commit

Permalink
Improving the implementation of DoneHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
Pauan committed Feb 24, 2018
1 parent e1957e2 commit c1a2e68
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/webcore/promise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ use super::promise_future::PromiseFuture;
/// See the documentation for [`done`](struct.Promise.html#method.done) for more information.
#[derive( Debug, Clone )]
pub struct DoneHandle {
callback: Value,
done: Value,
state: Value,
}

impl Cancel for DoneHandle {
fn cancel( &mut self ) {
js! { @(no_return)
@{&self.done}[0] = true;
@{&self.callback}.drop();
var state = @{&self.state};
state.cancelled = true;
state.callback.drop();
}
}
}
Expand Down Expand Up @@ -146,29 +146,30 @@ impl Promise {
callback( value );
};

let callback = js!( return @{Once( callback )}; );
let state = js!(
var callback = @{Once( callback )};

let done = js!(
var callback = @{&callback};
var done = [ false ];
var state = {
cancelled: false,
callback: callback
};

// TODO don't swallow any errors thrown inside callback
@{self}.then( function ( value ) {
if ( !done[0] ) {
if ( !state.cancelled ) {
callback( value, true );
}
}, function ( value ) {
if ( !done[0] ) {
if ( !state.cancelled ) {
callback( value, false );
}
} );

return done;
return state;
);

CancelOnDrop::new( DoneHandle {
callback,
done,
state,
} )
}

Expand Down

0 comments on commit c1a2e68

Please sign in to comment.