Skip to content

Instantly share code, notes, and snippets.

@erikolsson
Last active September 30, 2022 04:28
Show Gist options
  • Save erikolsson/02c822cb7ef502a1768f90767a6f3c71 to your computer and use it in GitHub Desktop.
Save erikolsson/02c822cb7ef502a1768f90767a6f3c71 to your computer and use it in GitHub Desktop.
// Copyright (c) 2021 Manuel Maly
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import Foundation
@dynamicMemberLookup
public struct SharedState<Local, Shared> {
public var local: Local
public var shared: Shared
public init(local: Local, shared: Shared) {
self.local = local
self.shared = shared
}
public subscript<T>(
dynamicMember keyPath: WritableKeyPath<Local, T>
) -> T {
get { local[keyPath: keyPath] }
set { local[keyPath: keyPath] = newValue }
}
public subscript<T>(
dynamicMember keyPath: KeyPath<Local, T>
) -> T {
local[keyPath: keyPath]
}
public subscript<T>(
dynamicMember keyPath: WritableKeyPath<Shared, T>
) -> T {
self.shared[keyPath: keyPath]
}
}
public extension SharedState {
var optional: SharedState? {
get { self }
set {
if let available = newValue {
self = available
}
}
}
}
public extension SharedState {
var scoped: Scoped {
get { Scoped(base: self) }
set { local = newValue.base.local; shared = newValue.base.shared }
}
@dynamicMemberLookup
struct Scoped {
var base: SharedState
public subscript<T>(
dynamicMember keyPath: WritableKeyPath<Local, T>
) -> SharedState<T, Shared> {
get { .init(local: base.local[keyPath: keyPath], shared: base.shared) }
set {
base.local[keyPath: keyPath] = newValue.local;
base.shared = newValue.shared
}
}
public subscript<T>(
dynamicMember keyPath: KeyPath<Local, T>
) -> SharedState<T, Shared> {
.init(local: base.local[keyPath: keyPath], shared: base.shared)
}
public subscript<T>(
dynamicMember keyPath: KeyPath<Local, T>
) -> SharedState<T, Shared>? {
.init(local: base.local[keyPath: keyPath], shared: base.shared)
}
}
}
extension SharedState: Equatable where Shared: Equatable, Local: Equatable {}
extension SharedState: Hashable where Shared: Hashable, Local: Hashable {}
extension SharedState: Encodable where Shared: Encodable, Local: Encodable {}
extension SharedState: Decodable where Shared: Decodable, Local: Decodable {}
public typealias S = SharedState
public protocol SharedStateProviding {}
@dynamicMemberLookup
public struct SharedStateProvidingScope<Local> {
var base: Local
public subscript<Shared>(
dynamicMember keyPath: WritableKeyPath<Local, Shared>
) -> SharedState<Local, Shared> {
get { .init(local: base, shared: base[keyPath: keyPath]) }
set {
base = newValue.local;
base[keyPath: keyPath] = newValue.shared
}
}
}
public extension SharedStateProviding {
var share: SharedStateProvidingScope<Self> {
get { SharedStateProvidingScope(base: self) }
set { self = newValue.base }
}
}
public extension SharedState where Local: OptionalType {
var unwrap: SharedState<Local.Wrapped, Shared>? {
get { local.map { .init(local: $0, shared: shared) } }
set {
if let value = newValue?.local {
local = Local(value)
}
}
}
}
public protocol OptionalType {
associatedtype Wrapped
init(_ some: Wrapped)
func map<U>(_ transform: (Wrapped) throws -> U) rethrows -> U?
}
extension Optional: OptionalType {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment