Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: observations #202

Merged
merged 11 commits into from
Aug 9, 2018
Merged

Feature: observations #202

merged 11 commits into from
Aug 9, 2018

Conversation

vadymmarkov
Copy link
Contributor

I refactored implementation so the API lets you pass any object as an observer, while also passing an observation closure. The observation closure will be removed automatically when the weekly captured observer has been deallocated. You can always remove observer manually by cancelling the token.

Storage observations

// Add observer
let token = storage.addStorageObserver(self) { observer, storage, change
  switch change {
  case .add(let key):
    print("Added \(key)")
  case .remove(let key):
    print("Removed \(key)")
  case .removeAll:
    print("Removed all")
  case .removeExpired:
    print("Removed expired")
  }
}

// Remove observer
token.cancel()

// Remove all observers
storage.removeAllStorageObservers()

Key observations

let key = "user1"

let token = storage.addObserver(self, forKey: key) { observer, storage, change in
  switch change {
  case .edit(let before, let after):
    print("Changed object for \(key) from \(String(describing: before)) to \(after)")
  case .remove:
    print("Removed \(key)")
  }
}

// Remove observer by token
token.cancel()

// Remove observer for key
storage.removeObserver(forKey: "user1")

// Remove all observers
storage.removeAllKeyObservers()

@vadymmarkov vadymmarkov requested a review from onmyway133 August 7, 2018 14:27
@vadymmarkov
Copy link
Contributor Author

Ping @RomainBoulay

@onmyway133
Copy link
Contributor

Great job @vadymmarkov 🚀

@vadymmarkov vadymmarkov merged commit 35d8eea into master Aug 9, 2018
@vadymmarkov vadymmarkov deleted the feature/observations branch August 9, 2018 10:19
@RomainBoulay
Copy link

👍👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants