doc.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // Generated by CoffeeScript 1.4.0
  2. (function() {
  3. var Doc, MicroEvent, types,
  4. __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  5. __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  6. if (typeof WEB === "undefined" || WEB === null) {
  7. types = require('../types');
  8. }
  9. if (typeof WEB !== "undefined" && WEB !== null) {
  10. exports.extendDoc = function(name, fn) {
  11. return Doc.prototype[name] = fn;
  12. };
  13. }
  14. Doc = (function() {
  15. function Doc(connection, name, openData) {
  16. this.connection = connection;
  17. this.name = name;
  18. this.shout = __bind(this.shout, this);
  19. this.flush = __bind(this.flush, this);
  20. openData || (openData = {});
  21. this.version = openData.v;
  22. this.snapshot = openData.snaphot;
  23. if (openData.type) {
  24. this._setType(openData.type);
  25. }
  26. this.state = 'closed';
  27. this.autoOpen = false;
  28. this._create = openData.create;
  29. this.inflightOp = null;
  30. this.inflightCallbacks = [];
  31. this.inflightSubmittedIds = [];
  32. this.pendingOp = null;
  33. this.pendingCallbacks = [];
  34. this.serverOps = {};
  35. }
  36. Doc.prototype._xf = function(client, server) {
  37. var client_, server_;
  38. if (this.type.transformX) {
  39. return this.type.transformX(client, server);
  40. } else {
  41. client_ = this.type.transform(client, server, 'left');
  42. server_ = this.type.transform(server, client, 'right');
  43. return [client_, server_];
  44. }
  45. };
  46. Doc.prototype._otApply = function(docOp, isRemote) {
  47. var oldSnapshot;
  48. oldSnapshot = this.snapshot;
  49. this.snapshot = this.type.apply(this.snapshot, docOp);
  50. this.emit('change', docOp, oldSnapshot);
  51. if (isRemote) {
  52. return this.emit('remoteop', docOp, oldSnapshot);
  53. }
  54. };
  55. Doc.prototype._connectionStateChanged = function(state, data) {
  56. switch (state) {
  57. case 'disconnected':
  58. this.state = 'closed';
  59. if (this.inflightOp) {
  60. this.inflightSubmittedIds.push(this.connection.id);
  61. }
  62. this.emit('closed');
  63. break;
  64. case 'ok':
  65. if (this.autoOpen) {
  66. this.open();
  67. }
  68. break;
  69. case 'stopped':
  70. if (typeof this._openCallback === "function") {
  71. this._openCallback(data);
  72. }
  73. }
  74. return this.emit(state, data);
  75. };
  76. Doc.prototype._setType = function(type) {
  77. var k, v, _ref;
  78. if (typeof type === 'string') {
  79. type = types[type];
  80. }
  81. if (!(type && type.compose)) {
  82. throw new Error('Support for types without compose() is not implemented');
  83. }
  84. this.type = type;
  85. if (type.api) {
  86. _ref = type.api;
  87. for (k in _ref) {
  88. v = _ref[k];
  89. this[k] = v;
  90. }
  91. return typeof this._register === "function" ? this._register() : void 0;
  92. } else {
  93. return this.provides = {};
  94. }
  95. };
  96. Doc.prototype._onMessage = function(msg) {
  97. var callback, docOp, error, oldInflightOp, op, path, response, undo, value, _i, _j, _len, _len1, _ref, _ref1, _ref2, _ref3, _ref4, _ref5, _ref6;
  98. if (msg.open === true) {
  99. this.state = 'open';
  100. this._create = false;
  101. if (this.created == null) {
  102. this.created = !!msg.create;
  103. }
  104. if (msg.type) {
  105. this._setType(msg.type);
  106. }
  107. if (msg.create) {
  108. this.created = true;
  109. this.snapshot = this.type.create();
  110. } else {
  111. if (this.created !== true) {
  112. this.created = false;
  113. }
  114. if (msg.snapshot !== void 0) {
  115. this.snapshot = msg.snapshot;
  116. }
  117. }
  118. if (msg.v != null) {
  119. this.version = msg.v;
  120. }
  121. if (this.inflightOp) {
  122. response = {
  123. doc: this.name,
  124. op: this.inflightOp,
  125. v: this.version
  126. };
  127. if (this.inflightSubmittedIds.length) {
  128. response.dupIfSource = this.inflightSubmittedIds;
  129. }
  130. this.connection.send(response);
  131. } else {
  132. this.flush();
  133. }
  134. this.emit('open');
  135. return typeof this._openCallback === "function" ? this._openCallback(null) : void 0;
  136. } else if (msg.open === false) {
  137. if (msg.error) {
  138. if (typeof console !== "undefined" && console !== null) {
  139. console.error("Could not open document: " + msg.error);
  140. }
  141. this.emit('error', msg.error);
  142. if (typeof this._openCallback === "function") {
  143. this._openCallback(msg.error);
  144. }
  145. }
  146. this.state = 'closed';
  147. this.emit('closed');
  148. if (typeof this._closeCallback === "function") {
  149. this._closeCallback();
  150. }
  151. return this._closeCallback = null;
  152. } else if (msg.op === null && error === 'Op already submitted') {
  153. } else if ((msg.op === void 0 && msg.v !== void 0) || (msg.op && (_ref = msg.meta.source, __indexOf.call(this.inflightSubmittedIds, _ref) >= 0))) {
  154. oldInflightOp = this.inflightOp;
  155. this.inflightOp = null;
  156. this.inflightSubmittedIds.length = 0;
  157. error = msg.error;
  158. if (error) {
  159. if (this.type.invert) {
  160. undo = this.type.invert(oldInflightOp);
  161. if (this.pendingOp) {
  162. _ref1 = this._xf(this.pendingOp, undo), this.pendingOp = _ref1[0], undo = _ref1[1];
  163. }
  164. this._otApply(undo, true);
  165. } else {
  166. this.emit('error', "Op apply failed (" + error + ") and the op could not be reverted");
  167. }
  168. _ref2 = this.inflightCallbacks;
  169. for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
  170. callback = _ref2[_i];
  171. callback(error);
  172. }
  173. } else {
  174. if (msg.v !== this.version) {
  175. throw new Error('Invalid version from server');
  176. }
  177. this.serverOps[this.version] = oldInflightOp;
  178. this.version++;
  179. this.emit('acknowledge', oldInflightOp);
  180. _ref3 = this.inflightCallbacks;
  181. for (_j = 0, _len1 = _ref3.length; _j < _len1; _j++) {
  182. callback = _ref3[_j];
  183. callback(null, oldInflightOp);
  184. }
  185. }
  186. return this.flush();
  187. } else if (msg.op) {
  188. if (msg.v < this.version) {
  189. return;
  190. }
  191. if (msg.doc !== this.name) {
  192. return this.emit('error', "Expected docName '" + this.name + "' but got " + msg.doc);
  193. }
  194. if (msg.v !== this.version) {
  195. return this.emit('error', "Expected version " + this.version + " but got " + msg.v);
  196. }
  197. op = msg.op;
  198. this.serverOps[this.version] = op;
  199. docOp = op;
  200. if (this.inflightOp !== null) {
  201. _ref4 = this._xf(this.inflightOp, docOp), this.inflightOp = _ref4[0], docOp = _ref4[1];
  202. }
  203. if (this.pendingOp !== null) {
  204. _ref5 = this._xf(this.pendingOp, docOp), this.pendingOp = _ref5[0], docOp = _ref5[1];
  205. }
  206. this.version++;
  207. return this._otApply(docOp, true);
  208. } else if (msg.meta) {
  209. _ref6 = msg.meta, path = _ref6.path, value = _ref6.value;
  210. switch (path != null ? path[0] : void 0) {
  211. case 'shout':
  212. return this.emit('shout', value);
  213. default:
  214. return typeof console !== "undefined" && console !== null ? console.warn('Unhandled meta op:', msg) : void 0;
  215. }
  216. } else {
  217. return typeof console !== "undefined" && console !== null ? console.warn('Unhandled document message:', msg) : void 0;
  218. }
  219. };
  220. Doc.prototype.flush = function() {
  221. if (!(this.connection.state === 'ok' && this.inflightOp === null && this.pendingOp !== null)) {
  222. return;
  223. }
  224. this.inflightOp = this.pendingOp;
  225. this.inflightCallbacks = this.pendingCallbacks;
  226. this.pendingOp = null;
  227. this.pendingCallbacks = [];
  228. return this.connection.send({
  229. doc: this.name,
  230. op: this.inflightOp,
  231. v: this.version
  232. });
  233. };
  234. Doc.prototype.submitOp = function(op, callback) {
  235. if (this.type.normalize != null) {
  236. op = this.type.normalize(op);
  237. }
  238. this.snapshot = this.type.apply(this.snapshot, op);
  239. if (this.pendingOp !== null) {
  240. this.pendingOp = this.type.compose(this.pendingOp, op);
  241. } else {
  242. this.pendingOp = op;
  243. }
  244. if (callback) {
  245. this.pendingCallbacks.push(callback);
  246. }
  247. this.emit('change', op);
  248. return setTimeout(this.flush, 0);
  249. };
  250. Doc.prototype.shout = function(msg) {
  251. return this.connection.send({
  252. doc: this.name,
  253. meta: {
  254. path: ['shout'],
  255. value: msg
  256. }
  257. });
  258. };
  259. Doc.prototype.open = function(callback) {
  260. var message,
  261. _this = this;
  262. this.autoOpen = true;
  263. if (this.state !== 'closed') {
  264. return;
  265. }
  266. message = {
  267. doc: this.name,
  268. open: true
  269. };
  270. if (this.snapshot === void 0) {
  271. message.snapshot = null;
  272. }
  273. if (this.type) {
  274. message.type = this.type.name;
  275. }
  276. if (this.version != null) {
  277. message.v = this.version;
  278. }
  279. if (this._create) {
  280. message.create = true;
  281. }
  282. this.connection.send(message);
  283. this.state = 'opening';
  284. return this._openCallback = function(error) {
  285. _this._openCallback = null;
  286. return typeof callback === "function" ? callback(error) : void 0;
  287. };
  288. };
  289. Doc.prototype.close = function(callback) {
  290. this.autoOpen = false;
  291. if (this.state === 'closed') {
  292. return typeof callback === "function" ? callback() : void 0;
  293. }
  294. this.connection.send({
  295. doc: this.name,
  296. open: false
  297. });
  298. this.state = 'closed';
  299. this.emit('closing');
  300. return this._closeCallback = callback;
  301. };
  302. return Doc;
  303. })();
  304. if (typeof WEB === "undefined" || WEB === null) {
  305. MicroEvent = require('./microevent');
  306. }
  307. MicroEvent.mixin(Doc);
  308. exports.Doc = Doc;
  309. }).call(this);