<commit_before>import { Selection } from './selection';
import { Collection } from './collection';
import { ArrayUtils } from './utils/arrayUtils';
import { Entity, DatasourceConfig, SortObject, FilterObject } from './interfaces';

export class DataSource {
  public entity: Entity;
  private selection: Selection;
  private key: string;
  private arrayUtils: ArrayUtils;
  private mainArray: Entity[];
  private config: DatasourceConfig;
  private eventIdCount: number;
  private eventCallBacks: Function[];
  private collection: Collection;

  constructor(selection: Selection, config?: DatasourceConfig) {
    // selection
    this.selection = selection || new Selection('single');
    // overide selection get row/key from row
    // why not in selection ? because I might need rowbased selection only
    this.selection.overrideGetRowKey(this.getRowKey.bind(this));
    this.selection.overrideGetRowKeys(this.getRowKeys.bind(this));
    // array helper helps with grouping/sorting and filtering
    this.arrayUtils = new ArrayUtils();
    // key if you dont want grid to add
    this.key = null;
    // main array fill contain all the data set to grid
    this.mainArray = null;
    // configuration
    this.config = config;
    if (config) {
      this.key = config.key || '__avgKey';
    } else {
      this.key = '__avgKey';
    }
    // todo, give option to override ArrayUtils, 
    // or and option to set params you pass into array helper to override some of its functionality
    // events, gridConnector will add event lister to datasource set to it
    this.eventIdCount = -1;
    this.eventCallBacks = [];
    // current entity, this is what users need to link inputs etc too
    this.entity = null;
    // create a collection
    this.collection = new Collection(this);

  }

  public getSelection(): Selection {
    return this.selection;
  }

  public getKey(): string {
    return this.key;
  }

  public length(): number {
    return this.collection.length;
  }

  public triggerEvent(event: string): void {
    // call all event listeners
    this.eventCallBacks.forEach((FN) => {
      FN(event);
    });
  }

  public addEventListener(callback: Function): number {
    // add key
    this.eventIdCount++;
    // add to callback queue
    this.eventCallBacks.push(callback);
    // return ID, so they can remove listnener
    return this.eventIdCount;
  }

  public removeEventListener(id: number): void {
    // remove listtener from id
    this.eventCallBacks.splice(id, 1);
  }

  public setArray(array: Entity[]): void {
    // new collection
    this.collection = new Collection(this);
    // clear stuff set in ArrayUtils
    this.selection.reset();
    this.arrayUtils.resetGrouping();
    this.arrayUtils.resetSort();

    // reset current entity
    this.entity = null;

    // set data to collection
    this.collection.setData(array);
    // set our main collection, we will use this for later
    this.mainArray = this.collection.getEntities();
    this.triggerEvent('collection_changed');
  }

  public push(array: Entity[]): void {
    if (Array.isArray(array)) {
      let grouping = this.arrayUtils.getGrouping();
      let collection = this.collection.getEntities();
      collection = collection.concat(array);
      this.collection.setData(collection);
      this.mainArray = this.collection.getEntities();
      this.arrayUtils.runOrderbyOn(this.collection.getEntities());
      let untouchedgrouped = this.collection.getEntities();
      if (grouping.length > 0) {
        let groupedArray = this.arrayUtils.group(untouchedgrouped, grouping, true);
        this.collection.setData(groupedArray, untouchedgrouped);
      }
      this.triggerEvent('collection_updated');
    }
  }

  public refresh(data?: any) {
    if (data) {
      this.collection = new Collection(this);
      this.collection.setData(data);
      this.mainArray = this.collection.getEntities();
    }
    let grouping = this.arrayUtils.getGrouping();
    this.arrayUtils.runOrderbyOn(this.collection.getEntities());
    if (grouping.length > 0) {
      let untouchedgrouped = this.collection.getEntities();
      let groupedArray = this.arrayUtils.group(untouchedgrouped, grouping, true);
      this.collection.setData(groupedArray, untouchedgrouped);
    }
    this.triggerEvent('collection_updated');
  }<commit_msg>fix(datasource) if replacing during refresh current entity will be set to null<commit_after>import { Selection } from './selection';
import { Collection } from './collection';
import { ArrayUtils } from './utils/arrayUtils';
import { Entity, DatasourceConfig, SortObject, FilterObject } from './interfaces';

export class DataSource {
  public entity: Entity;
  private selection: Selection;
  private key: string;
  private arrayUtils: ArrayUtils;
  private mainArray: Entity[];
  private config: DatasourceConfig;
  private eventIdCount: number;
  private eventCallBacks: Function[];
  private collection: Collection;

  constructor(selection: Selection, config?: DatasourceConfig) {
    // selection
    this.selection = selection || new Selection('single');
    // overide selection get row/key from row
    // why not in selection ? because I might need rowbased selection only
    this.selection.overrideGetRowKey(this.getRowKey.bind(this));
    this.selection.overrideGetRowKeys(this.getRowKeys.bind(this));
    // array helper helps with grouping/sorting and filtering
    this.arrayUtils = new ArrayUtils();
    // key if you dont want grid to add
    this.key = null;
    // main array fill contain all the data set to grid
    this.mainArray = null;
    // configuration
    this.config = config;
    if (config) {
      this.key = config.key || '__avgKey';
    } else {
      this.key = '__avgKey';
    }
    // todo, give option to override ArrayUtils, 
    // or and option to set params you pass into array helper to override some of its functionality
    // events, gridConnector will add event lister to datasource set to it
    this.eventIdCount = -1;
    this.eventCallBacks = [];
    // current entity, this is what users need to link inputs etc too
    this.entity = null;
    // create a collection
    this.collection = new Collection(this);

  }

  public getSelection(): Selection {
    return this.selection;
  }

  public getKey(): string {
    return this.key;
  }

  public length(): number {
    return this.collection.length;
  }

  public triggerEvent(event: string): void {
    // call all event listeners
    this.eventCallBacks.forEach((FN) => {
      FN(event);
    });
  }

  public addEventListener(callback: Function): number {
    // add key
    this.eventIdCount++;
    // add to callback queue
    this.eventCallBacks.push(callback);
    // return ID, so they can remove listnener
    return this.eventIdCount;
  }

  public removeEventListener(id: number): void {
    // remove listtener from id
    this.eventCallBacks.splice(id, 1);
  }

  public setArray(array: Entity[]): void {
    // new collection
    this.collection = new Collection(this);
    // clear stuff set in ArrayUtils
    this.selection.reset();
    this.arrayUtils.resetGrouping();
    this.arrayUtils.resetSort();

    // reset current entity
    this.entity = null;

    // set data to collection
    this.collection.setData(array);
    // set our main collection, we will use this for later
    this.mainArray = this.collection.getEntities();
    this.triggerEvent('collection_changed');
  }

  public push(array: Entity[]): void {
    if (Array.isArray(array)) {
      let grouping = this.arrayUtils.getGrouping();
      let collection = this.collection.getEntities();
      collection = collection.concat(array);
      this.collection.setData(collection);
      this.mainArray = this.collection.getEntities();
      this.arrayUtils.runOrderbyOn(this.collection.getEntities());
      let untouchedgrouped = this.collection.getEntities();
      if (grouping.length > 0) {
        let groupedArray = this.arrayUtils.group(untouchedgrouped, grouping, true);
        this.collection.setData(groupedArray, untouchedgrouped);
      }
      this.triggerEvent('collection_updated');
    }
  }

  public refresh(data?: any) {
    if (data) {
      this.collection = new Collection(this);
      this.collection.setData(data);
      this.mainArray = this.collection.getEntities();
      this.entity = null;
    }
    let grouping = this.arrayUtils.getGrouping();
    this.arrayUtils.runOrderbyOn(this.collection.getEntities());
    if (grouping.length > 0) {
      let untouchedgrouped = this.collection.getEntities();
      let groupedArray = this.arrayUtils.group(untouchedgrouped, grouping, true);
      this.collection.setData(groupedArray, untouchedgrouped);
    }
    this.triggerEvent('collection_updated');
  }
{'fraction_non_alphanumeric': 0.10908052541404911, 'fraction_numerical': 0.0009137635636778984, 'mean_word_length': 3.2217936354869816, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '6914173', 'n_tokens_mistral': 2473, 'n_tokens_neox': 2420, 'n_words': 681}