-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from ReactKit/feature/removable-handler
Add Canceller for removing progress/then handlers.
- Loading branch information
Showing
5 changed files
with
343 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// Cancellable.swift | ||
// SwiftTask | ||
// | ||
// Created by Yasuhiro Inami on 2015/05/09. | ||
// Copyright (c) 2015年 Yasuhiro Inami. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol Cancellable | ||
{ | ||
typealias Error | ||
|
||
// | ||
// NOTE: | ||
// Single `func cancel(error: Error) -> Bool` is preferred (as first implemented in 8a22ed5), | ||
// but two overloaded methods are required for SwiftTask ver 3.x API compatibility. | ||
// | ||
func cancel() -> Bool | ||
func cancel(#error: Error) -> Bool | ||
} | ||
|
||
public class Canceller: Cancellable | ||
{ | ||
private var cancelHandler: (Void -> Void)? | ||
|
||
public required init(cancelHandler: Void -> Void) | ||
{ | ||
self.cancelHandler = cancelHandler | ||
} | ||
|
||
public func cancel() -> Bool | ||
{ | ||
return self.cancel(error: ()) | ||
} | ||
|
||
public func cancel(#error: Void) -> Bool | ||
{ | ||
if let cancelHandler = self.cancelHandler { | ||
self.cancelHandler = nil | ||
cancelHandler() | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
} | ||
|
||
public class AutoCanceller: Canceller | ||
{ | ||
deinit | ||
{ | ||
self.cancel() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.