Skip to content

Commit 1c00eea

Browse files
committed
refactor snapshot
1 parent 3a258b2 commit 1c00eea

File tree

8 files changed

+159
-181
lines changed

8 files changed

+159
-181
lines changed

Firestore/Swift/Source/SwiftAPI/Pipeline/Pipeline.swift

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,32 @@ public struct Pipeline: @unchecked Sendable {
8787
self.db = db
8888
bridge = PipelineBridge(stages: stages.map { $0.bridge }, db: db)
8989
}
90+
91+
public struct Snapshot: Sendable {
92+
/// The Pipeline on which `execute()` was called to obtain this `Pipeline.Snapshot`.
93+
public let pipeline: Pipeline
9094

91-
/// Executes the defined pipeline and returns a `PipelineSnapshot` containing the results.
95+
/// An array of all the results in the `Pipeline.Snapshot`.
96+
public let results: [PipelineResult]
97+
98+
/// The time at which the pipeline producing this result was executed.
99+
public let executionTime: Timestamp
100+
101+
let bridge: __PipelineSnapshotBridge
102+
103+
init(_ bridge: __PipelineSnapshotBridge, pipeline: Pipeline) {
104+
self.bridge = bridge
105+
self.pipeline = pipeline
106+
executionTime = self.bridge.execution_time
107+
results = self.bridge.results.map { PipelineResult($0) }
108+
}
109+
}
110+
111+
/// Executes the defined pipeline and returns a `Pipeline.Snapshot` containing the results.
92112
///
93113
/// This method asynchronously sends the pipeline definition to Firestore for execution.
94114
/// The resulting documents, transformed and filtered by the pipeline stages, are returned
95-
/// within a `PipelineSnapshot`.
115+
/// within a `Pipeline.Snapshot`.
96116
///
97117
/// ```swift
98118
/// // let pipeline: Pipeline = ... // Assume a pipeline is already configured.
@@ -106,14 +126,14 @@ public struct Pipeline: @unchecked Sendable {
106126
/// ```
107127
///
108128
/// - Throws: An error if the pipeline execution fails on the backend.
109-
/// - Returns: A `PipelineSnapshot` containing the result of the pipeline execution.
110-
public func execute() async throws -> PipelineSnapshot {
129+
/// - Returns: A `Pipeline.Snapshot` containing the result of the pipeline execution.
130+
public func execute() async throws -> Pipeline.Snapshot {
111131
return try await withCheckedThrowingContinuation { continuation in
112132
self.bridge.execute { result, error in
113133
if let error {
114134
continuation.resume(throwing: error)
115135
} else {
116-
continuation.resume(returning: PipelineSnapshot(result!, pipeline: self))
136+
continuation.resume(returning: Pipeline.Snapshot(result!, pipeline: self))
117137
}
118138
}
119139
}

Firestore/Swift/Source/SwiftAPI/Pipeline/RealtimePipelineSnapshot.swift renamed to Firestore/Swift/Source/SwiftAPI/Pipeline/PipelineResultChange.swift

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,6 @@
1919
#endif // SWIFT_PACKAGE
2020
import Foundation
2121

22-
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
23-
struct RealtimePipelineSnapshot: Sendable {
24-
/// The Pipeline on which `execute()` was called to obtain this `PipelineSnapshot`.
25-
public let pipeline: RealtimePipeline
26-
27-
/// An array of all the results in the `PipelineSnapshot`.
28-
let results_cache: [PipelineResult]
29-
30-
public let changes: [PipelineResultChange]
31-
public let metadata: SnapshotMetadata
32-
33-
let bridge: __RealtimePipelineSnapshotBridge
34-
private var options: PipelineListenOptions
35-
36-
init(_ bridge: __RealtimePipelineSnapshotBridge,
37-
pipeline: RealtimePipeline,
38-
options: PipelineListenOptions) {
39-
self.bridge = bridge
40-
self.pipeline = pipeline
41-
self.options = options
42-
metadata = bridge.metadata
43-
results_cache = self.bridge.results
44-
.map { PipelineResult($0, options.serverTimestamps ?? .none) }
45-
changes = self.bridge.changes.map { PipelineResultChange($0) }
46-
}
47-
48-
public func results() -> [PipelineResult] {
49-
return results_cache
50-
}
51-
}
52-
5322
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
5423
struct PipelineResultChange: Sendable {
5524
public enum ChangeType {

Firestore/Swift/Source/SwiftAPI/Pipeline/PipelineSnapshot.swift

Lines changed: 0 additions & 41 deletions
This file was deleted.

Firestore/Swift/Source/SwiftAPI/Pipeline/RealtimePipeline.swift

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,44 @@ struct RealtimePipeline: @unchecked Sendable {
7979
self.db = db
8080
bridge = RealtimePipelineBridge(stages: stages.map { $0.bridge }, db: db)
8181
}
82+
83+
struct Snapshot: Sendable {
84+
/// The Pipeline on which `execute()` was called to obtain this `Pipeline.Snapshot`.
85+
public let pipeline: RealtimePipeline
86+
87+
/// An array of all the results in the `Pipeline.Snapshot`.
88+
let results_cache: [PipelineResult]
89+
90+
public let changes: [PipelineResultChange]
91+
public let metadata: SnapshotMetadata
92+
93+
let bridge: __RealtimePipelineSnapshotBridge
94+
private var options: PipelineListenOptions
95+
96+
init(_ bridge: __RealtimePipelineSnapshotBridge,
97+
pipeline: RealtimePipeline,
98+
options: PipelineListenOptions) {
99+
self.bridge = bridge
100+
self.pipeline = pipeline
101+
self.options = options
102+
metadata = bridge.metadata
103+
results_cache = self.bridge.results
104+
.map { PipelineResult($0, options.serverTimestamps ?? .none) }
105+
changes = self.bridge.changes.map { PipelineResultChange($0) }
106+
}
107+
108+
public func results() -> [PipelineResult] {
109+
return results_cache
110+
}
111+
}
82112

83113
private func addSnapshotListener(options: PipelineListenOptions,
84-
listener: @escaping (RealtimePipelineSnapshot?, Error?) -> Void)
114+
listener: @escaping (RealtimePipeline.Snapshot?, Error?) -> Void)
85115
-> ListenerRegistration {
86116
return bridge.addSnapshotListener(options: options.bridge) { snapshotBridge, error in
87117
if snapshotBridge != nil {
88118
listener(
89-
RealtimePipelineSnapshot(
119+
RealtimePipeline.Snapshot(
90120
snapshotBridge!,
91121
pipeline: self,
92122
options: options
@@ -100,7 +130,7 @@ struct RealtimePipeline: @unchecked Sendable {
100130
}
101131

102132
public func snapshotStream(options: PipelineListenOptions? = nil)
103-
-> AsyncThrowingStream<RealtimePipelineSnapshot, Error> {
133+
-> AsyncThrowingStream<RealtimePipeline.Snapshot, Error> {
104134
AsyncThrowingStream { continuation in
105135
let listener = self.addSnapshotListener(
106136
options: options ?? PipelineListenOptions()

Firestore/Swift/Tests/Integration/PipelineApiTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ final class PipelineApiTests: FSTIntegrationTestCase {
3636
let query: Query = db.collection("foo").limit(to: 2)
3737
let _: Pipeline = pipelineSource.create(from: query)
3838

39-
let _: PipelineSnapshot = try await pipeline.execute()
39+
let _: Pipeline.Snapshot = try await pipeline.execute()
4040
}
4141

4242
func testWhereStage() async throws {

0 commit comments

Comments
 (0)