@@ -14,16 +14,24 @@ public extension Pager {
1414 /// The underlying stream.
1515 public let stream : Stream
1616 /// The underlying offset generator.
17- public let offset : ( [ Output ] ) -> Offset ?
17+ public let offset : ( [ Output ] ) -> Instruction < Offset >
1818 }
1919}
2020
21+ /// An `enum` listing all valid instructions.
22+ public enum Instruction < Offset> {
23+ /// Stop paginating.
24+ case stop
25+ /// Load next offset.
26+ case load( Offset )
27+ }
28+
2129public extension Publisher {
2230 /// Create the iteration.
2331 ///
2432 /// - parameter offset: A valid offset generator.
2533 /// - returns: A valid `Pager.Iteration`.
26- func iterate< O> ( with offset: @escaping ( [ Output ] ) -> O ? ) -> Pager < O , Self > . Iteration {
34+ func iterate< O> ( with offset: @escaping ( [ Output ] ) -> Instruction < O > ) -> Pager < O , Self > . Iteration {
2735 . init( stream: self , offset: offset)
2836 }
2937
@@ -34,8 +42,15 @@ public extension Publisher {
3442 /// - offet: A valid offset generator.
3543 /// - returns: A valid `Pager.Iteration`.
3644 func iterate< O> ( stoppingAt exception: @escaping ( O ) -> Bool ,
37- with offset: @escaping ( [ Output ] ) -> O ? ) -> Pager < O , Self > . Iteration {
38- iterate { offset ( $0) . flatMap { exception ( $0) ? nil : $0 } }
45+ with offset: @escaping ( [ Output ] ) -> Instruction < O > ) -> Pager < O , Self > . Iteration {
46+ iterate {
47+ switch offset ( $0) {
48+ case . stop:
49+ return . stop
50+ case . load( let next) :
51+ return exception ( next) ? . stop : . load( next)
52+ }
53+ }
3954 }
4055
4156 /// Create the iteration, making sure we don't get stuck inside an infinite loop.
@@ -45,15 +60,15 @@ public extension Publisher {
4560 /// - offet: A valid offset generator.
4661 /// - returns: A valid `Pager.Iteration`.
4762 func iterate< O> ( stoppingAt exception: O ,
48- with offset: @escaping ( [ Output ] ) -> O ? ) -> Pager < O , Self > . Iteration where O: Equatable {
63+ with offset: @escaping ( [ Output ] ) -> Instruction < O > ) -> Pager < O , Self > . Iteration where O: Equatable {
4964 iterate ( stoppingAt: { $0 == exception } , with: offset)
5065 }
5166
5267 /// Create the iteration, after only one output.
5368 ///
5469 /// - parameter offset: A valid offset generator.
5570 /// - returns: A valid `Pager.Iteration`.
56- func iterateFirst< O> ( with offset: @escaping ( Output ? ) -> O ? ) -> Pager < O , Publishers . Output < Self > > . Iteration {
71+ func iterateFirst< O> ( with offset: @escaping ( Output ? ) -> Instruction < O > ) -> Pager < O , Publishers . Output < Self > > . Iteration {
5772 prefix ( 1 ) . iterate { offset ( $0. first) }
5873 }
5974
@@ -64,8 +79,15 @@ public extension Publisher {
6479 /// - offet: A valid offset generator.
6580 /// - returns: A valid `Pager.Iteration`.
6681 func iterateFirst< O> ( stoppingAt exception: @escaping ( O ) -> Bool ,
67- with offset: @escaping ( Output ? ) -> O ? ) -> Pager < O , Publishers . Output < Self > > . Iteration {
68- iterateFirst { offset ( $0) . flatMap { exception ( $0) ? nil : $0 } }
82+ with offset: @escaping ( Output ? ) -> Instruction < O > ) -> Pager < O , Publishers . Output < Self > > . Iteration {
83+ iterateFirst {
84+ switch offset ( $0) {
85+ case . stop:
86+ return . stop
87+ case . load( let next) :
88+ return exception ( next) ? . stop : . load( next)
89+ }
90+ }
6991 }
7092
7193 /// Create the iteration, after only one output, making sure we don't get stuck inside an infinite loop.
@@ -75,15 +97,15 @@ public extension Publisher {
7597 /// - offet: A valid offset generator.
7698 /// - returns: A valid `Pager.Iteration`.
7799 func iterateFirst< O> ( stoppingAt exception: O ,
78- with offset: @escaping ( Output ? ) -> O ? ) -> Pager < O , Publishers . Output < Self > > . Iteration where O: Equatable {
100+ with offset: @escaping ( Output ? ) -> Instruction < O > ) -> Pager < O , Publishers . Output < Self > > . Iteration where O: Equatable {
79101 iterateFirst ( stoppingAt: { $0 == exception } , with: offset)
80102 }
81103
82104 /// Create the iteration, on the last output alone.
83105 ///
84106 /// - parameter offset: A valid offset generator.
85107 /// - returns: A valid `Pager.Iteration`.
86- func iterateLast< O> ( with offset: @escaping ( Output ? ) -> O ? ) -> Pager < O , Publishers . Last < Self > > . Iteration {
108+ func iterateLast< O> ( with offset: @escaping ( Output ? ) -> Instruction < O > ) -> Pager < O , Publishers . Last < Self > > . Iteration {
87109 last ( ) . iterate { offset ( $0. first) }
88110 }
89111
@@ -94,8 +116,15 @@ public extension Publisher {
94116 /// - offet: A valid offset generator.
95117 /// - returns: A valid `Pager.Iteration`.
96118 func iterateLast< O> ( stoppingAt exception: @escaping ( O ) -> Bool ,
97- with offset: @escaping ( Output ? ) -> O ? ) -> Pager < O , Publishers . Last < Self > > . Iteration {
98- iterateLast { offset ( $0) . flatMap { exception ( $0) ? nil : $0 } }
119+ with offset: @escaping ( Output ? ) -> Instruction < O > ) -> Pager < O , Publishers . Last < Self > > . Iteration {
120+ iterateLast {
121+ switch offset ( $0) {
122+ case . stop:
123+ return . stop
124+ case . load( let next) :
125+ return exception ( next) ? . stop : . load( next)
126+ }
127+ }
99128 }
100129
101130 /// Create the iteration, on the last output alone., making sure we don't get stuck inside an infinite loop.
@@ -105,7 +134,7 @@ public extension Publisher {
105134 /// - offet: A valid offset generator.
106135 /// - returns: A valid `Pager.Iteration`.
107136 func iterateLast< O> ( stoppingAt exception: O ,
108- with offset: @escaping ( Output ? ) -> O ? ) -> Pager < O , Publishers . Last < Self > > . Iteration where O: Equatable {
137+ with offset: @escaping ( Output ? ) -> Instruction < O > ) -> Pager < O , Publishers . Last < Self > > . Iteration where O: Equatable {
109138 iterateLast ( stoppingAt: { $0 == exception } , with: offset)
110139 }
111140
@@ -114,7 +143,7 @@ public extension Publisher {
114143 /// - parameter continue: A valid offset boolean generator.
115144 /// - returns: A valid `Pager.Iteration`.
116145 func iterate( _ `continue`: @escaping ( [ Output ] ) -> Bool = { _ in true } ) -> Pager < Void , Self > . Iteration {
117- . init( stream: self ) { `continue` ( $0) ? ( ) : nil }
146+ . init( stream: self ) { `continue` ( $0) ? . load ( ( ) ) : . stop }
118147 }
119148}
120149
@@ -124,7 +153,7 @@ public extension Publisher where Failure == Never {
124153 /// - warning: The `Publisher` is still not guaranteed to return an output. You should only use this when you're certain it will.
125154 /// - parameter offset: A valid offset generator.
126155 /// - returns: A valid `Pager.Iteration`.
127- func iterateFirst< O> ( with offset: @escaping ( Output ) -> O ? ) -> Pager < O , Publishers . Output < Self > > . Iteration {
156+ func iterateFirst< O> ( with offset: @escaping ( Output ) -> Instruction < O > ) -> Pager < O , Publishers . Output < Self > > . Iteration {
128157 iterateFirst { offset ( $0!) }
129158 }
130159
@@ -135,7 +164,7 @@ public extension Publisher where Failure == Never {
135164 /// - offet: A valid offset generator.
136165 /// - returns: A valid `Pager.Iteration`.
137166 func iterateFirst< O> ( stoppingAt exception: @escaping ( O ) -> Bool ,
138- with offset: @escaping ( Output ) -> O ? ) -> Pager < O , Publishers . Output < Self > > . Iteration {
167+ with offset: @escaping ( Output ) -> Instruction < O > ) -> Pager < O , Publishers . Output < Self > > . Iteration {
139168 iterateFirst ( stoppingAt: exception) { offset ( $0!) }
140169 }
141170
@@ -146,7 +175,7 @@ public extension Publisher where Failure == Never {
146175 /// - offet: A valid offset generator.
147176 /// - returns: A valid `Pager.Iteration`.
148177 func iterateFirst< O> ( stoppingAt exception: O ,
149- with offset: @escaping ( Output ) -> O ? ) -> Pager < O , Publishers . Output < Self > > . Iteration where O: Equatable {
178+ with offset: @escaping ( Output ) -> Instruction < O > ) -> Pager < O , Publishers . Output < Self > > . Iteration where O: Equatable {
150179 iterateFirst ( stoppingAt: { $0 == exception } , with: offset)
151180 }
152181
@@ -155,7 +184,7 @@ public extension Publisher where Failure == Never {
155184 /// - warning: The `Publisher` is still not guaranteed to return an output. You should only use this when you're certain it will.
156185 /// - parameter offset: A valid offset generator.
157186 /// - returns: A valid `Pager.Iteration`.
158- func iterateLast< O> ( with offset: @escaping ( Output ) -> O ? ) -> Pager < O , Publishers . Last < Self > > . Iteration {
187+ func iterateLast< O> ( with offset: @escaping ( Output ) -> Instruction < O > ) -> Pager < O , Publishers . Last < Self > > . Iteration {
159188 iterateLast { offset ( $0!) }
160189 }
161190
@@ -166,7 +195,7 @@ public extension Publisher where Failure == Never {
166195 /// - offet: A valid offset generator.
167196 /// - returns: A valid `Pager.Iteration`.
168197 func iterateLast< O> ( stoppingAt exception: @escaping ( O ) -> Bool ,
169- with offset: @escaping ( Output ) -> O ? ) -> Pager < O , Publishers . Last < Self > > . Iteration {
198+ with offset: @escaping ( Output ) -> Instruction < O > ) -> Pager < O , Publishers . Last < Self > > . Iteration {
170199 iterateLast ( stoppingAt: exception) { offset ( $0!) }
171200 }
172201
@@ -178,7 +207,7 @@ public extension Publisher where Failure == Never {
178207 /// - offet: A valid offset generator.
179208 /// - returns: A valid `Pager.Iteration`.
180209 func iterateLast< O> ( stoppingAt exception: O ,
181- with offset: @escaping ( Output ) -> O ? ) -> Pager < O , Publishers . Last < Self > > . Iteration where O: Equatable {
210+ with offset: @escaping ( Output ) -> Instruction < O > ) -> Pager < O , Publishers . Last < Self > > . Iteration where O: Equatable {
182211 iterateLast ( stoppingAt: { $0 == exception } , with: offset)
183212 }
184213}
0 commit comments