UNPKG

@reactivex/ix-es5-esm

Version:

The Interactive Extensions for JavaScript

101 lines (99 loc) 4.19 kB
import { __extends, __generator } from "tslib"; import { IterableX } from '../iterablex.js'; import { MaxRefCountList, RefCountList } from './_refcountlist.js'; import { create } from '../create.js'; var MemoizeBuffer = /** @class */ (function (_super) { __extends(MemoizeBuffer, _super); function MemoizeBuffer(source, buffer) { var _this = _super.call(this) || this; _this._stopped = false; _this._source = source; _this._buffer = buffer; return _this; } // eslint-disable-next-line complexity MemoizeBuffer.prototype[Symbol.iterator] = function () { var i, hasValue, current, next; return __generator(this, function (_a) { switch (_a.label) { case 0: i = 0; _a.label = 1; case 1: _a.trys.push([1, , 7, 8]); _a.label = 2; case 2: if (!1) return [3 /*break*/, 6]; hasValue = false; current = {}; if (i >= this._buffer.count) { if (!this._stopped) { try { next = this._source.next(); hasValue = !next.done; // eslint-disable-next-line max-depth if (hasValue) { current = next.value; } } catch (e) { this._error = e; this._stopped = true; } } if (this._stopped) { throw this._error; } if (hasValue) { this._buffer.push(current); } } else { hasValue = true; } if (!hasValue) return [3 /*break*/, 4]; return [4 /*yield*/, this._buffer.get(i)]; case 3: _a.sent(); return [3 /*break*/, 5]; case 4: return [3 /*break*/, 6]; case 5: i++; return [3 /*break*/, 2]; case 6: return [3 /*break*/, 8]; case 7: this._buffer.done(); return [7 /*endfinally*/]; case 8: return [2 /*return*/]; } }); }; return MemoizeBuffer; }(IterableX)); /** * Memoizes the source sequence within a selector function where a specified number of iterators can get access * to all of the sequence's elements without causing multiple iterations over the source. * * @template TSource Source sequence element type. * @template TResult Result sequence element type. * @param {number} [readerCount=-1] Number of iterators that can access the underlying buffer. Once every * iterator has obtained an element from the buffer, the element is removed from the buffer. * @param {(value: Iterable<TSource>) => Iterable<TResult>} [selector] Selector function with memoized access * to the source sequence for a specified number of iterators. * @returns {(OperatorFunction<TSource, TSource | TResult>)} Sequence resulting from applying the selector function to the * memoized view over the source sequence. */ export function memoize(readerCount, selector) { if (readerCount === void 0) { readerCount = -1; } return function memoizeOperatorFunction(source) { if (!selector) { return readerCount === -1 ? new MemoizeBuffer(source[Symbol.iterator](), new MaxRefCountList()) : new MemoizeBuffer(source[Symbol.iterator](), new RefCountList(readerCount)); } return create(function () { return selector(memoize(readerCount)(source))[Symbol.iterator](); }); }; } //# sourceMappingURL=memoize.js.map