Skip to content

Refactor constraints #282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Sources/SwiftQueue/Constraint+Charging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ internal final class BatteryChargingConstraint: JobConstraint {
func willRun(operation: SqOperation) throws {}

func run(operation: SqOperation) -> Bool {
guard operation.info.requireCharging else {
return true
}

guard UIDevice.current.batteryState != .charging else {
return true
}
Expand Down
14 changes: 8 additions & 6 deletions Sources/SwiftQueue/Constraint+Deadline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ import Foundation

internal final class DeadlineConstraint: JobConstraint {

private let deadline: Date

init(deadline: Date) {
self.deadline = deadline
}

func willSchedule(queue: SqOperationQueue, operation: SqOperation) throws {
try check(operation: operation)
}
Expand All @@ -33,11 +39,7 @@ internal final class DeadlineConstraint: JobConstraint {
}

func run(operation: SqOperation) -> Bool {
guard let delay = operation.info.deadline else {
return true
}

operation.dispatchQueue.runAfter(delay.timeIntervalSinceNow, callback: { [weak operation] in
operation.dispatchQueue.runAfter(deadline.timeIntervalSinceNow, callback: { [weak operation] in
guard let ope = operation else { return }
guard !ope.isFinished else { return }

Expand All @@ -47,7 +49,7 @@ internal final class DeadlineConstraint: JobConstraint {
}

private func check(operation: SqOperation) throws {
if let deadline = operation.info.deadline, deadline < Date() {
if deadline < Date() {
throw SwiftQueueError.deadline
}
}
Expand Down
11 changes: 6 additions & 5 deletions Sources/SwiftQueue/Constraint+Delay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ import Foundation

internal final class DelayConstraint: JobConstraint {

private let delay: TimeInterval

init(delay: TimeInterval) {
self.delay = delay
}

func willSchedule(queue: SqOperationQueue, operation: SqOperation) throws {
// Nothing to do
}
Expand All @@ -33,11 +39,6 @@ internal final class DelayConstraint: JobConstraint {
}

func run(operation: SqOperation) -> Bool {
guard let delay = operation.info.delay else {
// No delay run immediately
return true
}

let epoch = Date().timeIntervalSince(operation.info.createTime)
guard epoch < delay else {
// Epoch already greater than delay
Expand Down
10 changes: 6 additions & 4 deletions Sources/SwiftQueue/Constraint+Timeout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ import Foundation

internal final class TimeoutConstraint: JobConstraint {

private let timeout: TimeInterval

init(timeout: TimeInterval) {
self.timeout = timeout
}

func willSchedule(queue: SqOperationQueue, operation: SqOperation) throws {
// Nothing to do
}
Expand All @@ -33,10 +39,6 @@ internal final class TimeoutConstraint: JobConstraint {
}

func run(operation: SqOperation) -> Bool {
guard let timeout = operation.info.timeout else {
return true
}

operation.dispatchQueue.runAfter(timeout) {
if operation.isExecuting && !operation.isFinished {
operation.cancel(with: SwiftQueueError.timeout)
Expand Down
12 changes: 6 additions & 6 deletions Sources/SwiftQueue/JobInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,20 @@ public struct JobInfo {
constraints.append(BatteryChargingConstraint())
}

if deadline != nil {
constraints.append(DeadlineConstraint())
if let deadline = deadline {
constraints.append(DeadlineConstraint(deadline: deadline))
}

if delay != nil {
constraints.append(DelayConstraint())
if let delay = delay {
constraints.append(DelayConstraint(delay: delay))
}

if requireNetwork != NetworkType.any {
constraints.append(NetworkConstraint())
}

if timeout != nil {
constraints.append(TimeoutConstraint())
if let timeout = timeout {
constraints.append(TimeoutConstraint(timeout: timeout))
}

return constraints
Expand Down