mirror.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. define(function(require, exports, module) {
  2. "use strict";
  3. var Document = require("../document").Document;
  4. var lang = require("../lib/lang");
  5. var Mirror = exports.Mirror = function(sender) {
  6. this.sender = sender;
  7. var doc = this.doc = new Document("");
  8. var deferredUpdate = this.deferredUpdate = lang.delayedCall(this.onUpdate.bind(this));
  9. var _self = this;
  10. sender.on("change", function(e) {
  11. doc.applyDeltas(e.data);
  12. if (_self.$timeout)
  13. return deferredUpdate.schedule(_self.$timeout);
  14. _self.onUpdate();
  15. });
  16. };
  17. (function() {
  18. this.$timeout = 500;
  19. this.setTimeout = function(timeout) {
  20. this.$timeout = timeout;
  21. };
  22. this.setValue = function(value) {
  23. this.doc.setValue(value);
  24. this.deferredUpdate.schedule(this.$timeout);
  25. };
  26. this.getValue = function(callbackId) {
  27. this.sender.callback(this.doc.getValue(), callbackId);
  28. };
  29. this.onUpdate = function() {
  30. // abstract method
  31. };
  32. this.isPending = function() {
  33. return this.deferredUpdate.isPending();
  34. };
  35. }).call(Mirror.prototype);
  36. });