Skip to content

Commit

Permalink
Add key observation registry
Browse files Browse the repository at this point in the history
  • Loading branch information
vadymmarkov committed Aug 6, 2018
1 parent 364d2ec commit 5e53be9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Cache.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@
D511464D2114775100197DCE /* StorageObservationRegistryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D511464A2114775100197DCE /* StorageObservationRegistryTests.swift */; };
D511464F21147B7C00197DCE /* ObservationTokenTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D511464E21147B7C00197DCE /* ObservationTokenTests.swift */; };
D511465121147B7C00197DCE /* ObservationTokenTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D511464E21147B7C00197DCE /* ObservationTokenTests.swift */; };
D51146532118337500197DCE /* KeyObservationRegistry.swift in Sources */ = {isa = PBXBuildFile; fileRef = D51146522118337500197DCE /* KeyObservationRegistry.swift */; };
D51146542118337500197DCE /* KeyObservationRegistry.swift in Sources */ = {isa = PBXBuildFile; fileRef = D51146522118337500197DCE /* KeyObservationRegistry.swift */; };
D51146552118337500197DCE /* KeyObservationRegistry.swift in Sources */ = {isa = PBXBuildFile; fileRef = D51146522118337500197DCE /* KeyObservationRegistry.swift */; };
D5291D1D1C2837DB00B702C9 /* Cache.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D5DC59E01C20593E003BD79B /* Cache.framework */; };
D5291D6A1C283B5400B702C9 /* Cache.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D5291D601C283B5300B702C9 /* Cache.framework */; };
D5291D851C283C7C00B702C9 /* TestHelper+OSX.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5291D811C283C7000B702C9 /* TestHelper+OSX.swift */; };
Expand Down Expand Up @@ -225,6 +228,7 @@
D2D4CC271FA342CA00E4A2D5 /* JSONWrapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONWrapperTests.swift; sourceTree = "<group>"; };
D511464A2114775100197DCE /* StorageObservationRegistryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageObservationRegistryTests.swift; sourceTree = "<group>"; };
D511464E21147B7C00197DCE /* ObservationTokenTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObservationTokenTests.swift; sourceTree = "<group>"; };
D51146522118337500197DCE /* KeyObservationRegistry.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyObservationRegistry.swift; sourceTree = "<group>"; };
D5291CDF1C28374800B702C9 /* TestHelper+iOS.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "TestHelper+iOS.swift"; sourceTree = "<group>"; };
D5291D181C2837DB00B702C9 /* Cache-iOS-Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Cache-iOS-Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
D5291D231C28380100B702C9 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -369,6 +373,7 @@
D270148720D11040003B45C7 /* Storage+Transform.swift */,
D5A9D1BE21134776005DBD3F /* StoreChange.swift */,
D5A9D1C221144B65005DBD3F /* StorageObservationRegistry.swift */,
D51146522118337500197DCE /* KeyObservationRegistry.swift */,
);
path = Storage;
sourceTree = "<group>";
Expand Down Expand Up @@ -841,6 +846,7 @@
D5A138C21EB29BFA00881A20 /* UIImage+Extensions.swift in Sources */,
D21B66851F6A723C00125DE1 /* DataSerializer.swift in Sources */,
D270147A20D1046A003B45C7 /* HybridStorage.swift in Sources */,
D51146552118337500197DCE /* KeyObservationRegistry.swift in Sources */,
D270148620D10E76003B45C7 /* AsyncStorage.swift in Sources */,
D270149620D125AC003B45C7 /* MemoryCapsule.swift in Sources */,
D28897071F8B79B300C61DEE /* JSONDecoder+Extensions.swift in Sources */,
Expand Down Expand Up @@ -929,6 +935,7 @@
D5A138C41EB29C2100881A20 /* NSImage+Extensions.swift in Sources */,
D21B667C1F6A723C00125DE1 /* DataSerializer.swift in Sources */,
D270147920D1046A003B45C7 /* HybridStorage.swift in Sources */,
D51146542118337500197DCE /* KeyObservationRegistry.swift in Sources */,
D270148520D10E76003B45C7 /* AsyncStorage.swift in Sources */,
D270149520D125AC003B45C7 /* MemoryCapsule.swift in Sources */,
D28897061F8B79B300C61DEE /* JSONDecoder+Extensions.swift in Sources */,
Expand Down Expand Up @@ -982,6 +989,7 @@
D270147820D1046A003B45C7 /* HybridStorage.swift in Sources */,
D270148420D10E76003B45C7 /* AsyncStorage.swift in Sources */,
D28897051F8B79B300C61DEE /* JSONDecoder+Extensions.swift in Sources */,
D51146532118337500197DCE /* KeyObservationRegistry.swift in Sources */,
D270148020D10982003B45C7 /* Storage.swift in Sources */,
D270149420D125AC003B45C7 /* MemoryCapsule.swift in Sources */,
D2CF98611F694FFA00CE8F68 /* MemoryConfig.swift in Sources */,
Expand Down
38 changes: 38 additions & 0 deletions Source/Shared/Storage/KeyObservationRegistry.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Foundation

public enum KeyChange<T> {
case edit(before: T?, after: T?)
case remove
}

public final class KeyObservationRegistry<Storage: StorageAware> {
public typealias Observation = (Storage, KeyChange<Storage.T>) -> Void
private(set) var observations = [String: Observation]()

@discardableResult
public func addObservation(_ observation: @escaping Observation, forKey key: String) -> ObservationToken {
observations[key] = observation

return ObservationToken { [weak self] in
self?.observations.removeValue(forKey: key)
}
}

public func removeObservation(forKey key: String) {
observations.removeValue(forKey: key)
}

public func removeObservation(token: ObservationToken) {
token.cancel()
}

public func removeAllObservations() {
observations.removeAll()
}

func notifyObservers(about change: KeyChange<Storage.T>, in storage: Storage) {
observations.values.forEach { closure in
closure(storage, change)
}
}
}
6 changes: 3 additions & 3 deletions Source/Shared/Storage/StorageObservationRegistry.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

public final class StorageObservationRegistry<T: StorageAware> {
public typealias Observation = (T, StorageChange) -> Void
public final class StorageObservationRegistry<Storage: StorageAware> {
public typealias Observation = (Storage, StorageChange) -> Void
private(set) var observations = [UUID: Observation]()

@discardableResult
Expand All @@ -22,7 +22,7 @@ public final class StorageObservationRegistry<T: StorageAware> {
observations.removeAll()
}

func notifyObservers(about change: StorageChange, in storage: T) {
func notifyObservers(about change: StorageChange, in storage: Storage) {
observations.values.forEach { closure in
closure(storage, change)
}
Expand Down

0 comments on commit 5e53be9

Please sign in to comment.