Skip to content

Commit

Permalink
Rename functions in StorageObservationRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
vadymmarkov committed Aug 3, 2018
1 parent cb85b99 commit 7c003de
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
10 changes: 5 additions & 5 deletions Source/Shared/Storage/HybridStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Foundation
public final class HybridStorage<T> {
public let memoryStorage: MemoryStorage<T>
public let diskStorage: DiskStorage<T>
public let registry = StorageObservationRegistry<HybridStorage>()
public let storageObservationRegistry = StorageObservationRegistry<HybridStorage>()

public init(memoryStorage: MemoryStorage<T>, diskStorage: DiskStorage<T>) {
self.memoryStorage = memoryStorage
Expand All @@ -27,25 +27,25 @@ extension HybridStorage: StorageAware {
public func removeObject(forKey key: String) throws {
memoryStorage.removeObject(forKey: key)
try diskStorage.removeObject(forKey: key)
registry.notifyObservers(about: .singleDeletion, in: self)
storageObservationRegistry.notifyObservers(about: .singleDeletion, in: self)
}

public func setObject(_ object: T, forKey key: String, expiry: Expiry? = nil) throws {
memoryStorage.setObject(object, forKey: key, expiry: expiry)
try diskStorage.setObject(object, forKey: key, expiry: expiry)
registry.notifyObservers(about: .addition, in: self)
storageObservationRegistry.notifyObservers(about: .addition, in: self)
}

public func removeAll() throws {
memoryStorage.removeAll()
try diskStorage.removeAll()
registry.notifyObservers(about: .allDeletion, in: self)
storageObservationRegistry.notifyObservers(about: .allDeletion, in: self)
}

public func removeExpiredObjects() throws {
memoryStorage.removeExpiredObjects()
try diskStorage.removeExpiredObjects()
registry.notifyObservers(about: .expiredDeletion, in: self)
storageObservationRegistry.notifyObservers(about: .expiredDeletion, in: self)
}
}

Expand Down
6 changes: 3 additions & 3 deletions Source/Shared/Storage/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public final class Storage<T> {
let syncStorage: SyncStorage<T>
let asyncStorage: AsyncStorage<T>

public let registry = StorageObservationRegistry<Storage>()
public let storageObservationRegistry = StorageObservationRegistry<Storage>()

/// Initialize storage with configuration options.
///
Expand Down Expand Up @@ -54,9 +54,9 @@ public final class Storage<T> {
}

private func subscribeToChanges(in storage: HybridStorage<T>) {
storage.registry.register { [weak self] _, change in
storage.storageObservationRegistry.addObservation { [weak self] _, change in
guard let strongSelf = self else { return }
self?.registry.notifyObservers(about: change, in: strongSelf)
self?.storageObservationRegistry.notifyObservers(about: change, in: strongSelf)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Source/Shared/Storage/StorageObservationRegistry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public final class StorageObservationRegistry<T: StorageAware> {
private(set) var observations = [UUID: Observation]()

@discardableResult
public func register(observation: @escaping Observation) -> ObservationToken {
public func addObservation(_ observation: @escaping Observation) -> ObservationToken {
let id = UUID()
observations[id] = observation

Expand All @@ -14,11 +14,11 @@ public final class StorageObservationRegistry<T: StorageAware> {
}
}

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

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

Expand Down
4 changes: 2 additions & 2 deletions Tests/iOS/Tests/Storage/HybridStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ final class HybridStorageTests: XCTestCase {
}
}

func testRegisterObservations() throws {
func testAddObservations() throws {
var changes = [StorageChange]()

storage.registry.register { storage, change in
storage.storageObservationRegistry.addObservation { storage, change in
changes.append(change)
}

Expand Down
24 changes: 12 additions & 12 deletions Tests/iOS/Tests/Storage/StorageObservationRegistryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,40 @@ final class StorageObservationRegistryTests: XCTestCase {
)
}

func testRegister() {
registry.register { _, _ in }
func testAddObservation() {
registry.addObservation { _, _ in }
XCTAssertEqual(registry.observations.count, 1)

registry.register { _, _ in }
registry.addObservation { _, _ in }
XCTAssertEqual(registry.observations.count, 2)
}

func testDeregister() {
let token = registry.register { _, _ in }
func testRemoveObservation() {
let token = registry.addObservation { _, _ in }
XCTAssertEqual(registry.observations.count, 1)

registry.deregister(token: token)
registry.removeObservation(token: token)
XCTAssertTrue(registry.observations.isEmpty)
}

func testDeregisterAll() {
registry.register { _, _ in }
registry.register { _, _ in }
func testRemoveAllObservation() {
registry.addObservation { _, _ in }
registry.addObservation { _, _ in }
XCTAssertEqual(registry.observations.count, 2)

registry.deregisterAll()
registry.removeAllObservations()
XCTAssertTrue(registry.observations.isEmpty)
}

func testNotifyObservers() {
var change1: StorageChange?
var change2: StorageChange?

registry.register { _, change in
registry.addObservation { _, change in
change1 = change
}

registry.register { _, change in
registry.addObservation { _, change in
change2 = change
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/iOS/Tests/Storage/StorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ final class StorageTests: XCTestCase {
XCTAssertEqual(cachedObject.firstName, "John")
}

func testRegisterObservations() throws {
func testAddObservations() throws {
var changes = [StorageChange]()

storage.registry.register { storage, change in
storage.storageObservationRegistry.addObservation { storage, change in
changes.append(change)
}

Expand Down

0 comments on commit 7c003de

Please sign in to comment.