added Publishers for addStorage functions

This commit is contained in:
John Estropia
2021-03-07 16:08:19 +09:00
parent 003bf897e2
commit bfb1df3c40
5 changed files with 324 additions and 0 deletions

View File

@@ -75,6 +75,78 @@ extension DataStack.ReactiveNamespace {
// MARK: Public
/**
Reactive extension for `CoreStore.DataStack`'s `addStorage(...)` API. Asynchronously adds a `StorageInterface` to the stack.
```
dataStack.reactive
.addStorage(
InMemoryStore(configuration: "Config1")
)
.sink(
receiveCompletion: { result in
// ...
},
receiveValue: { storage in
// ...
}
)
.store(in: &cancellables)
```
- parameter storage: the storage
- returns: A `Future` that emits a `StorageInterface` instance added to the `DataStack`. Note that the `StorageInterface` event value may not always be the same instance as the parameter argument if a previous `StorageInterface` was already added at the same URL and with the same configuration.
*/
public func addStorage<T: StorageInterface>(_ storage: T) -> Future<T, CoreStoreError> {
return .init { (promise) in
self.base.addStorage(
storage,
completion: { (result) in
switch result {
case .success(let storage):
promise(.success(storage))
case .failure(let error):
promise(.failure(error))
}
}
)
}
}
/**
Reactive extension for `CoreStore.DataStack`'s `addStorage(...)` API. Asynchronously adds a `LocalStorage` to the stack. Migrations are also initiated by default. The event emits `DataStack.AddStoragePublisher.Progress` `enum` values.
```
dataStack.reactive
.addStorage(
SQLiteStore(
fileName: "core_data.sqlite",
configuration: "Config1"
)
)
.sink(
receiveCompletion: { result in
// ...
},
receiveValue: { (progress) in
print("\(round(progress.fractionCompleted * 100)) %") // 0.0 ~ 1.0
}
)
.store(in: &cancellables)
```
- parameter storage: the local storage
- returns: A `DataStack.AddStoragePublisher` that emits a `DataStack.AddStoragePublisher.Progress` value with metadata for migration progress. Note that the `LocalStorage` event value may not always be the same instance as the parameter argument if a previous `LocalStorage` was already added at the same URL and with the same configuration.
*/
public func addStorage<T: LocalStorage>(_ storage: T) -> DataStack.AddStoragePublisher<T> {
return .init(
dataStack: self.base,
storage: storage
)
}
/**
Reactive extension for `CoreStore.DataStack`'s `importObject(...)` API. Creates an `ImportableObject` by importing from the specified import source. The event value will be the object instance correctly associated for the `DataStack`.
```

View File

@@ -0,0 +1,238 @@
//
// DataStack.AddStoragePublisher.swift
// CoreStore
//
// Copyright © 2021 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(Combine)
import Combine
import CoreData
// MARK: - DataStack
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
extension DataStack {
// MARK: - AddStoragePublisher
/**
A `Publisher` that emits a `ListSnapshot` whenever changes occur in the `ListPublisher`.
- SeeAlso: DataStack.reactive.addStorage(_:)
*/
public struct AddStoragePublisher<Storage: LocalStorage>: Publisher {
// MARK: Internal
internal let dataStack: DataStack
internal let storage: Storage
// MARK: Publisher
public typealias Output = Progress
public typealias Failure = CoreStoreError
public func receive<S: Subscriber>(subscriber: S) where S.Input == Output, S.Failure == Failure {
subscriber.receive(
subscription: AddStorageSubscription(
dataStack: self.dataStack,
storage: self.storage,
subscriber: subscriber
)
)
}
// MARK: - Progress
/**
A `Progress` contains info on a `LocalStorage`'s setup progress.
- SeeAlso: DataStack.reactive.addStorage(_:)
*/
public enum Progress {
/**
The `LocalStorage` is currently being migrated
*/
case migrating(storage: Storage, progressObject: Foundation.Progress)
/**
The `LocalStorage` has been added to the `DataStack` and is ready for reading and writing
*/
case finished(storage: Storage, migrationRequired: Bool)
/**
The fraction of the overall work completed by the migration. Returns a value between 0.0 and 1.0, inclusive.
*/
public var fractionCompleted: Double {
switch self {
case .migrating(_, let progressObject):
return progressObject.fractionCompleted
case .finished:
return 1
}
}
/**
Returns `true` if the storage was successfully added to the stack, `false` otherwise.
*/
public var isCompleted: Bool {
switch self {
case .migrating:
return false
case .finished:
return true
}
}
}
// MARK: - AddStorageSubscriber
fileprivate final class AddStorageSubscriber: Subscriber {
// MARK: Subscriber
typealias Failure = CoreStoreError
func receive(subscription: Subscription) {
subscription.request(.unlimited)
}
func receive(_ input: Output) -> Subscribers.Demand {
return .unlimited
}
func receive(completion: Subscribers.Completion<Failure>) {}
}
// MARK: - AddStorageSubscription
fileprivate final class AddStorageSubscription<S: Subscriber>: Subscription where S.Input == Output, S.Failure == CoreStoreError {
// MARK: FilePrivate
init(
dataStack: DataStack,
storage: Storage,
subscriber: S
) {
self.dataStack = dataStack
self.storage = storage
self.subscriber = subscriber
}
// MARK: Subscription
func request(_ demand: Subscribers.Demand) {
guard demand > 0 else {
return
}
var progress: Foundation.Progress?
progress = self.dataStack.addStorage(
self.storage,
completion: { [weak self] result in
guard
let self = self,
let subscriber = self.subscriber
else {
return
}
switch result {
case .success(let storage):
_ = subscriber.receive(
.finished(
storage: storage,
migrationRequired: progress != nil
)
)
subscriber.receive(
completion: .finished
)
case .failure(let error):
subscriber.receive(
completion: .failure(error)
)
}
}
)
guard let progress = progress else {
return
}
progress.cs_setProgressHandler { [weak self] progress in
guard
let self = self,
let subscriber = self.subscriber
else {
return
}
_ = subscriber.receive(
.migrating(
storage: self.storage,
progressObject: progress
)
)
}
}
// MARK: Cancellable
func cancel() {
self.subscriber = nil
}
// MARK: Private
private let dataStack: DataStack
private let storage: Storage
private var subscriber: S?
}
}
}
#endif

View File

@@ -37,6 +37,8 @@ extension ListPublisher {
/**
A `Publisher` that emits a `ListSnapshot` whenever changes occur in the `ListPublisher`.
- SeeAlso: ListPublisher.reactive.snapshot(emitInitialValue:)
*/
public struct SnapshotPublisher: Publisher {

View File

@@ -37,6 +37,8 @@ extension ObjectPublisher {
/**
A `Publisher` that emits an `ObjectSnapshot?` whenever changes occur in the `ObjectPublisher`. The event emits `nil` if the object has been deletd.
- SeeAlso: ObjectPublisher.reactive.snapshot(emitInitialValue:)
*/
public struct SnapshotPublisher: Publisher {