|
| 1 | +import FirebaseFirestore |
| 2 | +import Firebase |
| 3 | + |
| 4 | +public class FirebaseDistributedCounter { |
| 5 | + private let db: Firestore |
| 6 | + private let shardId = UUID().uuidString |
| 7 | + private var shards = [String: Double]() |
| 8 | + private let collectionId = "_counter_shards_" |
| 9 | + private let shardRef: CollectionReference |
| 10 | + private var docRef: DocumentReference |
| 11 | + private var field: String |
| 12 | + |
| 13 | + public init(docRef: DocumentReference, field: String) { |
| 14 | + self.docRef = docRef |
| 15 | + self.field = field |
| 16 | + db = Firestore.firestore() |
| 17 | + shardRef = docRef.collection(collectionId) |
| 18 | + shards[self.docRef.path] = 0 |
| 19 | + shards[shardRef.document(String(shardId)).path] = 0 |
| 20 | + shards[shardRef.document("\t" + String(shardId.prefix(4))).path] = 0 |
| 21 | + shards[shardRef.document("\t\t" + String(shardId.prefix(3))).path] = 0 |
| 22 | + shards[shardRef.document("\t\t\t" + String(shardId.prefix(2))).path] = 0 |
| 23 | + shards[shardRef.document("\t\t\t\t" + String(shardId.prefix(1))).path] = 0 |
| 24 | + } |
| 25 | + |
| 26 | + public func get(result: @escaping (Double?, Error?) -> Void) { |
| 27 | + var documentIds = [String]() |
| 28 | + |
| 29 | + for key in shards.keys { |
| 30 | + documentIds.append((key as NSString).lastPathComponent) |
| 31 | + } |
| 32 | + |
| 33 | + shardRef.whereField(FieldPath.documentID(), in: documentIds).getDocuments() { snapshot, error in |
| 34 | + if let error = error { |
| 35 | + print("FirestoreCounter: Error getting documents: \(error)") |
| 36 | + result(nil, error) |
| 37 | + } else { |
| 38 | + if let documents = snapshot?.documents { |
| 39 | + let values = documents.map { (documentSnap) -> Double in |
| 40 | + let document = documentSnap.data() |
| 41 | + if let amount = document[self.field] as? Double { |
| 42 | + return amount |
| 43 | + } else { |
| 44 | + return 0.0 |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + let sum = values.reduce(0.0) { $0 + $1 } |
| 49 | + result(sum, nil) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public func increment(by delta: Double) { |
| 56 | + let increment = FieldValue.increment(delta) |
| 57 | + var array = field.components(separatedBy: ".") |
| 58 | + var update = [String: Any]() |
| 59 | + |
| 60 | + array = array.reversed() |
| 61 | + |
| 62 | + for component in array { |
| 63 | + if (update.isEmpty) { |
| 64 | + update = [component: increment] |
| 65 | + } else { |
| 66 | + update = [component: update] |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + shardRef.document(shardId).setData(update, merge: true) |
| 71 | + } |
| 72 | + |
| 73 | + public func onSnapshot(_ listener: @escaping (Double?, Error?) -> Void) { |
| 74 | + shards.keys.forEach { path in |
| 75 | + db.document(path).addSnapshotListener { snapshot, error in |
| 76 | + if let error = error { |
| 77 | + print("FirestoreCounter: Error listening to document changes: \(error)") |
| 78 | + listener(nil, error) |
| 79 | + } else { |
| 80 | + guard let snapshot = snapshot, |
| 81 | + let snapshotValue = snapshot.get(self.field), |
| 82 | + let doubleVal = snapshotValue as? Double else { |
| 83 | + listener(nil, error) |
| 84 | + return |
| 85 | + } |
| 86 | + self.shards[path] = doubleVal |
| 87 | + let sum = self.shards.reduce(0.0) { $0 + $1.value } |
| 88 | + listener(sum, nil) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments