<commit_before>var Utils = {
  isArray: ('isArray' in Array) ? Array.isArray :
      function (value) {
          return Object.prototype.toString.call(value) === '[object Array]';
      }
};

var Listener = function(state) {
  this.state = state;
}

Listener.prototype.trigger = function() {
  if (this.state.element) {
    var newElement = document.createElement(this.state.component.tag);
    Client.renderElement(newElement, this.state.component, this.state.context);
    var el = this.state.element;
    el.parentNode.replaceChild(newElement, el);
    this.state.element = newElement;
  }
}

var Context = function () {
  this.listeners = {};
}

Context.prototype.trigger = function (postRenderCallback) {
  var args = Array.prototype.slice.call(arguments);
  var selectedListeners = [];
  args.forEach(function (event) {
    var l = this.listeners[event];
    if (l) {
      this.listeners[event] = [];
      selectedListeners = selectedListeners.concat(l)
    } else {
      console.warn('Warning! No event registered for "'+event+'"');
    }
  }.bind(this))
  selectedListeners.forEach(function (listener) {
    listener.trigger();
  })
  if (postRenderCallback) postRenderCallback(this.context, this.component, this.el);
}

Context.prototype.register = function(event, listener) {
  this.listeners[event] = this.listeners[event] || [];
  this.listeners[event].push(listener);
}

var Client = {
  createContext: function () {
    return new Context();
  },
  render: function (parent, input, context) {
    parent.innerHTML = '';
    this.recurse(parent,input,context);
  },
  renderElement: function (el, input, context) {
    Object.keys(input).forEach(function(key) {
      var val = input[key];
      if (val == null || typeof val == 'undefined' || key == 'tag') {
        // Ignore
      } else if (key == 'listen') {
        if (context && context.register) {
          val.forEach(function (observer) {
            context.register(observer, new Listener({
              element: el,
              component: input,
              context: context
            }));
          })
        }
      } else if (key == 'render') {
        Client.recurse(el, val, context);
      } else if (key == 'events') {
        Object.keys(val).forEach(function(eventType) {
          el.addEventListener(eventType, val[eventType])
        });
      } else {
        if (typeof val == 'function') {
          var value = val(context);
        } else {
          var value = val;
        }
        if (key == 'style') {
          Object.keys(value).forEach(function(styleKey) {
            el.style[styleKey] = value[styleKey];
          });
        } else {
          el[key] = value;
        }
      }<commit_msg>fixes + render event<commit_after>var Utils = {
  isArray: ('isArray' in Array) ? Array.isArray :
      function (value) {
          return Object.prototype.toString.call(value) === '[object Array]';
      }
};

var Listener = function(state) {
  this.state = state;
}

Listener.prototype.trigger = function() {
  if (this.state.element) {
    var newElement = document.createElement(this.state.component.tag);
    Client.renderElement(newElement, this.state.component, this.state.context);
    var el = this.state.element;
    el.parentNode.replaceChild(newElement, el);
    this.state.element = newElement;
  } else {
    console.warn('Warning! Element not found on trigger', this.state.component);
  }
}

var Context = function () {
  this.listeners = {};
}

Context.prototype.trigger = function () {
  var args = Array.prototype.slice.call(arguments);
  var selectedListeners = [];
  args.forEach(function (event) {
    var l = this.listeners[event];
    if (l) {
      this.listeners[event] = [];
      l.forEach(function (listener) {
        var found = false;
        selectedListeners.some(function (selected) {
          if (selected.state.component == listener.state.component) {
            found = true;
            return true;
          }
        })
        if (!found) selectedListeners.push(listener);
      })
    } else {
      console.warn('Warning! No event registered for "'+event+'"');
    }
  }.bind(this))

  selectedListeners.forEach(function (listener) {
    listener.trigger();
  })
  selectedListeners.forEach(function (listener) {
    var c = listener.state.component;
    if (c.events && c.events.render) {
      c.events.render({
        context: listener.state.context,
        target: listener.state.element,
        triggers: args
      });
    }
  })
}

Context.prototype.register = function(event, listener) {
  this.listeners[event] = this.listeners[event] || [];
  this.listeners[event].push(listener);
}

var Client = {
  createContext: function () {
    return new Context();
  },
  render: function (parent, input, context) {
    parent.innerHTML = '';
    this.recurse(parent,input,context);
  },
  renderElement: function (el, input, context) {
    Object.keys(input).forEach(function(key) {
      var val = input[key];
      if (val == null || typeof val == 'undefined' || key == 'tag') {
        // Ignore
      } else if (key == 'listen') {
        if (context && context.register) {
          val.forEach(function (observer) {
            context.register(observer, new Listener({
              element: el,
              component: input,
              context: context
            }));
          })
        }
      } else if (key == 'render') {
        Client.recurse(el, val, context);
      } else if (key == 'events') {
        Object.keys(val).forEach(function(eventType) {
          if (eventType != 'render') {
            el.addEventListener(eventType, val[eventType])
          }
        });
      } else {
        if (typeof val == 'function') {
          var value = val(context);
        } else {
          var value = val;
        }
        if (key == 'style') {
          Object.keys(value).forEach(function(styleKey) {
            el.style[styleKey] = value[styleKey];
          });
        } else {
          el[key] = value;
        }
      }
{'fraction_non_alphanumeric': 0.14575369785607445, 'fraction_numerical': 0.0, 'mean_word_length': 2.3980801806888765, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2305274', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1764, 'n_words': 365}