@reactivex/ix-es5-esm
Version:
The Interactive Extensions for JavaScript
113 lines (111 loc) • 5.04 kB
JavaScript
import { __asyncGenerator, __await, __extends, __generator } from "tslib";
import { AsyncIterableX } from '../asynciterablex.js';
import { MaxRefCountList, RefCountList, } from '../../iterable/operators/_refcountlist.js';
import { create } from '../create.js';
import { throwIfAborted } from '../../aborterror.js';
/** @ignore */
var MemoizeAsyncBuffer = /** @class */ (function (_super) {
__extends(MemoizeAsyncBuffer, _super);
function MemoizeAsyncBuffer(source, buffer) {
var _this = _super.call(this) || this;
_this._error = null;
_this._shared = null;
_this._stopped = false;
_this._source = source;
_this._buffer = buffer;
return _this;
}
MemoizeAsyncBuffer.prototype[Symbol.asyncIterator] = function (signal) {
throwIfAborted(signal);
return this._getIterable(0);
};
MemoizeAsyncBuffer.prototype._getIterable = function (offset) {
if (offset === void 0) { offset = 0; }
return __asyncGenerator(this, arguments, function _getIterable_1() {
var i, done, buffer;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
i = offset - 1;
done = false;
buffer = this._buffer;
_a.label = 1;
case 1:
_a.trys.push([1, , 11, 12]);
_a.label = 2;
case 2:
if (!(++i < buffer.count)) return [3 /*break*/, 5];
return [4 /*yield*/, __await(buffer.get(i))];
case 3: return [4 /*yield*/, _a.sent()];
case 4:
_a.sent();
return [3 /*break*/, 9];
case 5:
if (this._stopped) {
throw this._error;
}
if (this._shared === null) {
this._shared = this._source.next().then(function (r) {
_this._shared = null;
if (!r.done) {
buffer.push(r.value);
}
return r;
});
}
return [4 /*yield*/, __await(this._shared.catch(function (e) {
_this._error = e;
_this._stopped = true;
throw e;
}))];
case 6:
(done = (_a.sent()).done);
if (!!done) return [3 /*break*/, 9];
return [4 /*yield*/, __await(buffer.get(i))];
case 7: return [4 /*yield*/, _a.sent()];
case 8:
_a.sent();
_a.label = 9;
case 9:
if (!done) return [3 /*break*/, 2];
_a.label = 10;
case 10: return [3 /*break*/, 12];
case 11:
buffer.done();
return [7 /*endfinally*/];
case 12: return [2 /*return*/];
}
});
});
};
return MemoizeAsyncBuffer;
}(AsyncIterableX));
export { MemoizeAsyncBuffer };
/**
* 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: AsyncIterable<TSource>) => AsyncIterable<TResult>} [selector] Selector function with memoized access
* to the source sequence for a specified number of iterators.
* @returns {(OperatorAsyncFunction<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 MemoizeAsyncBuffer(source[Symbol.asyncIterator](), new MaxRefCountList())
: new MemoizeAsyncBuffer(source[Symbol.asyncIterator](), new RefCountList(readerCount));
}
return create(function () {
return selector(memoize(readerCount)(source))[Symbol.asyncIterator]();
});
};
}
//# sourceMappingURL=memoize.js.map