Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[BREAKING] Remove IteratorSequence. Do not attempt to detect iterators. #1589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 0 additions & 200 deletions __tests__/IterableSeq.ts

This file was deleted.

4 changes: 4 additions & 0 deletions __tests__/Seq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ describe('Seq', () => {
expect(Seq({ a: 1, b: 2, c: 3 }).size).toBe(3);
});

it('accepts an object with a next property', () => {
expect(Seq({ a: 1, b: 2, next: _ => _ }).size).toBe(3);
});

it('accepts a collection string', () => {
expect(Seq('foo').size).toBe(3);
});
Expand Down
65 changes: 6 additions & 59 deletions src/Seq.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,55 +288,6 @@ class CollectionSeq extends IndexedSeq {
}
}

class IteratorSeq extends IndexedSeq {
constructor(iterator) {
this._iterator = iterator;
this._iteratorCache = [];
}

__iterateUncached(fn, reverse) {
if (reverse) {
return this.cacheResult().__iterate(fn, reverse);
}
const iterator = this._iterator;
const cache = this._iteratorCache;
let iterations = 0;
while (iterations < cache.length) {
if (fn(cache[iterations], iterations++, this) === false) {
return iterations;
}
}
let step;
while (!(step = iterator.next()).done) {
const val = step.value;
cache[iterations] = val;
if (fn(val, iterations++, this) === false) {
break;
}
}
return iterations;
}

__iteratorUncached(type, reverse) {
if (reverse) {
return this.cacheResult().__iterator(type, reverse);
}
const iterator = this._iterator;
const cache = this._iteratorCache;
let iterations = 0;
return new Iterator(() => {
if (iterations >= cache.length) {
const step = iterator.next();
if (step.done) {
return step;
}
cache[iterations] = step.value;
}
return iteratorValue(type, iterations, cache[iterations++]);
});
}
}

// # pragma Helper functions

export function isSeq(maybeSeq) {
Expand All @@ -352,11 +303,9 @@ function emptySequence() {
export function keyedSeqFromValue(value) {
const seq = Array.isArray(value)
? new ArraySeq(value)
: isIterator(value)
? new IteratorSeq(value)
: hasIterator(value)
? new CollectionSeq(value)
: undefined;
: hasIterator(value)
? new CollectionSeq(value)
: undefined;
if (seq) {
return seq.fromEntrySeq();
}
Expand Down Expand Up @@ -395,9 +344,7 @@ function seqFromValue(value) {
function maybeIndexedSeqFromValue(value) {
return isArrayLike(value)
? new ArraySeq(value)
: isIterator(value)
? new IteratorSeq(value)
: hasIterator(value)
? new CollectionSeq(value)
: undefined;
: hasIterator(value)
? new CollectionSeq(value)
: undefined;
}
15 changes: 11 additions & 4 deletions type-definitions/Immutable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3008,10 +3008,13 @@ declare module Immutable {
* * If a `Seq`, that same `Seq`.
* * If an `Collection`, a `Seq` of the same kind (Keyed, Indexed, or Set).
* * If an Array-like, an `Seq.Indexed`.
* * If an Object with an Iterator, an `Seq.Indexed`.
* * If an Iterator, an `Seq.Indexed`.
* * If an Iterable Object, an `Seq.Indexed`.
* * If an Object, a `Seq.Keyed`.
*
* Note: An Iterator itself will be treated as an object, becoming a `Seq.Keyed`,
* which is usually not what you want. You should turn your Iterator Object into
* an iterable object by defining a Symbol.iterator (or @@iterator) method which
* returns `this`.
*/
export function Seq<S extends Seq<any, any>>(seq: S): S;
export function Seq<K, V>(collection: Collection.Keyed<K, V>): Seq.Keyed<K, V>;
Expand Down Expand Up @@ -3723,13 +3726,17 @@ declare module Immutable {
*
* * If an `Collection`, that same `Collection`.
* * If an Array-like, an `Collection.Indexed`.
* * If an Object with an Iterator, an `Collection.Indexed`.
* * If an Iterator, an `Collection.Indexed`.
* * If an Object with an Iterator defined, an `Collection.Indexed`.
* * If an Object, an `Collection.Keyed`.
*
* This methods forces the conversion of Objects and Strings to Collections.
* If you want to ensure that a Collection of one item is returned, use
* `Seq.of`.
*
* Note: An Iterator itself will be treated as an object, becoming a `Seq.Keyed`,
* which is usually not what you want. You should turn your Iterator Object into
* an iterable object by defining a Symbol.iterator (or @@iterator) method which
* returns `this`.
*/
export function Collection<I extends Collection<any, any>>(collection: I): I;
export function Collection<T>(collection: Iterable<T>): Collection.Indexed<T>;
Expand Down