2020-04-11 19:33:39 +00:00
|
|
|
//
|
|
|
|
// LazyDecoding.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 4/11/20.
|
|
|
|
// Copyright © 2020 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
private let decoder = PropertyListDecoder()
|
|
|
|
private let encoder = PropertyListEncoder()
|
|
|
|
|
|
|
|
@propertyWrapper
|
2022-12-15 00:46:02 +00:00
|
|
|
public struct LazilyDecoding<Enclosing: NSObject, Value: Codable> {
|
2020-04-11 19:33:39 +00:00
|
|
|
|
|
|
|
private let keyPath: ReferenceWritableKeyPath<Enclosing, Data?>
|
|
|
|
private let fallback: Value
|
|
|
|
private var value: Value?
|
2022-12-15 00:46:02 +00:00
|
|
|
private var observation: NSKeyValueObservation?
|
2023-01-28 18:41:22 +00:00
|
|
|
private var skipClearingOnNextUpdate = false
|
2020-04-11 19:33:39 +00:00
|
|
|
|
|
|
|
init(from keyPath: ReferenceWritableKeyPath<Enclosing, Data?>, fallback: Value) {
|
|
|
|
self.keyPath = keyPath
|
|
|
|
self.fallback = fallback
|
|
|
|
}
|
|
|
|
|
2020-05-06 22:40:12 +00:00
|
|
|
public var wrappedValue: Value {
|
2020-04-11 19:33:39 +00:00
|
|
|
get { fatalError("called LazilyDecoding wrappedValue getter") }
|
|
|
|
set { fatalError("called LazilyDecoding wrappedValue setter") }
|
|
|
|
}
|
|
|
|
|
2020-05-06 22:40:12 +00:00
|
|
|
public static subscript(_enclosingInstance instance: Enclosing, wrapped wrappedKeyPath: ReferenceWritableKeyPath<Enclosing, Value>, storage storageKeyPath: ReferenceWritableKeyPath<Enclosing, Self>) -> Value {
|
2020-04-11 19:33:39 +00:00
|
|
|
get {
|
|
|
|
var wrapper = instance[keyPath: storageKeyPath]
|
|
|
|
if let value = wrapper.value {
|
|
|
|
return value
|
|
|
|
} else {
|
|
|
|
guard let data = instance[keyPath: wrapper.keyPath] else { return wrapper.fallback }
|
|
|
|
do {
|
2023-01-28 18:41:22 +00:00
|
|
|
let value = try decoder.decode(Box.self, from: data)
|
2020-11-11 17:44:57 +00:00
|
|
|
wrapper.value = value.value
|
2022-12-15 00:46:02 +00:00
|
|
|
wrapper.observation = instance.observe(wrapper.keyPath, changeHandler: { instance, _ in
|
2023-01-28 18:41:22 +00:00
|
|
|
var wrapper = instance[keyPath: storageKeyPath]
|
|
|
|
if wrapper.skipClearingOnNextUpdate {
|
|
|
|
wrapper.skipClearingOnNextUpdate = false
|
|
|
|
} else {
|
|
|
|
wrapper.removeCachedValue()
|
|
|
|
}
|
|
|
|
instance[keyPath: storageKeyPath] = wrapper
|
2022-12-15 00:46:02 +00:00
|
|
|
})
|
2020-04-11 19:33:39 +00:00
|
|
|
instance[keyPath: storageKeyPath] = wrapper
|
2020-11-11 17:44:57 +00:00
|
|
|
return value.value
|
2020-04-11 19:33:39 +00:00
|
|
|
} catch {
|
|
|
|
return wrapper.fallback
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
var wrapper = instance[keyPath: storageKeyPath]
|
|
|
|
wrapper.value = newValue
|
2023-01-28 18:41:22 +00:00
|
|
|
wrapper.skipClearingOnNextUpdate = true
|
2020-04-11 19:33:39 +00:00
|
|
|
instance[keyPath: storageKeyPath] = wrapper
|
2020-11-11 17:44:57 +00:00
|
|
|
let newData = try! encoder.encode(Box(value: newValue))
|
2020-04-11 19:33:39 +00:00
|
|
|
instance[keyPath: wrapper.keyPath] = newData
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-28 18:41:22 +00:00
|
|
|
mutating func removeCachedValue() {
|
|
|
|
value = nil
|
|
|
|
observation = nil
|
|
|
|
}
|
|
|
|
|
2020-04-11 19:33:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension LazilyDecoding {
|
2022-06-07 03:58:43 +00:00
|
|
|
init<T>(arrayFrom keyPath: ReferenceWritableKeyPath<Enclosing, Data?>) where Value == [T] {
|
2022-05-13 21:33:07 +00:00
|
|
|
self.init(from: keyPath, fallback: [])
|
2020-04-11 19:33:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-11 17:44:57 +00:00
|
|
|
|
|
|
|
extension LazilyDecoding {
|
|
|
|
// PropertyListEncoder only allows top-level types to be dicts or arrays, which breaks encoding nil-able values.
|
|
|
|
// Wrapping everything in a Box ensures that it's always a dict.
|
2023-01-28 18:41:22 +00:00
|
|
|
struct Box: Codable {
|
|
|
|
let value: Value
|
2020-11-11 17:44:57 +00:00
|
|
|
}
|
|
|
|
}
|