(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() { // IME 入力開始時
      $.data(this, 'isComposing', true);
    }).on('compositionend', function() { // IME 確定時
      $.data(this, 'isComposing', false);

      var $el = $(this);
      $el.trigger('update');
    }).on('textchange change', function(ev) {
      var $el = $(this);

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

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