|
| 1 | +#if compiler(>=5.5) && canImport(_Concurrency) && os(Linux) |
| 2 | +/// Extension to `MongoCursor` to support async/await APIs. |
| 3 | +extension MongoCursor: AsyncSequence, AsyncIteratorProtocol { |
| 4 | + public typealias AsyncIterator = MongoCursor |
| 5 | + |
| 6 | + public typealias Element = T |
| 7 | + |
| 8 | + public func makeAsyncIterator() -> MongoCursor<T> { |
| 9 | + self |
| 10 | + } |
| 11 | + |
| 12 | + // TODO: SWIFT-1415 Make this a property rather than a method. |
| 13 | + /** |
| 14 | + * Indicates whether this cursor has the potential to return more data. |
| 15 | + * |
| 16 | + * This method is mainly useful if this cursor is tailable, since in that case `tryNext()` may return more results |
| 17 | + * even after returning `nil`. |
| 18 | + * |
| 19 | + * If this cursor is non-tailable, it will always be dead after either `tryNext()` returns `nil` or a |
| 20 | + * non-`DecodingError` error. |
| 21 | + * |
| 22 | + * This cursor will be dead after `next()` returns `nil` or throws a non-`DecodingError` error, regardless of the |
| 23 | + * cursor type. |
| 24 | + * |
| 25 | + * This cursor may still be alive after `next()` or `tryNext()` throws a `DecodingError`. |
| 26 | + */ |
| 27 | + public func isAlive() async throws -> Bool { |
| 28 | + try await self.isAlive().get() |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Returns the next `T` in this cursor, or `nil` if the cursor is exhausted or the current `Task` is cancelled. |
| 33 | + * |
| 34 | + * If this cursor is tailable, this method will continue polling until a non-empty batch is returned from the |
| 35 | + * server, or until the `Task` it is running in is cancelled. For this reason, we recommend to run tailable |
| 36 | + * cursors in their own `Task`s, and to terminate the cursor if/when needed by canceling the `Task`. |
| 37 | + * |
| 38 | + * - Warning: You *must not* call any cursor methods besides `isAlive()` while awaiting the result of this method. |
| 39 | + * Doing so will result in undefined behavior. |
| 40 | + * |
| 41 | + * - Returns: |
| 42 | + * The next `T` in this cursor, or `nil` if the cursor is exhausted or the current `Task` is cancelled. |
| 43 | + * |
| 44 | + * If an error is thrown, it is likely one of the following: |
| 45 | + * - `MongoError.CommandError` if an error occurs while fetching more results from the server. |
| 46 | + * - `MongoError.LogicError` if this function is called after the cursor has been exhausted. |
| 47 | + * - `MongoError.LogicError` if this function is called and the session associated with this cursor has been |
| 48 | + * ended. |
| 49 | + * - `DecodingError` if an error occurs decoding the server's response to a `T`. |
| 50 | + */ |
| 51 | + public func next() async throws -> T? { |
| 52 | + while try await self.isAlive() { |
| 53 | + if Task.isCancelled { |
| 54 | + return nil |
| 55 | + } |
| 56 | + if let doc = try await self.tryNext() { |
| 57 | + return doc |
| 58 | + } |
| 59 | + await Task.yield() |
| 60 | + } |
| 61 | + |
| 62 | + return nil |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Attempt to get the next `T` from the cursor, returning `nil` if there are no results. |
| 67 | + * |
| 68 | + * If this cursor is tailable, this method may be called repeatedly while `isAlive()` returns true to retrieve new |
| 69 | + * data. |
| 70 | + * |
| 71 | + * If this cursor is a tailable await cursor, it will wait for results server side for a maximum of `maxAwaitTimeMS` |
| 72 | + * before evaluating to `nil`. This option can be configured via options passed to the method that created this |
| 73 | + * cursor (e.g. the `maxAwaitTimeMS` option on the `FindOptions` passed to `find`). |
| 74 | + * |
| 75 | + * - Warning: You *must not* call any cursor methods besides `isAlive()` while awaiting the result of this method. |
| 76 | + * Doing so will result in undefined behavior. |
| 77 | + * |
| 78 | + * - Returns: |
| 79 | + * The next `T` in this cursor, or `nil` if there is no new data. |
| 80 | + * |
| 81 | + * If an error is thrown, it is likely one of the following: |
| 82 | + * - `MongoError.CommandError` if an error occurs while fetching more results from the server. |
| 83 | + * - `MongoError.LogicError` if this function is called after the cursor has been exhausted. |
| 84 | + * - `MongoError.LogicError` if this function is called and the session associated with this cursor has been |
| 85 | + * ended. |
| 86 | + * - `DecodingError` if an error occurs decoding the server's response to a `T`. |
| 87 | + */ |
| 88 | + public func tryNext() async throws -> T? { |
| 89 | + try await self.tryNext().get() |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Consolidate the currently available results of the cursor into an array of type `T`. |
| 94 | + * |
| 95 | + * If this cursor is not tailable, this method will exhaust it. |
| 96 | + * |
| 97 | + * If this cursor is tailable, `toArray` will only fetch the currently available results, and it |
| 98 | + * may return more data if it is called again while the cursor is still alive. |
| 99 | + * |
| 100 | + * - Warning: You *must not* call any cursor methods besides `isAlive()` while awaiting the result of this method. |
| 101 | + * Doing so will result in undefined behavior. |
| 102 | + * |
| 103 | + * - Returns: |
| 104 | + * An `T` containing the results currently available in this cursor. |
| 105 | + * |
| 106 | + * If an error is thrown, it is likely one of the following: |
| 107 | + * - `MongoError.CommandError` if an error occurs while fetching more results from the server. |
| 108 | + * - `MongoError.LogicError` if this function is called after the cursor has been exhausted. |
| 109 | + * - `MongoError.LogicError` if this function is called and the session associated with this cursor has been |
| 110 | + * ended. |
| 111 | + * - `DecodingError` if an error occurs decoding the server's responses to `T`s. |
| 112 | + */ |
| 113 | + public func toArray() async throws -> [T] { |
| 114 | + try await self.toArray().get() |
| 115 | + } |
| 116 | +} |
| 117 | +#endif |
0 commit comments