(function($) {
  jQuery.fn.observeUpdate = function(timeout, minLength) {
    var timer;

    timer = null;

    if (timeout === undefined) {
      timeout = 200;
    }

    $.data(this, 'isComposing', false);

    return this.on('compositionstart', function() {
      $.data(this, 'isComposing', true);
    }).on('compositionend', function() {
      $.data(this, 'isComposing', false);
    }).on('textchange change', function(ev) {
      var $el = $(this);

      clearTimeout(timer);
      timer = setTimeout(function() {
        if ($el.data('isComposing')) {
          return;
        }

        $el.trigger('update');
      }, timeout);
    });
  };
})(jQuery);
