Skip to content

Commit

Permalink
Add convenience methods when dealing with ValueStore of Collections
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkenso committed Nov 29, 2024
1 parent 0f0764b commit 75d415c
Showing 1 changed file with 81 additions and 2 deletions.
83 changes: 81 additions & 2 deletions Sources/SpellbookFoundation/ValueObserving/ValueStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ public final class ValueStore<Value>: ValueObserving {
valueLock.withLock { currentValueGet[updateDepth > 0 ? updateDepth - 1 : 0] }
}

public func update(_ value: Value, context: Any? = nil) {
update(context: context) { $0 = value }
@discardableResult
public func update(_ value: Value, context: Any? = nil) -> Value {
update(context: context) { updateSwap(&$0, value) }
}

public func update<Property>(
Expand Down Expand Up @@ -222,3 +223,81 @@ extension ValueStore {
.init { self.value }
}
}

extension ValueStore {
public convenience init() where Value: ExpressibleByArrayLiteral {
self.init(initialValue: [])
}

public func append(_ element: Value.Element) where Value: RangeReplaceableCollection {
update { $0.append(element) }
}

public func removeAll(where shouldBeRemoved: (Value.Element) -> Bool) where Value: RangeReplaceableCollection {
update { $0.removeAll(where: shouldBeRemoved) }
}
}

extension ValueStore {
public convenience init<Element: Hashable>() where Value == Set<Element> {
self.init(initialValue: [])
}

@discardableResult
public func insert<Element: Hashable>(
_ element: Element
) -> (inserted: Bool, memberAfterInsert: Element) where Value == Set<Element> {
update { $0.insert(element) }
}

@discardableResult
public func remove<Element: Hashable>(_ element: Element) -> Element? where Value == Set<Element> {
update { $0.remove(element) }
}

public func popFirst<Element: Hashable>() -> Element? where Value == Set<Element> {
update { $0.popFirst() }
}

public func formUnion<Element: Hashable, S>(
_ other: S
) where Value == Set<Element>, S: Sequence, Element == S.Element {
update { $0.formUnion(other) }
}

public func subtract<Element: Hashable, S>(
_ other: S
) where Value == Set<Element>, S: Sequence, Element == S.Element {
update { $0.subtract(other) }
}

public func formIntersection<Element: Hashable, S>(
_ other: S
) where Value == Set<Element>, S: Sequence, Element == S.Element {
update { $0.formIntersection(other) }
}

public func formSymmetricDifference<Element: Hashable, S>(
_ other: S
) where Value == Set<Element>, S: Sequence, Element == S.Element {
update { $0.formSymmetricDifference(other) }
}
}

extension ValueStore {
public func popFirst<Key: Hashable, Element>() -> Value.Element? where Value == [Key: Element] {
update { $0.popFirst() }
}

@discardableResult
public func updateValue<Key: Hashable, Element>(
_ value: Element, forKey key: Key
) -> Element? where Value == [Key: Element] {
update { $0.updateValue(value, forKey: key) }
}

@discardableResult
public func removeValue<Key: Hashable, Element>(forKey key: Key) -> Element? where Value == [Key: Element] {
update { $0.removeValue(forKey: key) }
}
}

0 comments on commit 75d415c

Please sign in to comment.