-
-
Notifications
You must be signed in to change notification settings - Fork 333
/
Invocations.swift
64 lines (52 loc) · 1.33 KB
/
Invocations.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import Foundation
/**
* For recording invocations of methods in a list in a thread safe manner.
*/
public class Invocations<T> {
public init() {}
private let queue = DispatchQueue(label: "Invocations")
private var _invocations: [T] = []
public var invocations: [T] {
return queue.sync {
return self._invocations
}
}
public var count: Int {
return queue.sync {
return self._invocations.count
}
}
public var first: T? {
return queue.sync {
return self._invocations.first
}
}
public var last: T? {
return queue.sync {
return self._invocations.last
}
}
public var isEmpty: Bool {
return queue.sync {
return self._invocations.isEmpty
}
}
public func get(_ index: Int) -> T? {
return queue.sync {
guard self._invocations.indices.contains(index) else {
return nil
}
return self._invocations[index]
}
}
public func record(_ invocation: T) {
queue.async {
self._invocations.append(invocation)
}
}
public func removeAll() {
queue.async {
self._invocations.removeAll()
}
}
}