| 1 |
// Copyright 2017 The Closure Library Authors. All Rights Reserved. |
| 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 |
/** |
| 16 |
* @fileoverview Shims between goog.iter.Iterator and ES6 iterator. |
| 17 |
*/ |
| 18 |
|
| 19 |
goog.module('goog.iter.es6'); |
| 20 |
|
| 21 |
const GoogIterable = goog.require('goog.iter.Iterable'); |
| 22 |
const GoogIterator = goog.require('goog.iter.Iterator'); |
| 23 |
const StopIteration = goog.require('goog.iter.StopIteration'); |
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* Common interface extending both `goog.iter.Iterable` and ES6 `Iterable`, |
| 28 |
* and providing `toGoog()` and `toEs6()` methods to get either kind |
| 29 |
* of iterator. `ShimIterable.of()` is the primary entry point for |
| 30 |
* this library. If it is given an iterable that is *not* also an |
| 31 |
* iterator, then it will inherit any reusability from its argument |
| 32 |
* (i.e. `ShimIterable.of(mySet)` will be reusable, since mySet makes |
| 33 |
* a fresh Iterator every time, whereas `ShimIterable.of(myIterator)` |
| 34 |
* will be one-shot). |
| 35 |
* |
| 36 |
* `ShimGoogIterator` and `ShimEs6Iterator` extend `ShimIterable` and |
| 37 |
* also implement one or the other iterator API. Since they extend |
| 38 |
* `ShimIterable`, it is easy to convert back and forth between the two |
| 39 |
* APIs. Any such conversion will expose a view to the same underlying |
| 40 |
* iterator, so elements pulled via one API will not be available from |
| 41 |
* the other. |
| 42 |
* |
| 43 |
* @interface |
| 44 |
* @extends {Iterable<VALUE>} |
| 45 |
* @template VALUE |
| 46 |
*/ |
| 47 |
class ShimIterable { |
| 48 |
/** @return {!GoogIterator<VALUE>} */ |
| 49 |
__iterator__() {} |
| 50 |
|
| 51 |
/** @return {!ShimGoogIterator<VALUE>} */ |
| 52 |
toGoog() {} |
| 53 |
|
| 54 |
/** @return {!ShimEs6Iterator<VALUE>} */ |
| 55 |
toEs6() {} |
| 56 |
|
| 57 |
/** |
| 58 |
* @param {!Iterable<VALUE>|!Iterator<VALUE>| |
| 59 |
* !GoogIterator<VALUE>|!GoogIterable} iter |
| 60 |
* @return {!ShimIterable} |
| 61 |
* @template VALUE |
| 62 |
*/ |
| 63 |
static of(iter) { |
| 64 |
if (iter instanceof ShimIterableImpl || iter instanceof ShimGoogIterator || |
| 65 |
iter instanceof ShimEs6Iterator) { |
| 66 |
return iter; |
| 67 |
} else if (typeof iter.next == 'function') { |
| 68 |
return new ShimIterableImpl( |
| 69 |
() => wrapGoog(/** @type {!Iterator|!GoogIterator} */ (iter))); |
| 70 |
} else if (typeof iter[Symbol.iterator] == 'function') { |
| 71 |
return new ShimIterableImpl(() => iter[Symbol.iterator]()); |
| 72 |
} else if (typeof iter.__iterator__ == 'function') { |
| 73 |
return new ShimIterableImpl(() => wrapGoog(iter.__iterator__())); |
| 74 |
} |
| 75 |
throw new Error('Not an iterator or iterable.'); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
/** |
| 81 |
* @param {!GoogIterator<VALUE>|!Iterator<VALUE>} iter |
| 82 |
* @return {!Iterator<VALUE>} |
| 83 |
* @template VALUE |
| 84 |
*/ |
| 85 |
const wrapGoog = (iter) => { |
| 86 |
if (!(iter instanceof GoogIterator)) return iter; |
| 87 |
let done = false; |
| 88 |
return /** @type {?} */ ({ |
| 89 |
next() { |
| 90 |
let value; |
| 91 |
while (!done) { |
| 92 |
try { |
| 93 |
value = iter.next(); |
| 94 |
break; |
| 95 |
} catch (err) { |
| 96 |
if (err !== StopIteration) throw err; |
| 97 |
done = true; |
| 98 |
} |
| 99 |
} |
| 100 |
return {value, done}; |
| 101 |
}, |
| 102 |
}); |
| 103 |
}; |
| 104 |
|
| 105 |
|
| 106 |
/** |
| 107 |
* Concrete (private) implementation of a non-iterator iterable. This is |
| 108 |
* separate from the iterator versions since it supports iterables that |
| 109 |
* are not "one-shot". |
| 110 |
* @implements {ShimIterable<VALUE>} |
| 111 |
* @template VALUE |
| 112 |
*/ |
| 113 |
class ShimIterableImpl { |
| 114 |
/** @param {function(): !Iterator<VALUE>} func */ |
| 115 |
constructor(func) { |
| 116 |
/** @const @private */ |
| 117 |
this.func_ = func; |
| 118 |
} |
| 119 |
|
| 120 |
/** @override */ |
| 121 |
__iterator__() { |
| 122 |
return new ShimGoogIterator(this.func_()); |
| 123 |
} |
| 124 |
|
| 125 |
/** @override */ |
| 126 |
toGoog() { |
| 127 |
return new ShimGoogIterator(this.func_()); |
| 128 |
} |
| 129 |
|
| 130 |
/** @override */ |
| 131 |
[Symbol.iterator]() { |
| 132 |
return new ShimEs6Iterator(this.func_()); |
| 133 |
} |
| 134 |
|
| 135 |
/** @override */ |
| 136 |
toEs6() { |
| 137 |
return new ShimEs6Iterator(this.func_()); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
/** |
| 143 |
* Concrete `goog.iter.Iterator` subclass that also implements `ShimIterable`. |
| 144 |
* @extends {GoogIterator<VALUE>} |
| 145 |
* @implements {ShimIterable<VALUE>} |
| 146 |
* @template VALUE |
| 147 |
*/ |
| 148 |
class ShimGoogIterator extends GoogIterator { |
| 149 |
/** @param {!Iterator<VALUE>} iter */ |
| 150 |
constructor(iter) { |
| 151 |
super(); |
| 152 |
this.iter_ = iter; |
| 153 |
} |
| 154 |
|
| 155 |
/** @override */ |
| 156 |
__iterator__() { |
| 157 |
// TODO(user): this seems ridiculous, but the compiler complains |
| 158 |
// that it's not implemented if we don't have it. |
| 159 |
return super.__iterator__(); |
| 160 |
} |
| 161 |
|
| 162 |
/** @override */ |
| 163 |
next() { |
| 164 |
const result = this.iter_.next(); |
| 165 |
if (result.done) throw StopIteration; |
| 166 |
return result.value; |
| 167 |
} |
| 168 |
|
| 169 |
/** @override */ |
| 170 |
toGoog() { |
| 171 |
return this; |
| 172 |
} |
| 173 |
|
| 174 |
/** @override */ |
| 175 |
[Symbol.iterator]() { |
| 176 |
return new ShimEs6Iterator(this.iter_); |
| 177 |
} |
| 178 |
|
| 179 |
/** @override */ |
| 180 |
toEs6() { |
| 181 |
return new ShimEs6Iterator(this.iter_); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
/** |
| 187 |
* Concrete ES6 `Iterator` that also implements `ShimIterable`. |
| 188 |
* @implements {IteratorIterable<VALUE>} |
| 189 |
* @extends {ShimIterableImpl<VALUE>} |
| 190 |
* @template VALUE |
| 191 |
*/ |
| 192 |
class ShimEs6Iterator extends ShimIterableImpl { |
| 193 |
/** @param {!Iterator<VALUE>} iter */ |
| 194 |
constructor(iter) { |
| 195 |
super(() => iter); |
| 196 |
/** @const @private */ |
| 197 |
this.iter_ = iter; |
| 198 |
} |
| 199 |
|
| 200 |
/** @override */ |
| 201 |
next() { |
| 202 |
return this.iter_.next(); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
exports = { |
| 208 |
ShimIterable, |
| 209 |
ShimEs6Iterator, |
| 210 |
ShimGoogIterator, |
| 211 |
}; |