2021-06-28 10:43:07 +00:00
|
|
|
import Foundation
|
|
|
|
import Siesta
|
|
|
|
|
|
|
|
final class Store<Data>: ResourceObserver, ObservableObject {
|
|
|
|
@Published private var all: Data?
|
|
|
|
|
2023-06-17 12:09:51 +00:00
|
|
|
var collection: Data { all ?? ([item].compactMap { $0 } as! Data) }
|
2021-06-28 10:43:07 +00:00
|
|
|
var item: Data? { all }
|
|
|
|
|
2021-07-29 22:34:13 +00:00
|
|
|
init(_ data: Data? = nil) {
|
|
|
|
if data != nil {
|
|
|
|
replace(data!)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 10:43:07 +00:00
|
|
|
func resourceChanged(_ resource: Resource, event _: ResourceEvent) {
|
|
|
|
if let items: Data = resource.typedContent() {
|
|
|
|
replace(items)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func replace(_ items: Data) {
|
|
|
|
all = items
|
|
|
|
}
|
2022-05-29 20:12:59 +00:00
|
|
|
|
|
|
|
func clear() {
|
|
|
|
all = nil
|
|
|
|
}
|
2021-06-28 10:43:07 +00:00
|
|
|
}
|