Skip to content

Commit

Permalink
Provide type-safe indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
SzymonMrozek committed May 23, 2018
1 parent 3cdaa1e commit 4f30072
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
11 changes: 6 additions & 5 deletions Cedric/Cedric.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ public class Cedric {
///
/// - Returns: Currently under operation tasks
public func getActiveTasks() -> [URLSessionDownloadTask] {
return items.map{ $0.task }
return items
.map{ $0.task }
.compactMap { $0 }
}

Expand All @@ -168,15 +169,15 @@ public class Cedric {
item.delegate = self

let operation = BlockOperation(block: { [weak item] in
guard let strongItem = item else { return }

// prevent locking queue
guard item != nil else { return }
let semaphore = DispatchSemaphore(value: 0)

strongItem.completionBlock = { [weak semaphore] in
item?.completionBlock = { [weak semaphore] in
semaphore?.signal()
}

strongItem.resume()
item?.resume()
semaphore.wait()
})

Expand Down
16 changes: 3 additions & 13 deletions Cedric/ConcurrentArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,24 @@ extension ConcurrentArray {

// Mutable
extension ConcurrentArray {
func append( _ element: T) {
internal func append( _ element: T) {
queue.async(flags: .barrier) {
self.array.append(element)
}
}

func remove(where predicate: @escaping (T) -> Bool) {
internal func remove(where predicate: @escaping (T) -> Bool) {
queue.async(flags: .barrier) {
guard let index = self.array.index(where: predicate) else { return }
self.array.remove(at: index)
}
}

func removeAll() {
internal func removeAll() {
queue.async(flags: .barrier) {
self.array.removeAll()
}
}
}

extension ConcurrentArray {

internal subscript(index: Int) -> T? {
get {
Expand Down Expand Up @@ -120,10 +117,3 @@ extension ConcurrentArray where T: Equatable {
return result
}
}

extension ConcurrentArray {

internal static func +=(left: inout ConcurrentArray, right: T) {
left.append(right)
}
}

0 comments on commit 4f30072

Please sign in to comment.