<commit_before>/**
 * Created by Dmitri on 3/29/2017.
 */
'use strict';

import Handler from './handler';

const DEFAULT = Symbol('Default');

const DIR_IN = 0;
const DIR_OUT = 1;

class PromiseHandler extends Handler
{
  constructor(config)
  {
    super(config);
    this.config.prMap = new Map();
  }

  _process(cntx, dir)
  {
    for(let arr of this.config.prMap.values())
    {
      const obj = arr[dir];
      if(obj)
        obj.filter.call(obj.self, cntx, obj.accept, obj.reject);
    }
  }

  _createPromise(filter, id, ...dirs)
  {
    const prObj = {};
    prObj.filter = filter;
    prObj.self = {};
    prObj.promise = new Promise((accept, reject) =>
    {
      prObj.accept = accept;
      prObj.reject = reject;
    });
    const arr = this.config.prMap.get(id) || [];
    for(let i = arr.length; --i >= 0;)
    {
      if(arr[i])
        arr[i].reject(`Promise ${id} deleted`);
      arr[i] = undefined;
    }
    for(let dir of dirs)
    {
      arr[dir] = prObj;
    }
    this.config.prMap.set(id, arr);
    return prObj.promise;
  }

  inbound(cntx)
  {
    this._process(cntx, DIR_IN)
  }

  outbound(cntx)
  {
    this._process(cntx, DIR_OUT)
  }

  getPromise(id)
  {
    const arr = this.config.prMap.get(id);
    if(!arr)
      return undefined;
    const obj = arr[DIR_IN] || arr[DIR_OUT];
    return obj ? obj.promise : undefined;
  }

  get promise()
  {
    return this.getPromise(DEFAULT);
  }<commit_msg>feat(handler): PromiseHandler garbage collects resolved promises<commit_after>/**
 * Created by Dmitri on 3/29/2017.
 */
'use strict';

import Handler from './handler';

const DEFAULT = Symbol('Default');

const DIR_IN = 0;
const DIR_OUT = 1;

class PromiseHandler extends Handler
{
  constructor(config)
  {
    super(config);
    this.config.prMap = new Map();
  }

  _process(cntx, dir)
  {
    for(let arr of this.config.prMap.values())
    {
      const obj = arr[dir];
      if(obj)
        obj.filter.call(obj, cntx, obj.accept, obj.reject);
    }
  }

  _createPromise(filter, id, ...dirs)
  {
    const prObj = {filter, id};
    const map = this.config.prMap;
    const arr = map.get(id) || [];
    for(let i = arr.length; --i >= 0;)
    {
      if(arr[i])
        arr[i].reject(`Promise ${id} deleted`);
      arr[i] = undefined;
    }
    for(let dir of dirs)
    {
      arr[dir] = prObj;
    }
    map.set(id, arr);
    prObj.promise = new Promise((accept, reject) =>
    {
      prObj.accept = (val) => {map.delete(id); return accept(val);};
      prObj.reject = (val) => {map.delete(id); return reject(val);};
    });
    return prObj.promise;
  }

  inbound(cntx)
  {
    this._process(cntx, DIR_IN)
  }

  outbound(cntx)
  {
    this._process(cntx, DIR_OUT)
  }

  getPromise(id)
  {
    const arr = this.config.prMap.get(id);
    if(!arr)
      return undefined;
    const obj = arr[DIR_IN] || arr[DIR_OUT];
    return obj ? obj.promise : undefined;
  }

  get promise()
  {
    return this.getPromise(DEFAULT);
  }
{'fraction_non_alphanumeric': 0.15785930663076406, 'fraction_numerical': 0.006731740154830024, 'mean_word_length': 2.4477958236658934, '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': '6474725', 'n_tokens_mistral': 1128, 'n_tokens_neox': 1097, 'n_words': 231}