<commit_before>/**
 * Created by Dmitri on 4/24/2016.
 */
'use strict';

import Source from './handler/source';
import Handler from './handler/handler';
import Context from './context';

const HEAD = Symbol('HEAD');
const TAIL = Symbol('TAIL');

const BEG = 'beg';
const END = 'end';

class HandlerWrapper
{
  constructor(handler, cntx)
  {
    if(!handler || !cntx)
      throw new Error(`Invalid handler: ${handler} or context: ${cntx}`);
    if(handler.onAdd)
      handler.onAdd(cntx);
    this.handler = handler;
    this.cntx = cntx;
    this.beg = null;
    this.end = null;
  }
  
  destroy()
  {
    if(this.handler.onRemove)
      this.handler.onRemove(this.cntx);
    this.cntx = null;
    this.handler = null;
  }
}

class Pipe extends Source
{
  constructor(config)
  {
    super(config);
    let priv =
    {
      cntx: new Context(this),
      registry: new Map(),
      queue: [],
      timer: null,
      externPrc: new Set()
    };
    Reflect.defineProperty(this, '_priv', {configurable:false, writable:false, value:priv});

    let head = new Handler({id:HEAD});
    let tail = new Handler({id:TAIL});
    head.outbound = (cntx) => {this._onExit(BEG, cntx)};
    tail.inbound = (cntx) => {this._onExit(END, cntx)};
    head = this._register(head);
    tail = this._register(tail);
    head.end = tail;
    tail.beg = head;
  }

  //===== PUBLIC METHODS ==========================================================================
  getHandler(obj)
  {
    const wrapper = this._getWrapper(obj);
    return wrapper !== null ? wrapper.handler : null;
  }

  addBeg(obj)
  {
    return this._add(obj, HEAD, true);
  }

  addEnd(obj)
  {
    return this._add(obj, TAIL);
  }

  addBefore(obj, ref)
  {
    return this._add(obj, ref);
  }

  addAfter(obj, ref)
  {
    return this._add(obj, ref, true);
  }

  remove(obj)
  {
    obj = this._getWrapper(obj);
    const id = obj.handler.id;
    if(id === HEAD || id === TAIL)
      return this;
    return this._remove(obj);
  }

  removeBeg()
  {
    const obj = this._getWrapper(HEAD).end;
    if(obj.handler.id !== TAIL)
      return this._remove(obj);
    return this;
  }

  removeEnd()
  {
    const obj = this._getWrapper(TAIL).beg;
    if(obj.handler.id !== HEAD)
      return this._remove(obj);
    return this;
  }

  removeBefore(obj)
  {
    obj = this._getWrapper(obj).beg;
    return obj.handler.id !== HEAD ? this._remove(obj) : this;
  }

  removeAfter(obj)
  {
    obj = this._getWrapper(obj).end;
    return obj.handler.id !== TAIL ? this._remove(obj) : this;
  }

  processInbound(obj = {}, start = HEAD, offset = 0)
  {
    this._process(obj, END, start, offset);
    return this;
  }

  processOutbound(obj ={}, start = TAIL, offset = 0)
  {
    this._process(obj, BEG, start, offset);
    return this;
  }

  submitInbound(msg = {}, start = HEAD, offset = 0)
  {
    this._submit(msg, END, start, offset);
    return this;
  }

  submitOutbound(msg = {}, start = TAIL, offset = 0)
  {
    this._submit(msg, BEG, start, offset);
    return this;
  }
<commit_msg>fix(pipe): Fixed potential null dereference<commit_after>/**
 * Created by Dmitri on 4/24/2016.
 */
'use strict';

import Source from './handler/source';
import Handler from './handler/handler';
import Context from './context';

const HEAD = Symbol('HEAD');
const TAIL = Symbol('TAIL');

const BEG = 'beg';
const END = 'end';

class HandlerWrapper
{
  constructor(handler, cntx)
  {
    if(!handler || !cntx)
      throw new Error(`Invalid handler: ${handler} or context: ${cntx}`);
    if(handler.onAdd)
      handler.onAdd(cntx);
    this.handler = handler;
    this.cntx = cntx;
    this.beg = null;
    this.end = null;
  }
  
  destroy()
  {
    if(this.handler.onRemove)
      this.handler.onRemove(this.cntx);
    this.cntx = null;
    this.handler = null;
  }
}

class Pipe extends Source
{
  constructor(config)
  {
    super(config);
    let priv =
    {
      cntx: new Context(this),
      registry: new Map(),
      queue: [],
      timer: null,
      externPrc: new Set()
    };
    Reflect.defineProperty(this, '_priv', {configurable:false, writable:false, value:priv});

    let head = new Handler({id:HEAD});
    let tail = new Handler({id:TAIL});
    head.outbound = (cntx) => {this._onExit(BEG, cntx)};
    tail.inbound = (cntx) => {this._onExit(END, cntx)};
    head = this._register(head);
    tail = this._register(tail);
    head.end = tail;
    tail.beg = head;
  }

  //===== PUBLIC METHODS ==========================================================================
  getHandler(obj)
  {
    const wrapper = this._getWrapper(obj);
    return wrapper !== null ? wrapper.handler : null;
  }

  addBeg(obj)
  {
    return this._add(obj, HEAD, true);
  }

  addEnd(obj)
  {
    return this._add(obj, TAIL);
  }

  addBefore(obj, ref)
  {
    return this._add(obj, ref);
  }

  addAfter(obj, ref)
  {
    return this._add(obj, ref, true);
  }

  remove(obj)
  {
    obj = this._getWrapper(obj);
    if(!obj)
      return this;
    const id = obj.handler.id;
    return id !== HEAD && id !== TAIL ? this._remove(obj) : this;
  }

  removeBeg()
  {
    const obj = this._getWrapper(HEAD).end;
    return obj.handler.id !== TAIL ? this._remove(obj) : this;
  }

  removeEnd()
  {
    const obj = this._getWrapper(TAIL).beg;
    return obj.handler.id !== HEAD ? this._remove(obj) : this;
  }

  removeBefore(obj)
  {
    obj = this._getWrapper(obj);
    if(!obj)
      return this;
    obj = obj.beg;
    return obj.handler.id !== HEAD ? this._remove(obj) : this;
  }

  removeAfter(obj)
  {
    obj = this._getWrapper(obj);
    if(!obj)
      return this;
    obj = obj.end;
    return obj.handler.id !== TAIL ? this._remove(obj) : this;
  }

  processInbound(obj = {}, start = HEAD, offset = 0)
  {
    this._process(obj, END, start, offset);
    return this;
  }

  processOutbound(obj ={}, start = TAIL, offset = 0)
  {
    this._process(obj, BEG, start, offset);
    return this;
  }

  submitInbound(msg = {}, start = HEAD, offset = 0)
  {
    this._submit(msg, END, start, offset);
    return this;
  }

  submitOutbound(msg = {}, start = TAIL, offset = 0)
  {
    this._submit(msg, BEG, start, offset);
    return this;
  }

{'fraction_non_alphanumeric': 0.17823700355182434, 'fraction_numerical': 0.003551824346141427, 'mean_word_length': 2.7751371115173673, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '445376', 'n_tokens_mistral': 2237, 'n_tokens_neox': 2143, 'n_words': 488}