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

Replace Set to Dictionary #624

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions YepKit/UserDefaults/YepUserDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ final public class Listenable<T> {
didSet {
setterAction(value)

for listener in listenerSet {
for listener in listeners.values {
listener.action(value)
}
}
Expand All @@ -70,13 +70,12 @@ final public class Listenable<T> {
public typealias SetterAction = (T) -> Void
var setterAction: SetterAction

var listenerSet = Set<Listener<T>>()
var listeners: [String: Listener<T>] = [:]

public func bindListener(_ name: String, action: @escaping Listener<T>.Action) {
removeListenerWithName(name)

let listener = Listener(name: name, action: action)
listenerSet.insert(listener)
listeners[name] = listener
}

public func bindAndFireListener(_ name: String, action: @escaping Listener<T>.Action) {
Expand All @@ -86,16 +85,11 @@ final public class Listenable<T> {
}

public func removeListenerWithName(_ name: String) {
for listener in listenerSet {
if listener.name == name {
listenerSet.remove(listener)
break
}
}
listeners[name] = nil
}

public func removeAllListeners() {
listenerSet.removeAll(keepingCapacity: false)
listeners.removeAll(keepingCapacity: false)
}

public init(_ v: T, setterAction action: @escaping SetterAction) {
Expand Down