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CAAC,OAAO,CAAC,CAAC;qBAC5B;iBACF;qBAAM;oBACL,QAAQ,GAAG,IAAI,CAAC;iBACjB;gBAED,IAAI,QAAQ,EAAE;oBACZ,MAAM,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;iBAC3B;qBAAM;oBACL,MAAM;iBACP;gBAED,CAAC,EAAE,CAAC;aACL;SACF;gBAAS;YACR,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;SACrB;IACH,CAAC;CACF;AA+BD;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,OAAO,CACrB,cAAsB,CAAC,CAAC,EACxB,QAA0D;IAE1D,OAAO,SAAS,uBAAuB,CAAC,MAAyB;QAC/D,IAAI,CAAC,QAAQ,EAAE;YACb,OAAO,WAAW,KAAK,CAAC,CAAC;gBACvB,CAAC,CAAC,IAAI,aAAa,CAAU,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,EAAE,IAAI,eAAe,EAAW,CAAC;gBACvF,CAAC,CAAC,IAAI,aAAa,CACjB,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,EACzB,IAAI,YAAY,CAAU,WAAW,CAAC,CACvC,CAAC;SACL;QACD,OAAO,MAAM,CAAoB,GAAG,EAAE,CACpC,QAAS,CAAC,OAAO,CAAU,WAAW,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CACpE,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC","file":"memoize.js","sourcesContent":["import { IterableX } from '../iterablex';\nimport { IRefCountList, MaxRefCountList, RefCountList } from './_refcountlist';\nimport { create } from '../create';\nimport { OperatorFunction } from '../../interfaces';\n\nclass MemoizeBuffer<T> extends IterableX<T> {\n  private _source: Iterator<T>;\n  private _buffer: IRefCountList<T>;\n  private _error: any;\n  private _stopped: boolean = false;\n\n  constructor(source: Iterator<T>, buffer: IRefCountList<T>) {\n    super();\n    this._source = source;\n    this._buffer = buffer;\n  }\n\n  // eslint-disable-next-line complexity\n  *[Symbol.iterator]() {\n    let i = 0;\n    try {\n      while (1) {\n        let hasValue = false;\n        let current = <T>{};\n        if (i >= this._buffer.count) {\n          if (!this._stopped) {\n            try {\n              const next = this._source.next();\n              hasValue = !next.done;\n              // eslint-disable-next-line max-depth\n              if (hasValue) {\n                current = next.value;\n              }\n            } catch (e) {\n              this._error = e;\n              this._stopped = true;\n            }\n          }\n\n          if (this._stopped) {\n            throw this._error;\n          }\n\n          if (hasValue) {\n            this._buffer.push(current);\n          }\n        } else {\n          hasValue = true;\n        }\n\n        if (hasValue) {\n          yield this._buffer.get(i);\n        } else {\n          break;\n        }\n\n        i++;\n      }\n    } finally {\n      this._buffer.done();\n    }\n  }\n}\n\n/**\n * Creates a buffer with a view over the source sequence, causing a specified number of iterators to obtain access\n * to all of the sequence's elements without causing multiple enumerations over the source.\n * @export\n * @template TSource Source sequence element type.\n * @param {number} [readerCount] Number of iterators that can access the underlying buffer.\n * Once every iterator has obtained an element from the buffer, the element is removed from the buffer.\n * @returns {OperatorFunction<TSource, TSource>} Buffer enabling a specified number of iterators to retrieve all\n * elements from the shared source sequence, without duplicating source iteration side-effects.\n */\nexport function memoize<TSource>(readerCount?: number): OperatorFunction<TSource, TSource>;\n/**\n * Memoizes the source sequence within a selector function where a specified number of iterators can get access\n * to all of the sequence's elements without causing multiple iterations over the source.\n *\n * @export\n * @template TSource Source sequence element type.\n * @template TResult Result sequence element type.\n * @param {number} [readerCount] Number of iterators that can access the underlying buffer. Once every\n * iterator has obtained an element from the buffer, the element is removed from the buffer.\n * @param {(value: Iterable<TSource>) => Iterable<TResult>} [selector] Selector function with memoized access\n * to the source sequence for a specified number of iterators.\n * @returns {OperatorFunction<TSource, TResult>} Sequence resulting from applying the selector function to the\n * memoized view over the source sequence.\n */\nexport function memoize<TSource, TResult>(\n  readerCount?: number,\n  selector?: (value: Iterable<TSource>) => Iterable<TResult>\n): OperatorFunction<TSource, TResult>;\n/**\n * Memoizes the source sequence within a selector function where a specified number of iterators can get access\n * to all of the sequence's elements without causing multiple iterations over the source.\n *\n * @export\n * @template TSource Source sequence element type.\n * @template TResult Result sequence element type.\n * @param {number} [readerCount=-1] Number of iterators that can access the underlying buffer. Once every\n * iterator has obtained an element from the buffer, the element is removed from the buffer.\n * @param {(value: Iterable<TSource>) => Iterable<TResult>} [selector] Selector function with memoized access\n * to the source sequence for a specified number of iterators.\n * @returns {(OperatorFunction<TSource, TSource | TResult>)} Sequence resulting from applying the selector function to the\n * memoized view over the source sequence.\n */\nexport function memoize<TSource, TResult = TSource>(\n  readerCount: number = -1,\n  selector?: (value: Iterable<TSource>) => Iterable<TResult>\n): OperatorFunction<TSource, TSource | TResult> {\n  return function memoizeOperatorFunction(source: Iterable<TSource>): IterableX<TSource | TResult> {\n    if (!selector) {\n      return readerCount === -1\n        ? new MemoizeBuffer<TSource>(source[Symbol.iterator](), new MaxRefCountList<TSource>())\n        : new MemoizeBuffer<TSource>(\n          source[Symbol.iterator](),\n          new RefCountList<TSource>(readerCount)\n        );\n    }\n    return create<TSource | TResult>(() =>\n      selector!(memoize<TSource>(readerCount)(source))[Symbol.iterator]()\n    );\n  };\n}\n"]}