-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Simon Brockmann
committed
May 7, 2019
1 parent
8208d46
commit a9a73a1
Showing
11 changed files
with
730 additions
and
107 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,132 @@ | ||
// | ||
// CancellationToken.swift | ||
// BoltsSwift | ||
// | ||
// Copyright © 2019 Facebook. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public typealias CancellationObserver = () -> Void | ||
|
||
public final class CancellationToken { | ||
public var cancellationRequested: Bool { | ||
get { | ||
return synchronizationQueue.sync(flags: .barrier) { () -> Bool in | ||
return _cancellationRequested | ||
|
||
} | ||
} | ||
} | ||
|
||
private let synchronizationQueue = DispatchQueue(label: "com.bolts.cancellationToken", attributes: DispatchQueue.Attributes.concurrent) | ||
private var _cancellationRequested: Bool | ||
private var _regestrations = [CancellationTokenRegistration]() | ||
private var _disposed: Bool | ||
private var _cancelDelayedWorkItem: DispatchWorkItem? | ||
|
||
public init() { | ||
_cancellationRequested = false | ||
_disposed = false | ||
} | ||
|
||
@discardableResult | ||
public func registerCancellationObserver(observer: @escaping CancellationObserver) -> CancellationTokenRegistration? { | ||
return synchronizationQueue.sync(flags: .barrier) { () -> CancellationTokenRegistration? in | ||
if _disposed { | ||
return nil | ||
} | ||
let registration = CancellationTokenRegistration.registrationWithToken(token: self, observer: observer) | ||
_regestrations.append(registration) | ||
return registration | ||
} | ||
} | ||
|
||
internal func unrigsterRegistration(registration: CancellationTokenRegistration) throws { | ||
try synchronizationQueue.sync(flags: .barrier) { () -> Void in | ||
try throwIfDisposed() | ||
if let index = _regestrations.firstIndex(where: { $0 === registration }) { | ||
_regestrations.remove(at: index) | ||
} | ||
} | ||
} | ||
|
||
internal func cancel() throws { | ||
var registrations: [CancellationTokenRegistration]? | ||
try synchronizationQueue.sync(flags: .barrier) { () -> Void in | ||
try throwIfDisposed() | ||
if _cancellationRequested { | ||
return | ||
} | ||
if let cancelWorkItem = _cancelDelayedWorkItem { | ||
cancelWorkItem.cancel() | ||
_cancelDelayedWorkItem = nil | ||
} | ||
_cancellationRequested = true | ||
registrations = [CancellationTokenRegistration](_regestrations) | ||
} | ||
if registrations == nil { | ||
return | ||
} | ||
try notifyCancellation(registrations: registrations!) | ||
} | ||
|
||
private func notifyCancellation(registrations: [CancellationTokenRegistration]) throws { | ||
for registration in registrations { | ||
try registration.notifyDelegate() | ||
} | ||
} | ||
|
||
internal func cancelAfterInterval(interval: TimeInterval) throws { | ||
try throwIfDisposed() | ||
|
||
if interval < 0 { | ||
throw IntervalError() | ||
} | ||
|
||
if interval == 0 { | ||
try cancel() | ||
return | ||
} | ||
|
||
try synchronizationQueue.sync(flags: .barrier) { () -> Void in | ||
try throwIfDisposed() | ||
if let cancelWorkItem = _cancelDelayedWorkItem { | ||
cancelWorkItem.cancel() | ||
_cancelDelayedWorkItem = nil | ||
} | ||
if _cancellationRequested { | ||
return | ||
} | ||
_cancelDelayedWorkItem = DispatchWorkItem { [weak self] in | ||
try? self?.cancel() | ||
} | ||
DispatchQueue.global().asyncAfter(deadline: .now() + interval, execute: _cancelDelayedWorkItem!) | ||
} | ||
} | ||
|
||
internal func dispose() throws { | ||
var registrations: [CancellationTokenRegistration]? | ||
synchronizationQueue.sync(flags: .barrier) { () -> Void in | ||
if _disposed { | ||
return | ||
} | ||
registrations = [CancellationTokenRegistration](_regestrations) | ||
_regestrations.removeAll() | ||
} | ||
if registrations != nil { | ||
try registrations!.forEach { | ||
try $0.dispose() | ||
} | ||
} | ||
synchronizationQueue.sync(flags: .barrier, execute: { () -> Void in | ||
_disposed = true | ||
}) | ||
} | ||
|
||
private func throwIfDisposed() throws { | ||
if _disposed { | ||
throw DisposedError() | ||
} | ||
} | ||
} |
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,52 @@ | ||
// | ||
// CancellationTokenRegistration.swift | ||
// BoltsSwift | ||
// | ||
// Copyright © 2019 Facebook. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public final class CancellationTokenRegistration { | ||
private var _disposed: Bool | ||
private let _synchronizationQueue = DispatchQueue(label: "com.bolts.cancellationTokenRegistration", attributes: DispatchQueue.Attributes.concurrent) | ||
private var _observer: CancellationObserver? | ||
private weak var _token: CancellationToken? | ||
|
||
private init(token: CancellationToken, observer: @escaping CancellationObserver) { | ||
_disposed = false | ||
_observer = observer | ||
_token = token | ||
} | ||
|
||
public class func registrationWithToken(token: CancellationToken, observer: @escaping CancellationObserver) -> CancellationTokenRegistration { | ||
return CancellationTokenRegistration(token: token, observer: observer) | ||
} | ||
|
||
public func dispose() throws { | ||
_synchronizationQueue.sync(flags: .barrier) { () -> Void in | ||
if _disposed { | ||
return | ||
} | ||
_disposed = true | ||
} | ||
if let token = _token { | ||
try token.unrigsterRegistration(registration: self) | ||
_token = nil | ||
} | ||
_observer = nil | ||
} | ||
|
||
internal func notifyDelegate() throws { | ||
try _synchronizationQueue.sync(flags: .barrier) { () -> Void in | ||
try throwIfDisposed() | ||
_observer?() | ||
} | ||
} | ||
|
||
private func throwIfDisposed() throws { | ||
if _disposed { | ||
throw DisposedError() | ||
} | ||
} | ||
} |
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,36 @@ | ||
// | ||
// CancellationTokenSource.swift | ||
// BoltsSwift | ||
// | ||
// Copyright © 2019 Facebook. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public final class CancellationTokenSource { | ||
public private(set) var token: CancellationToken | ||
|
||
public init() { | ||
token = CancellationToken() | ||
} | ||
|
||
public class func cancellationTokenSource() -> CancellationTokenSource { | ||
return CancellationTokenSource() | ||
} | ||
|
||
public func isCancellationRequested() -> Bool { | ||
return token.cancellationRequested | ||
} | ||
|
||
public func cancel() throws { | ||
try token.cancel() | ||
} | ||
|
||
public func cancelAfterInterval(interval: TimeInterval) throws { | ||
try token.cancelAfterInterval(interval: interval) | ||
} | ||
|
||
public func dispose() throws { | ||
try token.dispose() | ||
} | ||
} |
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
Oops, something went wrong.