{"version":3,"file":"recycle-view-repeater-strategy-DoWdPqVw.mjs","sources":["../../../../../darwin_arm64-fastbuild-ST-46c76129e412/bin/src/cdk/collections/array-data-source.ts","../../../../../darwin_arm64-fastbuild-ST-46c76129e412/bin/src/cdk/collections/view-repeater.ts","../../../../../darwin_arm64-fastbuild-ST-46c76129e412/bin/src/cdk/collections/recycle-view-repeater-strategy.ts"],"sourcesContent":["/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.dev/license\n */\n\nimport {Observable, isObservable, of as observableOf} from 'rxjs';\nimport {DataSource} from './data-source';\n\n/** DataSource wrapper for a native array. */\nexport class ArrayDataSource<T> extends DataSource<T> {\n  constructor(private _data: readonly T[] | Observable<readonly T[]>) {\n    super();\n  }\n\n  connect(): Observable<readonly T[]> {\n    return isObservable(this._data) ? this._data : observableOf(this._data);\n  }\n\n  disconnect() {}\n}\n","/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.dev/license\n */\n\nimport {\n  InjectionToken,\n  IterableChangeRecord,\n  IterableChanges,\n  TemplateRef,\n  ViewContainerRef,\n} from '@angular/core';\n\n/**\n * The context for an embedded view in the repeater's view container.\n *\n * @template T The type for the embedded view's $implicit property.\n */\nexport interface _ViewRepeaterItemContext<T> {\n  $implicit?: T;\n}\n\n/**\n * The arguments needed to construct an embedded view for an item in a view\n * container.\n *\n * @template C The type for the context passed to each embedded view.\n */\nexport interface _ViewRepeaterItemInsertArgs<C> {\n  templateRef: TemplateRef<C>;\n  context?: C;\n  index?: number;\n}\n\n/**\n * A factory that derives the embedded view context for an item in a view\n * container.\n *\n * @template T The type for the embedded view's $implicit property.\n * @template R The type for the item in each IterableDiffer change record.\n * @template C The type for the context passed to each embedded view.\n */\nexport type _ViewRepeaterItemContextFactory<T, R, C extends _ViewRepeaterItemContext<T>> = (\n  record: IterableChangeRecord<R>,\n  adjustedPreviousIndex: number | null,\n  currentIndex: number | null,\n) => _ViewRepeaterItemInsertArgs<C>;\n\n/**\n * Extracts the value of an item from an {@link IterableChangeRecord}.\n *\n * @template T The type for the embedded view's $implicit property.\n * @template R The type for the item in each IterableDiffer change record.\n */\nexport type _ViewRepeaterItemValueResolver<T, R> = (record: IterableChangeRecord<R>) => T;\n\n/** Indicates how a view was changed by a {@link _ViewRepeater}. */\nexport enum _ViewRepeaterOperation {\n  /** The content of an existing view was replaced with another item. */\n  REPLACED,\n  /** A new view was created with `createEmbeddedView`. */\n  INSERTED,\n  /** The position of a view changed, but the content remains the same. */\n  MOVED,\n  /** A view was detached from the view container. */\n  REMOVED,\n}\n\n/**\n * Meta data describing the state of a view after it was updated by a\n * {@link _ViewRepeater}.\n *\n * @template R The type for the item in each IterableDiffer change record.\n * @template C The type for the context passed to each embedded view.\n */\nexport interface _ViewRepeaterItemChange<R, C> {\n  /** The view's context after it was changed. */\n  context?: C;\n  /** Indicates how the view was changed. */\n  operation: _ViewRepeaterOperation;\n  /** The view's corresponding change record. */\n  record: IterableChangeRecord<R>;\n}\n\n/**\n * Type for a callback to be executed after a view has changed.\n *\n * @template R The type for the item in each IterableDiffer change record.\n * @template C The type for the context passed to each embedded view.\n */\nexport type _ViewRepeaterItemChanged<R, C> = (change: _ViewRepeaterItemChange<R, C>) => void;\n\n/**\n * Describes a strategy for rendering items in a {@link ViewContainerRef}.\n *\n * @template T The type for the embedded view's $implicit property.\n * @template R The type for the item in each IterableDiffer change record.\n * @template C The type for the context passed to each embedded view.\n */\nexport interface _ViewRepeater<T, R, C extends _ViewRepeaterItemContext<T>> {\n  applyChanges(\n    changes: IterableChanges<R>,\n    viewContainerRef: ViewContainerRef,\n    itemContextFactory: _ViewRepeaterItemContextFactory<T, R, C>,\n    itemValueResolver: _ViewRepeaterItemValueResolver<T, R>,\n    itemViewChanged?: _ViewRepeaterItemChanged<R, C>,\n  ): void;\n\n  detach(): void;\n}\n\n/**\n * Injection token for {@link _ViewRepeater}. This token is for use by Angular Material only.\n * @docs-private\n */\nexport const _VIEW_REPEATER_STRATEGY = new InjectionToken<\n  _ViewRepeater<unknown, unknown, _ViewRepeaterItemContext<unknown>>\n>('_ViewRepeater');\n","/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.dev/license\n */\n\nimport {\n  EmbeddedViewRef,\n  IterableChangeRecord,\n  IterableChanges,\n  ViewContainerRef,\n} from '@angular/core';\nimport {\n  _ViewRepeater,\n  _ViewRepeaterItemChanged,\n  _ViewRepeaterItemContext,\n  _ViewRepeaterItemContextFactory,\n  _ViewRepeaterItemInsertArgs,\n  _ViewRepeaterItemValueResolver,\n  _ViewRepeaterOperation,\n} from './view-repeater';\n\n/**\n * A repeater that caches views when they are removed from a\n * {@link ViewContainerRef}. When new items are inserted into the container,\n * the repeater will reuse one of the cached views instead of creating a new\n * embedded view. Recycling cached views reduces the quantity of expensive DOM\n * inserts.\n *\n * @template T The type for the embedded view's $implicit property.\n * @template R The type for the item in each IterableDiffer change record.\n * @template C The type for the context passed to each embedded view.\n */\nexport class _RecycleViewRepeaterStrategy<T, R, C extends _ViewRepeaterItemContext<T>>\n  implements _ViewRepeater<T, R, C>\n{\n  /**\n   * The size of the cache used to store unused views.\n   * Setting the cache size to `0` will disable caching. Defaults to 20 views.\n   */\n  viewCacheSize: number = 20;\n\n  /**\n   * View cache that stores embedded view instances that have been previously stamped out,\n   * but don't are not currently rendered. The view repeater will reuse these views rather than\n   * creating brand new ones.\n   *\n   * TODO(michaeljamesparsons) Investigate whether using a linked list would improve performance.\n   */\n  private _viewCache: EmbeddedViewRef<C>[] = [];\n\n  /** Apply changes to the DOM. */\n  applyChanges(\n    changes: IterableChanges<R>,\n    viewContainerRef: ViewContainerRef,\n    itemContextFactory: _ViewRepeaterItemContextFactory<T, R, C>,\n    itemValueResolver: _ViewRepeaterItemValueResolver<T, R>,\n    itemViewChanged?: _ViewRepeaterItemChanged<R, C>,\n  ) {\n    // Rearrange the views to put them in the right location.\n    changes.forEachOperation(\n      (\n        record: IterableChangeRecord<R>,\n        adjustedPreviousIndex: number | null,\n        currentIndex: number | null,\n      ) => {\n        let view: EmbeddedViewRef<C> | undefined;\n        let operation: _ViewRepeaterOperation;\n        if (record.previousIndex == null) {\n          // Item added.\n          const viewArgsFactory = () =>\n            itemContextFactory(record, adjustedPreviousIndex, currentIndex);\n          view = this._insertView(\n            viewArgsFactory,\n            currentIndex!,\n            viewContainerRef,\n            itemValueResolver(record),\n          );\n          operation = view ? _ViewRepeaterOperation.INSERTED : _ViewRepeaterOperation.REPLACED;\n        } else if (currentIndex == null) {\n          // Item removed.\n          this._detachAndCacheView(adjustedPreviousIndex!, viewContainerRef);\n          operation = _ViewRepeaterOperation.REMOVED;\n        } else {\n          // Item moved.\n          view = this._moveView(\n            adjustedPreviousIndex!,\n            currentIndex!,\n            viewContainerRef,\n            itemValueResolver(record),\n          );\n          operation = _ViewRepeaterOperation.MOVED;\n        }\n\n        if (itemViewChanged) {\n          itemViewChanged({\n            context: view?.context,\n            operation,\n            record,\n          });\n        }\n      },\n    );\n  }\n\n  detach() {\n    for (const view of this._viewCache) {\n      view.destroy();\n    }\n    this._viewCache = [];\n  }\n\n  /**\n   * Inserts a view for a new item, either from the cache or by creating a new\n   * one. Returns `undefined` if the item was inserted into a cached view.\n   */\n  private _insertView(\n    viewArgsFactory: () => _ViewRepeaterItemInsertArgs<C>,\n    currentIndex: number,\n    viewContainerRef: ViewContainerRef,\n    value: T,\n  ): EmbeddedViewRef<C> | undefined {\n    const cachedView = this._insertViewFromCache(currentIndex!, viewContainerRef);\n    if (cachedView) {\n      cachedView.context.$implicit = value;\n      return undefined;\n    }\n\n    const viewArgs = viewArgsFactory();\n    return viewContainerRef.createEmbeddedView(\n      viewArgs.templateRef,\n      viewArgs.context,\n      viewArgs.index,\n    );\n  }\n\n  /** Detaches the view at the given index and inserts into the view cache. */\n  private _detachAndCacheView(index: number, viewContainerRef: ViewContainerRef) {\n    const detachedView = viewContainerRef.detach(index) as EmbeddedViewRef<C>;\n    this._maybeCacheView(detachedView, viewContainerRef);\n  }\n\n  /** Moves view at the previous index to the current index. */\n  private _moveView(\n    adjustedPreviousIndex: number,\n    currentIndex: number,\n    viewContainerRef: ViewContainerRef,\n    value: T,\n  ): EmbeddedViewRef<C> {\n    const view = viewContainerRef.get(adjustedPreviousIndex!) as EmbeddedViewRef<C>;\n    viewContainerRef.move(view, currentIndex);\n    view.context.$implicit = value;\n    return view;\n  }\n\n  /**\n   * Cache the given detached view. If the cache is full, the view will be\n   * destroyed.\n   */\n  private _maybeCacheView(view: EmbeddedViewRef<C>, viewContainerRef: ViewContainerRef) {\n    if (this._viewCache.length < this.viewCacheSize) {\n      this._viewCache.push(view);\n    } else {\n      const index = viewContainerRef.indexOf(view);\n\n      // The host component could remove views from the container outside of\n      // the view repeater. It's unlikely this will occur, but just in case,\n      // destroy the view on its own, otherwise destroy it through the\n      // container to ensure that all the references are removed.\n      if (index === -1) {\n        view.destroy();\n      } else {\n        viewContainerRef.remove(index);\n      }\n    }\n  }\n\n  /** Inserts a recycled view from the cache at the given index. */\n  private _insertViewFromCache(\n    index: number,\n    viewContainerRef: ViewContainerRef,\n  ): EmbeddedViewRef<C> | null {\n    const cachedView = this._viewCache.pop();\n    if (cachedView) {\n      viewContainerRef.insert(cachedView, index);\n    }\n    return cachedView || null;\n  }\n}\n"],"names":["observableOf"],"mappings":";;;;AAWA;AACM,MAAO,eAAmB,SAAQ,UAAa,CAAA;AAC/B,IAAA,KAAA;AAApB,IAAA,WAAA,CAAoB,KAA8C,EAAA;AAChE,QAAA,KAAK,EAAE;QADW,IAAK,CAAA,KAAA,GAAL,KAAK;;IAIzB,OAAO,GAAA;QACL,OAAO,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,KAAK,GAAGA,EAAY,CAAC,IAAI,CAAC,KAAK,CAAC;;AAGzE,IAAA,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