<commit_before><!--
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
-->

<script>
  window.Vaadin = window.Vaadin || {};
  window.Vaadin.Grid = window.Vaadin.Grid || {};

  /**
   * @polymerMixin
   */
  Vaadin.Grid.DataProviderMixin = superClass => class DataProviderMixin extends superClass {

    static get properties() {
      return {

        /**
         * Number of items fetched at a time from the dataprovider.
         */
        pageSize: {
          type: Number,
          value: 50,
          observer: '_pageSizeChanged'
        },

        /**
         * Function that provides items lazily. Receives arguments `params`, `callback`
         *
         * `params.page` Requested page index
         *
         * `params.pageSize` Current page size
         *
         * `params.filters` Currently applied filters
         *
         * `params.sortOrders` Currently applied sorting orders
         *
         */
        dataProvider: {
          type: Object,
          notify: true,
          observer: '_dataProviderChanged'
        },

        /**
         * `true` while data is being requested from the data provider.
         */
        _loading: {
          type: Boolean,
          observer: '_loadingChanged'
        },

        _cache: {
          type: Object,
          value: function() {
            return {};
          }
        },

        _pendingRequests: {
          type: Object,
          value: function() {
            return {};
          }
        },
      };
    }

    ready() {
      super.ready();
      this.addEventListener('item-changed', this._templateItemChanged.bind(this));
    }

    _loadingChanged(loading) {
      this._toggleAttribute('loading', loading, this);
    }

    _templateItemChanged(e) {
      const item = e.detail.item;

      // TODO: We could avoid iterating rows by fixing _physicalIndexForKey mapping
      // in iron-list-behavior so that vidx could be used to fetch the correct row element.
      Array.prototype.forEach.call(Polymer.dom(this.$.items).children, row => {
        if (row.item === item) {
          row.iterateCells(cell => {
            // prevent _forwardInstancePath from firing duplicate `item-changed`
            // events on all the sibling cells.
            cell._templatizer._suppressItemChangeEvent = true;
            cell.instance.notifyPath('item.' + e.detail.path, e.detail.value);
            cell._templatizer._suppressItemChangeEvent = false;
          });
        }
      });
    }

    _getCachedItem(index) {
      const page = this._getPageForIndex(index);

      const p = this._cache && this._cache[page];

      if (p) {
        return p[index - page * this.pageSize];
      } else {
        return null;
      }
    }

    _getItem(index, el) {
      this._virtualIndexToItem = this._virtualIndexToItem || {};

      if (el.index) {
        delete this._virtualIndexToItem[el.index];
      }
      el.index = index;
      this._virtualIndexToItem[el.index] = el;

      this._updateItem(el, this._getCachedItem(index));
      this._eagerlyLoadPages();
      //
      const uncachedPages = this._uncachedPagesForPhysicalItems();
      if (uncachedPages.length > 0) {
        this._loading = true;
        // avoid debounce during scroll to mitigate scrollbar flickering on iOS.
        // debouncing only when data is needed to be fetched. eager loading
        // should somewhat prevent from getting here when scrolling slowly.
        this._debouncerLoad = Polymer.Debouncer.debounce(
          this._debouncerLoad,
          Polymer.Async.timeOut.after(100),
          () => {
            uncachedPages.forEach(i => this._loadPage(i));
          });
      }
    }

    _cachedPagesForPhysicalItems() {
      return this._pagesForPhysicalItems().filter(page => this._cache !== undefined && this._cache[page] !== undefined);
    }

    _uncachedPagesForPhysicalItems() {
      return this._pagesForPhysicalItems().filter(page => this._cache !== undefined && this._cache[page] === undefined);
    }

    _eagerlyLoadPages() {
      const pages = this._cachedPagesForPhysicalItems().slice(0);

      if (pages.length > 0) {
        pages.sort((a, b) => a > b);

        const prev = Math.max(0, pages[0] - 1);
        const next = Math.min(pages[pages.length - 1] + 1, Math.max(0, Math.floor(this.size / this.pageSize) - 1));

        this._loadPage(prev);
        this._loadPage(next);
      }
    }

    _pagesForPhysicalItems() {
      // TODO: potentially heavy operation to run first visible index,
      // reconsider if performance issues occur on data binding / scrolling.
      // TODO: _vidxOffset shouldn't be read from here.
      const firstVisiblePage = this._getPageForIndex(this._firstVisibleIndex + this._vidxOffset);

      return [firstVisiblePage].concat(
        this._physicalItems
          .filter(row => row.index)
          .map(row => this._getPageForIndex(row.index))
        ).reduce((prev, curr) => {
          if (prev.indexOf(curr) === -1) {
            prev.push(curr);
          }

          return prev;
        }, []);
    }

    _updateItems(page, items) {
      for (let i = 0; i < this.pageSize; i++) {
        const index = page * this.pageSize + i;
        const row = this._virtualIndexToItem && this._virtualIndexToItem[index];
        if (row) {
          this._updateItem(row, items[i]);

          this._debouncerUpdateHeights = Polymer.Debouncer.debounce(this._debouncerUpdateHeights,
            Polymer.Async.timeOut.after(1), () => this._updateMetrics(), this._positionItems(), this._updateScrollerSize());
        }
      }
    }
<commit_msg>Revert a bad cleanup<commit_after><!--
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
-->

<script>
  window.Vaadin = window.Vaadin || {};
  window.Vaadin.Grid = window.Vaadin.Grid || {};

  /**
   * @polymerMixin
   */
  Vaadin.Grid.DataProviderMixin = superClass => class DataProviderMixin extends superClass {

    static get properties() {
      return {

        /**
         * Number of items fetched at a time from the dataprovider.
         */
        pageSize: {
          type: Number,
          value: 50,
          observer: '_pageSizeChanged'
        },

        /**
         * Function that provides items lazily. Receives arguments `params`, `callback`
         *
         * `params.page` Requested page index
         *
         * `params.pageSize` Current page size
         *
         * `params.filters` Currently applied filters
         *
         * `params.sortOrders` Currently applied sorting orders
         *
         */
        dataProvider: {
          type: Object,
          notify: true,
          observer: '_dataProviderChanged'
        },

        /**
         * `true` while data is being requested from the data provider.
         */
        _loading: {
          type: Boolean,
          observer: '_loadingChanged'
        },

        _cache: {
          type: Object,
          value: function() {
            return {};
          }
        },

        _pendingRequests: {
          type: Object,
          value: function() {
            return {};
          }
        },
      };
    }

    ready() {
      super.ready();
      this.addEventListener('item-changed', this._templateItemChanged.bind(this));
    }

    _loadingChanged(loading) {
      this._toggleAttribute('loading', loading, this);
    }

    _templateItemChanged(e) {
      const item = e.detail.item;

      // TODO: We could avoid iterating rows by fixing _physicalIndexForKey mapping
      // in iron-list-behavior so that vidx could be used to fetch the correct row element.
      Array.prototype.forEach.call(Polymer.dom(this.$.items).children, row => {
        if (row.item === item) {
          row.iterateCells(cell => {
            // prevent _forwardInstancePath from firing duplicate `item-changed`
            // events on all the sibling cells.
            cell._templatizer._suppressItemChangeEvent = true;
            cell.instance.notifyPath('item.' + e.detail.path, e.detail.value);
            cell._templatizer._suppressItemChangeEvent = false;
          });
        }
      });
    }

    _getCachedItem(index) {
      const page = this._getPageForIndex(index);

      const p = this._cache && this._cache[page];

      if (p) {
        return p[index - page * this.pageSize];
      } else {
        return null;
      }
    }

    _getItem(index, el) {
      this._virtualIndexToItem = this._virtualIndexToItem || {};

      if (el.index) {
        delete this._virtualIndexToItem[el.index];
      }
      el.index = index;
      this._virtualIndexToItem[el.index] = el;

      this._updateItem(el, this._getCachedItem(index));
      this._eagerlyLoadPages();
      //
      const uncachedPages = this._uncachedPagesForPhysicalItems();
      if (uncachedPages.length > 0) {
        this._loading = true;
        // avoid debounce during scroll to mitigate scrollbar flickering on iOS.
        // debouncing only when data is needed to be fetched. eager loading
        // should somewhat prevent from getting here when scrolling slowly.
        this._debouncerLoad = Polymer.Debouncer.debounce(
          this._debouncerLoad,
          Polymer.Async.timeOut.after(100),
          () => {
            uncachedPages.forEach(i => this._loadPage(i));
          });
      }
    }

    _cachedPagesForPhysicalItems() {
      return this._pagesForPhysicalItems().filter(page => this._cache !== undefined && this._cache[page] !== undefined);
    }

    _uncachedPagesForPhysicalItems() {
      return this._pagesForPhysicalItems().filter(page => this._cache !== undefined && this._cache[page] === undefined);
    }

    _eagerlyLoadPages() {
      const pages = this._cachedPagesForPhysicalItems().slice(0);

      if (pages.length > 0) {
        pages.sort((a, b) => a > b);

        const prev = Math.max(0, pages[0] - 1);
        const next = Math.min(pages[pages.length - 1] + 1, Math.max(0, Math.floor(this.size / this.pageSize) - 1));

        this._loadPage(prev);
        this._loadPage(next);
      }
    }

    _pagesForPhysicalItems() {
      // TODO: potentially heavy operation to run first visible index,
      // reconsider if performance issues occur on data binding / scrolling.
      // TODO: _vidxOffset shouldn't be read from here.
      const firstVisiblePage = this._getPageForIndex(this._firstVisibleIndex + this._vidxOffset);

      return [firstVisiblePage].concat(
        this._physicalItems
          .filter(row => row.index)
          .map(row => this._getPageForIndex(row.index))
        ).reduce((prev, curr) => {
          if (prev.indexOf(curr) === -1) {
            prev.push(curr);
          }

          return prev;
        }, []);
    }

    _updateItems(page, items) {
      for (let i = 0; i < this.pageSize; i++) {
        const index = page * this.pageSize + i;
        const row = this._virtualIndexToItem && this._virtualIndexToItem[index];
        if (row) {
          this._updateItem(row, items[i]);

          this._debouncerUpdateHeights = Polymer.Debouncer.debounce(this._debouncerUpdateHeights,
            Polymer.Async.timeOut.after(1), () => {
              this._updateMetrics();
              this._positionItems();
              this._updateScrollerSize();
            }
          );
        }
      }
    }

{'fraction_non_alphanumeric': 0.11765218148567448, 'fraction_numerical': 0.004180092310371854, 'mean_word_length': 2.0086455331412103, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 37, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5984758', 'n_tokens_mistral': 3337, 'n_tokens_neox': 3172, 'n_words': 729}