<commit_before>import EventEmitter = require('eventemitter3');
import { Query, QueryConfiguration } from '../core/query';
import { BrowserBridge } from '../core/bridge';
import { Results } from '../models/response';
import { Pager } from './pager';
import { SelectedValueRefinement, SelectedRangeRefinement } from '../models/request';

export namespace Events {
  export const RESULTS = 'results';
  export const REFINEMENTS_CHANGED = 'refinements_changed';
  export const RESET = 'reset';
  export const REWRITE_QUERY = 'rewrite_query';
}

export type FluxRefinement = SelectedValueRefinement | SelectedRangeRefinement;

export class FluxCapacitor extends EventEmitter {
  private originalQuery: string = '';
  query: Query;
  bridge: BrowserBridge;
  results: Results;

  constructor(endpoint: string, config: QueryConfiguration & any = {}) {
    super();
    this.bridge = new BrowserBridge(endpoint);
    this.query = new Query().withConfiguration(config);
  }

  get page() {
    return new Pager(this);
  }

  search(query: string = this.originalQuery) {
    return this.bridge.search(this.query.withQuery(query))
      .then(res => {
        this.results = res;
        this.originalQuery = query;
        this.emit(Events.RESULTS, res);
      });
  }

  rewrite(query: string) {
    return this.search(query)
      .then(() => this.emit(Events.REWRITE_QUERY, query));
  }

  reset(query: string = this.originalQuery) {
    this.query = new Query();
    return this.search(query)
      .then(() => this.emit(Events.RESET, this.results));
  }

  resize(pageSize: number, offset?: number) {
    this.query.withConfiguration({ pageSize });
    if (offset !== undefined) this.query.skip(offset);
    return this.search();
  }

  private resetPaging(reset: boolean): Promise<any> {
    return reset ? this.page.reset() : this.search();
  }

  refine(refinement: FluxRefinement, config: RefinementConfig = { reset: true }) {
    this.query.withSelectedRefinements(refinement);
    return this.doRefinement(config);
  }

  unrefine(refinement: FluxRefinement, config: RefinementConfig = { reset: true }) {
    this.query.withoutSelectedRefinements(refinement);
    return this.doRefinement(config);
  }

  private doRefinement(config: RefinementConfig) {
    return this.resetPaging(config.reset)
      .then(() => this.emit(Events.REFINEMENTS_CHANGED, this.navigationInfo));
  }

  private get navigationInfo() {
    return {
      available: this.results.availableNavigation,
      selected: this.results.selectedNavigation
    };
  }
}

export interface RefinementConfig {
  reset?: boolean;
}<commit_msg>add config to skip search on refinement<commit_after>import EventEmitter = require('eventemitter3');
import { Query, QueryConfiguration } from '../core/query';
import { BrowserBridge } from '../core/bridge';
import { Results } from '../models/response';
import { Pager } from './pager';
import { SelectedValueRefinement, SelectedRangeRefinement } from '../models/request';

export namespace Events {
  export const RESULTS = 'results';
  export const REFINEMENTS_CHANGED = 'refinements_changed';
  export const RESET = 'reset';
  export const REWRITE_QUERY = 'rewrite_query';
}

export type FluxRefinement = SelectedValueRefinement | SelectedRangeRefinement;

export class FluxCapacitor extends EventEmitter {
  private originalQuery: string = '';
  query: Query;
  bridge: BrowserBridge;
  results: Results;

  constructor(endpoint: string, config: QueryConfiguration & any = {}) {
    super();
    this.bridge = new BrowserBridge(endpoint);
    this.query = new Query().withConfiguration(config);
  }

  get page() {
    return new Pager(this);
  }

  search(query: string = this.originalQuery) {
    return this.bridge.search(this.query.withQuery(query))
      .then(res => {
        this.results = res;
        this.originalQuery = query;
        this.emit(Events.RESULTS, res);
      });
  }

  rewrite(query: string) {
    return this.search(query)
      .then(() => this.emit(Events.REWRITE_QUERY, query));
  }

  reset(query: string = this.originalQuery) {
    this.query = new Query();
    return this.search(query)
      .then(() => this.emit(Events.RESET, this.results));
  }

  resize(pageSize: number, offset?: number) {
    this.query.withConfiguration({ pageSize });
    if (offset !== undefined) this.query.skip(offset);
    return this.search();
  }

  private resetPaging(reset: boolean): Promise<any> {
    return reset ? this.page.reset() : this.search();
  }

  refine(refinement: FluxRefinement, config: RefinementConfig = { reset: true }) {
    this.query.withSelectedRefinements(refinement);
    if (config.skipSearch) return Promise.resolve(true);
    return this.doRefinement(config);
  }

  unrefine(refinement: FluxRefinement, config: RefinementConfig = { reset: true }) {
    this.query.withoutSelectedRefinements(refinement);
    if (config.skipSearch) return Promise.resolve(true);
    return this.doRefinement(config);
  }

  private doRefinement(config: RefinementConfig) {
    return this.resetPaging(config.reset)
      .then(() => this.emit(Events.REFINEMENTS_CHANGED, this.navigationInfo));
  }

  private get navigationInfo() {
    return {
      available: this.results.availableNavigation,
      selected: this.results.selectedNavigation
    };
  }
}

export interface RefinementConfig {
  reset?: boolean;
  skipSearch?: boolean;
}
{'fraction_non_alphanumeric': 0.12806691449814125, 'fraction_numerical': 0.0003717472118959108, 'mean_word_length': 3.9503219871205153, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '1643686', 'n_tokens_mistral': 1611, 'n_tokens_neox': 1562, 'n_words': 354}