@@ -54,6 +54,7 @@ import {
54
54
} from './collectFields.js' ;
55
55
import { flattenAsyncIterable } from './flattenAsyncIterable.js' ;
56
56
import { mapAsyncIterable } from './mapAsyncIterable.js' ;
57
+ import { Publisher } from './publisher.js' ;
57
58
import {
58
59
getArgumentValues ,
59
60
getDirectiveValues ,
@@ -2104,155 +2105,6 @@ async function executeStreamIterator(
2104
2105
}
2105
2106
}
2106
2107
2107
- /**
2108
- * @internal
2109
- */
2110
- class Publisher {
2111
- subsequentPayloads : Set < AsyncPayloadRecord > ;
2112
-
2113
- constructor ( ) {
2114
- this . subsequentPayloads = new Set ( ) ;
2115
- }
2116
-
2117
- add ( payload : AsyncPayloadRecord ) {
2118
- this . subsequentPayloads . add ( payload ) ;
2119
- }
2120
-
2121
- filterSubsequentPayloads (
2122
- nullPath : Path ,
2123
- currentAsyncRecord : AsyncPayloadRecord | undefined ,
2124
- ) : void {
2125
- const nullPathArray = pathToArray ( nullPath ) ;
2126
- this . subsequentPayloads . forEach ( ( asyncRecord ) => {
2127
- if ( asyncRecord === currentAsyncRecord ) {
2128
- // don't remove payload from where error originates
2129
- return ;
2130
- }
2131
- for ( let i = 0 ; i < nullPathArray . length ; i ++ ) {
2132
- if ( asyncRecord . path [ i ] !== nullPathArray [ i ] ) {
2133
- // asyncRecord points to a path unaffected by this payload
2134
- return ;
2135
- }
2136
- }
2137
- // asyncRecord path points to nulled error field
2138
- if ( isStreamPayload ( asyncRecord ) && asyncRecord . iterator ?. return ) {
2139
- asyncRecord . iterator . return ( ) . catch ( ( ) => {
2140
- // ignore error
2141
- } ) ;
2142
- }
2143
- this . subsequentPayloads . delete ( asyncRecord ) ;
2144
- } ) ;
2145
- }
2146
-
2147
- getCompletedIncrementalResults ( ) : Array < IncrementalResult > {
2148
- const incrementalResults : Array < IncrementalResult > = [ ] ;
2149
- for ( const asyncPayloadRecord of this . subsequentPayloads ) {
2150
- const incrementalResult : IncrementalResult = { } ;
2151
- if ( ! asyncPayloadRecord . isCompleted ) {
2152
- continue ;
2153
- }
2154
- this . subsequentPayloads . delete ( asyncPayloadRecord ) ;
2155
- if ( isStreamPayload ( asyncPayloadRecord ) ) {
2156
- const items = asyncPayloadRecord . items ;
2157
- if ( asyncPayloadRecord . isCompletedIterator ) {
2158
- // async iterable resolver just finished but there may be pending payloads
2159
- continue ;
2160
- }
2161
- ( incrementalResult as IncrementalStreamResult ) . items = items ;
2162
- } else {
2163
- const data = asyncPayloadRecord . data ;
2164
- ( incrementalResult as IncrementalDeferResult ) . data = data ?? null ;
2165
- }
2166
-
2167
- incrementalResult . path = asyncPayloadRecord . path ;
2168
- if ( asyncPayloadRecord . label ) {
2169
- incrementalResult . label = asyncPayloadRecord . label ;
2170
- }
2171
- if ( asyncPayloadRecord . errors . length > 0 ) {
2172
- incrementalResult . errors = asyncPayloadRecord . errors ;
2173
- }
2174
- incrementalResults . push ( incrementalResult ) ;
2175
- }
2176
- return incrementalResults ;
2177
- }
2178
-
2179
- yieldSubsequentPayloads ( ) : AsyncGenerator <
2180
- SubsequentIncrementalExecutionResult ,
2181
- void ,
2182
- void
2183
- > {
2184
- let isDone = false ;
2185
- const publisher = this ;
2186
-
2187
- async function next ( ) : Promise <
2188
- IteratorResult < SubsequentIncrementalExecutionResult , void >
2189
- > {
2190
- if ( isDone ) {
2191
- return { value : undefined , done : true } ;
2192
- }
2193
-
2194
- await Promise . race (
2195
- Array . from ( publisher . subsequentPayloads ) . map ( ( p ) => p . promise ) ,
2196
- ) ;
2197
-
2198
- if ( isDone ) {
2199
- // a different call to next has exhausted all payloads
2200
- return { value : undefined , done : true } ;
2201
- }
2202
-
2203
- const incremental = publisher . getCompletedIncrementalResults ( ) ;
2204
- const hasNext = publisher . subsequentPayloads . size > 0 ;
2205
-
2206
- if ( ! incremental . length && hasNext ) {
2207
- return next ( ) ;
2208
- }
2209
-
2210
- if ( ! hasNext ) {
2211
- isDone = true ;
2212
- }
2213
-
2214
- return {
2215
- value : incremental . length ? { incremental, hasNext } : { hasNext } ,
2216
- done : false ,
2217
- } ;
2218
- }
2219
-
2220
- function returnStreamIterators ( ) {
2221
- const promises : Array < Promise < IteratorResult < unknown > > > = [ ] ;
2222
- publisher . subsequentPayloads . forEach ( ( asyncPayloadRecord ) => {
2223
- if (
2224
- isStreamPayload ( asyncPayloadRecord ) &&
2225
- asyncPayloadRecord . iterator ?. return
2226
- ) {
2227
- promises . push ( asyncPayloadRecord . iterator . return ( ) ) ;
2228
- }
2229
- } ) ;
2230
- return Promise . all ( promises ) ;
2231
- }
2232
-
2233
- return {
2234
- [ Symbol . asyncIterator ] ( ) {
2235
- return this ;
2236
- } ,
2237
- next,
2238
- async return ( ) : Promise <
2239
- IteratorResult < SubsequentIncrementalExecutionResult , void >
2240
- > {
2241
- await returnStreamIterators ( ) ;
2242
- isDone = true ;
2243
- return { value : undefined , done : true } ;
2244
- } ,
2245
- async throw (
2246
- error ?: unknown ,
2247
- ) : Promise < IteratorResult < SubsequentIncrementalExecutionResult , void > > {
2248
- await returnStreamIterators ( ) ;
2249
- isDone = true ;
2250
- return Promise . reject ( error ) ;
2251
- } ,
2252
- } ;
2253
- }
2254
- }
2255
-
2256
2108
class DeferredFragmentRecord {
2257
2109
type : 'defer' ;
2258
2110
errors : Array < GraphQLError > ;
@@ -2354,10 +2206,6 @@ class StreamRecord {
2354
2206
}
2355
2207
}
2356
2208
2357
- type AsyncPayloadRecord = DeferredFragmentRecord | StreamRecord ;
2209
+ export type { StreamRecord } ;
2358
2210
2359
- function isStreamPayload (
2360
- asyncPayload : AsyncPayloadRecord ,
2361
- ) : asyncPayload is StreamRecord {
2362
- return asyncPayload . type === 'stream' ;
2363
- }
2211
+ export type AsyncPayloadRecord = DeferredFragmentRecord | StreamRecord ;
0 commit comments