|
| 1 | +/** Copyright (c) Facebook, Inc. and its affiliates. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + * |
| 15 | + * @flow |
| 16 | + */ |
| 17 | + |
| 18 | +'use strict'; |
| 19 | + |
| 20 | +import type {ISubscriber, ISubscription} from 'rsocket-types'; |
| 21 | +import Flowable from './Flowable'; |
| 22 | + |
| 23 | +// $FlowFixMe |
| 24 | +export default class FlowableAsyncIterable<T> implements AsyncIterable<T> { |
| 25 | + _source: Flowable<T>; |
| 26 | + _prefetch: number; |
| 27 | + |
| 28 | + constructor(source: Flowable<T>, prefetch: number = 256) { |
| 29 | + this._source = source; |
| 30 | + this._prefetch = prefetch; |
| 31 | + } |
| 32 | + |
| 33 | + asyncIterator(): AsyncIterator<T> { |
| 34 | + const asyncIteratorSubscriber = new AsyncIteratorSubscriber(this._prefetch); |
| 35 | + this._source.subscribe(asyncIteratorSubscriber); |
| 36 | + return asyncIteratorSubscriber; |
| 37 | + } |
| 38 | + |
| 39 | + // $FlowFixMe |
| 40 | + [Symbol.asyncIterator](): AsyncIterator<T> { |
| 41 | + return this.asyncIterator(); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// $FlowFixMe |
| 46 | +class AsyncIteratorSubscriber<T> implements ISubscriber<T>, AsyncIterator<T> { |
| 47 | + _values: T[]; |
| 48 | + _prefetch: number; |
| 49 | + _limit: number; |
| 50 | + |
| 51 | + _subscription: ISubscription; |
| 52 | + |
| 53 | + _produced: number; |
| 54 | + |
| 55 | + _done: boolean; |
| 56 | + _error: Error; |
| 57 | + |
| 58 | + _resolve: ?( |
| 59 | + result: Promise<IteratorResult<T, void>> | IteratorResult<T, void>, |
| 60 | + ) => void; |
| 61 | + _reject: ?(reason?: any) => void; |
| 62 | + |
| 63 | + constructor(prefetch: number = 256) { |
| 64 | + this._prefetch = prefetch; |
| 65 | + this._values = []; |
| 66 | + this._limit = |
| 67 | + prefetch === Number.MAX_SAFE_INTEGER |
| 68 | + ? Number.MAX_SAFE_INTEGER |
| 69 | + : prefetch - (prefetch >> 2); |
| 70 | + this._produced = 0; |
| 71 | + } |
| 72 | + |
| 73 | + onSubscribe(subscription: ISubscription): void { |
| 74 | + this._subscription = subscription; |
| 75 | + subscription.request(this._prefetch); |
| 76 | + } |
| 77 | + |
| 78 | + onNext(value: T): void { |
| 79 | + const resolve = this._resolve; |
| 80 | + if (resolve) { |
| 81 | + this._resolve = undefined; |
| 82 | + this._reject = undefined; |
| 83 | + |
| 84 | + if (++this._produced === this._limit) { |
| 85 | + this._produced = 0; |
| 86 | + this._subscription.request(this._limit); |
| 87 | + } |
| 88 | + |
| 89 | + resolve({done: false, value}); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + this._values.push(value); |
| 94 | + } |
| 95 | + |
| 96 | + onComplete(): void { |
| 97 | + this._done = true; |
| 98 | + |
| 99 | + const resolve = this._resolve; |
| 100 | + if (resolve) { |
| 101 | + this._resolve = undefined; |
| 102 | + this._reject = undefined; |
| 103 | + |
| 104 | + resolve({done: true}); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + onError(error: Error): void { |
| 109 | + this._done = true; |
| 110 | + this._error = error; |
| 111 | + |
| 112 | + const reject = this._reject; |
| 113 | + if (reject) { |
| 114 | + this._resolve = undefined; |
| 115 | + this._reject = undefined; |
| 116 | + |
| 117 | + reject(error); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + next(): Promise<IteratorResult<T, void>> { |
| 122 | + const value = this._values.shift(); |
| 123 | + if (value) { |
| 124 | + if (++this._produced === this._limit) { |
| 125 | + this._produced = 0; |
| 126 | + this._subscription.request(this._limit); |
| 127 | + } |
| 128 | + |
| 129 | + return Promise.resolve({done: false, value}); |
| 130 | + } else if (this._done) { |
| 131 | + if (this._error) { |
| 132 | + return Promise.reject(this._error); |
| 133 | + } else { |
| 134 | + return Promise.resolve({done: true}); |
| 135 | + } |
| 136 | + } else { |
| 137 | + return new Promise((resolve, reject) => { |
| 138 | + this._resolve = resolve; |
| 139 | + this._reject = reject; |
| 140 | + }); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return(): Promise<IteratorResult<T, void>> { |
| 145 | + this._subscription.cancel(); |
| 146 | + return Promise.resolve({done: true}); |
| 147 | + } |
| 148 | + |
| 149 | + // $FlowFixMe |
| 150 | + [Symbol.asyncIterator](): AsyncIterator<T> { |
| 151 | + return this; |
| 152 | + } |
| 153 | +} |
0 commit comments