forked from hyperoslo/Cache
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request hyperoslo#201 from hyperoslo/feature/storage-obser…
…vations Feature: storage observations
- Loading branch information
Showing
10 changed files
with
242 additions
and
4 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,11 @@ | ||
public final class ObservationToken { | ||
private let cancellationClosure: () -> Void | ||
|
||
init(cancellationClosure: @escaping () -> Void) { | ||
self.cancellationClosure = cancellationClosure | ||
} | ||
|
||
public func cancel() { | ||
cancellationClosure() | ||
} | ||
} |
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
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,30 @@ | ||
import Foundation | ||
|
||
public final class StorageObservationRegistry<T: StorageAware> { | ||
public typealias Observation = (T, StorageChange) -> Void | ||
private(set) var observations = [UUID: Observation]() | ||
|
||
@discardableResult | ||
public func addObservation(_ observation: @escaping Observation) -> ObservationToken { | ||
let id = UUID() | ||
observations[id] = observation | ||
|
||
return ObservationToken { [weak self] in | ||
self?.observations.removeValue(forKey: id) | ||
} | ||
} | ||
|
||
public func removeObservation(token: ObservationToken) { | ||
token.cancel() | ||
} | ||
|
||
public func removeAllObservations() { | ||
observations.removeAll() | ||
} | ||
|
||
func notifyObservers(about change: StorageChange, in storage: T) { | ||
observations.values.forEach { closure in | ||
closure(storage, change) | ||
} | ||
} | ||
} |
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,17 @@ | ||
public enum StorageChange: Equatable { | ||
case add(key: String) | ||
case remove(key: String) | ||
case removeAll | ||
case removeExpired | ||
} | ||
|
||
public func == (lhs: StorageChange, rhs: StorageChange) -> Bool { | ||
switch (lhs, rhs) { | ||
case (.add(let key1), .add(let key2)), (.remove(let key1), .remove(let key2)): | ||
return key1 == key2 | ||
case (.removeAll, .removeAll), (.removeExpired, .removeExpired): | ||
return true | ||
default: | ||
return false | ||
} | ||
} |
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,15 @@ | ||
import XCTest | ||
@testable import Cache | ||
|
||
final class ObservationTokenTests: XCTestCase { | ||
func testCancel() { | ||
var cancelled = false | ||
|
||
let token = ObservationToken { | ||
cancelled = true | ||
} | ||
|
||
token.cancel() | ||
XCTAssertTrue(cancelled) | ||
} | ||
} |
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.