Skip to content

Commit

Permalink
fix(task): remove cancel method from Task trait(yewstack#884)
Browse files Browse the repository at this point in the history
In order to avoid misuse It was decided to remove `cancel` from `Task` trait
and delegate such logic to `Drop`.
  • Loading branch information
kakoc committed Jan 26, 2020
1 parent 38f4054 commit e89ac55
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 56 deletions.
33 changes: 15 additions & 18 deletions src/services/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,29 +420,26 @@ impl Task for FetchTask {
false
}
}
fn cancel(&mut self) {
// Fetch API doesn't support request cancelling in all browsers
// and we should use this workaround with a flag.
// In that case, request not canceled, but callback won't be called.
let handle = self
.0
.take()
.expect("tried to cancel request fetching twice");
js! { @(no_return)
var handle = @{handle};
handle.active = false;
handle.callback.drop();
if (handle.abortController) {
handle.abortController.abort();
}
}
}
}

impl Drop for FetchTask {
fn drop(&mut self) {
if self.is_active() {
self.cancel();
// Fetch API doesn't support request cancelling in all browsers
// and we should use this workaround with a flag.
// In that case, request not canceled, but callback won't be called.
let handle = self
.0
.take()
.expect("tried to cancel request fetching twice");
js! { @(no_return)
var handle = @{handle};
handle.active = false;
handle.callback.drop();
if (handle.abortController) {
handle.abortController.abort();
}
}
}
}
}
15 changes: 6 additions & 9 deletions src/services/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,17 @@ impl Task for IntervalTask {
fn is_active(&self) -> bool {
self.0.is_some()
}
fn cancel(&mut self) {
let handle = self.0.take().expect("tried to cancel interval twice");
js! { @(no_return)
var handle = @{handle};
clearInterval(handle.interval_id);
handle.callback.drop();
}
}
}

impl Drop for IntervalTask {
fn drop(&mut self) {
if self.is_active() {
self.cancel();
let handle = self.0.take().expect("tried to cancel interval twice");
js! { @(no_return)
var handle = @{handle};
clearInterval(handle.interval_id);
handle.callback.drop();
}
}
}
}
2 changes: 0 additions & 2 deletions src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ use std::time::Duration;
pub trait Task: Drop {
/// Returns `true` if task is active.
fn is_active(&self) -> bool;
/// Cancel current service's routine.
fn cancel(&mut self);
}

#[doc(hidden)]
Expand Down
6 changes: 1 addition & 5 deletions src/services/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,12 @@ impl Task for ReaderTask {
fn is_active(&self) -> bool {
self.file_reader.ready_state() == FileReaderReadyState::Loading
}

fn cancel(&mut self) {
self.file_reader.abort();
}
}

impl Drop for ReaderTask {
fn drop(&mut self) {
if self.is_active() {
self.cancel();
self.file_reader.abort();
}
}
}
15 changes: 6 additions & 9 deletions src/services/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,17 @@ impl Task for RenderTask {
fn is_active(&self) -> bool {
self.0.is_some()
}
fn cancel(&mut self) {
let handle = self.0.take().expect("tried to cancel render twice");
js! { @(no_return)
var handle = @{handle};
cancelAnimationFrame(handle.render_id);
handle.callback.drop();
}
}
}

impl Drop for RenderTask {
fn drop(&mut self) {
if self.is_active() {
self.cancel();
let handle = self.0.take().expect("tried to cancel render twice");
js! { @(no_return)
var handle = @{handle};
cancelAnimationFrame(handle.render_id);
handle.callback.drop();
}
}
}
}
15 changes: 6 additions & 9 deletions src/services/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,17 @@ impl Task for TimeoutTask {
fn is_active(&self) -> bool {
self.0.is_some()
}
fn cancel(&mut self) {
let handle = self.0.take().expect("tried to cancel timeout twice");
js! { @(no_return)
var handle = @{handle};
clearTimeout(handle.timeout_id);
handle.callback.drop();
}
}
}

impl Drop for TimeoutTask {
fn drop(&mut self) {
if self.is_active() {
self.cancel();
let handle = self.0.take().expect("tried to cancel timeout twice");
js! { @(no_return)
var handle = @{handle};
clearTimeout(handle.timeout_id);
handle.callback.drop();
}
}
}
}
5 changes: 1 addition & 4 deletions src/services/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,12 @@ impl Task for WebSocketTask {
fn is_active(&self) -> bool {
self.ws.ready_state() == SocketReadyState::Open
}
fn cancel(&mut self) {
self.ws.close();
}
}

impl Drop for WebSocketTask {
fn drop(&mut self) {
if self.is_active() {
self.cancel();
self.ws.close();
}
}
}

0 comments on commit e89ac55

Please sign in to comment.