├── README.md ├── dist └── gitmint.browser.js └── index.html /README.md: -------------------------------------------------------------------------------- 1 | # AMA 2 | 捕蛇者说 ask me anything 3 | 4 | DO NOT USE this repo! Visit https://ama.pythonhunter.org/ instead. 5 | -------------------------------------------------------------------------------- /dist/gitmint.browser.js: -------------------------------------------------------------------------------- 1 | var Gitmint = 2 | /******/ 3 | (function(modules) { // webpackBootstrap 4 | /******/ // The module cache 5 | /******/ 6 | var installedModules = {}; 7 | /******/ 8 | /******/ // The require function 9 | /******/ 10 | function __webpack_require__(moduleId) { 11 | /******/ 12 | /******/ // Check if module is in cache 13 | /******/ 14 | if (installedModules[moduleId]) { 15 | /******/ 16 | return installedModules[moduleId].exports; 17 | /******/ 18 | } 19 | /******/ // Create a new module (and put it into the cache) 20 | /******/ 21 | var module = installedModules[moduleId] = { 22 | /******/ 23 | i: moduleId, 24 | /******/ 25 | l: false, 26 | /******/ 27 | exports: {} 28 | /******/ 29 | }; 30 | /******/ 31 | /******/ // Execute the module function 32 | /******/ 33 | modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); 34 | /******/ 35 | /******/ // Flag the module as loaded 36 | /******/ 37 | module.l = true; 38 | /******/ 39 | /******/ // Return the exports of the module 40 | /******/ 41 | return module.exports; 42 | /******/ 43 | } 44 | /******/ 45 | /******/ 46 | /******/ // expose the modules object (__webpack_modules__) 47 | /******/ 48 | __webpack_require__.m = modules; 49 | /******/ 50 | /******/ // expose the module cache 51 | /******/ 52 | __webpack_require__.c = installedModules; 53 | /******/ 54 | /******/ // identity function for calling harmony imports with the correct context 55 | /******/ 56 | __webpack_require__.i = function(value) { 57 | return value; 58 | }; 59 | /******/ 60 | /******/ // define getter function for harmony exports 61 | /******/ 62 | __webpack_require__.d = function(exports, name, getter) { 63 | /******/ 64 | if (!__webpack_require__.o(exports, name)) { 65 | /******/ 66 | Object.defineProperty(exports, name, { 67 | /******/ 68 | configurable: false, 69 | /******/ 70 | enumerable: true, 71 | /******/ 72 | get: getter 73 | /******/ 74 | }); 75 | /******/ 76 | } 77 | /******/ 78 | }; 79 | /******/ 80 | /******/ // getDefaultExport function for compatibility with non-harmony modules 81 | /******/ 82 | __webpack_require__.n = function(module) { 83 | /******/ 84 | var getter = module && module.__esModule ? 85 | /******/ 86 | function getDefault() { 87 | return module['default']; 88 | } : 89 | /******/ 90 | function getModuleExports() { 91 | return module; 92 | }; 93 | /******/ 94 | __webpack_require__.d(getter, 'a', getter); 95 | /******/ 96 | return getter; 97 | /******/ 98 | }; 99 | /******/ 100 | /******/ // Object.prototype.hasOwnProperty.call 101 | /******/ 102 | __webpack_require__.o = function(object, property) { 103 | return Object.prototype.hasOwnProperty.call(object, property); 104 | }; 105 | /******/ 106 | /******/ // __webpack_public_path__ 107 | /******/ 108 | __webpack_require__.p = ""; 109 | /******/ 110 | /******/ // Load entry module and return exports 111 | /******/ 112 | return __webpack_require__(__webpack_require__.s = 5); 113 | /******/ 114 | }) 115 | /************************************************************************/ 116 | /******/ 117 | ([ 118 | /* 0 */ 119 | /***/ 120 | (function(module, exports, __webpack_require__) { 121 | 122 | "use strict"; 123 | 124 | 125 | Object.defineProperty(exports, "__esModule", { 126 | value: true 127 | }); 128 | var LS_ACCESS_TOKEN_KEY = exports.LS_ACCESS_TOKEN_KEY = 'gitment-comments-token'; 129 | var LS_USER_KEY = exports.LS_USER_KEY = 'gitment-user-info'; 130 | 131 | var NOT_INITIALIZED_ERROR = exports.NOT_INITIALIZED_ERROR = new Error('Comments Not Initialized'); 132 | 133 | /***/ 134 | }), 135 | /* 1 */ 136 | /***/ 137 | (function(module, exports, __webpack_require__) { 138 | 139 | "use strict"; 140 | /* WEBPACK VAR INJECTION */ 141 | (function(global) { 142 | 143 | Object.defineProperty(exports, "__esModule", { 144 | value: true 145 | }); 146 | 147 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function(obj) { 148 | return typeof obj; 149 | } : function(obj) { 150 | return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; 151 | }; 152 | 153 | /** MobX - (c) Michel Weststrate 2015, 2016 - MIT Licensed */ 154 | /*! ***************************************************************************** 155 | Copyright (c) Microsoft Corporation. All rights reserved. 156 | Licensed under the Apache License, Version 2.0 (the "License"); you may not use 157 | this file except in compliance with the License. You may obtain a copy of the 158 | License at http://www.apache.org/licenses/LICENSE-2.0 159 | 160 | THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 161 | KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 162 | WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 163 | MERCHANTABLITY OR NON-INFRINGEMENT. 164 | 165 | See the Apache Version 2.0 License for specific language governing permissions 166 | and limitations under the License. 167 | ***************************************************************************** */ 168 | /* global Reflect, Promise */ 169 | 170 | var extendStatics = Object.setPrototypeOf || { 171 | __proto__: [] 172 | } 173 | instanceof Array && function(d, b) { 174 | d.__proto__ = b; 175 | } || function(d, b) { 176 | for (var p in b) { 177 | if (b.hasOwnProperty(p)) d[p] = b[p]; 178 | } 179 | }; 180 | 181 | function __extends(d, b) { 182 | extendStatics(d, b); 183 | 184 | function __() { 185 | this.constructor = d; 186 | } 187 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 188 | } 189 | 190 | /** 191 | * Anything that can be used to _store_ state is an Atom in mobx. Atoms have two important jobs 192 | * 193 | * 1) detect when they are being _used_ and report this (using reportObserved). This allows mobx to make the connection between running functions and the data they used 194 | * 2) they should notify mobx whenever they have _changed_. This way mobx can re-run any functions (derivations) that are using this atom. 195 | */ 196 | var BaseAtom = function() { 197 | /** 198 | * Create a new atom. For debugging purposes it is recommended to give it a name. 199 | * The onBecomeObserved and onBecomeUnobserved callbacks can be used for resource management. 200 | */ 201 | function BaseAtom(name) { 202 | if (name === void 0) { 203 | name = "Atom@" + getNextId(); 204 | } 205 | this.name = name; 206 | this.isPendingUnobservation = true; // for effective unobserving. BaseAtom has true, for extra optimization, so its onBecomeUnobserved never gets called, because it's not needed 207 | this.observers = []; 208 | this.observersIndexes = {}; 209 | this.diffValue = 0; 210 | this.lastAccessedBy = 0; 211 | this.lowestObserverState = IDerivationState.NOT_TRACKING; 212 | } 213 | BaseAtom.prototype.onBecomeUnobserved = function() { 214 | // noop 215 | }; 216 | /** 217 | * Invoke this method to notify mobx that your atom has been used somehow. 218 | */ 219 | BaseAtom.prototype.reportObserved = function() { 220 | reportObserved(this); 221 | }; 222 | /** 223 | * Invoke this method _after_ this method has changed to signal mobx that all its observers should invalidate. 224 | */ 225 | BaseAtom.prototype.reportChanged = function() { 226 | startBatch(); 227 | propagateChanged(this); 228 | endBatch(); 229 | }; 230 | BaseAtom.prototype.toString = function() { 231 | return this.name; 232 | }; 233 | return BaseAtom; 234 | }(); 235 | var Atom = function(_super) { 236 | __extends(Atom, _super); 237 | /** 238 | * Create a new atom. For debugging purposes it is recommended to give it a name. 239 | * The onBecomeObserved and onBecomeUnobserved callbacks can be used for resource management. 240 | */ 241 | function Atom(name, onBecomeObservedHandler, onBecomeUnobservedHandler) { 242 | if (name === void 0) { 243 | name = "Atom@" + getNextId(); 244 | } 245 | if (onBecomeObservedHandler === void 0) { 246 | onBecomeObservedHandler = noop; 247 | } 248 | if (onBecomeUnobservedHandler === void 0) { 249 | onBecomeUnobservedHandler = noop; 250 | } 251 | var _this = _super.call(this, name) || this; 252 | _this.name = name; 253 | _this.onBecomeObservedHandler = onBecomeObservedHandler; 254 | _this.onBecomeUnobservedHandler = onBecomeUnobservedHandler; 255 | _this.isPendingUnobservation = false; // for effective unobserving. 256 | _this.isBeingTracked = false; 257 | return _this; 258 | } 259 | Atom.prototype.reportObserved = function() { 260 | startBatch(); 261 | _super.prototype.reportObserved.call(this); 262 | if (!this.isBeingTracked) { 263 | this.isBeingTracked = true; 264 | this.onBecomeObservedHandler(); 265 | } 266 | endBatch(); 267 | return !!globalState.trackingDerivation; 268 | // return doesn't really give useful info, because it can be as well calling computed which calls atom (no reactions) 269 | // also it could not trigger when calculating reaction dependent on Atom because Atom's value was cached by computed called by given reaction. 270 | }; 271 | Atom.prototype.onBecomeUnobserved = function() { 272 | this.isBeingTracked = false; 273 | this.onBecomeUnobservedHandler(); 274 | }; 275 | return Atom; 276 | }(BaseAtom); 277 | var isAtom = createInstanceofPredicate("Atom", BaseAtom); 278 | 279 | function hasInterceptors(interceptable) { 280 | return interceptable.interceptors && interceptable.interceptors.length > 0; 281 | } 282 | 283 | function registerInterceptor(interceptable, handler) { 284 | var interceptors = interceptable.interceptors || (interceptable.interceptors = []); 285 | interceptors.push(handler); 286 | return once(function() { 287 | var idx = interceptors.indexOf(handler); 288 | if (idx !== -1) interceptors.splice(idx, 1); 289 | }); 290 | } 291 | 292 | function interceptChange(interceptable, change) { 293 | var prevU = untrackedStart(); 294 | try { 295 | var interceptors = interceptable.interceptors; 296 | if (interceptors) 297 | for (var i = 0, l = interceptors.length; i < l; i++) { 298 | change = interceptors[i](change); 299 | invariant(!change || change.type, "Intercept handlers should return nothing or a change object"); 300 | if (!change) break; 301 | } 302 | return change; 303 | } finally { 304 | untrackedEnd(prevU); 305 | } 306 | } 307 | 308 | function hasListeners(listenable) { 309 | return listenable.changeListeners && listenable.changeListeners.length > 0; 310 | } 311 | 312 | function registerListener(listenable, handler) { 313 | var listeners = listenable.changeListeners || (listenable.changeListeners = []); 314 | listeners.push(handler); 315 | return once(function() { 316 | var idx = listeners.indexOf(handler); 317 | if (idx !== -1) listeners.splice(idx, 1); 318 | }); 319 | } 320 | 321 | function notifyListeners(listenable, change) { 322 | var prevU = untrackedStart(); 323 | var listeners = listenable.changeListeners; 324 | if (!listeners) return; 325 | listeners = listeners.slice(); 326 | for (var i = 0, l = listeners.length; i < l; i++) { 327 | listeners[i](change); 328 | } 329 | untrackedEnd(prevU); 330 | } 331 | 332 | function isSpyEnabled() { 333 | return !!globalState.spyListeners.length; 334 | } 335 | 336 | function spyReport(event) { 337 | if (!globalState.spyListeners.length) return; 338 | var listeners = globalState.spyListeners; 339 | for (var i = 0, l = listeners.length; i < l; i++) { 340 | listeners[i](event); 341 | } 342 | } 343 | 344 | function spyReportStart(event) { 345 | var change = objectAssign({}, event, { 346 | spyReportStart: true 347 | }); 348 | spyReport(change); 349 | } 350 | var END_EVENT = { 351 | spyReportEnd: true 352 | }; 353 | 354 | function spyReportEnd(change) { 355 | if (change) spyReport(objectAssign({}, change, END_EVENT)); 356 | else spyReport(END_EVENT); 357 | } 358 | 359 | function spy(listener) { 360 | globalState.spyListeners.push(listener); 361 | return once(function() { 362 | var idx = globalState.spyListeners.indexOf(listener); 363 | if (idx !== -1) globalState.spyListeners.splice(idx, 1); 364 | }); 365 | } 366 | 367 | function iteratorSymbol() { 368 | return typeof Symbol === "function" && Symbol.iterator || "@@iterator"; 369 | } 370 | var IS_ITERATING_MARKER = "__$$iterating"; 371 | 372 | function arrayAsIterator(array) { 373 | // returning an array for entries(), values() etc for maps was a mis-interpretation of the specs.., 374 | // yet it is quite convenient to be able to use the response both as array directly and as iterator 375 | // it is suboptimal, but alas... 376 | invariant(array[IS_ITERATING_MARKER] !== true, "Illegal state: cannot recycle array as iterator"); 377 | addHiddenFinalProp(array, IS_ITERATING_MARKER, true); 378 | var idx = -1; 379 | addHiddenFinalProp(array, "next", function next() { 380 | idx++; 381 | return { 382 | done: idx >= this.length, 383 | value: idx < this.length ? this[idx] : undefined 384 | }; 385 | }); 386 | return array; 387 | } 388 | 389 | function declareIterator(prototType, iteratorFactory) { 390 | addHiddenFinalProp(prototType, iteratorSymbol(), iteratorFactory); 391 | } 392 | 393 | var MAX_SPLICE_SIZE = 10000; // See e.g. https://github.com/mobxjs/mobx/issues/859 394 | // Detects bug in safari 9.1.1 (or iOS 9 safari mobile). See #364 395 | var safariPrototypeSetterInheritanceBug = function() { 396 | var v = false; 397 | var p = {}; 398 | Object.defineProperty(p, "0", { 399 | set: function set() { 400 | v = true; 401 | } 402 | }); 403 | Object.create(p)["0"] = 1; 404 | return v === false; 405 | }(); 406 | /** 407 | * This array buffer contains two lists of properties, so that all arrays 408 | * can recycle their property definitions, which significantly improves performance of creating 409 | * properties on the fly. 410 | */ 411 | var OBSERVABLE_ARRAY_BUFFER_SIZE = 0; 412 | // Typescript workaround to make sure ObservableArray extends Array 413 | var StubArray = function() { 414 | function StubArray() {} 415 | return StubArray; 416 | }(); 417 | 418 | function inherit(ctor, proto) { 419 | if (typeof Object["setPrototypeOf"] !== "undefined") { 420 | Object["setPrototypeOf"](ctor.prototype, proto); 421 | } else if (typeof ctor.prototype.__proto__ !== "undefined") { 422 | ctor.prototype.__proto__ = proto; 423 | } else { 424 | ctor["prototype"] = proto; 425 | } 426 | } 427 | inherit(StubArray, Array.prototype); 428 | var ObservableArrayAdministration = function() { 429 | function ObservableArrayAdministration(name, enhancer, array, owned) { 430 | this.array = array; 431 | this.owned = owned; 432 | this.values = []; 433 | this.lastKnownLength = 0; 434 | this.interceptors = null; 435 | this.changeListeners = null; 436 | this.atom = new BaseAtom(name || "ObservableArray@" + getNextId()); 437 | this.enhancer = function(newV, oldV) { 438 | return enhancer(newV, oldV, name + "[..]"); 439 | }; 440 | } 441 | ObservableArrayAdministration.prototype.dehanceValue = function(value) { 442 | if (this.dehancer !== undefined) return this.dehancer(value); 443 | return value; 444 | }; 445 | ObservableArrayAdministration.prototype.dehanceValues = function(values) { 446 | if (this.dehancer !== undefined) return values.map(this.dehancer); 447 | return values; 448 | }; 449 | ObservableArrayAdministration.prototype.intercept = function(handler) { 450 | return registerInterceptor(this, handler); 451 | }; 452 | ObservableArrayAdministration.prototype.observe = function(listener, fireImmediately) { 453 | if (fireImmediately === void 0) { 454 | fireImmediately = false; 455 | } 456 | if (fireImmediately) { 457 | listener({ 458 | object: this.array, 459 | type: "splice", 460 | index: 0, 461 | added: this.values.slice(), 462 | addedCount: this.values.length, 463 | removed: [], 464 | removedCount: 0 465 | }); 466 | } 467 | return registerListener(this, listener); 468 | }; 469 | ObservableArrayAdministration.prototype.getArrayLength = function() { 470 | this.atom.reportObserved(); 471 | return this.values.length; 472 | }; 473 | ObservableArrayAdministration.prototype.setArrayLength = function(newLength) { 474 | if (typeof newLength !== "number" || newLength < 0) throw new Error("[mobx.array] Out of range: " + newLength); 475 | var currentLength = this.values.length; 476 | if (newLength === currentLength) return; 477 | else if (newLength > currentLength) { 478 | var newItems = new Array(newLength - currentLength); 479 | for (var i = 0; i < newLength - currentLength; i++) { 480 | newItems[i] = undefined; 481 | } // No Array.fill everywhere... 482 | this.spliceWithArray(currentLength, 0, newItems); 483 | } else this.spliceWithArray(newLength, currentLength - newLength); 484 | }; 485 | // adds / removes the necessary numeric properties to this object 486 | ObservableArrayAdministration.prototype.updateArrayLength = function(oldLength, delta) { 487 | if (oldLength !== this.lastKnownLength) throw new Error("[mobx] Modification exception: the internal structure of an observable array was changed. Did you use peek() to change it?"); 488 | this.lastKnownLength += delta; 489 | if (delta > 0 && oldLength + delta + 1 > OBSERVABLE_ARRAY_BUFFER_SIZE) reserveArrayBuffer(oldLength + delta + 1); 490 | }; 491 | ObservableArrayAdministration.prototype.spliceWithArray = function(index, deleteCount, newItems) { 492 | var _this = this; 493 | checkIfStateModificationsAreAllowed(this.atom); 494 | var length = this.values.length; 495 | if (index === undefined) index = 0; 496 | else if (index > length) index = length; 497 | else if (index < 0) index = Math.max(0, length + index); 498 | if (arguments.length === 1) deleteCount = length - index; 499 | else if (deleteCount === undefined || deleteCount === null) deleteCount = 0; 500 | else deleteCount = Math.max(0, Math.min(deleteCount, length - index)); 501 | if (newItems === undefined) newItems = []; 502 | if (hasInterceptors(this)) { 503 | var change = interceptChange(this, { 504 | object: this.array, 505 | type: "splice", 506 | index: index, 507 | removedCount: deleteCount, 508 | added: newItems 509 | }); 510 | if (!change) return EMPTY_ARRAY; 511 | deleteCount = change.removedCount; 512 | newItems = change.added; 513 | } 514 | newItems = newItems.map(function(v) { 515 | return _this.enhancer(v, undefined); 516 | }); 517 | var lengthDelta = newItems.length - deleteCount; 518 | this.updateArrayLength(length, lengthDelta); // create or remove new entries 519 | var res = this.spliceItemsIntoValues(index, deleteCount, newItems); 520 | if (deleteCount !== 0 || newItems.length !== 0) this.notifyArraySplice(index, newItems, res); 521 | return this.dehanceValues(res); 522 | }; 523 | ObservableArrayAdministration.prototype.spliceItemsIntoValues = function(index, deleteCount, newItems) { 524 | if (newItems.length < MAX_SPLICE_SIZE) { 525 | return (_a = this.values).splice.apply(_a, [index, deleteCount].concat(newItems)); 526 | } else { 527 | var res = this.values.slice(index, index + deleteCount); 528 | this.values = this.values.slice(0, index).concat(newItems, this.values.slice(index + deleteCount)); 529 | return res; 530 | } 531 | var _a; 532 | }; 533 | ObservableArrayAdministration.prototype.notifyArrayChildUpdate = function(index, newValue, oldValue) { 534 | var notifySpy = !this.owned && isSpyEnabled(); 535 | var notify = hasListeners(this); 536 | var change = notify || notifySpy ? { 537 | object: this.array, 538 | type: "update", 539 | index: index, 540 | newValue: newValue, 541 | oldValue: oldValue 542 | } : null; 543 | if (notifySpy) spyReportStart(change); 544 | this.atom.reportChanged(); 545 | if (notify) notifyListeners(this, change); 546 | if (notifySpy) spyReportEnd(); 547 | }; 548 | ObservableArrayAdministration.prototype.notifyArraySplice = function(index, added, removed) { 549 | var notifySpy = !this.owned && isSpyEnabled(); 550 | var notify = hasListeners(this); 551 | var change = notify || notifySpy ? { 552 | object: this.array, 553 | type: "splice", 554 | index: index, 555 | removed: removed, 556 | added: added, 557 | removedCount: removed.length, 558 | addedCount: added.length 559 | } : null; 560 | if (notifySpy) spyReportStart(change); 561 | this.atom.reportChanged(); 562 | // conform: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/observe 563 | if (notify) notifyListeners(this, change); 564 | if (notifySpy) spyReportEnd(); 565 | }; 566 | return ObservableArrayAdministration; 567 | }(); 568 | var ObservableArray = function(_super) { 569 | __extends(ObservableArray, _super); 570 | 571 | function ObservableArray(initialValues, enhancer, name, owned) { 572 | if (name === void 0) { 573 | name = "ObservableArray@" + getNextId(); 574 | } 575 | if (owned === void 0) { 576 | owned = false; 577 | } 578 | var _this = _super.call(this) || this; 579 | var adm = new ObservableArrayAdministration(name, enhancer, _this, owned); 580 | addHiddenFinalProp(_this, "$mobx", adm); 581 | if (initialValues && initialValues.length) { 582 | _this.spliceWithArray(0, 0, initialValues); 583 | } 584 | if (safariPrototypeSetterInheritanceBug) { 585 | // Seems that Safari won't use numeric prototype setter untill any * numeric property is 586 | // defined on the instance. After that it works fine, even if this property is deleted. 587 | Object.defineProperty(adm.array, "0", ENTRY_0); 588 | } 589 | return _this; 590 | } 591 | ObservableArray.prototype.intercept = function(handler) { 592 | return this.$mobx.intercept(handler); 593 | }; 594 | ObservableArray.prototype.observe = function(listener, fireImmediately) { 595 | if (fireImmediately === void 0) { 596 | fireImmediately = false; 597 | } 598 | return this.$mobx.observe(listener, fireImmediately); 599 | }; 600 | ObservableArray.prototype.clear = function() { 601 | return this.splice(0); 602 | }; 603 | ObservableArray.prototype.concat = function() { 604 | var arrays = []; 605 | for (var _i = 0; _i < arguments.length; _i++) { 606 | arrays[_i] = arguments[_i]; 607 | } 608 | this.$mobx.atom.reportObserved(); 609 | return Array.prototype.concat.apply(this.peek(), arrays.map(function(a) { 610 | return isObservableArray(a) ? a.peek() : a; 611 | })); 612 | }; 613 | ObservableArray.prototype.replace = function(newItems) { 614 | return this.$mobx.spliceWithArray(0, this.$mobx.values.length, newItems); 615 | }; 616 | /** 617 | * Converts this array back to a (shallow) javascript structure. 618 | * For a deep clone use mobx.toJS 619 | */ 620 | ObservableArray.prototype.toJS = function() { 621 | return this.slice(); 622 | }; 623 | ObservableArray.prototype.toJSON = function() { 624 | // Used by JSON.stringify 625 | return this.toJS(); 626 | }; 627 | ObservableArray.prototype.peek = function() { 628 | this.$mobx.atom.reportObserved(); 629 | return this.$mobx.dehanceValues(this.$mobx.values); 630 | }; 631 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find 632 | ObservableArray.prototype.find = function(predicate, thisArg, fromIndex) { 633 | if (fromIndex === void 0) { 634 | fromIndex = 0; 635 | } 636 | var idx = this.findIndex.apply(this, arguments); 637 | return idx === -1 ? undefined : this.get(idx); 638 | }; 639 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex 640 | ObservableArray.prototype.findIndex = function(predicate, thisArg, fromIndex) { 641 | if (fromIndex === void 0) { 642 | fromIndex = 0; 643 | } 644 | var items = this.peek(), 645 | l = items.length; 646 | for (var i = fromIndex; i < l; i++) { 647 | if (predicate.call(thisArg, items[i], i, this)) return i; 648 | } 649 | return -1; 650 | }; 651 | /* 652 | functions that do alter the internal structure of the array, (based on lib.es6.d.ts) 653 | since these functions alter the inner structure of the array, the have side effects. 654 | Because the have side effects, they should not be used in computed function, 655 | and for that reason the do not call dependencyState.notifyObserved 656 | */ 657 | ObservableArray.prototype.splice = function(index, deleteCount) { 658 | var newItems = []; 659 | for (var _i = 2; _i < arguments.length; _i++) { 660 | newItems[_i - 2] = arguments[_i]; 661 | } 662 | switch (arguments.length) { 663 | case 0: 664 | return []; 665 | case 1: 666 | return this.$mobx.spliceWithArray(index); 667 | case 2: 668 | return this.$mobx.spliceWithArray(index, deleteCount); 669 | } 670 | return this.$mobx.spliceWithArray(index, deleteCount, newItems); 671 | }; 672 | ObservableArray.prototype.spliceWithArray = function(index, deleteCount, newItems) { 673 | return this.$mobx.spliceWithArray(index, deleteCount, newItems); 674 | }; 675 | ObservableArray.prototype.push = function() { 676 | var items = []; 677 | for (var _i = 0; _i < arguments.length; _i++) { 678 | items[_i] = arguments[_i]; 679 | } 680 | var adm = this.$mobx; 681 | adm.spliceWithArray(adm.values.length, 0, items); 682 | return adm.values.length; 683 | }; 684 | ObservableArray.prototype.pop = function() { 685 | return this.splice(Math.max(this.$mobx.values.length - 1, 0), 1)[0]; 686 | }; 687 | ObservableArray.prototype.shift = function() { 688 | return this.splice(0, 1)[0]; 689 | }; 690 | ObservableArray.prototype.unshift = function() { 691 | var items = []; 692 | for (var _i = 0; _i < arguments.length; _i++) { 693 | items[_i] = arguments[_i]; 694 | } 695 | var adm = this.$mobx; 696 | adm.spliceWithArray(0, 0, items); 697 | return adm.values.length; 698 | }; 699 | ObservableArray.prototype.reverse = function() { 700 | // reverse by default mutates in place before returning the result 701 | // which makes it both a 'derivation' and a 'mutation'. 702 | // so we deviate from the default and just make it an dervitation 703 | var clone = this.slice(); 704 | return clone.reverse.apply(clone, arguments); 705 | }; 706 | ObservableArray.prototype.sort = function(compareFn) { 707 | // sort by default mutates in place before returning the result 708 | // which goes against all good practices. Let's not change the array in place! 709 | var clone = this.slice(); 710 | return clone.sort.apply(clone, arguments); 711 | }; 712 | ObservableArray.prototype.remove = function(value) { 713 | var idx = this.$mobx.dehanceValues(this.$mobx.values).indexOf(value); 714 | if (idx > -1) { 715 | this.splice(idx, 1); 716 | return true; 717 | } 718 | return false; 719 | }; 720 | ObservableArray.prototype.move = function(fromIndex, toIndex) { 721 | function checkIndex(index) { 722 | if (index < 0) { 723 | throw new Error("[mobx.array] Index out of bounds: " + index + " is negative"); 724 | } 725 | var length = this.$mobx.values.length; 726 | if (index >= length) { 727 | throw new Error("[mobx.array] Index out of bounds: " + index + " is not smaller than " + length); 728 | } 729 | } 730 | checkIndex.call(this, fromIndex); 731 | checkIndex.call(this, toIndex); 732 | if (fromIndex === toIndex) { 733 | return; 734 | } 735 | var oldItems = this.$mobx.values; 736 | var newItems; 737 | if (fromIndex < toIndex) { 738 | newItems = oldItems.slice(0, fromIndex).concat(oldItems.slice(fromIndex + 1, toIndex + 1), [oldItems[fromIndex]], oldItems.slice(toIndex + 1)); 739 | } else { 740 | newItems = oldItems.slice(0, toIndex).concat([oldItems[fromIndex]], oldItems.slice(toIndex, fromIndex), oldItems.slice(fromIndex + 1)); 741 | } 742 | this.replace(newItems); 743 | }; 744 | // See #734, in case property accessors are unreliable... 745 | ObservableArray.prototype.get = function(index) { 746 | var impl = this.$mobx; 747 | if (impl) { 748 | if (index < impl.values.length) { 749 | impl.atom.reportObserved(); 750 | return impl.dehanceValue(impl.values[index]); 751 | } 752 | console.warn("[mobx.array] Attempt to read an array index (" + index + ") that is out of bounds (" + impl.values.length + "). Please check length first. Out of bound indices will not be tracked by MobX"); 753 | } 754 | return undefined; 755 | }; 756 | // See #734, in case property accessors are unreliable... 757 | ObservableArray.prototype.set = function(index, newValue) { 758 | var adm = this.$mobx; 759 | var values = adm.values; 760 | if (index < values.length) { 761 | // update at index in range 762 | checkIfStateModificationsAreAllowed(adm.atom); 763 | var oldValue = values[index]; 764 | if (hasInterceptors(adm)) { 765 | var change = interceptChange(adm, { 766 | type: "update", 767 | object: this, 768 | index: index, 769 | newValue: newValue 770 | }); 771 | if (!change) return; 772 | newValue = change.newValue; 773 | } 774 | newValue = adm.enhancer(newValue, oldValue); 775 | var changed = newValue !== oldValue; 776 | if (changed) { 777 | values[index] = newValue; 778 | adm.notifyArrayChildUpdate(index, newValue, oldValue); 779 | } 780 | } else if (index === values.length) { 781 | // add a new item 782 | adm.spliceWithArray(index, 0, [newValue]); 783 | } else { 784 | // out of bounds 785 | throw new Error("[mobx.array] Index out of bounds, " + index + " is larger than " + values.length); 786 | } 787 | }; 788 | return ObservableArray; 789 | }(StubArray); 790 | declareIterator(ObservableArray.prototype, function() { 791 | return arrayAsIterator(this.slice()); 792 | }); 793 | Object.defineProperty(ObservableArray.prototype, "length", { 794 | enumerable: false, 795 | configurable: true, 796 | get: function get() { 797 | return this.$mobx.getArrayLength(); 798 | }, 799 | set: function set(newLength) { 800 | this.$mobx.setArrayLength(newLength); 801 | } 802 | }); 803 | /** 804 | * Wrap function from prototype 805 | */ 806 | ["every", "filter", "forEach", "indexOf", "join", "lastIndexOf", "map", "reduce", "reduceRight", "slice", "some", "toString", "toLocaleString"].forEach(function(funcName) { 807 | var baseFunc = Array.prototype[funcName]; 808 | invariant(typeof baseFunc === "function", "Base function not defined on Array prototype: '" + funcName + "'"); 809 | addHiddenProp(ObservableArray.prototype, funcName, function() { 810 | return baseFunc.apply(this.peek(), arguments); 811 | }); 812 | }); 813 | /** 814 | * We don't want those to show up in `for (const key in ar)` ... 815 | */ 816 | makeNonEnumerable(ObservableArray.prototype, ["constructor", "intercept", "observe", "clear", "concat", "get", "replace", "toJS", "toJSON", "peek", "find", "findIndex", "splice", "spliceWithArray", "push", "pop", "set", "shift", "unshift", "reverse", "sort", "remove", "move", "toString", "toLocaleString"]); 817 | // See #364 818 | var ENTRY_0 = createArrayEntryDescriptor(0); 819 | 820 | function createArrayEntryDescriptor(index) { 821 | return { 822 | enumerable: false, 823 | configurable: false, 824 | get: function get() { 825 | // TODO: Check `this`?, see #752? 826 | return this.get(index); 827 | }, 828 | set: function set(value) { 829 | this.set(index, value); 830 | } 831 | }; 832 | } 833 | 834 | function createArrayBufferItem(index) { 835 | Object.defineProperty(ObservableArray.prototype, "" + index, createArrayEntryDescriptor(index)); 836 | } 837 | 838 | function reserveArrayBuffer(max) { 839 | for (var index = OBSERVABLE_ARRAY_BUFFER_SIZE; index < max; index++) { 840 | createArrayBufferItem(index); 841 | } 842 | OBSERVABLE_ARRAY_BUFFER_SIZE = max; 843 | } 844 | reserveArrayBuffer(1000); 845 | var isObservableArrayAdministration = createInstanceofPredicate("ObservableArrayAdministration", ObservableArrayAdministration); 846 | 847 | function isObservableArray(thing) { 848 | return isObject(thing) && isObservableArrayAdministration(thing.$mobx); 849 | } 850 | 851 | var UNCHANGED = {}; 852 | var ObservableValue = function(_super) { 853 | __extends(ObservableValue, _super); 854 | 855 | function ObservableValue(value, enhancer, name, notifySpy) { 856 | if (name === void 0) { 857 | name = "ObservableValue@" + getNextId(); 858 | } 859 | if (notifySpy === void 0) { 860 | notifySpy = true; 861 | } 862 | var _this = _super.call(this, name) || this; 863 | _this.enhancer = enhancer; 864 | _this.hasUnreportedChange = false; 865 | _this.dehancer = undefined; 866 | _this.value = enhancer(value, undefined, name); 867 | if (notifySpy && isSpyEnabled()) { 868 | // only notify spy if this is a stand-alone observable 869 | spyReport({ 870 | type: "create", 871 | object: _this, 872 | newValue: _this.value 873 | }); 874 | } 875 | return _this; 876 | } 877 | ObservableValue.prototype.dehanceValue = function(value) { 878 | if (this.dehancer !== undefined) return this.dehancer(value); 879 | return value; 880 | }; 881 | ObservableValue.prototype.set = function(newValue) { 882 | var oldValue = this.value; 883 | newValue = this.prepareNewValue(newValue); 884 | if (newValue !== UNCHANGED) { 885 | var notifySpy = isSpyEnabled(); 886 | if (notifySpy) { 887 | spyReportStart({ 888 | type: "update", 889 | object: this, 890 | newValue: newValue, 891 | oldValue: oldValue 892 | }); 893 | } 894 | this.setNewValue(newValue); 895 | if (notifySpy) spyReportEnd(); 896 | } 897 | }; 898 | ObservableValue.prototype.prepareNewValue = function(newValue) { 899 | checkIfStateModificationsAreAllowed(this); 900 | if (hasInterceptors(this)) { 901 | var change = interceptChange(this, { 902 | object: this, 903 | type: "update", 904 | newValue: newValue 905 | }); 906 | if (!change) return UNCHANGED; 907 | newValue = change.newValue; 908 | } 909 | // apply modifier 910 | newValue = this.enhancer(newValue, this.value, this.name); 911 | return this.value !== newValue ? newValue : UNCHANGED; 912 | }; 913 | ObservableValue.prototype.setNewValue = function(newValue) { 914 | var oldValue = this.value; 915 | this.value = newValue; 916 | this.reportChanged(); 917 | if (hasListeners(this)) { 918 | notifyListeners(this, { 919 | type: "update", 920 | object: this, 921 | newValue: newValue, 922 | oldValue: oldValue 923 | }); 924 | } 925 | }; 926 | ObservableValue.prototype.get = function() { 927 | this.reportObserved(); 928 | return this.dehanceValue(this.value); 929 | }; 930 | ObservableValue.prototype.intercept = function(handler) { 931 | return registerInterceptor(this, handler); 932 | }; 933 | ObservableValue.prototype.observe = function(listener, fireImmediately) { 934 | if (fireImmediately) listener({ 935 | object: this, 936 | type: "update", 937 | newValue: this.value, 938 | oldValue: undefined 939 | }); 940 | return registerListener(this, listener); 941 | }; 942 | ObservableValue.prototype.toJSON = function() { 943 | return this.get(); 944 | }; 945 | ObservableValue.prototype.toString = function() { 946 | return this.name + "[" + this.value + "]"; 947 | }; 948 | ObservableValue.prototype.valueOf = function() { 949 | return toPrimitive(this.get()); 950 | }; 951 | return ObservableValue; 952 | }(BaseAtom); 953 | ObservableValue.prototype[primitiveSymbol()] = ObservableValue.prototype.valueOf; 954 | var isObservableValue = createInstanceofPredicate("ObservableValue", ObservableValue); 955 | 956 | var messages = { 957 | "m001": "It is not allowed to assign new values to @action fields", 958 | "m002": "`runInAction` expects a function", 959 | "m003": "`runInAction` expects a function without arguments", 960 | "m004": "autorun expects a function", 961 | "m005": "Warning: attempted to pass an action to autorun. Actions are untracked and will not trigger on state changes. Use `reaction` or wrap only your state modification code in an action.", 962 | "m006": "Warning: attempted to pass an action to autorunAsync. Actions are untracked and will not trigger on state changes. Use `reaction` or wrap only your state modification code in an action.", 963 | "m007": "reaction only accepts 2 or 3 arguments. If migrating from MobX 2, please provide an options object", 964 | "m008": "wrapping reaction expression in `asReference` is no longer supported, use options object instead", 965 | "m009": "@computed can only be used on getter functions, like: '@computed get myProps() { return ...; }'. It looks like it was used on a property.", 966 | "m010": "@computed can only be used on getter functions, like: '@computed get myProps() { return ...; }'", 967 | "m011": "First argument to `computed` should be an expression. If using computed as decorator, don't pass it arguments", 968 | "m012": "computed takes one or two arguments if used as function", 969 | "m013": "[mobx.expr] 'expr' should only be used inside other reactive functions.", 970 | "m014": "extendObservable expected 2 or more arguments", 971 | "m015": "extendObservable expects an object as first argument", 972 | "m016": "extendObservable should not be used on maps, use map.merge instead", 973 | "m017": "all arguments of extendObservable should be objects", 974 | "m018": "extending an object with another observable (object) is not supported. Please construct an explicit propertymap, using `toJS` if need. See issue #540", 975 | "m019": "[mobx.isObservable] isObservable(object, propertyName) is not supported for arrays and maps. Use map.has or array.length instead.", 976 | "m020": "modifiers can only be used for individual object properties", 977 | "m021": "observable expects zero or one arguments", 978 | "m022": "@observable can not be used on getters, use @computed instead", 979 | "m023": "Using `transaction` is deprecated, use `runInAction` or `(@)action` instead.", 980 | "m024": "whyRun() can only be used if a derivation is active, or by passing an computed value / reaction explicitly. If you invoked whyRun from inside a computation; the computation is currently suspended but re-evaluating because somebody requested its value.", 981 | "m025": "whyRun can only be used on reactions and computed values", 982 | "m026": "`action` can only be invoked on functions", 983 | "m028": "It is not allowed to set `useStrict` when a derivation is running", 984 | "m029": "INTERNAL ERROR only onBecomeUnobserved shouldn't be called twice in a row", 985 | "m030a": "Since strict-mode is enabled, changing observed observable values outside actions is not allowed. Please wrap the code in an `action` if this change is intended. Tried to modify: ", 986 | "m030b": "Side effects like changing state are not allowed at this point. Are you trying to modify state from, for example, the render function of a React component? Tried to modify: ", 987 | "m031": "Computed values are not allowed to cause side effects by changing observables that are already being observed. Tried to modify: ", 988 | "m032": "* This computation is suspended (not in use by any reaction) and won't run automatically.\n Didn't expect this computation to be suspended at this point?\n 1. Make sure this computation is used by a reaction (reaction, autorun, observer).\n 2. Check whether you are using this computation synchronously (in the same stack as they reaction that needs it).", 989 | "m033": "`observe` doesn't support the fire immediately property for observable maps.", 990 | "m034": "`mobx.map` is deprecated, use `new ObservableMap` or `mobx.observable.map` instead", 991 | "m035": "Cannot make the designated object observable; it is not extensible", 992 | "m036": "It is not possible to get index atoms from arrays", 993 | "m037": "Hi there! I'm sorry you have just run into an exception.\nIf your debugger ends up here, know that some reaction (like the render() of an observer component, autorun or reaction)\nthrew an exception and that mobx caught it, to avoid that it brings the rest of your application down.\nThe original cause of the exception (the code that caused this reaction to run (again)), is still in the stack.\n\nHowever, more interesting is the actual stack trace of the error itself.\nHopefully the error is an instanceof Error, because in that case you can inspect the original stack of the error from where it was thrown.\nSee `error.stack` property, or press the very subtle \"(...)\" link you see near the console.error message that probably brought you here.\nThat stack is more interesting than the stack of this console.error itself.\n\nIf the exception you see is an exception you created yourself, make sure to use `throw new Error(\"Oops\")` instead of `throw \"Oops\"`,\nbecause the javascript environment will only preserve the original stack trace in the first form.\n\nYou can also make sure the debugger pauses the next time this very same exception is thrown by enabling \"Pause on caught exception\".\n(Note that it might pause on many other, unrelated exception as well).\n\nIf that all doesn't help you out, feel free to open an issue https://github.com/mobxjs/mobx/issues!\n", 994 | "m038": "Missing items in this list?\n 1. Check whether all used values are properly marked as observable (use isObservable to verify)\n 2. Make sure you didn't dereference values too early. MobX observes props, not primitives. E.g: use 'person.name' instead of 'name' in your computation.\n" 995 | }; 996 | 997 | function getMessage(id) { 998 | return messages[id]; 999 | } 1000 | 1001 | function createAction(actionName, fn) { 1002 | invariant(typeof fn === "function", getMessage("m026")); 1003 | invariant(typeof actionName === "string" && actionName.length > 0, "actions should have valid names, got: '" + actionName + "'"); 1004 | var res = function res() { 1005 | return executeAction(actionName, fn, this, arguments); 1006 | }; 1007 | res.originalFn = fn; 1008 | res.isMobxAction = true; 1009 | return res; 1010 | } 1011 | 1012 | function executeAction(actionName, fn, scope, args) { 1013 | var runInfo = startAction(actionName, fn, scope, args); 1014 | try { 1015 | return fn.apply(scope, args); 1016 | } finally { 1017 | endAction(runInfo); 1018 | } 1019 | } 1020 | 1021 | function startAction(actionName, fn, scope, args) { 1022 | var notifySpy = isSpyEnabled() && !!actionName; 1023 | var startTime = 0; 1024 | if (notifySpy) { 1025 | startTime = Date.now(); 1026 | var l = args && args.length || 0; 1027 | var flattendArgs = new Array(l); 1028 | if (l > 0) 1029 | for (var i = 0; i < l; i++) { 1030 | flattendArgs[i] = args[i]; 1031 | } 1032 | spyReportStart({ 1033 | type: "action", 1034 | name: actionName, 1035 | fn: fn, 1036 | object: scope, 1037 | arguments: flattendArgs 1038 | }); 1039 | } 1040 | var prevDerivation = untrackedStart(); 1041 | startBatch(); 1042 | var prevAllowStateChanges = allowStateChangesStart(true); 1043 | return { 1044 | prevDerivation: prevDerivation, 1045 | prevAllowStateChanges: prevAllowStateChanges, 1046 | notifySpy: notifySpy, 1047 | startTime: startTime 1048 | }; 1049 | } 1050 | 1051 | function endAction(runInfo) { 1052 | allowStateChangesEnd(runInfo.prevAllowStateChanges); 1053 | endBatch(); 1054 | untrackedEnd(runInfo.prevDerivation); 1055 | if (runInfo.notifySpy) spyReportEnd({ 1056 | time: Date.now() - runInfo.startTime 1057 | }); 1058 | } 1059 | 1060 | function useStrict(strict) { 1061 | invariant(globalState.trackingDerivation === null, getMessage("m028")); 1062 | globalState.strictMode = strict; 1063 | globalState.allowStateChanges = !strict; 1064 | } 1065 | 1066 | function isStrictModeEnabled() { 1067 | return globalState.strictMode; 1068 | } 1069 | 1070 | function allowStateChanges(allowStateChanges, func) { 1071 | // TODO: deprecate / refactor this function in next major 1072 | // Currently only used by `@observer` 1073 | // Proposed change: remove first param, rename to `forbidStateChanges`, 1074 | // require error callback instead of the hardcoded error message now used 1075 | // Use `inAction` instead of allowStateChanges in derivation.ts to check strictMode 1076 | var prev = allowStateChangesStart(allowStateChanges); 1077 | var res; 1078 | try { 1079 | res = func(); 1080 | } finally { 1081 | allowStateChangesEnd(prev); 1082 | } 1083 | return res; 1084 | } 1085 | 1086 | function allowStateChangesStart(allowStateChanges) { 1087 | var prev = globalState.allowStateChanges; 1088 | globalState.allowStateChanges = allowStateChanges; 1089 | return prev; 1090 | } 1091 | 1092 | function allowStateChangesEnd(prev) { 1093 | globalState.allowStateChanges = prev; 1094 | } 1095 | 1096 | /** 1097 | * Constructs a decorator, that normalizes the differences between 1098 | * TypeScript and Babel. Mainly caused by the fact that legacy-decorator cannot assign 1099 | * values during instance creation to properties that have a getter setter. 1100 | * 1101 | * - Sigh - 1102 | * 1103 | * Also takes care of the difference between @decorator field and @decorator(args) field, and different forms of values. 1104 | * For performance (cpu and mem) reasons the properties are always defined on the prototype (at least initially). 1105 | * This means that these properties despite being enumerable might not show up in Object.keys() (but they will show up in for...in loops). 1106 | */ 1107 | function createClassPropertyDecorator( 1108 | /** 1109 | * This function is invoked once, when the property is added to a new instance. 1110 | * When this happens is not strictly determined due to differences in TS and Babel: 1111 | * Typescript: Usually when constructing the new instance 1112 | * Babel, sometimes Typescript: during the first get / set 1113 | * Both: when calling `runLazyInitializers(instance)` 1114 | */ 1115 | onInitialize, _get, _set, enumerable, 1116 | /** 1117 | * Can this decorator invoked with arguments? e.g. @decorator(args) 1118 | */ 1119 | allowCustomArguments) { 1120 | function classPropertyDecorator(target, key, descriptor, customArgs, argLen) { 1121 | if (argLen === void 0) { 1122 | argLen = 0; 1123 | } 1124 | invariant(allowCustomArguments || quacksLikeADecorator(arguments), "This function is a decorator, but it wasn't invoked like a decorator"); 1125 | if (!descriptor) { 1126 | // typescript (except for getter / setters) 1127 | var newDescriptor = { 1128 | enumerable: enumerable, 1129 | configurable: true, 1130 | get: function get() { 1131 | if (!this.__mobxInitializedProps || this.__mobxInitializedProps[key] !== true) typescriptInitializeProperty(this, key, undefined, onInitialize, customArgs, descriptor); 1132 | return _get.call(this, key); 1133 | }, 1134 | set: function set(v) { 1135 | if (!this.__mobxInitializedProps || this.__mobxInitializedProps[key] !== true) { 1136 | typescriptInitializeProperty(this, key, v, onInitialize, customArgs, descriptor); 1137 | } else { 1138 | _set.call(this, key, v); 1139 | } 1140 | } 1141 | }; 1142 | if (arguments.length < 3 || arguments.length === 5 && argLen < 3) { 1143 | // Typescript target is ES3, so it won't define property for us 1144 | // or using Reflect.decorate polyfill, which will return no descriptor 1145 | // (see https://github.com/mobxjs/mobx/issues/333) 1146 | Object.defineProperty(target, key, newDescriptor); 1147 | } 1148 | return newDescriptor; 1149 | } else { 1150 | // babel and typescript getter / setter props 1151 | if (!hasOwnProperty(target, "__mobxLazyInitializers")) { 1152 | addHiddenProp(target, "__mobxLazyInitializers", target.__mobxLazyInitializers && target.__mobxLazyInitializers.slice() || [] // support inheritance 1153 | ); 1154 | } 1155 | var value_1 = descriptor.value, 1156 | initializer_1 = descriptor.initializer; 1157 | target.__mobxLazyInitializers.push(function(instance) { 1158 | onInitialize(instance, key, initializer_1 ? initializer_1.call(instance) : value_1, customArgs, descriptor); 1159 | }); 1160 | return { 1161 | enumerable: enumerable, 1162 | configurable: true, 1163 | get: function get() { 1164 | if (this.__mobxDidRunLazyInitializers !== true) runLazyInitializers(this); 1165 | return _get.call(this, key); 1166 | }, 1167 | set: function set(v) { 1168 | if (this.__mobxDidRunLazyInitializers !== true) runLazyInitializers(this); 1169 | _set.call(this, key, v); 1170 | } 1171 | }; 1172 | } 1173 | } 1174 | if (allowCustomArguments) { 1175 | /** If custom arguments are allowed, we should return a function that returns a decorator */ 1176 | return function() { 1177 | /** Direct invocation: @decorator bla */ 1178 | if (quacksLikeADecorator(arguments)) return classPropertyDecorator.apply(null, arguments); 1179 | /** Indirect invocation: @decorator(args) bla */ 1180 | var outerArgs = arguments; 1181 | var argLen = arguments.length; 1182 | return function(target, key, descriptor) { 1183 | return classPropertyDecorator(target, key, descriptor, outerArgs, argLen); 1184 | }; 1185 | }; 1186 | } 1187 | return classPropertyDecorator; 1188 | } 1189 | 1190 | function typescriptInitializeProperty(instance, key, v, onInitialize, customArgs, baseDescriptor) { 1191 | if (!hasOwnProperty(instance, "__mobxInitializedProps")) addHiddenProp(instance, "__mobxInitializedProps", {}); 1192 | instance.__mobxInitializedProps[key] = true; 1193 | onInitialize(instance, key, v, customArgs, baseDescriptor); 1194 | } 1195 | 1196 | function runLazyInitializers(instance) { 1197 | if (instance.__mobxDidRunLazyInitializers === true) return; 1198 | if (instance.__mobxLazyInitializers) { 1199 | addHiddenProp(instance, "__mobxDidRunLazyInitializers", true); 1200 | instance.__mobxDidRunLazyInitializers && instance.__mobxLazyInitializers.forEach(function(initializer) { 1201 | return initializer(instance); 1202 | }); 1203 | } 1204 | } 1205 | 1206 | function quacksLikeADecorator(args) { 1207 | return (args.length === 2 || args.length === 3) && typeof args[1] === "string"; 1208 | } 1209 | 1210 | var actionFieldDecorator = createClassPropertyDecorator(function(target, key, value, args, originalDescriptor) { 1211 | var actionName = args && args.length === 1 ? args[0] : value.name || key || ""; 1212 | var wrappedAction = action(actionName, value); 1213 | addHiddenProp(target, key, wrappedAction); 1214 | }, function(key) { 1215 | return this[key]; 1216 | }, function() { 1217 | invariant(false, getMessage("m001")); 1218 | }, false, true); 1219 | var boundActionDecorator = createClassPropertyDecorator(function(target, key, value) { 1220 | defineBoundAction(target, key, value); 1221 | }, function(key) { 1222 | return this[key]; 1223 | }, function() { 1224 | invariant(false, getMessage("m001")); 1225 | }, false, false); 1226 | var action = function action(arg1, arg2, arg3, arg4) { 1227 | if (arguments.length === 1 && typeof arg1 === "function") return createAction(arg1.name || "", arg1); 1228 | if (arguments.length === 2 && typeof arg2 === "function") return createAction(arg1, arg2); 1229 | if (arguments.length === 1 && typeof arg1 === "string") return namedActionDecorator(arg1); 1230 | return namedActionDecorator(arg2).apply(null, arguments); 1231 | }; 1232 | action.bound = function boundAction(arg1, arg2, arg3) { 1233 | if (typeof arg1 === "function") { 1234 | var action_1 = createAction("", arg1); 1235 | action_1.autoBind = true; 1236 | return action_1; 1237 | } 1238 | return boundActionDecorator.apply(null, arguments); 1239 | }; 1240 | 1241 | function namedActionDecorator(name) { 1242 | return function(target, prop, descriptor) { 1243 | if (descriptor && typeof descriptor.value === "function") { 1244 | // TypeScript @action method() { }. Defined on proto before being decorated 1245 | // Don't use the field decorator if we are just decorating a method 1246 | descriptor.value = createAction(name, descriptor.value); 1247 | descriptor.enumerable = false; 1248 | descriptor.configurable = true; 1249 | return descriptor; 1250 | } 1251 | // bound instance methods 1252 | return actionFieldDecorator(name).apply(this, arguments); 1253 | }; 1254 | } 1255 | 1256 | function runInAction(arg1, arg2, arg3) { 1257 | var actionName = typeof arg1 === "string" ? arg1 : arg1.name || ""; 1258 | var fn = typeof arg1 === "function" ? arg1 : arg2; 1259 | var scope = typeof arg1 === "function" ? arg2 : arg3; 1260 | invariant(typeof fn === "function", getMessage("m002")); 1261 | invariant(fn.length === 0, getMessage("m003")); 1262 | invariant(typeof actionName === "string" && actionName.length > 0, "actions should have valid names, got: '" + actionName + "'"); 1263 | return executeAction(actionName, fn, scope, undefined); 1264 | } 1265 | 1266 | function isAction(thing) { 1267 | return typeof thing === "function" && thing.isMobxAction === true; 1268 | } 1269 | 1270 | function defineBoundAction(target, propertyName, fn) { 1271 | var res = function res() { 1272 | return executeAction(propertyName, fn, target, arguments); 1273 | }; 1274 | res.isMobxAction = true; 1275 | addHiddenProp(target, propertyName, res); 1276 | } 1277 | 1278 | function identityComparer(a, b) { 1279 | return a === b; 1280 | } 1281 | 1282 | function structuralComparer(a, b) { 1283 | if (typeof a === 'number' && typeof b === 'number' && isNaN(a) && isNaN(b)) { 1284 | return true; 1285 | } 1286 | return deepEqual(a, b); 1287 | } 1288 | 1289 | function defaultComparer(a, b) { 1290 | if (typeof a === 'number' && typeof b === 'number' && isNaN(a) && isNaN(b)) { 1291 | return true; 1292 | } 1293 | return identityComparer(a, b); 1294 | } 1295 | var comparer = { 1296 | identity: identityComparer, 1297 | structural: structuralComparer, 1298 | default: defaultComparer 1299 | }; 1300 | 1301 | function autorun(arg1, arg2, arg3) { 1302 | var name, view, scope; 1303 | if (typeof arg1 === "string") { 1304 | name = arg1; 1305 | view = arg2; 1306 | scope = arg3; 1307 | } else { 1308 | name = arg1.name || "Autorun@" + getNextId(); 1309 | view = arg1; 1310 | scope = arg2; 1311 | } 1312 | invariant(typeof view === "function", getMessage("m004")); 1313 | invariant(isAction(view) === false, getMessage("m005")); 1314 | if (scope) view = view.bind(scope); 1315 | var reaction = new Reaction(name, function() { 1316 | this.track(reactionRunner); 1317 | }); 1318 | 1319 | function reactionRunner() { 1320 | view(reaction); 1321 | } 1322 | reaction.schedule(); 1323 | return reaction.getDisposer(); 1324 | } 1325 | 1326 | function when(arg1, arg2, arg3, arg4) { 1327 | var name, predicate, effect, scope; 1328 | if (typeof arg1 === "string") { 1329 | name = arg1; 1330 | predicate = arg2; 1331 | effect = arg3; 1332 | scope = arg4; 1333 | } else { 1334 | name = "When@" + getNextId(); 1335 | predicate = arg1; 1336 | effect = arg2; 1337 | scope = arg3; 1338 | } 1339 | var disposer = autorun(name, function(r) { 1340 | if (predicate.call(scope)) { 1341 | r.dispose(); 1342 | var prevUntracked = untrackedStart(); 1343 | effect.call(scope); 1344 | untrackedEnd(prevUntracked); 1345 | } 1346 | }); 1347 | return disposer; 1348 | } 1349 | 1350 | function autorunAsync(arg1, arg2, arg3, arg4) { 1351 | var name, func, delay, scope; 1352 | if (typeof arg1 === "string") { 1353 | name = arg1; 1354 | func = arg2; 1355 | delay = arg3; 1356 | scope = arg4; 1357 | } else { 1358 | name = arg1.name || "AutorunAsync@" + getNextId(); 1359 | func = arg1; 1360 | delay = arg2; 1361 | scope = arg3; 1362 | } 1363 | invariant(isAction(func) === false, getMessage("m006")); 1364 | if (delay === void 0) delay = 1; 1365 | if (scope) func = func.bind(scope); 1366 | var isScheduled = false; 1367 | var r = new Reaction(name, function() { 1368 | if (!isScheduled) { 1369 | isScheduled = true; 1370 | setTimeout(function() { 1371 | isScheduled = false; 1372 | if (!r.isDisposed) r.track(reactionRunner); 1373 | }, delay); 1374 | } 1375 | }); 1376 | 1377 | function reactionRunner() { 1378 | func(r); 1379 | } 1380 | r.schedule(); 1381 | return r.getDisposer(); 1382 | } 1383 | 1384 | function reaction(expression, effect, arg3) { 1385 | if (arguments.length > 3) { 1386 | fail(getMessage("m007")); 1387 | } 1388 | if (isModifierDescriptor(expression)) { 1389 | fail(getMessage("m008")); 1390 | } 1391 | var opts; 1392 | if ((typeof arg3 === "undefined" ? "undefined" : _typeof(arg3)) === "object") { 1393 | opts = arg3; 1394 | } else { 1395 | opts = {}; 1396 | } 1397 | opts.name = opts.name || expression.name || effect.name || "Reaction@" + getNextId(); 1398 | opts.fireImmediately = arg3 === true || opts.fireImmediately === true; 1399 | opts.delay = opts.delay || 0; 1400 | opts.compareStructural = opts.compareStructural || opts.struct || false; 1401 | effect = action(opts.name, opts.context ? effect.bind(opts.context) : effect); 1402 | if (opts.context) { 1403 | expression = expression.bind(opts.context); 1404 | } 1405 | var firstTime = true; 1406 | var isScheduled = false; 1407 | var value; 1408 | var equals = opts.equals ? opts.equals : opts.compareStructural || opts.struct ? comparer.structural : comparer.default; 1409 | var r = new Reaction(opts.name, function() { 1410 | if (firstTime || opts.delay < 1) { 1411 | reactionRunner(); 1412 | } else if (!isScheduled) { 1413 | isScheduled = true; 1414 | setTimeout(function() { 1415 | isScheduled = false; 1416 | reactionRunner(); 1417 | }, opts.delay); 1418 | } 1419 | }); 1420 | 1421 | function reactionRunner() { 1422 | if (r.isDisposed) return; 1423 | var changed = false; 1424 | r.track(function() { 1425 | var nextValue = expression(r); 1426 | changed = firstTime || !equals(value, nextValue); 1427 | value = nextValue; 1428 | }); 1429 | if (firstTime && opts.fireImmediately) effect(value, r); 1430 | if (!firstTime && changed === true) effect(value, r); 1431 | if (firstTime) firstTime = false; 1432 | } 1433 | r.schedule(); 1434 | return r.getDisposer(); 1435 | } 1436 | 1437 | /** 1438 | * A node in the state dependency root that observes other nodes, and can be observed itself. 1439 | * 1440 | * ComputedValue will remember result of the computation for duration of a batch, or being observed 1441 | * During this time it will recompute only when one of its direct dependencies changed, 1442 | * but only when it is being accessed with `ComputedValue.get()`. 1443 | * 1444 | * Implementation description: 1445 | * 1. First time it's being accessed it will compute and remember result 1446 | * give back remembered result until 2. happens 1447 | * 2. First time any deep dependency change, propagate POSSIBLY_STALE to all observers, wait for 3. 1448 | * 3. When it's being accessed, recompute if any shallow dependency changed. 1449 | * if result changed: propagate STALE to all observers, that were POSSIBLY_STALE from the last step. 1450 | * go to step 2. either way 1451 | * 1452 | * If at any point it's outside batch and it isn't observed: reset everything and go to 1. 1453 | */ 1454 | var ComputedValue = function() { 1455 | /** 1456 | * Create a new computed value based on a function expression. 1457 | * 1458 | * The `name` property is for debug purposes only. 1459 | * 1460 | * The `equals` property specifies the comparer function to use to determine if a newly produced 1461 | * value differs from the previous value. Two comparers are provided in the library; `defaultComparer` 1462 | * compares based on identity comparison (===), and `structualComparer` deeply compares the structure. 1463 | * Structural comparison can be convenient if you always produce an new aggregated object and 1464 | * don't want to notify observers if it is structurally the same. 1465 | * This is useful for working with vectors, mouse coordinates etc. 1466 | */ 1467 | function ComputedValue(derivation, scope, equals, name, setter) { 1468 | this.derivation = derivation; 1469 | this.scope = scope; 1470 | this.equals = equals; 1471 | this.dependenciesState = IDerivationState.NOT_TRACKING; 1472 | this.observing = []; // nodes we are looking at. Our value depends on these nodes 1473 | this.newObserving = null; // during tracking it's an array with new observed observers 1474 | this.isPendingUnobservation = false; 1475 | this.observers = []; 1476 | this.observersIndexes = {}; 1477 | this.diffValue = 0; 1478 | this.runId = 0; 1479 | this.lastAccessedBy = 0; 1480 | this.lowestObserverState = IDerivationState.UP_TO_DATE; 1481 | this.unboundDepsCount = 0; 1482 | this.__mapid = "#" + getNextId(); 1483 | this.value = new CaughtException(null); 1484 | this.isComputing = false; // to check for cycles 1485 | this.isRunningSetter = false; 1486 | this.name = name || "ComputedValue@" + getNextId(); 1487 | if (setter) this.setter = createAction(name + "-setter", setter); 1488 | } 1489 | ComputedValue.prototype.onBecomeStale = function() { 1490 | propagateMaybeChanged(this); 1491 | }; 1492 | ComputedValue.prototype.onBecomeUnobserved = function() { 1493 | clearObserving(this); 1494 | this.value = undefined; 1495 | }; 1496 | /** 1497 | * Returns the current value of this computed value. 1498 | * Will evaluate its computation first if needed. 1499 | */ 1500 | ComputedValue.prototype.get = function() { 1501 | invariant(!this.isComputing, "Cycle detected in computation " + this.name, this.derivation); 1502 | if (globalState.inBatch === 0) { 1503 | // This is an minor optimization which could be omitted to simplify the code 1504 | // The computedValue is accessed outside of any mobx stuff. Batch observing should be enough and don't need 1505 | // tracking as it will never be called again inside this batch. 1506 | startBatch(); 1507 | if (shouldCompute(this)) this.value = this.computeValue(false); 1508 | endBatch(); 1509 | } else { 1510 | reportObserved(this); 1511 | if (shouldCompute(this)) 1512 | if (this.trackAndCompute()) propagateChangeConfirmed(this); 1513 | } 1514 | var result = this.value; 1515 | if (isCaughtException(result)) throw result.cause; 1516 | return result; 1517 | }; 1518 | ComputedValue.prototype.peek = function() { 1519 | var res = this.computeValue(false); 1520 | if (isCaughtException(res)) throw res.cause; 1521 | return res; 1522 | }; 1523 | ComputedValue.prototype.set = function(value) { 1524 | if (this.setter) { 1525 | invariant(!this.isRunningSetter, "The setter of computed value '" + this.name + "' is trying to update itself. Did you intend to update an _observable_ value, instead of the computed property?"); 1526 | this.isRunningSetter = true; 1527 | try { 1528 | this.setter.call(this.scope, value); 1529 | } finally { 1530 | this.isRunningSetter = false; 1531 | } 1532 | } else invariant(false, "[ComputedValue '" + this.name + "'] It is not possible to assign a new value to a computed value."); 1533 | }; 1534 | ComputedValue.prototype.trackAndCompute = function() { 1535 | if (isSpyEnabled()) { 1536 | spyReport({ 1537 | object: this.scope, 1538 | type: "compute", 1539 | fn: this.derivation 1540 | }); 1541 | } 1542 | var oldValue = this.value; 1543 | var newValue = this.value = this.computeValue(true); 1544 | return isCaughtException(oldValue) || isCaughtException(newValue) || !this.equals(oldValue, newValue); 1545 | }; 1546 | ComputedValue.prototype.computeValue = function(track) { 1547 | this.isComputing = true; 1548 | globalState.computationDepth++; 1549 | var res; 1550 | if (track) { 1551 | res = trackDerivedFunction(this, this.derivation, this.scope); 1552 | } else { 1553 | try { 1554 | res = this.derivation.call(this.scope); 1555 | } catch (e) { 1556 | res = new CaughtException(e); 1557 | } 1558 | } 1559 | globalState.computationDepth--; 1560 | this.isComputing = false; 1561 | return res; 1562 | }; 1563 | 1564 | ComputedValue.prototype.observe = function(listener, fireImmediately) { 1565 | var _this = this; 1566 | var firstTime = true; 1567 | var prevValue = undefined; 1568 | return autorun(function() { 1569 | var newValue = _this.get(); 1570 | if (!firstTime || fireImmediately) { 1571 | var prevU = untrackedStart(); 1572 | listener({ 1573 | type: "update", 1574 | object: _this, 1575 | newValue: newValue, 1576 | oldValue: prevValue 1577 | }); 1578 | untrackedEnd(prevU); 1579 | } 1580 | firstTime = false; 1581 | prevValue = newValue; 1582 | }); 1583 | }; 1584 | ComputedValue.prototype.toJSON = function() { 1585 | return this.get(); 1586 | }; 1587 | ComputedValue.prototype.toString = function() { 1588 | return this.name + "[" + this.derivation.toString() + "]"; 1589 | }; 1590 | ComputedValue.prototype.valueOf = function() { 1591 | return toPrimitive(this.get()); 1592 | }; 1593 | 1594 | ComputedValue.prototype.whyRun = function() { 1595 | var isTracking = Boolean(globalState.trackingDerivation); 1596 | var observing = unique(this.isComputing ? this.newObserving : this.observing).map(function(dep) { 1597 | return dep.name; 1598 | }); 1599 | var observers = unique(getObservers(this).map(function(dep) { 1600 | return dep.name; 1601 | })); 1602 | return "\nWhyRun? computation '" + this.name + "':\n * Running because: " + (isTracking ? "[active] the value of this computation is needed by a reaction" : this.isComputing ? "[get] The value of this computed was requested outside a reaction" : "[idle] not running at the moment") + "\n" + (this.dependenciesState === IDerivationState.NOT_TRACKING ? getMessage("m032") : " * This computation will re-run if any of the following observables changes:\n " + joinStrings(observing) + "\n " + (this.isComputing && isTracking ? " (... or any observable accessed during the remainder of the current run)" : "") + "\n\t" + getMessage("m038") + "\n\n * If the outcome of this computation changes, the following observers will be re-run:\n " + joinStrings(observers) + "\n"); 1603 | }; 1604 | return ComputedValue; 1605 | }(); 1606 | ComputedValue.prototype[primitiveSymbol()] = ComputedValue.prototype.valueOf; 1607 | var isComputedValue = createInstanceofPredicate("ComputedValue", ComputedValue); 1608 | 1609 | var ObservableObjectAdministration = function() { 1610 | function ObservableObjectAdministration(target, name) { 1611 | this.target = target; 1612 | this.name = name; 1613 | this.values = {}; 1614 | this.changeListeners = null; 1615 | this.interceptors = null; 1616 | } 1617 | /** 1618 | * Observes this object. Triggers for the events 'add', 'update' and 'delete'. 1619 | * See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/observe 1620 | * for callback details 1621 | */ 1622 | ObservableObjectAdministration.prototype.observe = function(callback, fireImmediately) { 1623 | invariant(fireImmediately !== true, "`observe` doesn't support the fire immediately property for observable objects."); 1624 | return registerListener(this, callback); 1625 | }; 1626 | ObservableObjectAdministration.prototype.intercept = function(handler) { 1627 | return registerInterceptor(this, handler); 1628 | }; 1629 | return ObservableObjectAdministration; 1630 | }(); 1631 | 1632 | function asObservableObject(target, name) { 1633 | if (isObservableObject(target)) return target.$mobx; 1634 | invariant(Object.isExtensible(target), getMessage("m035")); 1635 | if (!isPlainObject(target)) name = (target.constructor.name || "ObservableObject") + "@" + getNextId(); 1636 | if (!name) name = "ObservableObject@" + getNextId(); 1637 | var adm = new ObservableObjectAdministration(target, name); 1638 | addHiddenFinalProp(target, "$mobx", adm); 1639 | return adm; 1640 | } 1641 | 1642 | function defineObservablePropertyFromDescriptor(adm, propName, descriptor, defaultEnhancer) { 1643 | if (adm.values[propName]) { 1644 | // already observable property 1645 | invariant("value" in descriptor, "The property " + propName + " in " + adm.name + " is already observable, cannot redefine it as computed property"); 1646 | adm.target[propName] = descriptor.value; // the property setter will make 'value' reactive if needed. 1647 | return; 1648 | } 1649 | // not yet observable property 1650 | if ("value" in descriptor) { 1651 | // not a computed value 1652 | if (isModifierDescriptor(descriptor.value)) { 1653 | // x : ref(someValue) 1654 | var modifierDescriptor = descriptor.value; 1655 | defineObservableProperty(adm, propName, modifierDescriptor.initialValue, modifierDescriptor.enhancer); 1656 | } else if (isAction(descriptor.value) && descriptor.value.autoBind === true) { 1657 | defineBoundAction(adm.target, propName, descriptor.value.originalFn); 1658 | } else if (isComputedValue(descriptor.value)) { 1659 | // x: computed(someExpr) 1660 | defineComputedPropertyFromComputedValue(adm, propName, descriptor.value); 1661 | } else { 1662 | // x: someValue 1663 | defineObservableProperty(adm, propName, descriptor.value, defaultEnhancer); 1664 | } 1665 | } else { 1666 | // get x() { return 3 } set x(v) { } 1667 | defineComputedProperty(adm, propName, descriptor.get, descriptor.set, comparer.default, true); 1668 | } 1669 | } 1670 | 1671 | function defineObservableProperty(adm, propName, newValue, enhancer) { 1672 | assertPropertyConfigurable(adm.target, propName); 1673 | if (hasInterceptors(adm)) { 1674 | var change = interceptChange(adm, { 1675 | object: adm.target, 1676 | name: propName, 1677 | type: "add", 1678 | newValue: newValue 1679 | }); 1680 | if (!change) return; 1681 | newValue = change.newValue; 1682 | } 1683 | var observable = adm.values[propName] = new ObservableValue(newValue, enhancer, adm.name + "." + propName, false); 1684 | newValue = observable.value; // observableValue might have changed it 1685 | Object.defineProperty(adm.target, propName, generateObservablePropConfig(propName)); 1686 | notifyPropertyAddition(adm, adm.target, propName, newValue); 1687 | } 1688 | 1689 | function defineComputedProperty(adm, propName, getter, setter, equals, asInstanceProperty) { 1690 | if (asInstanceProperty) assertPropertyConfigurable(adm.target, propName); 1691 | adm.values[propName] = new ComputedValue(getter, adm.target, equals, adm.name + "." + propName, setter); 1692 | if (asInstanceProperty) { 1693 | Object.defineProperty(adm.target, propName, generateComputedPropConfig(propName)); 1694 | } 1695 | } 1696 | 1697 | function defineComputedPropertyFromComputedValue(adm, propName, computedValue) { 1698 | var name = adm.name + "." + propName; 1699 | computedValue.name = name; 1700 | if (!computedValue.scope) computedValue.scope = adm.target; 1701 | adm.values[propName] = computedValue; 1702 | Object.defineProperty(adm.target, propName, generateComputedPropConfig(propName)); 1703 | } 1704 | var observablePropertyConfigs = {}; 1705 | var computedPropertyConfigs = {}; 1706 | 1707 | function generateObservablePropConfig(propName) { 1708 | return observablePropertyConfigs[propName] || (observablePropertyConfigs[propName] = { 1709 | configurable: true, 1710 | enumerable: true, 1711 | get: function get() { 1712 | return this.$mobx.values[propName].get(); 1713 | }, 1714 | set: function set(v) { 1715 | setPropertyValue(this, propName, v); 1716 | } 1717 | }); 1718 | } 1719 | 1720 | function generateComputedPropConfig(propName) { 1721 | return computedPropertyConfigs[propName] || (computedPropertyConfigs[propName] = { 1722 | configurable: true, 1723 | enumerable: false, 1724 | get: function get() { 1725 | return this.$mobx.values[propName].get(); 1726 | }, 1727 | set: function set(v) { 1728 | return this.$mobx.values[propName].set(v); 1729 | } 1730 | }); 1731 | } 1732 | 1733 | function setPropertyValue(instance, name, newValue) { 1734 | var adm = instance.$mobx; 1735 | var observable = adm.values[name]; 1736 | // intercept 1737 | if (hasInterceptors(adm)) { 1738 | var change = interceptChange(adm, { 1739 | type: "update", 1740 | object: instance, 1741 | name: name, 1742 | newValue: newValue 1743 | }); 1744 | if (!change) return; 1745 | newValue = change.newValue; 1746 | } 1747 | newValue = observable.prepareNewValue(newValue); 1748 | // notify spy & observers 1749 | if (newValue !== UNCHANGED) { 1750 | var notify = hasListeners(adm); 1751 | var notifySpy = isSpyEnabled(); 1752 | var change = notify || notifySpy ? { 1753 | type: "update", 1754 | object: instance, 1755 | oldValue: observable.value, 1756 | name: name, 1757 | newValue: newValue 1758 | } : null; 1759 | if (notifySpy) spyReportStart(change); 1760 | observable.setNewValue(newValue); 1761 | if (notify) notifyListeners(adm, change); 1762 | if (notifySpy) spyReportEnd(); 1763 | } 1764 | } 1765 | 1766 | function notifyPropertyAddition(adm, object, name, newValue) { 1767 | var notify = hasListeners(adm); 1768 | var notifySpy = isSpyEnabled(); 1769 | var change = notify || notifySpy ? { 1770 | type: "add", 1771 | object: object, 1772 | name: name, 1773 | newValue: newValue 1774 | } : null; 1775 | if (notifySpy) spyReportStart(change); 1776 | if (notify) notifyListeners(adm, change); 1777 | if (notifySpy) spyReportEnd(); 1778 | } 1779 | var isObservableObjectAdministration = createInstanceofPredicate("ObservableObjectAdministration", ObservableObjectAdministration); 1780 | 1781 | function isObservableObject(thing) { 1782 | if (isObject(thing)) { 1783 | // Initializers run lazily when transpiling to babel, so make sure they are run... 1784 | runLazyInitializers(thing); 1785 | return isObservableObjectAdministration(thing.$mobx); 1786 | } 1787 | return false; 1788 | } 1789 | 1790 | /** 1791 | * Returns true if the provided value is reactive. 1792 | * @param value object, function or array 1793 | * @param property if property is specified, checks whether value.property is reactive. 1794 | */ 1795 | function isObservable(value, property) { 1796 | if (value === null || value === undefined) return false; 1797 | if (property !== undefined) { 1798 | if (isObservableArray(value) || isObservableMap(value)) throw new Error(getMessage("m019")); 1799 | else if (isObservableObject(value)) { 1800 | var o = value.$mobx; 1801 | return o.values && !!o.values[property]; 1802 | } 1803 | return false; 1804 | } 1805 | // For first check, see #701 1806 | return isObservableObject(value) || !!value.$mobx || isAtom(value) || isReaction(value) || isComputedValue(value); 1807 | } 1808 | 1809 | function createDecoratorForEnhancer(enhancer) { 1810 | invariant(!!enhancer, ":("); 1811 | return createClassPropertyDecorator(function(target, name, baseValue, _, baseDescriptor) { 1812 | assertPropertyConfigurable(target, name); 1813 | invariant(!baseDescriptor || !baseDescriptor.get, getMessage("m022")); 1814 | var adm = asObservableObject(target, undefined); 1815 | defineObservableProperty(adm, name, baseValue, enhancer); 1816 | }, function(name) { 1817 | var observable = this.$mobx.values[name]; 1818 | if (observable === undefined) return undefined; 1819 | return observable.get(); 1820 | }, function(name, value) { 1821 | setPropertyValue(this, name, value); 1822 | }, true, false); 1823 | } 1824 | 1825 | function extendObservable(target) { 1826 | var properties = []; 1827 | for (var _i = 1; _i < arguments.length; _i++) { 1828 | properties[_i - 1] = arguments[_i]; 1829 | } 1830 | return extendObservableHelper(target, deepEnhancer, properties); 1831 | } 1832 | 1833 | function extendShallowObservable(target) { 1834 | var properties = []; 1835 | for (var _i = 1; _i < arguments.length; _i++) { 1836 | properties[_i - 1] = arguments[_i]; 1837 | } 1838 | return extendObservableHelper(target, referenceEnhancer, properties); 1839 | } 1840 | 1841 | function extendObservableHelper(target, defaultEnhancer, properties) { 1842 | invariant(arguments.length >= 2, getMessage("m014")); 1843 | invariant((typeof target === "undefined" ? "undefined" : _typeof(target)) === "object", getMessage("m015")); 1844 | invariant(!isObservableMap(target), getMessage("m016")); 1845 | properties.forEach(function(propSet) { 1846 | invariant((typeof propSet === "undefined" ? "undefined" : _typeof(propSet)) === "object", getMessage("m017")); 1847 | invariant(!isObservable(propSet), getMessage("m018")); 1848 | }); 1849 | var adm = asObservableObject(target); 1850 | var definedProps = {}; 1851 | // Note could be optimised if properties.length === 1 1852 | for (var i = properties.length - 1; i >= 0; i--) { 1853 | var propSet = properties[i]; 1854 | for (var key in propSet) { 1855 | if (definedProps[key] !== true && hasOwnProperty(propSet, key)) { 1856 | definedProps[key] = true; 1857 | if (target === propSet && !isPropertyConfigurable(target, key)) continue; // see #111, skip non-configurable or non-writable props for `observable(object)`. 1858 | var descriptor = Object.getOwnPropertyDescriptor(propSet, key); 1859 | defineObservablePropertyFromDescriptor(adm, key, descriptor, defaultEnhancer); 1860 | } 1861 | } 1862 | } 1863 | return target; 1864 | } 1865 | 1866 | var deepDecorator = createDecoratorForEnhancer(deepEnhancer); 1867 | var shallowDecorator = createDecoratorForEnhancer(shallowEnhancer); 1868 | var refDecorator = createDecoratorForEnhancer(referenceEnhancer); 1869 | var deepStructDecorator = createDecoratorForEnhancer(deepStructEnhancer); 1870 | var refStructDecorator = createDecoratorForEnhancer(refStructEnhancer); 1871 | /** 1872 | * Turns an object, array or function into a reactive structure. 1873 | * @param v the value which should become observable. 1874 | */ 1875 | function createObservable(v) { 1876 | if (v === void 0) { 1877 | v = undefined; 1878 | } 1879 | // @observable someProp; 1880 | if (typeof arguments[1] === "string") return deepDecorator.apply(null, arguments); 1881 | invariant(arguments.length <= 1, getMessage("m021")); 1882 | invariant(!isModifierDescriptor(v), getMessage("m020")); 1883 | // it is an observable already, done 1884 | if (isObservable(v)) return v; 1885 | // something that can be converted and mutated? 1886 | var res = deepEnhancer(v, undefined, undefined); 1887 | // this value could be converted to a new observable data structure, return it 1888 | if (res !== v) return res; 1889 | // otherwise, just box it 1890 | return observable.box(v); 1891 | } 1892 | var IObservableFactories = function() { 1893 | function IObservableFactories() {} 1894 | IObservableFactories.prototype.box = function(value, name) { 1895 | if (arguments.length > 2) incorrectlyUsedAsDecorator("box"); 1896 | return new ObservableValue(value, deepEnhancer, name); 1897 | }; 1898 | IObservableFactories.prototype.shallowBox = function(value, name) { 1899 | if (arguments.length > 2) incorrectlyUsedAsDecorator("shallowBox"); 1900 | return new ObservableValue(value, referenceEnhancer, name); 1901 | }; 1902 | IObservableFactories.prototype.array = function(initialValues, name) { 1903 | if (arguments.length > 2) incorrectlyUsedAsDecorator("array"); 1904 | return new ObservableArray(initialValues, deepEnhancer, name); 1905 | }; 1906 | IObservableFactories.prototype.shallowArray = function(initialValues, name) { 1907 | if (arguments.length > 2) incorrectlyUsedAsDecorator("shallowArray"); 1908 | return new ObservableArray(initialValues, referenceEnhancer, name); 1909 | }; 1910 | IObservableFactories.prototype.map = function(initialValues, name) { 1911 | if (arguments.length > 2) incorrectlyUsedAsDecorator("map"); 1912 | return new ObservableMap(initialValues, deepEnhancer, name); 1913 | }; 1914 | IObservableFactories.prototype.shallowMap = function(initialValues, name) { 1915 | if (arguments.length > 2) incorrectlyUsedAsDecorator("shallowMap"); 1916 | return new ObservableMap(initialValues, referenceEnhancer, name); 1917 | }; 1918 | IObservableFactories.prototype.object = function(props, name) { 1919 | if (arguments.length > 2) incorrectlyUsedAsDecorator("object"); 1920 | var res = {}; 1921 | // convert to observable object 1922 | asObservableObject(res, name); 1923 | // add properties 1924 | extendObservable(res, props); 1925 | return res; 1926 | }; 1927 | IObservableFactories.prototype.shallowObject = function(props, name) { 1928 | if (arguments.length > 2) incorrectlyUsedAsDecorator("shallowObject"); 1929 | var res = {}; 1930 | asObservableObject(res, name); 1931 | extendShallowObservable(res, props); 1932 | return res; 1933 | }; 1934 | IObservableFactories.prototype.ref = function() { 1935 | if (arguments.length < 2) { 1936 | // although ref creates actually a modifier descriptor, the type of the resultig properties 1937 | // of the object is `T` in the end, when the descriptors are interpreted 1938 | return createModifierDescriptor(referenceEnhancer, arguments[0]); 1939 | } else { 1940 | return refDecorator.apply(null, arguments); 1941 | } 1942 | }; 1943 | IObservableFactories.prototype.shallow = function() { 1944 | if (arguments.length < 2) { 1945 | // although ref creates actually a modifier descriptor, the type of the resultig properties 1946 | // of the object is `T` in the end, when the descriptors are interpreted 1947 | return createModifierDescriptor(shallowEnhancer, arguments[0]); 1948 | } else { 1949 | return shallowDecorator.apply(null, arguments); 1950 | } 1951 | }; 1952 | IObservableFactories.prototype.deep = function() { 1953 | if (arguments.length < 2) { 1954 | // although ref creates actually a modifier descriptor, the type of the resultig properties 1955 | // of the object is `T` in the end, when the descriptors are interpreted 1956 | return createModifierDescriptor(deepEnhancer, arguments[0]); 1957 | } else { 1958 | return deepDecorator.apply(null, arguments); 1959 | } 1960 | }; 1961 | IObservableFactories.prototype.struct = function() { 1962 | if (arguments.length < 2) { 1963 | // although ref creates actually a modifier descriptor, the type of the resultig properties 1964 | // of the object is `T` in the end, when the descriptors are interpreted 1965 | return createModifierDescriptor(deepStructEnhancer, arguments[0]); 1966 | } else { 1967 | return deepStructDecorator.apply(null, arguments); 1968 | } 1969 | }; 1970 | return IObservableFactories; 1971 | }(); 1972 | var observable = createObservable; 1973 | // weird trick to keep our typings nicely with our funcs, and still extend the observable function 1974 | // ES6 class methods aren't enumerable, can't use Object.keys 1975 | Object.getOwnPropertyNames(IObservableFactories.prototype).filter(function(name) { 1976 | return name !== "constructor"; 1977 | }).forEach(function(name) { 1978 | return observable[name] = IObservableFactories.prototype[name]; 1979 | }); 1980 | observable.deep.struct = observable.struct; 1981 | observable.ref.struct = function() { 1982 | if (arguments.length < 2) { 1983 | return createModifierDescriptor(refStructEnhancer, arguments[0]); 1984 | } else { 1985 | return refStructDecorator.apply(null, arguments); 1986 | } 1987 | }; 1988 | 1989 | function incorrectlyUsedAsDecorator(methodName) { 1990 | fail("Expected one or two arguments to observable." + methodName + ". Did you accidentally try to use observable." + methodName + " as decorator?"); 1991 | } 1992 | 1993 | function isModifierDescriptor(thing) { 1994 | return (typeof thing === "undefined" ? "undefined" : _typeof(thing)) === "object" && thing !== null && thing.isMobxModifierDescriptor === true; 1995 | } 1996 | 1997 | function createModifierDescriptor(enhancer, initialValue) { 1998 | invariant(!isModifierDescriptor(initialValue), "Modifiers cannot be nested"); 1999 | return { 2000 | isMobxModifierDescriptor: true, 2001 | initialValue: initialValue, 2002 | enhancer: enhancer 2003 | }; 2004 | } 2005 | 2006 | function deepEnhancer(v, _, name) { 2007 | if (isModifierDescriptor(v)) fail("You tried to assign a modifier wrapped value to a collection, please define modifiers when creating the collection, not when modifying it"); 2008 | // it is an observable already, done 2009 | if (isObservable(v)) return v; 2010 | // something that can be converted and mutated? 2011 | if (Array.isArray(v)) return observable.array(v, name); 2012 | if (isPlainObject(v)) return observable.object(v, name); 2013 | if (isES6Map(v)) return observable.map(v, name); 2014 | return v; 2015 | } 2016 | 2017 | function shallowEnhancer(v, _, name) { 2018 | if (isModifierDescriptor(v)) fail("You tried to assign a modifier wrapped value to a collection, please define modifiers when creating the collection, not when modifying it"); 2019 | if (v === undefined || v === null) return v; 2020 | if (isObservableObject(v) || isObservableArray(v) || isObservableMap(v)) return v; 2021 | if (Array.isArray(v)) return observable.shallowArray(v, name); 2022 | if (isPlainObject(v)) return observable.shallowObject(v, name); 2023 | if (isES6Map(v)) return observable.shallowMap(v, name); 2024 | return fail("The shallow modifier / decorator can only used in combination with arrays, objects and maps"); 2025 | } 2026 | 2027 | function referenceEnhancer(newValue) { 2028 | // never turn into an observable 2029 | return newValue; 2030 | } 2031 | 2032 | function deepStructEnhancer(v, oldValue, name) { 2033 | // don't confuse structurally compare enhancer with ref enhancer! The latter is probably 2034 | // more suited for immutable objects 2035 | if (deepEqual(v, oldValue)) return oldValue; 2036 | // it is an observable already, done 2037 | if (isObservable(v)) return v; 2038 | // something that can be converted and mutated? 2039 | if (Array.isArray(v)) return new ObservableArray(v, deepStructEnhancer, name); 2040 | if (isES6Map(v)) return new ObservableMap(v, deepStructEnhancer, name); 2041 | if (isPlainObject(v)) { 2042 | var res = {}; 2043 | asObservableObject(res, name); 2044 | extendObservableHelper(res, deepStructEnhancer, [v]); 2045 | return res; 2046 | } 2047 | return v; 2048 | } 2049 | 2050 | function refStructEnhancer(v, oldValue, name) { 2051 | if (deepEqual(v, oldValue)) return oldValue; 2052 | return v; 2053 | } 2054 | 2055 | /** 2056 | * @deprecated 2057 | * During a transaction no views are updated until the end of the transaction. 2058 | * The transaction will be run synchronously nonetheless. 2059 | * 2060 | * Deprecated to simplify api; transactions offer no real benefit above actions. 2061 | * 2062 | * @param action a function that updates some reactive state 2063 | * @returns any value that was returned by the 'action' parameter. 2064 | */ 2065 | function transaction(action, thisArg) { 2066 | if (thisArg === void 0) { 2067 | thisArg = undefined; 2068 | } 2069 | deprecated(getMessage("m023")); 2070 | return runInTransaction.apply(undefined, arguments); 2071 | } 2072 | 2073 | function runInTransaction(action, thisArg) { 2074 | if (thisArg === void 0) { 2075 | thisArg = undefined; 2076 | } 2077 | return executeAction("", action); 2078 | } 2079 | 2080 | var ObservableMapMarker = {}; 2081 | var ObservableMap = function() { 2082 | function ObservableMap(initialData, enhancer, name) { 2083 | if (enhancer === void 0) { 2084 | enhancer = deepEnhancer; 2085 | } 2086 | if (name === void 0) { 2087 | name = "ObservableMap@" + getNextId(); 2088 | } 2089 | this.enhancer = enhancer; 2090 | this.name = name; 2091 | this.$mobx = ObservableMapMarker; 2092 | this._data = Object.create(null); 2093 | this._hasMap = Object.create(null); // hasMap, not hashMap >-). 2094 | this._keys = new ObservableArray(undefined, referenceEnhancer, this.name + ".keys()", true); 2095 | this.interceptors = null; 2096 | this.changeListeners = null; 2097 | this.dehancer = undefined; 2098 | this.merge(initialData); 2099 | } 2100 | ObservableMap.prototype._has = function(key) { 2101 | return typeof this._data[key] !== "undefined"; 2102 | }; 2103 | ObservableMap.prototype.has = function(key) { 2104 | if (!this.isValidKey(key)) return false; 2105 | key = "" + key; 2106 | if (this._hasMap[key]) return this._hasMap[key].get(); 2107 | return this._updateHasMapEntry(key, false).get(); 2108 | }; 2109 | ObservableMap.prototype.set = function(key, value) { 2110 | this.assertValidKey(key); 2111 | key = "" + key; 2112 | var hasKey = this._has(key); 2113 | if (hasInterceptors(this)) { 2114 | var change = interceptChange(this, { 2115 | type: hasKey ? "update" : "add", 2116 | object: this, 2117 | newValue: value, 2118 | name: key 2119 | }); 2120 | if (!change) return this; 2121 | value = change.newValue; 2122 | } 2123 | if (hasKey) { 2124 | this._updateValue(key, value); 2125 | } else { 2126 | this._addValue(key, value); 2127 | } 2128 | return this; 2129 | }; 2130 | ObservableMap.prototype.delete = function(key) { 2131 | var _this = this; 2132 | this.assertValidKey(key); 2133 | key = "" + key; 2134 | if (hasInterceptors(this)) { 2135 | var change = interceptChange(this, { 2136 | type: "delete", 2137 | object: this, 2138 | name: key 2139 | }); 2140 | if (!change) return false; 2141 | } 2142 | if (this._has(key)) { 2143 | var notifySpy = isSpyEnabled(); 2144 | var notify = hasListeners(this); 2145 | var change = notify || notifySpy ? { 2146 | type: "delete", 2147 | object: this, 2148 | oldValue: this._data[key].value, 2149 | name: key 2150 | } : null; 2151 | if (notifySpy) spyReportStart(change); 2152 | runInTransaction(function() { 2153 | _this._keys.remove(key); 2154 | _this._updateHasMapEntry(key, false); 2155 | var observable$$1 = _this._data[key]; 2156 | observable$$1.setNewValue(undefined); 2157 | _this._data[key] = undefined; 2158 | }); 2159 | if (notify) notifyListeners(this, change); 2160 | if (notifySpy) spyReportEnd(); 2161 | return true; 2162 | } 2163 | return false; 2164 | }; 2165 | ObservableMap.prototype._updateHasMapEntry = function(key, value) { 2166 | // optimization; don't fill the hasMap if we are not observing, or remove entry if there are no observers anymore 2167 | var entry = this._hasMap[key]; 2168 | if (entry) { 2169 | entry.setNewValue(value); 2170 | } else { 2171 | entry = this._hasMap[key] = new ObservableValue(value, referenceEnhancer, this.name + "." + key + "?", false); 2172 | } 2173 | return entry; 2174 | }; 2175 | ObservableMap.prototype._updateValue = function(name, newValue) { 2176 | var observable$$1 = this._data[name]; 2177 | newValue = observable$$1.prepareNewValue(newValue); 2178 | if (newValue !== UNCHANGED) { 2179 | var notifySpy = isSpyEnabled(); 2180 | var notify = hasListeners(this); 2181 | var change = notify || notifySpy ? { 2182 | type: "update", 2183 | object: this, 2184 | oldValue: observable$$1.value, 2185 | name: name, 2186 | newValue: newValue 2187 | } : null; 2188 | if (notifySpy) spyReportStart(change); 2189 | observable$$1.setNewValue(newValue); 2190 | if (notify) notifyListeners(this, change); 2191 | if (notifySpy) spyReportEnd(); 2192 | } 2193 | }; 2194 | ObservableMap.prototype._addValue = function(name, newValue) { 2195 | var _this = this; 2196 | runInTransaction(function() { 2197 | var observable$$1 = _this._data[name] = new ObservableValue(newValue, _this.enhancer, _this.name + "." + name, false); 2198 | newValue = observable$$1.value; // value might have been changed 2199 | _this._updateHasMapEntry(name, true); 2200 | _this._keys.push(name); 2201 | }); 2202 | var notifySpy = isSpyEnabled(); 2203 | var notify = hasListeners(this); 2204 | var change = notify || notifySpy ? { 2205 | type: "add", 2206 | object: this, 2207 | name: name, 2208 | newValue: newValue 2209 | } : null; 2210 | if (notifySpy) spyReportStart(change); 2211 | if (notify) notifyListeners(this, change); 2212 | if (notifySpy) spyReportEnd(); 2213 | }; 2214 | ObservableMap.prototype.get = function(key) { 2215 | key = "" + key; 2216 | if (this.has(key)) return this.dehanceValue(this._data[key].get()); 2217 | return this.dehanceValue(undefined); 2218 | }; 2219 | ObservableMap.prototype.dehanceValue = function(value) { 2220 | if (this.dehancer !== undefined) { 2221 | return this.dehancer(value); 2222 | } 2223 | return value; 2224 | }; 2225 | ObservableMap.prototype.keys = function() { 2226 | return arrayAsIterator(this._keys.slice()); 2227 | }; 2228 | ObservableMap.prototype.values = function() { 2229 | return arrayAsIterator(this._keys.map(this.get, this)); 2230 | }; 2231 | ObservableMap.prototype.entries = function() { 2232 | var _this = this; 2233 | return arrayAsIterator(this._keys.map(function(key) { 2234 | return [key, _this.get(key)]; 2235 | })); 2236 | }; 2237 | ObservableMap.prototype.forEach = function(callback, thisArg) { 2238 | var _this = this; 2239 | this.keys().forEach(function(key) { 2240 | return callback.call(thisArg, _this.get(key), key, _this); 2241 | }); 2242 | }; 2243 | /** Merge another object into this object, returns this. */ 2244 | ObservableMap.prototype.merge = function(other) { 2245 | var _this = this; 2246 | if (isObservableMap(other)) { 2247 | other = other.toJS(); 2248 | } 2249 | runInTransaction(function() { 2250 | if (isPlainObject(other)) Object.keys(other).forEach(function(key) { 2251 | return _this.set(key, other[key]); 2252 | }); 2253 | else if (Array.isArray(other)) other.forEach(function(_a) { 2254 | var key = _a[0], 2255 | value = _a[1]; 2256 | return _this.set(key, value); 2257 | }); 2258 | else if (isES6Map(other)) other.forEach(function(value, key) { 2259 | return _this.set(key, value); 2260 | }); 2261 | else if (other !== null && other !== undefined) fail("Cannot initialize map from " + other); 2262 | }); 2263 | return this; 2264 | }; 2265 | ObservableMap.prototype.clear = function() { 2266 | var _this = this; 2267 | runInTransaction(function() { 2268 | untracked(function() { 2269 | _this.keys().forEach(_this.delete, _this); 2270 | }); 2271 | }); 2272 | }; 2273 | ObservableMap.prototype.replace = function(values) { 2274 | var _this = this; 2275 | runInTransaction(function() { 2276 | _this.clear(); 2277 | _this.merge(values); 2278 | }); 2279 | return this; 2280 | }; 2281 | Object.defineProperty(ObservableMap.prototype, "size", { 2282 | get: function get() { 2283 | return this._keys.length; 2284 | }, 2285 | enumerable: true, 2286 | configurable: true 2287 | }); 2288 | /** 2289 | * Returns a shallow non observable object clone of this map. 2290 | * Note that the values migth still be observable. For a deep clone use mobx.toJS. 2291 | */ 2292 | ObservableMap.prototype.toJS = function() { 2293 | var _this = this; 2294 | var res = {}; 2295 | this.keys().forEach(function(key) { 2296 | return res[key] = _this.get(key); 2297 | }); 2298 | return res; 2299 | }; 2300 | ObservableMap.prototype.toJSON = function() { 2301 | // Used by JSON.stringify 2302 | return this.toJS(); 2303 | }; 2304 | ObservableMap.prototype.isValidKey = function(key) { 2305 | if (key === null || key === undefined) return false; 2306 | if (typeof key === "string" || typeof key === "number" || typeof key === "boolean") return true; 2307 | return false; 2308 | }; 2309 | ObservableMap.prototype.assertValidKey = function(key) { 2310 | if (!this.isValidKey(key)) throw new Error("[mobx.map] Invalid key: '" + key + "', only strings, numbers and booleans are accepted as key in observable maps."); 2311 | }; 2312 | ObservableMap.prototype.toString = function() { 2313 | var _this = this; 2314 | return this.name + "[{ " + this.keys().map(function(key) { 2315 | return key + ": " + ("" + _this.get(key)); 2316 | }).join(", ") + " }]"; 2317 | }; 2318 | /** 2319 | * Observes this object. Triggers for the events 'add', 'update' and 'delete'. 2320 | * See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/observe 2321 | * for callback details 2322 | */ 2323 | ObservableMap.prototype.observe = function(listener, fireImmediately) { 2324 | invariant(fireImmediately !== true, getMessage("m033")); 2325 | return registerListener(this, listener); 2326 | }; 2327 | ObservableMap.prototype.intercept = function(handler) { 2328 | return registerInterceptor(this, handler); 2329 | }; 2330 | return ObservableMap; 2331 | }(); 2332 | declareIterator(ObservableMap.prototype, function() { 2333 | return this.entries(); 2334 | }); 2335 | 2336 | function map(initialValues) { 2337 | deprecated("`mobx.map` is deprecated, use `new ObservableMap` or `mobx.observable.map` instead"); 2338 | return observable.map(initialValues); 2339 | } 2340 | /* 'var' fixes small-build issue */ 2341 | var isObservableMap = createInstanceofPredicate("ObservableMap", ObservableMap); 2342 | 2343 | var EMPTY_ARRAY = []; 2344 | Object.freeze(EMPTY_ARRAY); 2345 | 2346 | function getGlobal() { 2347 | return global; 2348 | } 2349 | 2350 | function getNextId() { 2351 | return ++globalState.mobxGuid; 2352 | } 2353 | 2354 | function fail(message, thing) { 2355 | invariant(false, message, thing); 2356 | throw "X"; // unreachable 2357 | } 2358 | 2359 | function invariant(check, message, thing) { 2360 | if (!check) throw new Error("[mobx] Invariant failed: " + message + (thing ? " in '" + thing + "'" : "")); 2361 | } 2362 | /** 2363 | * Prints a deprecation message, but only one time. 2364 | * Returns false if the deprecated message was already printed before 2365 | */ 2366 | var deprecatedMessages = []; 2367 | 2368 | function deprecated(msg) { 2369 | if (deprecatedMessages.indexOf(msg) !== -1) return false; 2370 | deprecatedMessages.push(msg); 2371 | console.error("[mobx] Deprecated: " + msg); 2372 | return true; 2373 | } 2374 | /** 2375 | * Makes sure that the provided function is invoked at most once. 2376 | */ 2377 | function once(func) { 2378 | var invoked = false; 2379 | return function() { 2380 | if (invoked) return; 2381 | invoked = true; 2382 | return func.apply(this, arguments); 2383 | }; 2384 | } 2385 | var noop = function noop() {}; 2386 | 2387 | function unique(list) { 2388 | var res = []; 2389 | list.forEach(function(item) { 2390 | if (res.indexOf(item) === -1) res.push(item); 2391 | }); 2392 | return res; 2393 | } 2394 | 2395 | function joinStrings(things, limit, separator) { 2396 | if (limit === void 0) { 2397 | limit = 100; 2398 | } 2399 | if (separator === void 0) { 2400 | separator = " - "; 2401 | } 2402 | if (!things) return ""; 2403 | var sliced = things.slice(0, limit); 2404 | return "" + sliced.join(separator) + (things.length > limit ? " (... and " + (things.length - limit) + "more)" : ""); 2405 | } 2406 | 2407 | function isObject(value) { 2408 | return value !== null && (typeof value === "undefined" ? "undefined" : _typeof(value)) === "object"; 2409 | } 2410 | 2411 | function isPlainObject(value) { 2412 | if (value === null || (typeof value === "undefined" ? "undefined" : _typeof(value)) !== "object") return false; 2413 | var proto = Object.getPrototypeOf(value); 2414 | return proto === Object.prototype || proto === null; 2415 | } 2416 | 2417 | function objectAssign() { 2418 | var res = arguments[0]; 2419 | for (var i = 1, l = arguments.length; i < l; i++) { 2420 | var source = arguments[i]; 2421 | for (var key in source) { 2422 | if (hasOwnProperty(source, key)) { 2423 | res[key] = source[key]; 2424 | } 2425 | } 2426 | } 2427 | return res; 2428 | } 2429 | var prototypeHasOwnProperty = Object.prototype.hasOwnProperty; 2430 | 2431 | function hasOwnProperty(object, propName) { 2432 | return prototypeHasOwnProperty.call(object, propName); 2433 | } 2434 | 2435 | function makeNonEnumerable(object, propNames) { 2436 | for (var i = 0; i < propNames.length; i++) { 2437 | addHiddenProp(object, propNames[i], object[propNames[i]]); 2438 | } 2439 | } 2440 | 2441 | function addHiddenProp(object, propName, value) { 2442 | Object.defineProperty(object, propName, { 2443 | enumerable: false, 2444 | writable: true, 2445 | configurable: true, 2446 | value: value 2447 | }); 2448 | } 2449 | 2450 | function addHiddenFinalProp(object, propName, value) { 2451 | Object.defineProperty(object, propName, { 2452 | enumerable: false, 2453 | writable: false, 2454 | configurable: true, 2455 | value: value 2456 | }); 2457 | } 2458 | 2459 | function isPropertyConfigurable(object, prop) { 2460 | var descriptor = Object.getOwnPropertyDescriptor(object, prop); 2461 | return !descriptor || descriptor.configurable !== false && descriptor.writable !== false; 2462 | } 2463 | 2464 | function assertPropertyConfigurable(object, prop) { 2465 | invariant(isPropertyConfigurable(object, prop), "Cannot make property '" + prop + "' observable, it is not configurable and writable in the target object"); 2466 | } 2467 | 2468 | function getEnumerableKeys(obj) { 2469 | var res = []; 2470 | for (var key in obj) { 2471 | res.push(key); 2472 | } 2473 | return res; 2474 | } 2475 | /** 2476 | * Naive deepEqual. Doesn't check for prototype, non-enumerable or out-of-range properties on arrays. 2477 | * If you have such a case, you probably should use this function but something fancier :). 2478 | */ 2479 | function deepEqual(a, b) { 2480 | if (a === null && b === null) return true; 2481 | if (a === undefined && b === undefined) return true; 2482 | if ((typeof a === "undefined" ? "undefined" : _typeof(a)) !== "object") return a === b; 2483 | var aIsArray = isArrayLike(a); 2484 | var aIsMap = isMapLike(a); 2485 | if (aIsArray !== isArrayLike(b)) { 2486 | return false; 2487 | } else if (aIsMap !== isMapLike(b)) { 2488 | return false; 2489 | } else if (aIsArray) { 2490 | if (a.length !== b.length) return false; 2491 | for (var i = a.length - 1; i >= 0; i--) { 2492 | if (!deepEqual(a[i], b[i])) return false; 2493 | } 2494 | return true; 2495 | } else if (aIsMap) { 2496 | if (a.size !== b.size) return false; 2497 | var equals_1 = true; 2498 | a.forEach(function(value, key) { 2499 | equals_1 = equals_1 && deepEqual(b.get(key), value); 2500 | }); 2501 | return equals_1; 2502 | } else if ((typeof a === "undefined" ? "undefined" : _typeof(a)) === "object" && (typeof b === "undefined" ? "undefined" : _typeof(b)) === "object") { 2503 | if (a === null || b === null) return false; 2504 | if (isMapLike(a) && isMapLike(b)) { 2505 | if (a.size !== b.size) return false; 2506 | // Freaking inefficient.... Create PR if you run into this :) Much appreciated! 2507 | return deepEqual(observable.shallowMap(a).entries(), observable.shallowMap(b).entries()); 2508 | } 2509 | if (getEnumerableKeys(a).length !== getEnumerableKeys(b).length) return false; 2510 | for (var prop in a) { 2511 | if (!(prop in b)) return false; 2512 | if (!deepEqual(a[prop], b[prop])) return false; 2513 | } 2514 | return true; 2515 | } 2516 | return false; 2517 | } 2518 | 2519 | function createInstanceofPredicate(name, clazz) { 2520 | var propName = "isMobX" + name; 2521 | clazz.prototype[propName] = true; 2522 | return function(x) { 2523 | return isObject(x) && x[propName] === true; 2524 | }; 2525 | } 2526 | /** 2527 | * Returns whether the argument is an array, disregarding observability. 2528 | */ 2529 | function isArrayLike(x) { 2530 | return Array.isArray(x) || isObservableArray(x); 2531 | } 2532 | 2533 | function isMapLike(x) { 2534 | return isES6Map(x) || isObservableMap(x); 2535 | } 2536 | 2537 | function isES6Map(thing) { 2538 | if (getGlobal().Map !== undefined && thing instanceof getGlobal().Map) return true; 2539 | return false; 2540 | } 2541 | 2542 | function primitiveSymbol() { 2543 | return typeof Symbol === "function" && Symbol.toPrimitive || "@@toPrimitive"; 2544 | } 2545 | 2546 | function toPrimitive(value) { 2547 | return value === null ? null : (typeof value === "undefined" ? "undefined" : _typeof(value)) === "object" ? "" + value : value; 2548 | } 2549 | 2550 | /** 2551 | * These values will persist if global state is reset 2552 | */ 2553 | var persistentKeys = ["mobxGuid", "resetId", "spyListeners", "strictMode", "runId"]; 2554 | var MobXGlobals = function() { 2555 | function MobXGlobals() { 2556 | /** 2557 | * MobXGlobals version. 2558 | * MobX compatiblity with other versions loaded in memory as long as this version matches. 2559 | * It indicates that the global state still stores similar information 2560 | */ 2561 | this.version = 5; 2562 | /** 2563 | * Currently running derivation 2564 | */ 2565 | this.trackingDerivation = null; 2566 | /** 2567 | * Are we running a computation currently? (not a reaction) 2568 | */ 2569 | this.computationDepth = 0; 2570 | /** 2571 | * Each time a derivation is tracked, it is assigned a unique run-id 2572 | */ 2573 | this.runId = 0; 2574 | /** 2575 | * 'guid' for general purpose. Will be persisted amongst resets. 2576 | */ 2577 | this.mobxGuid = 0; 2578 | /** 2579 | * Are we in a batch block? (and how many of them) 2580 | */ 2581 | this.inBatch = 0; 2582 | /** 2583 | * Observables that don't have observers anymore, and are about to be 2584 | * suspended, unless somebody else accesses it in the same batch 2585 | * 2586 | * @type {IObservable[]} 2587 | */ 2588 | this.pendingUnobservations = []; 2589 | /** 2590 | * List of scheduled, not yet executed, reactions. 2591 | */ 2592 | this.pendingReactions = []; 2593 | /** 2594 | * Are we currently processing reactions? 2595 | */ 2596 | this.isRunningReactions = false; 2597 | /** 2598 | * Is it allowed to change observables at this point? 2599 | * In general, MobX doesn't allow that when running computations and React.render. 2600 | * To ensure that those functions stay pure. 2601 | */ 2602 | this.allowStateChanges = true; 2603 | /** 2604 | * If strict mode is enabled, state changes are by default not allowed 2605 | */ 2606 | this.strictMode = false; 2607 | /** 2608 | * Used by createTransformer to detect that the global state has been reset. 2609 | */ 2610 | this.resetId = 0; 2611 | /** 2612 | * Spy callbacks 2613 | */ 2614 | this.spyListeners = []; 2615 | /** 2616 | * Globally attached error handlers that react specifically to errors in reactions 2617 | */ 2618 | this.globalReactionErrorHandlers = []; 2619 | } 2620 | return MobXGlobals; 2621 | }(); 2622 | var globalState = new MobXGlobals(); 2623 | var shareGlobalStateCalled = false; 2624 | var runInIsolationCalled = false; 2625 | var warnedAboutMultipleInstances = false; { 2626 | var global_1 = getGlobal(); 2627 | if (!global_1.__mobxInstanceCount) { 2628 | global_1.__mobxInstanceCount = 1; 2629 | } else { 2630 | global_1.__mobxInstanceCount++; 2631 | setTimeout(function() { 2632 | if (!shareGlobalStateCalled && !runInIsolationCalled && !warnedAboutMultipleInstances) { 2633 | warnedAboutMultipleInstances = true; 2634 | console.warn("[mobx] Warning: there are multiple mobx instances active. This might lead to unexpected results. See https://github.com/mobxjs/mobx/issues/1082 for details."); 2635 | } 2636 | }); 2637 | } 2638 | } 2639 | 2640 | function isolateGlobalState() { 2641 | runInIsolationCalled = true; 2642 | getGlobal().__mobxInstanceCount--; 2643 | } 2644 | 2645 | function shareGlobalState() { 2646 | // TODO: remove in 4.0; just use peer dependencies instead. 2647 | deprecated("Using `shareGlobalState` is not recommended, use peer dependencies instead. See https://github.com/mobxjs/mobx/issues/1082 for details."); 2648 | shareGlobalStateCalled = true; 2649 | var global = getGlobal(); 2650 | var ownState = globalState; 2651 | /** 2652 | * Backward compatibility check 2653 | */ 2654 | if (global.__mobservableTrackingStack || global.__mobservableViewStack) throw new Error("[mobx] An incompatible version of mobservable is already loaded."); 2655 | if (global.__mobxGlobal && global.__mobxGlobal.version !== ownState.version) throw new Error("[mobx] An incompatible version of mobx is already loaded."); 2656 | if (global.__mobxGlobal) globalState = global.__mobxGlobal; 2657 | else global.__mobxGlobal = ownState; 2658 | } 2659 | 2660 | function getGlobalState() { 2661 | return globalState; 2662 | } 2663 | 2664 | /** 2665 | * For testing purposes only; this will break the internal state of existing observables, 2666 | * but can be used to get back at a stable state after throwing errors 2667 | */ 2668 | function resetGlobalState() { 2669 | globalState.resetId++; 2670 | var defaultGlobals = new MobXGlobals(); 2671 | for (var key in defaultGlobals) { 2672 | if (persistentKeys.indexOf(key) === -1) globalState[key] = defaultGlobals[key]; 2673 | } 2674 | globalState.allowStateChanges = !globalState.strictMode; 2675 | } 2676 | 2677 | function hasObservers(observable) { 2678 | return observable.observers && observable.observers.length > 0; 2679 | } 2680 | 2681 | function getObservers(observable) { 2682 | return observable.observers; 2683 | } 2684 | 2685 | function addObserver(observable, node) { 2686 | // invariant(node.dependenciesState !== -1, "INTERNAL ERROR, can add only dependenciesState !== -1"); 2687 | // invariant(observable._observers.indexOf(node) === -1, "INTERNAL ERROR add already added node"); 2688 | // invariantObservers(observable); 2689 | var l = observable.observers.length; 2690 | if (l) { 2691 | observable.observersIndexes[node.__mapid] = l; 2692 | } 2693 | observable.observers[l] = node; 2694 | if (observable.lowestObserverState > node.dependenciesState) observable.lowestObserverState = node.dependenciesState; 2695 | // invariantObservers(observable); 2696 | // invariant(observable._observers.indexOf(node) !== -1, "INTERNAL ERROR didnt add node"); 2697 | } 2698 | 2699 | function removeObserver(observable, node) { 2700 | // invariant(globalState.inBatch > 0, "INTERNAL ERROR, remove should be called only inside batch"); 2701 | // invariant(observable._observers.indexOf(node) !== -1, "INTERNAL ERROR remove already removed node"); 2702 | // invariantObservers(observable); 2703 | if (observable.observers.length === 1) { 2704 | // deleting last observer 2705 | observable.observers.length = 0; 2706 | queueForUnobservation(observable); 2707 | } else { 2708 | // deleting from _observersIndexes is straight forward, to delete from _observers, let's swap `node` with last element 2709 | var list = observable.observers; 2710 | var map = observable.observersIndexes; 2711 | var filler = list.pop(); // get last element, which should fill the place of `node`, so the array doesnt have holes 2712 | if (filler !== node) { 2713 | var index = map[node.__mapid] || 0; // getting index of `node`. this is the only place we actually use map. 2714 | if (index) { 2715 | map[filler.__mapid] = index; 2716 | } else { 2717 | delete map[filler.__mapid]; 2718 | } 2719 | list[index] = filler; 2720 | } 2721 | delete map[node.__mapid]; 2722 | } 2723 | // invariantObservers(observable); 2724 | // invariant(observable._observers.indexOf(node) === -1, "INTERNAL ERROR remove already removed node2"); 2725 | } 2726 | 2727 | function queueForUnobservation(observable) { 2728 | if (!observable.isPendingUnobservation) { 2729 | // invariant(globalState.inBatch > 0, "INTERNAL ERROR, remove should be called only inside batch"); 2730 | // invariant(observable._observers.length === 0, "INTERNAL ERROR, shuold only queue for unobservation unobserved observables"); 2731 | observable.isPendingUnobservation = true; 2732 | globalState.pendingUnobservations.push(observable); 2733 | } 2734 | } 2735 | /** 2736 | * Batch starts a transaction, at least for purposes of memoizing ComputedValues when nothing else does. 2737 | * During a batch `onBecomeUnobserved` will be called at most once per observable. 2738 | * Avoids unnecessary recalculations. 2739 | */ 2740 | function startBatch() { 2741 | globalState.inBatch++; 2742 | } 2743 | 2744 | function endBatch() { 2745 | if (--globalState.inBatch === 0) { 2746 | runReactions(); 2747 | // the batch is actually about to finish, all unobserving should happen here. 2748 | var list = globalState.pendingUnobservations; 2749 | for (var i = 0; i < list.length; i++) { 2750 | var observable = list[i]; 2751 | observable.isPendingUnobservation = false; 2752 | if (observable.observers.length === 0) { 2753 | observable.onBecomeUnobserved(); 2754 | // NOTE: onBecomeUnobserved might push to `pendingUnobservations` 2755 | } 2756 | } 2757 | globalState.pendingUnobservations = []; 2758 | } 2759 | } 2760 | 2761 | function reportObserved(observable) { 2762 | var derivation = globalState.trackingDerivation; 2763 | if (derivation !== null) { 2764 | /** 2765 | * Simple optimization, give each derivation run an unique id (runId) 2766 | * Check if last time this observable was accessed the same runId is used 2767 | * if this is the case, the relation is already known 2768 | */ 2769 | if (derivation.runId !== observable.lastAccessedBy) { 2770 | observable.lastAccessedBy = derivation.runId; 2771 | derivation.newObserving[derivation.unboundDepsCount++] = observable; 2772 | } 2773 | } else if (observable.observers.length === 0) { 2774 | queueForUnobservation(observable); 2775 | } 2776 | } 2777 | /** 2778 | * NOTE: current propagation mechanism will in case of self reruning autoruns behave unexpectedly 2779 | * It will propagate changes to observers from previous run 2780 | * It's hard or maybe impossible (with reasonable perf) to get it right with current approach 2781 | * Hopefully self reruning autoruns aren't a feature people should depend on 2782 | * Also most basic use cases should be ok 2783 | */ 2784 | // Called by Atom when its value changes 2785 | function propagateChanged(observable) { 2786 | // invariantLOS(observable, "changed start"); 2787 | if (observable.lowestObserverState === IDerivationState.STALE) return; 2788 | observable.lowestObserverState = IDerivationState.STALE; 2789 | var observers = observable.observers; 2790 | var i = observers.length; 2791 | while (i--) { 2792 | var d = observers[i]; 2793 | if (d.dependenciesState === IDerivationState.UP_TO_DATE) d.onBecomeStale(); 2794 | d.dependenciesState = IDerivationState.STALE; 2795 | } 2796 | // invariantLOS(observable, "changed end"); 2797 | } 2798 | // Called by ComputedValue when it recalculate and its value changed 2799 | function propagateChangeConfirmed(observable) { 2800 | // invariantLOS(observable, "confirmed start"); 2801 | if (observable.lowestObserverState === IDerivationState.STALE) return; 2802 | observable.lowestObserverState = IDerivationState.STALE; 2803 | var observers = observable.observers; 2804 | var i = observers.length; 2805 | while (i--) { 2806 | var d = observers[i]; 2807 | if (d.dependenciesState === IDerivationState.POSSIBLY_STALE) d.dependenciesState = IDerivationState.STALE; 2808 | else if (d.dependenciesState === IDerivationState.UP_TO_DATE) observable.lowestObserverState = IDerivationState.UP_TO_DATE; 2809 | } 2810 | // invariantLOS(observable, "confirmed end"); 2811 | } 2812 | // Used by computed when its dependency changed, but we don't wan't to immediately recompute. 2813 | function propagateMaybeChanged(observable) { 2814 | // invariantLOS(observable, "maybe start"); 2815 | if (observable.lowestObserverState !== IDerivationState.UP_TO_DATE) return; 2816 | observable.lowestObserverState = IDerivationState.POSSIBLY_STALE; 2817 | var observers = observable.observers; 2818 | var i = observers.length; 2819 | while (i--) { 2820 | var d = observers[i]; 2821 | if (d.dependenciesState === IDerivationState.UP_TO_DATE) { 2822 | d.dependenciesState = IDerivationState.POSSIBLY_STALE; 2823 | d.onBecomeStale(); 2824 | } 2825 | } 2826 | // invariantLOS(observable, "maybe end"); 2827 | } 2828 | 2829 | var IDerivationState; 2830 | (function(IDerivationState) { 2831 | // before being run or (outside batch and not being observed) 2832 | // at this point derivation is not holding any data about dependency tree 2833 | IDerivationState[IDerivationState["NOT_TRACKING"] = -1] = "NOT_TRACKING"; 2834 | // no shallow dependency changed since last computation 2835 | // won't recalculate derivation 2836 | // this is what makes mobx fast 2837 | IDerivationState[IDerivationState["UP_TO_DATE"] = 0] = "UP_TO_DATE"; 2838 | // some deep dependency changed, but don't know if shallow dependency changed 2839 | // will require to check first if UP_TO_DATE or POSSIBLY_STALE 2840 | // currently only ComputedValue will propagate POSSIBLY_STALE 2841 | // 2842 | // having this state is second big optimization: 2843 | // don't have to recompute on every dependency change, but only when it's needed 2844 | IDerivationState[IDerivationState["POSSIBLY_STALE"] = 1] = "POSSIBLY_STALE"; 2845 | // shallow dependency changed 2846 | // will need to recompute when it's needed 2847 | IDerivationState[IDerivationState["STALE"] = 2] = "STALE"; 2848 | })(IDerivationState || (exports.IDerivationState = IDerivationState = {})); 2849 | var CaughtException = function() { 2850 | function CaughtException(cause) { 2851 | this.cause = cause; 2852 | // Empty 2853 | } 2854 | return CaughtException; 2855 | }(); 2856 | 2857 | function isCaughtException(e) { 2858 | return e instanceof CaughtException; 2859 | } 2860 | /** 2861 | * Finds out whether any dependency of the derivation has actually changed. 2862 | * If dependenciesState is 1 then it will recalculate dependencies, 2863 | * if any dependency changed it will propagate it by changing dependenciesState to 2. 2864 | * 2865 | * By iterating over the dependencies in the same order that they were reported and 2866 | * stopping on the first change, all the recalculations are only called for ComputedValues 2867 | * that will be tracked by derivation. That is because we assume that if the first x 2868 | * dependencies of the derivation doesn't change then the derivation should run the same way 2869 | * up until accessing x-th dependency. 2870 | */ 2871 | function shouldCompute(derivation) { 2872 | switch (derivation.dependenciesState) { 2873 | case IDerivationState.UP_TO_DATE: 2874 | return false; 2875 | case IDerivationState.NOT_TRACKING: 2876 | case IDerivationState.STALE: 2877 | return true; 2878 | case IDerivationState.POSSIBLY_STALE: { 2879 | var prevUntracked = untrackedStart(); // no need for those computeds to be reported, they will be picked up in trackDerivedFunction. 2880 | var obs = derivation.observing, 2881 | l = obs.length; 2882 | for (var i = 0; i < l; i++) { 2883 | var obj = obs[i]; 2884 | if (isComputedValue(obj)) { 2885 | try { 2886 | obj.get(); 2887 | } catch (e) { 2888 | // we are not interested in the value *or* exception at this moment, but if there is one, notify all 2889 | untrackedEnd(prevUntracked); 2890 | return true; 2891 | } 2892 | // if ComputedValue `obj` actually changed it will be computed and propagated to its observers. 2893 | // and `derivation` is an observer of `obj` 2894 | if (derivation.dependenciesState === IDerivationState.STALE) { 2895 | untrackedEnd(prevUntracked); 2896 | return true; 2897 | } 2898 | } 2899 | } 2900 | changeDependenciesStateTo0(derivation); 2901 | untrackedEnd(prevUntracked); 2902 | return false; 2903 | } 2904 | } 2905 | } 2906 | 2907 | function isComputingDerivation() { 2908 | return globalState.trackingDerivation !== null; // filter out actions inside computations 2909 | } 2910 | 2911 | function checkIfStateModificationsAreAllowed(atom) { 2912 | var hasObservers$$1 = atom.observers.length > 0; 2913 | // Should never be possible to change an observed observable from inside computed, see #798 2914 | if (globalState.computationDepth > 0 && hasObservers$$1) fail(getMessage("m031") + atom.name); 2915 | // Should not be possible to change observed state outside strict mode, except during initialization, see #563 2916 | if (!globalState.allowStateChanges && hasObservers$$1) fail(getMessage(globalState.strictMode ? "m030a" : "m030b") + atom.name); 2917 | } 2918 | /** 2919 | * Executes the provided function `f` and tracks which observables are being accessed. 2920 | * The tracking information is stored on the `derivation` object and the derivation is registered 2921 | * as observer of any of the accessed observables. 2922 | */ 2923 | function trackDerivedFunction(derivation, f, context) { 2924 | // pre allocate array allocation + room for variation in deps 2925 | // array will be trimmed by bindDependencies 2926 | changeDependenciesStateTo0(derivation); 2927 | derivation.newObserving = new Array(derivation.observing.length + 100); 2928 | derivation.unboundDepsCount = 0; 2929 | derivation.runId = ++globalState.runId; 2930 | var prevTracking = globalState.trackingDerivation; 2931 | globalState.trackingDerivation = derivation; 2932 | var result; 2933 | try { 2934 | result = f.call(context); 2935 | } catch (e) { 2936 | result = new CaughtException(e); 2937 | } 2938 | globalState.trackingDerivation = prevTracking; 2939 | bindDependencies(derivation); 2940 | return result; 2941 | } 2942 | /** 2943 | * diffs newObserving with observing. 2944 | * update observing to be newObserving with unique observables 2945 | * notify observers that become observed/unobserved 2946 | */ 2947 | function bindDependencies(derivation) { 2948 | // invariant(derivation.dependenciesState !== IDerivationState.NOT_TRACKING, "INTERNAL ERROR bindDependencies expects derivation.dependenciesState !== -1"); 2949 | var prevObserving = derivation.observing; 2950 | var observing = derivation.observing = derivation.newObserving; 2951 | var lowestNewObservingDerivationState = IDerivationState.UP_TO_DATE; 2952 | derivation.newObserving = null; // newObserving shouldn't be needed outside tracking 2953 | // Go through all new observables and check diffValue: (this list can contain duplicates): 2954 | // 0: first occurrence, change to 1 and keep it 2955 | // 1: extra occurrence, drop it 2956 | var i0 = 0, 2957 | l = derivation.unboundDepsCount; 2958 | for (var i = 0; i < l; i++) { 2959 | var dep = observing[i]; 2960 | if (dep.diffValue === 0) { 2961 | dep.diffValue = 1; 2962 | if (i0 !== i) observing[i0] = dep; 2963 | i0++; 2964 | } 2965 | // Upcast is 'safe' here, because if dep is IObservable, `dependenciesState` will be undefined, 2966 | // not hitting the condition 2967 | if (dep.dependenciesState > lowestNewObservingDerivationState) { 2968 | lowestNewObservingDerivationState = dep.dependenciesState; 2969 | } 2970 | } 2971 | observing.length = i0; 2972 | // Go through all old observables and check diffValue: (it is unique after last bindDependencies) 2973 | // 0: it's not in new observables, unobserve it 2974 | // 1: it keeps being observed, don't want to notify it. change to 0 2975 | l = prevObserving.length; 2976 | while (l--) { 2977 | var dep = prevObserving[l]; 2978 | if (dep.diffValue === 0) { 2979 | removeObserver(dep, derivation); 2980 | } 2981 | dep.diffValue = 0; 2982 | } 2983 | // Go through all new observables and check diffValue: (now it should be unique) 2984 | // 0: it was set to 0 in last loop. don't need to do anything. 2985 | // 1: it wasn't observed, let's observe it. set back to 0 2986 | while (i0--) { 2987 | var dep = observing[i0]; 2988 | if (dep.diffValue === 1) { 2989 | dep.diffValue = 0; 2990 | addObserver(dep, derivation); 2991 | } 2992 | } 2993 | // Some new observed derivations might become stale during this derivation computation 2994 | // so say had no chance to propagate staleness (#916) 2995 | if (lowestNewObservingDerivationState !== IDerivationState.UP_TO_DATE) { 2996 | derivation.dependenciesState = lowestNewObservingDerivationState; 2997 | derivation.onBecomeStale(); 2998 | } 2999 | } 3000 | 3001 | function clearObserving(derivation) { 3002 | // invariant(globalState.inBatch > 0, "INTERNAL ERROR clearObserving should be called only inside batch"); 3003 | var obs = derivation.observing; 3004 | derivation.observing = []; 3005 | var i = obs.length; 3006 | while (i--) { 3007 | removeObserver(obs[i], derivation); 3008 | } 3009 | derivation.dependenciesState = IDerivationState.NOT_TRACKING; 3010 | } 3011 | 3012 | function untracked(action) { 3013 | var prev = untrackedStart(); 3014 | var res = action(); 3015 | untrackedEnd(prev); 3016 | return res; 3017 | } 3018 | 3019 | function untrackedStart() { 3020 | var prev = globalState.trackingDerivation; 3021 | globalState.trackingDerivation = null; 3022 | return prev; 3023 | } 3024 | 3025 | function untrackedEnd(prev) { 3026 | globalState.trackingDerivation = prev; 3027 | } 3028 | /** 3029 | * needed to keep `lowestObserverState` correct. when changing from (2 or 1) to 0 3030 | * 3031 | */ 3032 | function changeDependenciesStateTo0(derivation) { 3033 | if (derivation.dependenciesState === IDerivationState.UP_TO_DATE) return; 3034 | derivation.dependenciesState = IDerivationState.UP_TO_DATE; 3035 | var obs = derivation.observing; 3036 | var i = obs.length; 3037 | while (i--) { 3038 | obs[i].lowestObserverState = IDerivationState.UP_TO_DATE; 3039 | } 3040 | } 3041 | 3042 | var Reaction = function() { 3043 | function Reaction(name, onInvalidate) { 3044 | if (name === void 0) { 3045 | name = "Reaction@" + getNextId(); 3046 | } 3047 | this.name = name; 3048 | this.onInvalidate = onInvalidate; 3049 | this.observing = []; // nodes we are looking at. Our value depends on these nodes 3050 | this.newObserving = []; 3051 | this.dependenciesState = IDerivationState.NOT_TRACKING; 3052 | this.diffValue = 0; 3053 | this.runId = 0; 3054 | this.unboundDepsCount = 0; 3055 | this.__mapid = "#" + getNextId(); 3056 | this.isDisposed = false; 3057 | this._isScheduled = false; 3058 | this._isTrackPending = false; 3059 | this._isRunning = false; 3060 | } 3061 | Reaction.prototype.onBecomeStale = function() { 3062 | this.schedule(); 3063 | }; 3064 | Reaction.prototype.schedule = function() { 3065 | if (!this._isScheduled) { 3066 | this._isScheduled = true; 3067 | globalState.pendingReactions.push(this); 3068 | runReactions(); 3069 | } 3070 | }; 3071 | Reaction.prototype.isScheduled = function() { 3072 | return this._isScheduled; 3073 | }; 3074 | /** 3075 | * internal, use schedule() if you intend to kick off a reaction 3076 | */ 3077 | Reaction.prototype.runReaction = function() { 3078 | if (!this.isDisposed) { 3079 | startBatch(); 3080 | this._isScheduled = false; 3081 | if (shouldCompute(this)) { 3082 | this._isTrackPending = true; 3083 | this.onInvalidate(); 3084 | if (this._isTrackPending && isSpyEnabled()) { 3085 | // onInvalidate didn't trigger track right away.. 3086 | spyReport({ 3087 | object: this, 3088 | type: "scheduled-reaction" 3089 | }); 3090 | } 3091 | } 3092 | endBatch(); 3093 | } 3094 | }; 3095 | Reaction.prototype.track = function(fn) { 3096 | startBatch(); 3097 | var notify = isSpyEnabled(); 3098 | var startTime; 3099 | if (notify) { 3100 | startTime = Date.now(); 3101 | spyReportStart({ 3102 | object: this, 3103 | type: "reaction", 3104 | fn: fn 3105 | }); 3106 | } 3107 | this._isRunning = true; 3108 | var result = trackDerivedFunction(this, fn, undefined); 3109 | this._isRunning = false; 3110 | this._isTrackPending = false; 3111 | if (this.isDisposed) { 3112 | // disposed during last run. Clean up everything that was bound after the dispose call. 3113 | clearObserving(this); 3114 | } 3115 | if (isCaughtException(result)) this.reportExceptionInDerivation(result.cause); 3116 | if (notify) { 3117 | spyReportEnd({ 3118 | time: Date.now() - startTime 3119 | }); 3120 | } 3121 | endBatch(); 3122 | }; 3123 | Reaction.prototype.reportExceptionInDerivation = function(error) { 3124 | var _this = this; 3125 | if (this.errorHandler) { 3126 | this.errorHandler(error, this); 3127 | return; 3128 | } 3129 | var message = "[mobx] Encountered an uncaught exception that was thrown by a reaction or observer component, in: '" + this; 3130 | var messageToUser = getMessage("m037"); 3131 | console.error(message || messageToUser /* latter will not be true, make sure uglify doesn't remove */ , error); 3132 | /** If debugging brought you here, please, read the above message :-). Tnx! */ 3133 | if (isSpyEnabled()) { 3134 | spyReport({ 3135 | type: "error", 3136 | message: message, 3137 | error: error, 3138 | object: this 3139 | }); 3140 | } 3141 | globalState.globalReactionErrorHandlers.forEach(function(f) { 3142 | return f(error, _this); 3143 | }); 3144 | }; 3145 | Reaction.prototype.dispose = function() { 3146 | if (!this.isDisposed) { 3147 | this.isDisposed = true; 3148 | if (!this._isRunning) { 3149 | startBatch(); 3150 | clearObserving(this); // if disposed while running, clean up later. Maybe not optimal, but rare case 3151 | endBatch(); 3152 | } 3153 | } 3154 | }; 3155 | Reaction.prototype.getDisposer = function() { 3156 | var r = this.dispose.bind(this); 3157 | r.$mobx = this; 3158 | r.onError = registerErrorHandler; 3159 | return r; 3160 | }; 3161 | Reaction.prototype.toString = function() { 3162 | return "Reaction[" + this.name + "]"; 3163 | }; 3164 | Reaction.prototype.whyRun = function() { 3165 | var observing = unique(this._isRunning ? this.newObserving : this.observing).map(function(dep) { 3166 | return dep.name; 3167 | }); 3168 | return "\nWhyRun? reaction '" + this.name + "':\n * Status: [" + (this.isDisposed ? "stopped" : this._isRunning ? "running" : this.isScheduled() ? "scheduled" : "idle") + "]\n * This reaction will re-run if any of the following observables changes:\n " + joinStrings(observing) + "\n " + (this._isRunning ? " (... or any observable accessed during the remainder of the current run)" : "") + "\n\t" + getMessage("m038") + "\n"; 3169 | }; 3170 | return Reaction; 3171 | }(); 3172 | 3173 | function registerErrorHandler(handler) { 3174 | invariant(this && this.$mobx && isReaction(this.$mobx), "Invalid `this`"); 3175 | invariant(!this.$mobx.errorHandler, "Only one onErrorHandler can be registered"); 3176 | this.$mobx.errorHandler = handler; 3177 | } 3178 | 3179 | function onReactionError(handler) { 3180 | globalState.globalReactionErrorHandlers.push(handler); 3181 | return function() { 3182 | var idx = globalState.globalReactionErrorHandlers.indexOf(handler); 3183 | if (idx >= 0) globalState.globalReactionErrorHandlers.splice(idx, 1); 3184 | }; 3185 | } 3186 | /** 3187 | * Magic number alert! 3188 | * Defines within how many times a reaction is allowed to re-trigger itself 3189 | * until it is assumed that this is gonna be a never ending loop... 3190 | */ 3191 | var MAX_REACTION_ITERATIONS = 100; 3192 | var reactionScheduler = function reactionScheduler(f) { 3193 | return f(); 3194 | }; 3195 | 3196 | function runReactions() { 3197 | // Trampolining, if runReactions are already running, new reactions will be picked up 3198 | if (globalState.inBatch > 0 || globalState.isRunningReactions) return; 3199 | reactionScheduler(runReactionsHelper); 3200 | } 3201 | 3202 | function runReactionsHelper() { 3203 | globalState.isRunningReactions = true; 3204 | var allReactions = globalState.pendingReactions; 3205 | var iterations = 0; 3206 | // While running reactions, new reactions might be triggered. 3207 | // Hence we work with two variables and check whether 3208 | // we converge to no remaining reactions after a while. 3209 | while (allReactions.length > 0) { 3210 | if (++iterations === MAX_REACTION_ITERATIONS) { 3211 | console.error("Reaction doesn't converge to a stable state after " + MAX_REACTION_ITERATIONS + " iterations." + (" Probably there is a cycle in the reactive function: " + allReactions[0])); 3212 | allReactions.splice(0); // clear reactions 3213 | } 3214 | var remainingReactions = allReactions.splice(0); 3215 | for (var i = 0, l = remainingReactions.length; i < l; i++) { 3216 | remainingReactions[i].runReaction(); 3217 | } 3218 | } 3219 | globalState.isRunningReactions = false; 3220 | } 3221 | var isReaction = createInstanceofPredicate("Reaction", Reaction); 3222 | 3223 | function setReactionScheduler(fn) { 3224 | var baseScheduler = reactionScheduler; 3225 | reactionScheduler = function reactionScheduler(f) { 3226 | return fn(function() { 3227 | return baseScheduler(f); 3228 | }); 3229 | }; 3230 | } 3231 | 3232 | function asReference(value) { 3233 | deprecated("asReference is deprecated, use observable.ref instead"); 3234 | return observable.ref(value); 3235 | } 3236 | 3237 | function asStructure(value) { 3238 | deprecated("asStructure is deprecated. Use observable.struct, computed.struct or reaction options instead."); 3239 | return observable.struct(value); 3240 | } 3241 | 3242 | function asFlat(value) { 3243 | deprecated("asFlat is deprecated, use observable.shallow instead"); 3244 | return observable.shallow(value); 3245 | } 3246 | 3247 | function asMap(data) { 3248 | deprecated("asMap is deprecated, use observable.map or observable.shallowMap instead"); 3249 | return observable.map(data || {}); 3250 | } 3251 | 3252 | function createComputedDecorator(equals) { 3253 | return createClassPropertyDecorator(function(target, name, _, __, originalDescriptor) { 3254 | invariant(typeof originalDescriptor !== "undefined", getMessage("m009")); 3255 | invariant(typeof originalDescriptor.get === "function", getMessage("m010")); 3256 | var adm = asObservableObject(target, ""); 3257 | defineComputedProperty(adm, name, originalDescriptor.get, originalDescriptor.set, equals, false); 3258 | }, function(name) { 3259 | var observable = this.$mobx.values[name]; 3260 | if (observable === undefined) return undefined; 3261 | return observable.get(); 3262 | }, function(name, value) { 3263 | this.$mobx.values[name].set(value); 3264 | }, false, false); 3265 | } 3266 | var computedDecorator = createComputedDecorator(comparer.default); 3267 | var computedStructDecorator = createComputedDecorator(comparer.structural); 3268 | /** 3269 | * Decorator for class properties: @computed get value() { return expr; }. 3270 | * For legacy purposes also invokable as ES5 observable created: `computed(() => expr)`; 3271 | */ 3272 | var computed = function computed(arg1, arg2, arg3) { 3273 | if (typeof arg2 === "string") { 3274 | return computedDecorator.apply(null, arguments); 3275 | } 3276 | invariant(typeof arg1 === "function", getMessage("m011")); 3277 | invariant(arguments.length < 3, getMessage("m012")); 3278 | var opts = (typeof arg2 === "undefined" ? "undefined" : _typeof(arg2)) === "object" ? arg2 : {}; 3279 | opts.setter = typeof arg2 === "function" ? arg2 : opts.setter; 3280 | var equals = opts.equals ? opts.equals : opts.compareStructural || opts.struct ? comparer.structural : comparer.default; 3281 | return new ComputedValue(arg1, opts.context, equals, opts.name || arg1.name || "", opts.setter); 3282 | }; 3283 | computed.struct = computedStructDecorator; 3284 | computed.equals = createComputedDecorator; 3285 | 3286 | function getAtom(thing, property) { 3287 | if ((typeof thing === "undefined" ? "undefined" : _typeof(thing)) === "object" && thing !== null) { 3288 | if (isObservableArray(thing)) { 3289 | invariant(property === undefined, getMessage("m036")); 3290 | return thing.$mobx.atom; 3291 | } 3292 | if (isObservableMap(thing)) { 3293 | var anyThing = thing; 3294 | if (property === undefined) return getAtom(anyThing._keys); 3295 | var observable = anyThing._data[property] || anyThing._hasMap[property]; 3296 | invariant(!!observable, "the entry '" + property + "' does not exist in the observable map '" + getDebugName(thing) + "'"); 3297 | return observable; 3298 | } 3299 | // Initializers run lazily when transpiling to babel, so make sure they are run... 3300 | runLazyInitializers(thing); 3301 | if (property && !thing.$mobx) thing[property]; // See #1072 // TODO: remove in 4.0 3302 | if (isObservableObject(thing)) { 3303 | if (!property) return fail("please specify a property"); 3304 | var observable = thing.$mobx.values[property]; 3305 | invariant(!!observable, "no observable property '" + property + "' found on the observable object '" + getDebugName(thing) + "'"); 3306 | return observable; 3307 | } 3308 | if (isAtom(thing) || isComputedValue(thing) || isReaction(thing)) { 3309 | return thing; 3310 | } 3311 | } else if (typeof thing === "function") { 3312 | if (isReaction(thing.$mobx)) { 3313 | // disposer function 3314 | return thing.$mobx; 3315 | } 3316 | } 3317 | return fail("Cannot obtain atom from " + thing); 3318 | } 3319 | 3320 | function getAdministration(thing, property) { 3321 | invariant(thing, "Expecting some object"); 3322 | if (property !== undefined) return getAdministration(getAtom(thing, property)); 3323 | if (isAtom(thing) || isComputedValue(thing) || isReaction(thing)) return thing; 3324 | if (isObservableMap(thing)) return thing; 3325 | // Initializers run lazily when transpiling to babel, so make sure they are run... 3326 | runLazyInitializers(thing); 3327 | if (thing.$mobx) return thing.$mobx; 3328 | invariant(false, "Cannot obtain administration from " + thing); 3329 | } 3330 | 3331 | function getDebugName(thing, property) { 3332 | var named; 3333 | if (property !== undefined) named = getAtom(thing, property); 3334 | else if (isObservableObject(thing) || isObservableMap(thing)) named = getAdministration(thing); 3335 | else named = getAtom(thing); // valid for arrays as well 3336 | return named.name; 3337 | } 3338 | 3339 | function isComputed(value, property) { 3340 | if (value === null || value === undefined) return false; 3341 | if (property !== undefined) { 3342 | if (isObservableObject(value) === false) return false; 3343 | var atom = getAtom(value, property); 3344 | return isComputedValue(atom); 3345 | } 3346 | return isComputedValue(value); 3347 | } 3348 | 3349 | function observe(thing, propOrCb, cbOrFire, fireImmediately) { 3350 | if (typeof cbOrFire === "function") return observeObservableProperty(thing, propOrCb, cbOrFire, fireImmediately); 3351 | else return observeObservable(thing, propOrCb, cbOrFire); 3352 | } 3353 | 3354 | function observeObservable(thing, listener, fireImmediately) { 3355 | return getAdministration(thing).observe(listener, fireImmediately); 3356 | } 3357 | 3358 | function observeObservableProperty(thing, property, listener, fireImmediately) { 3359 | return getAdministration(thing, property).observe(listener, fireImmediately); 3360 | } 3361 | 3362 | function intercept(thing, propOrHandler, handler) { 3363 | if (typeof handler === "function") return interceptProperty(thing, propOrHandler, handler); 3364 | else return interceptInterceptable(thing, propOrHandler); 3365 | } 3366 | 3367 | function interceptInterceptable(thing, handler) { 3368 | return getAdministration(thing).intercept(handler); 3369 | } 3370 | 3371 | function interceptProperty(thing, property, handler) { 3372 | return getAdministration(thing, property).intercept(handler); 3373 | } 3374 | 3375 | /** 3376 | * expr can be used to create temporarily views inside views. 3377 | * This can be improved to improve performance if a value changes often, but usually doesn't affect the outcome of an expression. 3378 | * 3379 | * In the following example the expression prevents that a component is rerender _each time_ the selection changes; 3380 | * instead it will only rerenders when the current todo is (de)selected. 3381 | * 3382 | * reactiveComponent((props) => { 3383 | * const todo = props.todo; 3384 | * const isSelected = mobx.expr(() => props.viewState.selection === todo); 3385 | * return
{todo.title}
3386 | * }); 3387 | * 3388 | */ 3389 | function expr(expr, scope) { 3390 | if (!isComputingDerivation()) console.warn(getMessage("m013")); 3391 | // optimization: would be more efficient if the expr itself wouldn't be evaluated first on the next change, but just a 'changed' signal would be fired 3392 | return computed(expr, { 3393 | context: scope 3394 | }).get(); 3395 | } 3396 | 3397 | function toJS(source, detectCycles, __alreadySeen) { 3398 | if (detectCycles === void 0) { 3399 | detectCycles = true; 3400 | } 3401 | if (__alreadySeen === void 0) { 3402 | __alreadySeen = []; 3403 | } 3404 | // optimization: using ES6 map would be more efficient! 3405 | // optimization: lift this function outside toJS, this makes recursion expensive 3406 | function cache(value) { 3407 | if (detectCycles) __alreadySeen.push([source, value]); 3408 | return value; 3409 | } 3410 | if (isObservable(source)) { 3411 | if (detectCycles && __alreadySeen === null) __alreadySeen = []; 3412 | if (detectCycles && source !== null && (typeof source === "undefined" ? "undefined" : _typeof(source)) === "object") { 3413 | for (var i = 0, l = __alreadySeen.length; i < l; i++) { 3414 | if (__alreadySeen[i][0] === source) return __alreadySeen[i][1]; 3415 | } 3416 | } 3417 | if (isObservableArray(source)) { 3418 | var res = cache([]); 3419 | var toAdd = source.map(function(value) { 3420 | return toJS(value, detectCycles, __alreadySeen); 3421 | }); 3422 | res.length = toAdd.length; 3423 | for (var i = 0, l = toAdd.length; i < l; i++) { 3424 | res[i] = toAdd[i]; 3425 | } 3426 | return res; 3427 | } 3428 | if (isObservableObject(source)) { 3429 | var res = cache({}); 3430 | for (var key in source) { 3431 | res[key] = toJS(source[key], detectCycles, __alreadySeen); 3432 | } 3433 | return res; 3434 | } 3435 | if (isObservableMap(source)) { 3436 | var res_1 = cache({}); 3437 | source.forEach(function(value, key) { 3438 | return res_1[key] = toJS(value, detectCycles, __alreadySeen); 3439 | }); 3440 | return res_1; 3441 | } 3442 | if (isObservableValue(source)) return toJS(source.get(), detectCycles, __alreadySeen); 3443 | } 3444 | return source; 3445 | } 3446 | 3447 | function createTransformer(transformer, onCleanup) { 3448 | invariant(typeof transformer === "function" && transformer.length < 2, "createTransformer expects a function that accepts one argument"); 3449 | // Memoizes: object id -> reactive view that applies transformer to the object 3450 | var objectCache = {}; 3451 | // If the resetId changes, we will clear the object cache, see #163 3452 | // This construction is used to avoid leaking refs to the objectCache directly 3453 | var resetId = globalState.resetId; 3454 | // Local transformer class specifically for this transformer 3455 | var Transformer = function(_super) { 3456 | __extends(Transformer, _super); 3457 | 3458 | function Transformer(sourceIdentifier, sourceObject) { 3459 | var _this = _super.call(this, function() { 3460 | return transformer(sourceObject); 3461 | }, undefined, comparer.default, "Transformer-" + transformer.name + "-" + sourceIdentifier, undefined) || this; 3462 | _this.sourceIdentifier = sourceIdentifier; 3463 | _this.sourceObject = sourceObject; 3464 | return _this; 3465 | } 3466 | Transformer.prototype.onBecomeUnobserved = function() { 3467 | var lastValue = this.value; 3468 | _super.prototype.onBecomeUnobserved.call(this); 3469 | delete objectCache[this.sourceIdentifier]; 3470 | if (onCleanup) onCleanup(lastValue, this.sourceObject); 3471 | }; 3472 | return Transformer; 3473 | }(ComputedValue); 3474 | return function(object) { 3475 | if (resetId !== globalState.resetId) { 3476 | objectCache = {}; 3477 | resetId = globalState.resetId; 3478 | } 3479 | var identifier = getMemoizationId(object); 3480 | var reactiveTransformer = objectCache[identifier]; 3481 | if (reactiveTransformer) return reactiveTransformer.get(); 3482 | // Not in cache; create a reactive view 3483 | reactiveTransformer = objectCache[identifier] = new Transformer(identifier, object); 3484 | return reactiveTransformer.get(); 3485 | }; 3486 | } 3487 | 3488 | function getMemoizationId(object) { 3489 | if (typeof object === 'string' || typeof object === 'number') return object; 3490 | if (object === null || (typeof object === "undefined" ? "undefined" : _typeof(object)) !== "object") throw new Error("[mobx] transform expected some kind of object or primitive value, got: " + object); 3491 | var tid = object.$transformId; 3492 | if (tid === undefined) { 3493 | tid = getNextId(); 3494 | addHiddenProp(object, "$transformId", tid); 3495 | } 3496 | return tid; 3497 | } 3498 | 3499 | function log(msg) { 3500 | console.log(msg); 3501 | return msg; 3502 | } 3503 | 3504 | function whyRun(thing, prop) { 3505 | switch (arguments.length) { 3506 | case 0: 3507 | thing = globalState.trackingDerivation; 3508 | if (!thing) return log(getMessage("m024")); 3509 | break; 3510 | case 2: 3511 | thing = getAtom(thing, prop); 3512 | break; 3513 | } 3514 | thing = getAtom(thing); 3515 | if (isComputedValue(thing)) return log(thing.whyRun()); 3516 | else if (isReaction(thing)) return log(thing.whyRun()); 3517 | return fail(getMessage("m025")); 3518 | } 3519 | 3520 | function getDependencyTree(thing, property) { 3521 | return nodeToDependencyTree(getAtom(thing, property)); 3522 | } 3523 | 3524 | function nodeToDependencyTree(node) { 3525 | var result = { 3526 | name: node.name 3527 | }; 3528 | if (node.observing && node.observing.length > 0) result.dependencies = unique(node.observing).map(nodeToDependencyTree); 3529 | return result; 3530 | } 3531 | 3532 | function getObserverTree(thing, property) { 3533 | return nodeToObserverTree(getAtom(thing, property)); 3534 | } 3535 | 3536 | function nodeToObserverTree(node) { 3537 | var result = { 3538 | name: node.name 3539 | }; 3540 | if (hasObservers(node)) result.observers = getObservers(node).map(nodeToObserverTree); 3541 | return result; 3542 | } 3543 | 3544 | function interceptReads(thing, propOrHandler, handler) { 3545 | var target; 3546 | if (isObservableMap(thing) || isObservableArray(thing) || isObservableValue(thing)) { 3547 | target = getAdministration(thing); 3548 | } else if (isObservableObject(thing)) { 3549 | if (typeof propOrHandler !== "string") return fail("InterceptReads can only be used with a specific property, not with an object in general"); 3550 | target = getAdministration(thing, propOrHandler); 3551 | } else { 3552 | return fail("Expected observable map, object or array as first array"); 3553 | } 3554 | if (target.dehancer !== undefined) return fail("An intercept reader was already established"); 3555 | target.dehancer = typeof propOrHandler === "function" ? propOrHandler : handler; 3556 | return function() { 3557 | target.dehancer = undefined; 3558 | }; 3559 | } 3560 | 3561 | /** 3562 | * (c) Michel Weststrate 2015 - 2016 3563 | * MIT Licensed 3564 | * 3565 | * Welcome to the mobx sources! To get an global overview of how MobX internally works, 3566 | * this is a good place to start: 3567 | * https://medium.com/@mweststrate/becoming-fully-reactive-an-in-depth-explanation-of-mobservable-55995262a254#.xvbh6qd74 3568 | * 3569 | * Source folders: 3570 | * =============== 3571 | * 3572 | * - api/ Most of the public static methods exposed by the module can be found here. 3573 | * - core/ Implementation of the MobX algorithm; atoms, derivations, reactions, dependency trees, optimizations. Cool stuff can be found here. 3574 | * - types/ All the magic that is need to have observable objects, arrays and values is in this folder. Including the modifiers like `asFlat`. 3575 | * - utils/ Utility stuff. 3576 | * 3577 | */ 3578 | var extras = { 3579 | allowStateChanges: allowStateChanges, 3580 | deepEqual: deepEqual, 3581 | getAtom: getAtom, 3582 | getDebugName: getDebugName, 3583 | getDependencyTree: getDependencyTree, 3584 | getAdministration: getAdministration, 3585 | getGlobalState: getGlobalState, 3586 | getObserverTree: getObserverTree, 3587 | interceptReads: interceptReads, 3588 | isComputingDerivation: isComputingDerivation, 3589 | isSpyEnabled: isSpyEnabled, 3590 | onReactionError: onReactionError, 3591 | reserveArrayBuffer: reserveArrayBuffer, 3592 | resetGlobalState: resetGlobalState, 3593 | isolateGlobalState: isolateGlobalState, 3594 | shareGlobalState: shareGlobalState, 3595 | spyReport: spyReport, 3596 | spyReportEnd: spyReportEnd, 3597 | spyReportStart: spyReportStart, 3598 | setReactionScheduler: setReactionScheduler 3599 | }; 3600 | var everything = { 3601 | Reaction: Reaction, 3602 | untracked: untracked, 3603 | Atom: Atom, 3604 | BaseAtom: BaseAtom, 3605 | useStrict: useStrict, 3606 | isStrictModeEnabled: isStrictModeEnabled, 3607 | spy: spy, 3608 | comparer: comparer, 3609 | asReference: asReference, 3610 | asFlat: asFlat, 3611 | asStructure: asStructure, 3612 | asMap: asMap, 3613 | isModifierDescriptor: isModifierDescriptor, 3614 | isObservableObject: isObservableObject, 3615 | isBoxedObservable: isObservableValue, 3616 | isObservableArray: isObservableArray, 3617 | ObservableMap: ObservableMap, 3618 | isObservableMap: isObservableMap, 3619 | map: map, 3620 | transaction: transaction, 3621 | observable: observable, 3622 | computed: computed, 3623 | isObservable: isObservable, 3624 | isComputed: isComputed, 3625 | extendObservable: extendObservable, 3626 | extendShallowObservable: extendShallowObservable, 3627 | observe: observe, 3628 | intercept: intercept, 3629 | autorun: autorun, 3630 | autorunAsync: autorunAsync, 3631 | when: when, 3632 | reaction: reaction, 3633 | action: action, 3634 | isAction: isAction, 3635 | runInAction: runInAction, 3636 | expr: expr, 3637 | toJS: toJS, 3638 | createTransformer: createTransformer, 3639 | whyRun: whyRun, 3640 | isArrayLike: isArrayLike, 3641 | extras: extras 3642 | }; 3643 | var warnedAboutDefaultExport = false; 3644 | var _loop_1 = function _loop_1(p) { 3645 | var val = everything[p]; 3646 | Object.defineProperty(everything, p, { 3647 | get: function get() { 3648 | if (!warnedAboutDefaultExport) { 3649 | warnedAboutDefaultExport = true; 3650 | console.warn('Using default export (`import mobx from \'mobx\'`) is deprecated ' + 'and won’t work in mobx@4.0.0\n' + 'Use `import * as mobx from \'mobx\'` instead'); 3651 | } 3652 | return val; 3653 | } 3654 | }); 3655 | }; 3656 | for (var p in everything) { 3657 | _loop_1(p); 3658 | } 3659 | if ((typeof __MOBX_DEVTOOLS_GLOBAL_HOOK__ === "undefined" ? "undefined" : _typeof(__MOBX_DEVTOOLS_GLOBAL_HOOK__)) === "object") { 3660 | __MOBX_DEVTOOLS_GLOBAL_HOOK__.injectMobx({ 3661 | spy: spy, 3662 | extras: extras 3663 | }); 3664 | } 3665 | 3666 | exports.extras = extras; 3667 | exports.Reaction = Reaction; 3668 | exports.untracked = untracked; 3669 | exports.IDerivationState = IDerivationState; 3670 | exports.Atom = Atom; 3671 | exports.BaseAtom = BaseAtom; 3672 | exports.useStrict = useStrict; 3673 | exports.isStrictModeEnabled = isStrictModeEnabled; 3674 | exports.spy = spy; 3675 | exports.comparer = comparer; 3676 | exports.asReference = asReference; 3677 | exports.asFlat = asFlat; 3678 | exports.asStructure = asStructure; 3679 | exports.asMap = asMap; 3680 | exports.isModifierDescriptor = isModifierDescriptor; 3681 | exports.isObservableObject = isObservableObject; 3682 | exports.isBoxedObservable = isObservableValue; 3683 | exports.isObservableArray = isObservableArray; 3684 | exports.ObservableMap = ObservableMap; 3685 | exports.isObservableMap = isObservableMap; 3686 | exports.map = map; 3687 | exports.transaction = transaction; 3688 | exports.observable = observable; 3689 | exports.IObservableFactories = IObservableFactories; 3690 | exports.computed = computed; 3691 | exports.isObservable = isObservable; 3692 | exports.isComputed = isComputed; 3693 | exports.extendObservable = extendObservable; 3694 | exports.extendShallowObservable = extendShallowObservable; 3695 | exports.observe = observe; 3696 | exports.intercept = intercept; 3697 | exports.autorun = autorun; 3698 | exports.autorunAsync = autorunAsync; 3699 | exports.when = when; 3700 | exports.reaction = reaction; 3701 | exports.action = action; 3702 | exports.isAction = isAction; 3703 | exports.runInAction = runInAction; 3704 | exports.expr = expr; 3705 | exports.toJS = toJS; 3706 | exports.createTransformer = createTransformer; 3707 | exports.whyRun = whyRun; 3708 | exports.isArrayLike = isArrayLike; 3709 | exports.default = everything; 3710 | /* WEBPACK VAR INJECTION */ 3711 | }.call(exports, __webpack_require__(4))) 3712 | 3713 | /***/ 3714 | }), 3715 | /* 2 */ 3716 | /***/ 3717 | (function(module, exports, __webpack_require__) { 3718 | 3719 | "use strict"; 3720 | 3721 | 3722 | Object.defineProperty(exports, "__esModule", { 3723 | value: true 3724 | }); 3725 | 3726 | var _slicedToArray = function() { 3727 | function sliceIterator(arr, i) { 3728 | var _arr = []; 3729 | var _n = true; 3730 | var _d = false; 3731 | var _e = undefined; 3732 | try { 3733 | for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { 3734 | _arr.push(_s.value); 3735 | if (i && _arr.length === i) break; 3736 | } 3737 | } catch (err) { 3738 | _d = true; 3739 | _e = err; 3740 | } finally { 3741 | try { 3742 | if (!_n && _i["return"]) _i["return"](); 3743 | } finally { 3744 | if (_d) throw _e; 3745 | } 3746 | } 3747 | return _arr; 3748 | } 3749 | return function(arr, i) { 3750 | if (Array.isArray(arr)) { 3751 | return arr; 3752 | } else if (Symbol.iterator in Object(arr)) { 3753 | return sliceIterator(arr, i); 3754 | } else { 3755 | throw new TypeError("Invalid attempt to destructure non-iterable instance"); 3756 | } 3757 | }; 3758 | }(); 3759 | 3760 | var _icons = __webpack_require__(6); 3761 | 3762 | var _constants = __webpack_require__(0); 3763 | 3764 | var _translator = __webpack_require__(7); 3765 | 3766 | var translator = _interopRequireWildcard(_translator); 3767 | 3768 | function _interopRequireWildcard(obj) { 3769 | if (obj && obj.__esModule) { 3770 | return obj; 3771 | } else { 3772 | var newObj = {}; 3773 | if (obj != null) { 3774 | for (var key in obj) { 3775 | if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; 3776 | } 3777 | } 3778 | newObj.default = obj; 3779 | return newObj; 3780 | } 3781 | } 3782 | 3783 | var culture = /^([^-]{1,3})(-|$)/; 3784 | var $ = function $(x) { 3785 | return (0, translator.default)(x); 3786 | }; 3787 | 3788 | function renderHeader(_ref, instance) { 3789 | var meta = _ref.meta, 3790 | user = _ref.user, 3791 | reactions = _ref.reactions; 3792 | 3793 | var container = document.createElement('div'); 3794 | container.lang = culture.test(instance.lang) ? instance.lang : "en-US"; 3795 | container.className = 'gitment-container gitment-header-container'; 3796 | 3797 | var likeButton = document.createElement('span'); 3798 | var likedReaction = reactions.find(function(reaction) { 3799 | return reaction.content === 'heart' && reaction.user.login === user.login; 3800 | }); 3801 | likeButton.className = 'gitment-header-like-btn'; 3802 | likeButton.innerHTML = '\n ' + _icons.heart + '\n ' + (likedReaction ? 'Unlike' : 'Like') + '\n ' + (meta.reactions && meta.reactions.heart ? ' \u2022 ' + meta.reactions.heart + ' Liked' : '') + '\n '; 3803 | 3804 | if (likedReaction) { 3805 | likeButton.classList.add('liked'); 3806 | likeButton.onclick = function() { 3807 | return instance.unlike(); 3808 | }; 3809 | } else { 3810 | likeButton.classList.remove('liked'); 3811 | likeButton.onclick = function() { 3812 | return instance.like(); 3813 | }; 3814 | } 3815 | container.appendChild(likeButton); 3816 | 3817 | var commentsCount = document.createElement('span'); 3818 | commentsCount.innerHTML = '\n ' + (meta.comments ? ' \u2022 ' + meta.comments + ' Comments' : '') + '\n '; 3819 | container.appendChild(commentsCount); 3820 | 3821 | var issueLink = document.createElement('a'); 3822 | issueLink.className = 'gitment-header-issue-link'; 3823 | issueLink.href = meta.html_url || "javascript:void(0)"; 3824 | issueLink.target = '_blank'; 3825 | issueLink.innerText = $('Issue Page'); 3826 | container.appendChild(issueLink); 3827 | 3828 | return container; 3829 | } 3830 | 3831 | function renderComments(_ref2, instance) { 3832 | var meta = _ref2.meta, 3833 | comments = _ref2.comments, 3834 | commentReactions = _ref2.commentReactions, 3835 | currentPage = _ref2.currentPage, 3836 | user = _ref2.user, 3837 | error = _ref2.error; 3838 | 3839 | var container = document.createElement('div'); 3840 | container.lang = culture.test(instance.lang) ? instance.lang : "en-US"; 3841 | container.className = 'gitment-container gitment-comments-container'; 3842 | 3843 | if (error) { 3844 | var errorBlock = document.createElement('div'); 3845 | errorBlock.className = 'gitment-comments-error'; 3846 | 3847 | if (error === _constants.NOT_INITIALIZED_ERROR && user.login && ~(instance.admin || [instance.owner]).map(function(x) { 3848 | return x.toLowerCase(); 3849 | }).indexOf(user.login.toLowerCase())) { 3850 | // && user.login.toLowerCase() === instance.owner.toLowerCase()) { 3851 | var initHint = document.createElement('div'); 3852 | var initButton = document.createElement('button'); 3853 | initButton.className = 'gitment-comments-init-btn'; 3854 | initButton.onclick = function() { 3855 | initButton.setAttribute('disabled', true); 3856 | instance.init().catch(function(e) { 3857 | initButton.removeAttribute('disabled'); 3858 | alert(e); 3859 | }); 3860 | }; 3861 | initButton.innerText = $('Initialize Comments'); 3862 | initHint.appendChild(initButton); 3863 | errorBlock.appendChild(initHint); 3864 | } else { 3865 | errorBlock.innerText = $(error); 3866 | } 3867 | container.appendChild(errorBlock); 3868 | return container; 3869 | } else if (comments === undefined) { 3870 | var loading = document.createElement('div'); 3871 | loading.innerText = $('Loading comments...'); 3872 | loading.className = 'gitment-comments-loading'; 3873 | container.appendChild(loading); 3874 | return container; 3875 | } else if (!comments.length) { 3876 | var emptyBlock = document.createElement('div'); 3877 | emptyBlock.className = 'gitment-comments-empty'; 3878 | emptyBlock.innerText = $('No Comment Yet'); 3879 | container.appendChild(emptyBlock); 3880 | return container; 3881 | } 3882 | 3883 | var commentsList = document.createElement('ul'); 3884 | commentsList.className = 'gitment-comments-list'; 3885 | 3886 | comments.forEach(function(comment) { 3887 | var createDate = new Date(comment.created_at); 3888 | var updateDate = new Date(comment.updated_at); 3889 | var commentItem = document.createElement('li'); 3890 | commentItem.className = 'gitment-comment'; 3891 | commentItem.innerHTML = '\n \n \n \n
\n
\n \n ' + comment.user.login + '\n \n commented on\n ' + createDate.toDateString() + '\n ' + (createDate.toString() !== updateDate.toString() ? ' \u2022 edited' : '') + '\n
' + _icons.heart + ' ' + (comment.reactions.heart || '') + '
\n
\n
' + comment.body_html + '
\n
\n '; 3892 | var likeButton = commentItem.querySelector('.gitment-comment-like-btn'); 3893 | var likedReaction = commentReactions[comment.id] && commentReactions[comment.id].find(function(reaction) { 3894 | return reaction.content === 'heart' && reaction.user.login === user.login; 3895 | }); 3896 | if (likedReaction) { 3897 | likeButton.classList.add('liked'); 3898 | likeButton.onclick = function() { 3899 | return instance.unlikeAComment(comment.id); 3900 | }; 3901 | } else { 3902 | likeButton.classList.remove('liked'); 3903 | likeButton.onclick = function() { 3904 | return instance.likeAComment(comment.id); 3905 | }; 3906 | } 3907 | 3908 | // dirty 3909 | // use a blank image to trigger height calculating when element rendered 3910 | var imgTrigger = document.createElement('img'); 3911 | var markdownBody = commentItem.querySelector('.gitment-comment-body'); 3912 | imgTrigger.className = 'gitment-hidden'; 3913 | imgTrigger.src = "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="; 3914 | imgTrigger.onload = function() { 3915 | if (markdownBody.clientHeight > instance.maxCommentHeight) { 3916 | markdownBody.classList.add('gitment-comment-body-folded'); 3917 | markdownBody.style.maxHeight = instance.maxCommentHeight + 'px'; 3918 | markdownBody.title = 'Click to Expand'; 3919 | markdownBody.onclick = function() { 3920 | markdownBody.classList.remove('gitment-comment-body-folded'); 3921 | markdownBody.style.maxHeight = ''; 3922 | markdownBody.title = ''; 3923 | markdownBody.onclick = null; 3924 | }; 3925 | } 3926 | }; 3927 | commentItem.appendChild(imgTrigger); 3928 | 3929 | commentsList.appendChild(commentItem); 3930 | }); 3931 | 3932 | container.appendChild(commentsList); 3933 | 3934 | if (meta) { 3935 | var pageCount = Math.ceil(meta.comments / instance.perPage); 3936 | if (pageCount > 1) { 3937 | var pagination = document.createElement('ul'); 3938 | pagination.className = 'gitment-comments-pagination'; 3939 | 3940 | if (currentPage > 1) { 3941 | var previousButton = document.createElement('li'); 3942 | previousButton.className = 'gitment-comments-page-item'; 3943 | previousButton.innerText = $('Previous'); 3944 | previousButton.onclick = function() { 3945 | return instance.goto(currentPage - 1); 3946 | }; 3947 | pagination.appendChild(previousButton); 3948 | } 3949 | 3950 | var _loop = function _loop(i) { 3951 | var pageItem = document.createElement('li'); 3952 | pageItem.className = 'gitment-comments-page-item'; 3953 | pageItem.innerText = i; 3954 | pageItem.onclick = function() { 3955 | return instance.goto(i); 3956 | }; 3957 | if (currentPage === i) pageItem.classList.add('gitment-selected'); 3958 | pagination.appendChild(pageItem); 3959 | }; 3960 | 3961 | for (var i = 1; i <= pageCount; i++) { 3962 | _loop(i); 3963 | } 3964 | 3965 | if (currentPage < pageCount) { 3966 | var nextButton = document.createElement('li'); 3967 | nextButton.className = 'gitment-comments-page-item'; 3968 | nextButton.innerText = $('Next'); 3969 | nextButton.onclick = function() { 3970 | return instance.goto(currentPage + 1); 3971 | }; 3972 | pagination.appendChild(nextButton); 3973 | } 3974 | 3975 | container.appendChild(pagination); 3976 | } 3977 | } 3978 | 3979 | return container; 3980 | } 3981 | 3982 | function renderEditor(_ref3, instance) { 3983 | var user = _ref3.user, 3984 | error = _ref3.error; 3985 | 3986 | var container = document.createElement('div'); 3987 | container.lang = culture.test(instance.lang) ? instance.lang : "en-US"; 3988 | container.className = 'gitment-container gitment-editor-container'; 3989 | 3990 | var shouldDisable = user.login && !error ? '' : 'disabled'; 3991 | var disabledTip = user.login ? '' : $('Login to Comment'); 3992 | container.innerHTML = '\n ' + (user.login ? '\n \n ' : user.isLoggingIn ? '
' + _icons.spinner + '
' : '\n ' + _icons.github + '\n ') + '\n \n
\n
\n \n \n
\n
\n
\n \n
\n
\n
\n
\n
\n
\n \n '; 3993 | if (user.login) { 3994 | container.querySelector('.gitment-editor-logout-link').onclick = function() { 3995 | return instance.logout(); 3996 | }; 3997 | } 3998 | 3999 | var writeField = container.querySelector('.gitment-editor-write-field'); 4000 | var previewField = container.querySelector('.gitment-editor-preview-field'); 4001 | 4002 | var textarea = writeField.querySelector('textarea'); 4003 | textarea.oninput = function() { 4004 | textarea.style.height = 'auto'; 4005 | var style = window.getComputedStyle(textarea, null); 4006 | var height = parseInt(style.height, 10); 4007 | var clientHeight = textarea.clientHeight; 4008 | var scrollHeight = textarea.scrollHeight; 4009 | if (clientHeight < scrollHeight) { 4010 | textarea.style.height = height + scrollHeight - clientHeight + 'px'; 4011 | } 4012 | }; 4013 | 4014 | var _container$querySelec = container.querySelectorAll('.gitment-editor-tab'), 4015 | _container$querySelec2 = _slicedToArray(_container$querySelec, 2), 4016 | writeTab = _container$querySelec2[0], 4017 | previewTab = _container$querySelec2[1]; 4018 | 4019 | writeTab.onclick = function() { 4020 | writeTab.classList.add('gitment-selected'); 4021 | previewTab.classList.remove('gitment-selected'); 4022 | writeField.classList.remove('gitment-hidden'); 4023 | previewField.classList.add('gitment-hidden'); 4024 | 4025 | textarea.focus(); 4026 | }; 4027 | previewTab.onclick = function() { 4028 | previewTab.classList.add('gitment-selected'); 4029 | writeTab.classList.remove('gitment-selected'); 4030 | previewField.classList.remove('gitment-hidden'); 4031 | writeField.classList.add('gitment-hidden'); 4032 | 4033 | var preview = previewField.querySelector('.gitment-editor-preview'); 4034 | var content = textarea.value.trim(); 4035 | if (!content) { 4036 | preview.innerText = $('Nothing to preview'); 4037 | return; 4038 | } 4039 | 4040 | preview.innerText = $('Loading preview...'); 4041 | instance.markdown(content).then(function(html) { 4042 | return preview.innerHTML = html; 4043 | }); 4044 | }; 4045 | 4046 | var submitButton = container.querySelector('.gitment-editor-submit'); 4047 | submitButton.onclick = function() { 4048 | submitButton.innerText = $('Submitting...'); 4049 | submitButton.setAttribute('disabled', true); 4050 | instance.post(textarea.value.trim()).then(function(data) { 4051 | textarea.value = ''; 4052 | textarea.style.height = 'auto'; 4053 | submitButton.removeAttribute('disabled'); 4054 | submitButton.innerText = $('Comment'); 4055 | }).catch(function(e) { 4056 | alert(e); 4057 | submitButton.removeAttribute('disabled'); 4058 | submitButton.innerText = $('Comment'); 4059 | }); 4060 | }; 4061 | 4062 | return container; 4063 | } 4064 | 4065 | function renderFooter(state, instance) { 4066 | var container = document.createElement('div'); 4067 | container.lang = culture.test(instance.lang) ? instance.lang : "en-US"; 4068 | container.className = 'gitment-container gitment-footer-container'; 4069 | container.innerHTML = '\n Powered by\n \n Gitment\n \n '; 4070 | return container; 4071 | } 4072 | 4073 | function renderCounter(_ref4, instance) { 4074 | var meta = _ref4.meta; 4075 | 4076 | var counter = document.querySelector(".post-comments-count.gitment-comments-count"); 4077 | if (counter && 4078 | // (instance.id.lastIndexOf(counter.getAttribute('data-xid')) > -1) && 4079 | counter.getAttribute('itemprop') == "commentsCount") { 4080 | counter.innerText = meta && meta.comments || 0; 4081 | } 4082 | } 4083 | 4084 | function render(state, instance) { 4085 | var container = document.createElement('div'); 4086 | container.lang = culture.test(instance.lang) ? instance.lang : "en-US"; 4087 | 4088 | // rewrite by gitmint 4089 | $ = instance.lang && translator[instance.lang] || translator.fromLanguageCode(container.lang); 4090 | instance.updateCount = function() { 4091 | return instance.renderCounter(state, instance); 4092 | }; 4093 | 4094 | container.className = 'gitment-container gitment-root-container'; 4095 | var parts = (!instance.above) ? ['renderHeader', 'renderComments', 'renderEditor', 'renderFooter'] : ['renderHeader', 'renderEditor', 'renderFooter', 'renderComments']; 4096 | parts.forEach(function(partName) { 4097 | container.appendChild(instance[partName](state, instance)); 4098 | }); 4099 | return container; 4100 | } 4101 | 4102 | exports.default = { 4103 | render: render, 4104 | renderHeader: renderHeader, 4105 | renderComments: renderComments, 4106 | renderEditor: renderEditor, 4107 | renderFooter: renderFooter, 4108 | renderCounter: renderCounter 4109 | }; 4110 | 4111 | /***/ 4112 | }), 4113 | /* 3 */ 4114 | /***/ 4115 | (function(module, exports, __webpack_require__) { 4116 | 4117 | "use strict"; 4118 | 4119 | 4120 | Object.defineProperty(exports, "__esModule", { 4121 | value: true 4122 | }); 4123 | exports.http = exports.Query = exports.isString = undefined; 4124 | 4125 | var _slicedToArray = function() { 4126 | function sliceIterator(arr, i) { 4127 | var _arr = []; 4128 | var _n = true; 4129 | var _d = false; 4130 | var _e = undefined; 4131 | try { 4132 | for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { 4133 | _arr.push(_s.value); 4134 | if (i && _arr.length === i) break; 4135 | } 4136 | } catch (err) { 4137 | _d = true; 4138 | _e = err; 4139 | } finally { 4140 | try { 4141 | if (!_n && _i["return"]) _i["return"](); 4142 | } finally { 4143 | if (_d) throw _e; 4144 | } 4145 | } 4146 | return _arr; 4147 | } 4148 | return function(arr, i) { 4149 | if (Array.isArray(arr)) { 4150 | return arr; 4151 | } else if (Symbol.iterator in Object(arr)) { 4152 | return sliceIterator(arr, i); 4153 | } else { 4154 | throw new TypeError("Invalid attempt to destructure non-iterable instance"); 4155 | } 4156 | }; 4157 | }(); 4158 | 4159 | exports.getTargetContainer = getTargetContainer; 4160 | 4161 | var _constants = __webpack_require__(0); 4162 | 4163 | var isString = exports.isString = function isString(s) { 4164 | return toString.call(s) === '[object String]'; 4165 | }; 4166 | 4167 | function getTargetContainer(container) { 4168 | var targetContainer = void 0; 4169 | if (container instanceof Element) { 4170 | targetContainer = container; 4171 | } else if (isString(container)) { 4172 | targetContainer = document.getElementById(container); 4173 | } else { 4174 | targetContainer = document.createElement('div'); 4175 | } 4176 | 4177 | return targetContainer; 4178 | } 4179 | 4180 | var Query = exports.Query = { 4181 | parse: function parse() { 4182 | var search = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window.location.search; 4183 | 4184 | if (!search) return {}; 4185 | var queryString = search[0] === '?' ? search.substring(1) : search; 4186 | var query = {}; 4187 | queryString.split('&').forEach(function(queryStr) { 4188 | var _queryStr$split = queryStr.split('='), 4189 | _queryStr$split2 = _slicedToArray(_queryStr$split, 2), 4190 | key = _queryStr$split2[0], 4191 | value = _queryStr$split2[1]; 4192 | 4193 | if (key) query[key] = value; 4194 | }); 4195 | 4196 | return query; 4197 | }, 4198 | stringify: function stringify(query) { 4199 | var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '?'; 4200 | 4201 | var queryString = Object.keys(query).map(function(key) { 4202 | return key + '=' + encodeURIComponent(query[key] || ''); 4203 | }).join('&'); 4204 | return queryString ? prefix + queryString : ''; 4205 | } 4206 | }; 4207 | 4208 | function ajaxFactory(method) { 4209 | return function(apiPath) { 4210 | var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; 4211 | var base = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'https://api.github.com'; 4212 | 4213 | var req = new XMLHttpRequest(); 4214 | var token = localStorage.getItem(_constants.LS_ACCESS_TOKEN_KEY); 4215 | if (base !== 'https://api.github.com') token = null; 4216 | 4217 | var url = '' + base + apiPath; 4218 | var body = null; 4219 | if (method === 'GET' || method === 'DELETE') { 4220 | url += isString(data) ? data : Query.stringify(data); 4221 | } 4222 | 4223 | var p = new Promise(function(resolve, reject) { 4224 | req.addEventListener('load', function() { 4225 | var contentType = req.getResponseHeader('content-type'); 4226 | var res = req.responseText; 4227 | 4228 | var data = res; 4229 | if (/urlencoded/.test(contentType)) { 4230 | data = req.responseText ? Query.parse(res) : {}; 4231 | if (data.error) return reject(new Error(data.error_description)); 4232 | } else if (/json/.test(contentType)) { 4233 | data = req.responseText ? JSON.parse(res) : {}; 4234 | if (data.message) return reject(new Error(data.message)); 4235 | } 4236 | resolve(data); 4237 | }); 4238 | req.addEventListener('error', function(error) { 4239 | return reject(error); 4240 | }); 4241 | }); 4242 | req.open(method, url, true); 4243 | 4244 | req.setRequestHeader('Accept', 'application/vnd.github.squirrel-girl-preview, application/vnd.github.html+json, application/x-www-form-urlencoded, application/vnd.github.machine-man-preview+json'); 4245 | if (token) { 4246 | req.setRequestHeader('Authorization', 'token ' + token); 4247 | } 4248 | if (method !== 'GET' && method !== 'DELETE') { 4249 | if (isString(data)) { 4250 | body = data; 4251 | req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 4252 | } else { 4253 | body = JSON.stringify(data); 4254 | req.setRequestHeader('Content-Type', 'application/json'); 4255 | } 4256 | } 4257 | 4258 | req.send(body); 4259 | return p; 4260 | }; 4261 | } 4262 | 4263 | var http = exports.http = { 4264 | get: ajaxFactory('GET'), 4265 | post: ajaxFactory('POST'), 4266 | delete: ajaxFactory('DELETE'), 4267 | put: ajaxFactory('PUT') 4268 | }; 4269 | 4270 | /***/ 4271 | }), 4272 | /* 4 */ 4273 | /***/ 4274 | (function(module, exports, __webpack_require__) { 4275 | 4276 | "use strict"; 4277 | 4278 | 4279 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function(obj) { 4280 | return typeof obj; 4281 | } : function(obj) { 4282 | return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; 4283 | }; 4284 | 4285 | var g; 4286 | 4287 | // This works in non-strict mode 4288 | g = function() { 4289 | return this; 4290 | }(); 4291 | 4292 | try { 4293 | // This works if eval is allowed (see CSP) 4294 | g = g || Function("return this")() || (1, eval)("this"); 4295 | } catch (e) { 4296 | // This works if the window reference is available 4297 | if ((typeof window === "undefined" ? "undefined" : _typeof(window)) === "object") g = window; 4298 | } 4299 | 4300 | // g can still be undefined, but nothing to do about it... 4301 | // We return undefined, instead of nothing here, so it's 4302 | // easier to handle this case. if(!global) { ...} 4303 | 4304 | module.exports = g; 4305 | 4306 | /***/ 4307 | }), 4308 | /* 5 */ 4309 | /***/ 4310 | (function(module, exports, __webpack_require__) { 4311 | 4312 | "use strict"; 4313 | 4314 | 4315 | var _createClass = function() { 4316 | function defineProperties(target, props) { 4317 | for (var i = 0; i < props.length; i++) { 4318 | var descriptor = props[i]; 4319 | descriptor.enumerable = descriptor.enumerable || false; 4320 | descriptor.configurable = true; 4321 | if ("value" in descriptor) descriptor.writable = true; 4322 | Object.defineProperty(target, descriptor.key, descriptor); 4323 | } 4324 | } 4325 | return function(Constructor, protoProps, staticProps) { 4326 | if (protoProps) defineProperties(Constructor.prototype, protoProps); 4327 | if (staticProps) defineProperties(Constructor, staticProps); 4328 | return Constructor; 4329 | }; 4330 | }(); 4331 | 4332 | var _mobx = __webpack_require__(1); 4333 | 4334 | var _constants = __webpack_require__(0); 4335 | 4336 | var _utils = __webpack_require__(3); 4337 | 4338 | var _default = __webpack_require__(2); 4339 | 4340 | var _default2 = _interopRequireDefault(_default); 4341 | 4342 | function _interopRequireDefault(obj) { 4343 | return obj && obj.__esModule ? obj : { 4344 | default: obj 4345 | }; 4346 | } 4347 | 4348 | function _classCallCheck(instance, Constructor) { 4349 | if (!(instance instanceof Constructor)) { 4350 | throw new TypeError("Cannot call a class as a function"); 4351 | } 4352 | } 4353 | 4354 | // @see: https://developer.github.com/apps/building-integrations/setting-up-and-registering-oauth-apps/about-scopes-for-oauth-apps 4355 | var scope = 'public_repo'; 4356 | 4357 | // Github setting of 'Authorization callback URL' in your OAuth application 4358 | var force_redirect_protocol = '$&'; 4359 | // A RegExp to match protocol and domain 4360 | var rx_url_with_protocol = /^((https?:\/\/+){0,1}[^\/]*)(.*)/; 4361 | 4362 | function extendRenderer(instance, renderer) { 4363 | instance[renderer] = function(container) { 4364 | var targetContainer = (0, _utils.getTargetContainer)(container); 4365 | var render = instance.theme[renderer] || instance.defaultTheme[renderer]; 4366 | 4367 | (0, _mobx.autorun)(function() { 4368 | var e = render(instance.state, instance); 4369 | if (e) { 4370 | if (targetContainer.firstChild) { 4371 | targetContainer.replaceChild(e, targetContainer.firstChild); 4372 | } else { 4373 | targetContainer.appendChild(e); 4374 | } 4375 | } 4376 | }); 4377 | 4378 | return targetContainer; 4379 | }; 4380 | } 4381 | 4382 | var Gitment = function() { 4383 | _createClass(Gitment, [{ 4384 | key: 'accessToken', 4385 | get: function get() { 4386 | return localStorage.getItem(_constants.LS_ACCESS_TOKEN_KEY); 4387 | }, 4388 | set: function set(token) { 4389 | localStorage.setItem(_constants.LS_ACCESS_TOKEN_KEY, token); 4390 | } 4391 | }, { 4392 | key: 'loginLink', 4393 | get: function get() { 4394 | var oauthUri = 'https://github.com/login/oauth/authorize'; 4395 | var redirect_uri = this.oauth.redirect_uri || window.location.href.replace(/^https?/i, this.oauth.redirect_protocol || force_redirect_protocol); 4396 | 4397 | var oauthParams = Object.assign({ 4398 | scope: scope, 4399 | redirect_uri: redirect_uri 4400 | }, this.oauth); 4401 | 4402 | return '' + oauthUri + _utils.Query.stringify(oauthParams); 4403 | } 4404 | }]); 4405 | 4406 | function Gitment() { 4407 | var _this = this; 4408 | 4409 | var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; 4410 | 4411 | _classCallCheck(this, Gitment); 4412 | 4413 | this.defaultTheme = _default2.default; 4414 | this.useTheme(_default2.default); 4415 | 4416 | var internalId; 4417 | Object.defineProperties(this, { 4418 | 'updateCount': { 4419 | value: new Function(), 4420 | writable: true 4421 | }, 4422 | 'id': { 4423 | get: function get() { 4424 | return internalId; 4425 | }, 4426 | set: function set(id) { 4427 | return internalId = id !== window.location.href ? id : '' + window.location.origin + window.location.pathname + window.location.search; 4428 | } 4429 | } 4430 | }); 4431 | 4432 | Object.assign(this, { 4433 | id: window.location.href, 4434 | title: window.document.title, 4435 | link: window.location.href, 4436 | desc: '', 4437 | labels: [], 4438 | theme: _default2.default, 4439 | oauth: {}, 4440 | perPage: 20, 4441 | maxCommentHeight: 250 4442 | }, options); 4443 | 4444 | this.useTheme(this.theme); 4445 | 4446 | var user = {}; 4447 | try { 4448 | var userInfo = localStorage.getItem(_constants.LS_USER_KEY); 4449 | if (this.accessToken && userInfo) { 4450 | Object.assign(user, JSON.parse(userInfo), { 4451 | fromCache: true 4452 | }); 4453 | } 4454 | } catch (e) { 4455 | localStorage.removeItem(_constants.LS_USER_KEY); 4456 | } 4457 | 4458 | this.state = (0, _mobx.observable)({ 4459 | user: user, 4460 | error: null, 4461 | meta: {}, 4462 | comments: undefined, 4463 | reactions: [], 4464 | commentReactions: {}, 4465 | currentPage: 1 4466 | }); 4467 | 4468 | // NOTE: the proxy_gateway accept form-urlencoded only!!! 4469 | // - PHP implement at https://github.com/aimingoo/intersect 4470 | var query = _utils.Query.parse(); 4471 | if (query.code) { 4472 | var _oauth = this.oauth, 4473 | client_id = _oauth.client_id, 4474 | client_secret = _oauth.client_secret, 4475 | proxy_gateway = _oauth.proxy_gateway; 4476 | 4477 | var code = query.code; 4478 | delete query.code; 4479 | var search = _utils.Query.stringify(query); 4480 | var replacedUrl = '' + window.location.origin + window.location.pathname + search + window.location.hash; 4481 | history.replaceState({}, '', replacedUrl); 4482 | 4483 | Object.assign(this, { 4484 | id: replacedUrl, 4485 | link: replacedUrl 4486 | }, options); 4487 | 4488 | this.state.user.isLoggingIn = true; 4489 | var logging = !proxy_gateway ? _utils.http.post('https://gh-oauth.imsun.net', { 4490 | code: code, 4491 | client_id: client_id, 4492 | client_secret: client_secret 4493 | }, '') : _utils.http.post('/login/oauth/access_token', 'code=' + code + '&client_id=' + client_id, proxy_gateway); 4494 | logging.then(function(data) { 4495 | _this.accessToken = data.access_token; 4496 | _this.update(); 4497 | }).catch(function(e) { 4498 | _this.state.user.isLoggingIn = false; 4499 | alert(e); 4500 | }); 4501 | } else { 4502 | this.update(); 4503 | } 4504 | } 4505 | 4506 | _createClass(Gitment, [{ 4507 | key: 'init', 4508 | value: function init() { 4509 | var _this2 = this; 4510 | 4511 | return this.createIssue().then(function() { 4512 | return _this2.loadComments(); 4513 | }).then(function(comments) { 4514 | _this2.state.error = null; 4515 | return comments; 4516 | }); 4517 | } 4518 | }, { 4519 | key: 'useTheme', 4520 | value: function useTheme() { 4521 | var _this3 = this; 4522 | 4523 | var theme = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; 4524 | 4525 | this.theme = theme; 4526 | 4527 | var renderers = Object.keys(this.theme); 4528 | renderers.forEach(function(renderer) { 4529 | return extendRenderer(_this3, renderer); 4530 | }); 4531 | } 4532 | }, { 4533 | key: 'update', 4534 | value: function update() { 4535 | var _this4 = this; 4536 | 4537 | return Promise.all([this.loadMeta(), this.loadUserInfo()]).then(function() { 4538 | return Promise.all([_this4.loadComments().then(function() { 4539 | return _this4.loadCommentReactions(); 4540 | }), _this4.loadReactions()]); 4541 | }).catch(function(e) { 4542 | return _this4.state.error = e; 4543 | }); 4544 | } 4545 | }, { 4546 | key: 'markdown', 4547 | value: function markdown(text) { 4548 | return _utils.http.post('/markdown', { 4549 | text: text, 4550 | mode: 'gfm' 4551 | }); 4552 | } 4553 | }, { 4554 | key: 'createIssue', 4555 | value: function createIssue() { 4556 | var _this5 = this; 4557 | 4558 | var id = this.id, 4559 | owner = this.owner, 4560 | repo = this.repo, 4561 | title = this.title, 4562 | link = this.link, 4563 | desc = this.desc, 4564 | labels = this.labels; 4565 | 4566 | 4567 | return _utils.http.post('/repos/' + owner + '/' + repo + '/issues', { 4568 | title: title, 4569 | labels: labels.concat(['gitment', id]), 4570 | body: link + '\n\n' + desc 4571 | }).then(function(meta) { 4572 | _this5.state.meta = meta; 4573 | return meta; 4574 | }); 4575 | } 4576 | }, { 4577 | key: 'getIssue', 4578 | value: function getIssue() { 4579 | if (this.state.meta.id) return Promise.resolve(this.state.meta); 4580 | 4581 | return this.loadMeta(); 4582 | } 4583 | }, { 4584 | key: 'post', 4585 | value: function post(body) { 4586 | var _this6 = this; 4587 | 4588 | return this.getIssue().then(function(issue) { 4589 | var matched = issue.comments_url.match(rx_url_with_protocol); 4590 | return _utils.http.post(matched[3], { 4591 | body: body 4592 | }, matched[1] || undefined); 4593 | }).then(function(data) { 4594 | _this6.state.meta.comments++; 4595 | _this6.updateCount(); 4596 | var pageCount = Math.ceil(_this6.state.meta.comments / _this6.perPage); 4597 | if (_this6.state.currentPage === pageCount) { 4598 | _this6.state.comments.push(data); 4599 | } 4600 | return data; 4601 | }); 4602 | } 4603 | }, { 4604 | key: 'loadMeta', 4605 | value: function loadMeta() { 4606 | var _this7 = this; 4607 | 4608 | var id = this.id, 4609 | owner = this.owner, 4610 | admin = this.admin, 4611 | repo = this.repo; 4612 | 4613 | return _utils.http.get('/repos/' + owner + '/' + repo + '/issues', { 4614 | labels: id 4615 | }).then(function(issues) { 4616 | if (issues.length) { 4617 | // recheck creator for organization 4618 | // - or check 'issue.user.login and issue.user.site_admin' ? 4619 | var allowed = (admin || [owner]).map(function(x) { 4620 | return x.toLowerCase(); 4621 | }); 4622 | issues = issues.filter(function(issue) { 4623 | return ~allowed.indexOf(issue.user.login.toLowerCase()); 4624 | }).sort(function(left, right) { 4625 | return new Date(left.created_at) - new Date(right.created_at); 4626 | }); 4627 | } 4628 | if (!issues.length) return Promise.reject(_constants.NOT_INITIALIZED_ERROR); 4629 | _this7.state.meta = issues[0]; 4630 | _this7.updateCount(); 4631 | return issues[0]; 4632 | }); 4633 | } 4634 | }, { 4635 | key: 'loadComments', 4636 | value: function loadComments() { 4637 | var _this8 = this; 4638 | 4639 | var page = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.state.currentPage; 4640 | 4641 | return this.getIssue().then(function(issue) { 4642 | var matched = issue.comments_url.match(rx_url_with_protocol); 4643 | return _utils.http.get(matched[3], { 4644 | page: page, 4645 | per_page: _this8.perPage 4646 | }, matched[1] || undefined); 4647 | }).then(function(comments) { 4648 | _this8.state.comments = comments; 4649 | return comments; 4650 | }); 4651 | } 4652 | }, { 4653 | key: 'loadUserInfo', 4654 | value: function loadUserInfo() { 4655 | var _this9 = this; 4656 | 4657 | if (!this.accessToken) { 4658 | this.logout(); 4659 | return Promise.resolve({}); 4660 | } 4661 | 4662 | return _utils.http.get('/user').then(function(user) { 4663 | _this9.state.user = user; 4664 | localStorage.setItem(_constants.LS_USER_KEY, JSON.stringify(user)); 4665 | return user; 4666 | }); 4667 | } 4668 | }, { 4669 | key: 'loadReactions', 4670 | value: function loadReactions() { 4671 | var _this10 = this; 4672 | 4673 | if (!this.accessToken) { 4674 | this.state.reactions = []; 4675 | return Promise.resolve([]); 4676 | } 4677 | 4678 | return this.getIssue().then(function(issue) { 4679 | if (!issue.reactions.total_count) return []; 4680 | return _utils.http.get(issue.reactions.url, {}, ''); 4681 | }).then(function(reactions) { 4682 | _this10.state.reactions = reactions; 4683 | return reactions; 4684 | }); 4685 | } 4686 | }, { 4687 | key: 'loadCommentReactions', 4688 | value: function loadCommentReactions() { 4689 | var _this11 = this; 4690 | 4691 | if (!this.accessToken) { 4692 | this.state.commentReactions = {}; 4693 | return Promise.resolve([]); 4694 | } 4695 | 4696 | var comments = this.state.comments; 4697 | var comentReactions = {}; 4698 | 4699 | return Promise.all(comments.map(function(comment) { 4700 | if (!comment.reactions.total_count) return []; 4701 | 4702 | var owner = _this11.owner, 4703 | repo = _this11.repo; 4704 | 4705 | return _utils.http.get('/repos/' + owner + '/' + repo + '/issues/comments/' + comment.id + '/reactions', {}); 4706 | })).then(function(reactionsArray) { 4707 | comments.forEach(function(comment, index) { 4708 | comentReactions[comment.id] = reactionsArray[index]; 4709 | }); 4710 | _this11.state.commentReactions = comentReactions; 4711 | 4712 | return comentReactions; 4713 | }); 4714 | } 4715 | }, { 4716 | key: 'login', 4717 | value: function login() { 4718 | window.location.href = this.loginLink; 4719 | } 4720 | }, { 4721 | key: 'logout', 4722 | value: function logout() { 4723 | localStorage.removeItem(_constants.LS_ACCESS_TOKEN_KEY); 4724 | localStorage.removeItem(_constants.LS_USER_KEY); 4725 | this.state.user = {}; 4726 | } 4727 | }, { 4728 | key: 'goto', 4729 | value: function goto(page) { 4730 | this.state.currentPage = page; 4731 | this.state.comments = undefined; 4732 | return this.loadComments(page); 4733 | } 4734 | }, { 4735 | key: 'like', 4736 | value: function like() { 4737 | var _this12 = this; 4738 | 4739 | if (!this.accessToken) { 4740 | alert('Login to Like'); 4741 | return Promise.reject(); 4742 | } 4743 | 4744 | var owner = this.owner, 4745 | repo = this.repo; 4746 | 4747 | 4748 | return _utils.http.post('/repos/' + owner + '/' + repo + '/issues/' + this.state.meta.number + '/reactions', { 4749 | content: 'heart' 4750 | }).then(function(reaction) { 4751 | _this12.state.reactions.push(reaction); 4752 | _this12.state.meta.reactions.heart++; 4753 | }); 4754 | } 4755 | }, { 4756 | key: 'unlike', 4757 | value: function unlike() { 4758 | var _this13 = this; 4759 | 4760 | if (!this.accessToken) return Promise.reject(); 4761 | 4762 | var _state = this.state, 4763 | user = _state.user, 4764 | reactions = _state.reactions; 4765 | 4766 | var index = reactions.findIndex(function(reaction) { 4767 | return reaction.user.login === user.login; 4768 | }); 4769 | return _utils.http.delete('/reactions/' + reactions[index].id).then(function() { 4770 | reactions.splice(index, 1); 4771 | _this13.state.meta.reactions.heart--; 4772 | }); 4773 | } 4774 | }, { 4775 | key: 'likeAComment', 4776 | value: function likeAComment(commentId) { 4777 | var _this14 = this; 4778 | 4779 | if (!this.accessToken) { 4780 | alert('Login to Like'); 4781 | return Promise.reject(); 4782 | } 4783 | 4784 | var owner = this.owner, 4785 | repo = this.repo; 4786 | 4787 | var comment = this.state.comments.find(function(comment) { 4788 | return comment.id === commentId; 4789 | }); 4790 | 4791 | return _utils.http.post('/repos/' + owner + '/' + repo + '/issues/comments/' + commentId + '/reactions', { 4792 | content: 'heart' 4793 | }).then(function(reaction) { 4794 | _this14.state.commentReactions[commentId].push(reaction); 4795 | comment.reactions.heart++; 4796 | }); 4797 | } 4798 | }, { 4799 | key: 'unlikeAComment', 4800 | value: function unlikeAComment(commentId) { 4801 | if (!this.accessToken) return Promise.reject(); 4802 | 4803 | var reactions = this.state.commentReactions[commentId]; 4804 | var comment = this.state.comments.find(function(comment) { 4805 | return comment.id === commentId; 4806 | }); 4807 | var user = this.state.user; 4808 | 4809 | var index = reactions.findIndex(function(reaction) { 4810 | return reaction.user.login === user.login; 4811 | }); 4812 | 4813 | return _utils.http.delete('/reactions/' + reactions[index].id).then(function() { 4814 | reactions.splice(index, 1); 4815 | comment.reactions.heart--; 4816 | }); 4817 | } 4818 | }]); 4819 | 4820 | return Gitment; 4821 | }(); 4822 | 4823 | module.exports = Gitment; 4824 | 4825 | /***/ 4826 | }), 4827 | /* 6 */ 4828 | /***/ 4829 | (function(module, exports, __webpack_require__) { 4830 | 4831 | "use strict"; 4832 | 4833 | 4834 | Object.defineProperty(exports, "__esModule", { 4835 | value: true 4836 | }); 4837 | /** 4838 | * Modified from https://github.com/evil-icons/evil-icons 4839 | */ 4840 | 4841 | var close = exports.close = ''; 4842 | var github = exports.github = ''; 4843 | var heart = exports.heart = ''; 4844 | var spinner = exports.spinner = ''; 4845 | 4846 | /***/ 4847 | }), 4848 | /* 7 */ 4849 | /***/ 4850 | (function(module, exports, __webpack_require__) { 4851 | 4852 | "use strict"; 4853 | 4854 | 4855 | Object.defineProperty(exports, "__esModule", { 4856 | value: true 4857 | }); 4858 | exports.english = english; 4859 | exports.chinese = chinese; 4860 | exports.chineseTraditional = chineseTraditional; 4861 | exports.fromLanguageCode = fromLanguageCode; 4862 | 4863 | function english(Text) { 4864 | return Text; 4865 | } 4866 | 4867 | function chinese(Text) { 4868 | return { 4869 | 'Issue Page': '所有评论', 4870 | 'Initialize Comments': '初始化本文的评论页', 4871 | 'Loading comments...': '加载评论...', 4872 | 'Error: Comments Not Initialized': '(未开放评论)', 4873 | 'No Comment Yet': '(还没有评论)', 4874 | 'Previous': '上一页', 4875 | 'Next': '下一页', 4876 | 'Nothing to preview': '(没有预览)', 4877 | 'Loading preview...': '加载预览...', 4878 | 'Submitting...': '正在提交评论...', 4879 | 'Comment': '发送', 4880 | 'Write': '评论', 4881 | 'Preview': '预览', 4882 | 'Logging in...': '登入中...', 4883 | 'Login to Comment': '请登入以发表评论', 4884 | 'Leave a comment': '(发表评论)', 4885 | 'Login': '登入', 4886 | 'Logout': '退出' 4887 | } [Text] || Text; 4888 | } 4889 | 4890 | function chineseTraditional(Text) { 4891 | return { 4892 | 'Issue Page': '所有評論', 4893 | 'Initialize Comments': '初始化本文的評論頁', 4894 | 'Loading comments...': '加載評論...', 4895 | 'Error: Comments Not Initialized': '(未開放評論)', 4896 | 'No Comment Yet': '(還沒有評論)', 4897 | 'Previous': '上一頁', 4898 | 'Next': '下一頁', 4899 | 'Nothing to preview': '(沒有預覽)', 4900 | 'Loading preview...': '加載預覽...', 4901 | 'Submitting...': '正在提交評論...', 4902 | 'Comment': '發送', 4903 | 'Write': '評論', 4904 | 'Preview': '預覽', 4905 | 'Logging in...': '登入中...', 4906 | 'Login to Comment': '請登入以發表評論', 4907 | 'Leave a comment': '(發表評論)', 4908 | 'Login': '登入', 4909 | 'Logout': '登出' 4910 | } [Text] || Text; 4911 | } 4912 | 4913 | // @see: 4914 | // https://www.w3.org/TR/1999/REC-html401-19991224/struct/dirlang.html#h-8.1.1 4915 | // https://gist.github.com/JamieMason/3748498 4916 | function fromLanguageCode(code) { 4917 | var culture = /^([^-]+)-/; 4918 | return { 4919 | 'en': english, 4920 | 'zh': chinese, 4921 | 'zh-CN': chinese, 4922 | 'zh-CHS': chinese, 4923 | 'zh-TW': chineseTraditional, 4924 | 'zh-HK': chineseTraditional, 4925 | 'zh-CHT': chineseTraditional, 4926 | 'en-US': english 4927 | } [code] || culture.test(code) && fromLanguageCode(code.match(culture)[1]) || english; 4928 | } 4929 | 4930 | exports.default = english; 4931 | 4932 | /***/ 4933 | }) 4934 | /******/ 4935 | ]); 4936 | //# sourceMappingURL=gitmint.browser.js.map -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 捕蛇者说 Ask me anything 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 55 | --------------------------------------------------------------------------------