WIP: documentation

This commit is contained in:
John Estropia
2019-10-18 08:19:55 +09:00
parent 6e3e540d0a
commit ddf599ba85
8 changed files with 85 additions and 216 deletions

View File

@@ -34,9 +34,9 @@ import CoreData
extension CoreStore {
/**
Using the `defaultStack`, creates an `ObjectMonitor` for the specified `ObjectRepresentation`. Multiple `ObjectObserver`s may then register themselves to be notified when changes are made to the `DynamicObject`.
Using the `defaultStack`, creates an `ObjectMonitor` for the specified `DynamicObject`. Multiple `ObjectObserver`s may then register themselves to be notified when changes are made to the `DynamicObject`.
- parameter object: the `ObjectRepresentation` to observe changes from
- parameter object: the `DynamicObject` to observe changes from
- returns: a `ObjectMonitor` that monitors changes to `object`
*/
public static func monitorObject<O: DynamicObject>(_ object: O) -> ObjectMonitor<O> {

View File

@@ -32,6 +32,17 @@ import CoreData
// MARK: - DataStack
extension DataStack {
/**
Creates a `LiveObject` for the specified `DynamicObject`. Multiple objects may then register themselves to be notified when changes are made to the `DynamicObject`.
- parameter object: the `DynamicObject` to observe changes from
- returns: a `LiveObject` that broadcasts changes to `object`
*/
public func liveObject<O: DynamicObject>(_ object: O) -> LiveObject<O> {
return LiveObject<O>(objectID: object.cs_id(), context: self.unsafeContext())
}
public func liveList<D>(_ from: From<D>, _ fetchClauses: FetchClause...) -> LiveList<D> {

View File

@@ -33,9 +33,9 @@ import CoreData
extension DataStack {
/**
Creates an `ObjectMonitor` for the specified `ObjectRepresentation`. Multiple `ObjectObserver`s may then register themselves to be notified when changes are made to the `DynamicObject`.
Creates an `ObjectMonitor` for the specified `DynamicObject`. Multiple `ObjectObserver`s may then register themselves to be notified when changes are made to the `DynamicObject`.
- parameter object: the `ObjectRepresentation` to observe changes from
- parameter object: the `DynamicObject` to observe changes from
- returns: a `ObjectMonitor` that monitors changes to `object`
*/
public func monitorObject<O: DynamicObject>(_ object: O) -> ObjectMonitor<O> {

View File

@@ -1,5 +1,5 @@
//
// EnvironmentKeys.swift
// EnvironmentValues+DataSources.swift
// CoreStore
//
// Copyright © 2018 John Rommel Estropia
@@ -28,20 +28,6 @@
import SwiftUI
// MARK: - DataStackEnvironmentKey
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
public struct DataStackEnvironmentKey: EnvironmentKey {
// MARK: Public
public static var defaultValue: DataStack {
return Shared.defaultStack
}
}
// MARK: - EnvironmentValues
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
@@ -49,12 +35,36 @@ extension EnvironmentValues {
// MARK: Public
/**
The `DataStack` instance injected to `self`:
```
@Environment(\.dataStack)
var dataStack: DataStack
```
*/
public var dataStack: DataStack {
get {
return self[DataStackEnvironmentKey.self]
return self[DataStackKey.self]
}
set {
self[DataStackEnvironmentKey.self] = newValue
self[DataStackKey.self] = newValue
}
}
// MARK: - DataStackEnvironmentKey
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
fileprivate struct DataStackKey: EnvironmentKey {
// MARK: FilePrivate
fileprivate static var defaultValue: DataStack {
return Shared.defaultStack
}
}
}

View File

@@ -38,6 +38,37 @@ import SwiftUI
// MARK: - LiveList
/**
`LiveList` tracks a diffable list of `DynamicObject` instances. Unlike `ListMonitor`s, `LiveList` are more lightweight and access objects lazily. `LiveList`s are also designed to work well with `DiffableDataSource.TableView`s and `DiffableDataSource.CollectionView`s. Objects that need to be notified of `LiveList` changes may register themselves to its `addObserver(_:_:)` method:
```
let liveList = Shared.defaultStack.liveList(
From<Person>()
.where(\.title == "Engineer")
.orderBy(.ascending(\.lastName))
)
liveList.addObserver(self) { (liveList) in
// Handle changes
}
```
The `LiveList` instance needs to be held on (retained) for as long as the list needs to be observed.
Observers registered via `addObserver(_:_:)` are not retained. `LiveList` only keeps a `weak` reference to all observers, thus keeping itself free from retain-cycles.
`LiveList`s may optionally be created with sections:
```
let liveList = Shared.defaultStack.liveList(
From<Person>()
.sectionBy(\.age") { "Age \($0)" }
.where(\.title == "Engineer")
.orderBy(.ascending(\.lastName))
)
liveList.addObserver(self) { (liveList) in
// Handle changes
}
```
*/
@available(macOS 10.12, *)
public final class LiveList<O: DynamicObject>: Hashable {
// MARK: Public

View File

@@ -1,173 +0,0 @@
//
// LiveQuery.swift
// CoreStore
//
// Copyright © 2018 John Rommel Estropia
//
// 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.
//
//#if canImport(SwiftUI) && canImport(Combine)
//
//import CoreData
//import Combine
//import SwiftUI
//
//
//#warning("TODO: autoupdating doesn't work yet")
//@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
//@propertyWrapper
//public struct LiveQuery<Result: LiveResult>: DynamicProperty {
//
// // MARK: Public
//
// @Environment(\.dataStack)
// public var dataStack: DataStack
//
// public typealias ObjectType = Result.ObjectType
//
//
// // MARK: @propertyWrapper
//
// public var wrappedValue: Result {
//
// get {
//
// return self.backingWrapper!.wrappedValue
// }
// set {
//
// self.backingWrapper!.wrappedValue = newValue
// }
// }
//
// public var projectedValue: ObservedObject<Result>.Wrapper {
//
// return self.backingWrapper!.projectedValue
// }
//
//
// // MARK: DynamicProperty
//
// public mutating func update() {
//
// SwiftUI.withAnimation {
//
// let dataStack = self.dataStack
//
// if self.backingWrapper == nil {
//
// self.backingWrapper = ObservedObject(wrappedValue: self.newLiveResult(dataStack))
// }
// else {
//
// self.backingWrapper!.wrappedValue = self.newLiveResult(dataStack)
// }
// self.backingWrapper!.update()
// }
// }
//
//
// // MARK: FilePrivate
//
// fileprivate let newLiveResult: (DataStack) -> Result
//
// fileprivate init(newLiveResult: @escaping (DataStack) -> Result) {
//
// self.newLiveResult = newLiveResult
// }
//
//
// // MARK: Private
//
//// private var nonMutatingWrappedValue: LazyNonmutating<Result> = .init { fatalError() }
//
// private var currentDataStack: DataStack?
// private var backingWrapper: ObservedObject<Result>?
//
// private mutating func set(dataStack: DataStack) -> Bool {
//
// guard self.currentDataStack != dataStack else {
//
// return false
// }
// self.currentDataStack = dataStack
// if self.backingWrapper == nil {
//
// self.backingWrapper = ObservedObject(wrappedValue: self.newLiveResult(dataStack))
// }
// else {
//
// self.backingWrapper?.wrappedValue = self.newLiveResult(dataStack)
// }
// return true
// }
//
//
// // MARK: - LazyNonmutating
//
// fileprivate final class LazyNonmutating<Value> {
//
// // MARK: FilePrivate
//
// lazy var wrappedValue: Value = self.initializer()
//
// init(_ initializer: @escaping () -> Value) {
//
// self.initializer = initializer
// }
//
//
// // MARK: Private
//
// private var initializer: () -> Value
// }
//}
//
//
//#if canImport(UIKit) || canImport(AppKit)
//
//@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
//extension LiveQuery {
//
// public init<D: DynamicObject>(liveList: LiveList<D>) where Result == LiveList<D> {
//
// self.init(
// newLiveResult: { _ in liveList }
// )
// }
//
// public init<D: DynamicObject>(_ clauseChain: FetchChainBuilder<D>) where Result == LiveList<D> {
//
// self.init(
// newLiveResult: { $0.liveList(clauseChain) }
// )
// }
//
// public init<D: DynamicObject>(_ clauseChain: SectionMonitorChainBuilder<D>) where Result == LiveList<D> {
//
// self.init(
// newLiveResult: { $0.liveList(clauseChain) }
// )
// }
//}
//
//#endif
//
//#endif

View File

@@ -33,9 +33,9 @@ import CoreData
extension UnsafeDataTransaction {
/**
Creates a `ObjectMonitor` for the specified `ObjectRepresentation`. Multiple `ObjectObserver`s may then register themselves to be notified when changes are made to the `DynamicObject`.
Creates an `ObjectMonitor` for the specified `DynamicObject`. Multiple `ObjectObserver`s may then register themselves to be notified when changes are made to the `DynamicObject`.
- parameter object: the `ObjectRepresentation` to observe changes from
- parameter object: the `DynamicObject` to observe changes from
- returns: a `ObjectMonitor` that monitors changes to `object`
*/
public func monitorObject<O: DynamicObject>(_ object: O) -> ObjectMonitor<O> {