diff --git a/chrome/content/zotero/bindings/attachmentbox.xml b/chrome/content/zotero/bindings/attachmentbox.xml index 2d6b29513c..b032eb02ae 100644 --- a/chrome/content/zotero/bindings/attachmentbox.xml +++ b/chrome/content/zotero/bindings/attachmentbox.xml @@ -142,7 +142,7 @@ Zotero.spawn(function* () { Zotero.debug('Refreshing attachment box'); - yield [this.item.loadItemData(), this.item.loadNote()]; + yield Zotero.Promise.all([this.item.loadItemData(), this.item.loadNote()]); var attachmentBox = document.getAnonymousNodes(this)[0]; var title = this._id('title'); diff --git a/chrome/content/zotero/itemPane.js b/chrome/content/zotero/itemPane.js index 79c2de5cc2..2a4703af22 100644 --- a/chrome/content/zotero/itemPane.js +++ b/chrome/content/zotero/itemPane.js @@ -148,7 +148,7 @@ var ZoteroItemPane = new function() { box.mode = 'edit'; } - yield [item.loadItemData(), item.loadCreators()]; + yield Zotero.Promise.all([item.loadItemData(), item.loadCreators()]); box.item = item; }); diff --git a/chrome/content/zotero/locateMenu.js b/chrome/content/zotero/locateMenu.js index 1eca96ec9d..f85b91f51d 100644 --- a/chrome/content/zotero/locateMenu.js +++ b/chrome/content/zotero/locateMenu.js @@ -380,7 +380,7 @@ var Zotero_LocateMenu = new function() { return _getURL(item).then((val) => val !== false); } this.handleItems = Zotero.Promise.coroutine(function* (items, event) { - var urls = yield [_getURL(item) for each(item in items)]; + var urls = yield Zotero.Promise.all([_getURL(item) for each(item in items)]); ZoteroPane_Local.loadURI([url for each(url in urls) if(url)], event); }); diff --git a/chrome/content/zotero/xpcom/data/dataObject.js b/chrome/content/zotero/xpcom/data/dataObject.js index f20a9fb52d..9d0f6d78fe 100644 --- a/chrome/content/zotero/xpcom/data/dataObject.js +++ b/chrome/content/zotero/xpcom/data/dataObject.js @@ -288,10 +288,10 @@ Zotero.DataObject.prototype._getLinkedObject = Zotero.Promise.coroutine(function var predicate = Zotero.Relations.linkedObjectPredicate; var uri = Zotero.URI['get' + this._ObjectType + 'URI'](this); - var links = yield [ + var links = yield Zotero.Promise.all([ Zotero.Relations.getSubject(false, predicate, uri), Zotero.Relations.getObject(uri, predicate, false) - ]; + ]); links = links[0].concat(links[1]); if (!links.length) { diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index b2c8c16c92..a185858a4c 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -1080,7 +1080,7 @@ Zotero.Item.prototype.addRelatedItem = Zotero.Promise.coroutine(function* (itemI return false; } - var current = yield this._getRelatedItems(true); + var current = this._getRelatedItems(true); if (current.indexOf(itemID) != -1) { Zotero.debug("Item " + this.id + " already related to item " + itemID + " in Zotero.Item.addItem()"); @@ -1115,7 +1115,7 @@ Zotero.Item.prototype.removeRelatedItem = Zotero.Promise.coroutine(function* (it } itemID = parsedInt; - var current = yield this._getRelatedItems(true); + var current = this._getRelatedItems(true); var index = current.indexOf(itemID); if (index == -1) { @@ -1630,7 +1630,7 @@ Zotero.Item.prototype.save = Zotero.Promise.coroutine(function* (options) { if (this._changed.relatedItems) { var removed = []; var newids = []; - var currentIDs = yield this._getRelatedItems(true); + var currentIDs = this._getRelatedItems(true); for each(var id in currentIDs) { newids.push(id); @@ -1655,7 +1655,7 @@ Zotero.Item.prototype.save = Zotero.Promise.coroutine(function* (options) { if (this._changed.relatedItems) { var removed = []; var newids = []; - var currentIDs = yield this._getRelatedItems(true); + var currentIDs = this._getRelatedItems(true); for each(var id in this._previousData.related) { if (currentIDs.indexOf(id) == -1) { @@ -4195,7 +4195,7 @@ Zotero.Item.prototype.toJSON = Zotero.Promise.coroutine(function* (options, patc // Tags obj.tags = []; yield this.loadTags() - var tags = yield this.getTags(); + var tags = this.getTags(); for (let i=0; i 0 && !rtraceline.test(lines[i])) && lines[i] !== "From previous event:") ) { @@ -967,8 +967,8 @@ function originatesFromRejection(e) { } function isError(obj) { - // Added by Zotero - return obj.message && obj.stack; + // Added by Zotero + return obj.message && obj.stack; return obj instanceof Error; } @@ -1458,7 +1458,7 @@ PromiseSpawn.prototype._continue = function PromiseSpawn$_continue(result) { ); global.debug(e); global.debug(e.stack); - + this._throw(new TypeError("A value was yielded that could not be treated as a promise")); return; } @@ -1508,11 +1508,6 @@ Promise.coroutine.addYieldHandler = function(fn) { yieldHandlers.push(fn); }; -// Added by Zotero to allow yielding arrays of promises in coroutines -Promise.coroutine.addYieldHandler(function(yieldedValue) { - if (Array.isArray(yieldedValue)) return Promise.all(yieldedValue); -}); - Promise.spawn = function Promise$Spawn(generatorFunction) { deprecated("Promise.spawn is deprecated. Use Promise.coroutine instead."); if (typeof generatorFunction !== "function") { @@ -1919,6 +1914,14 @@ Promise.prototype._progress = function Promise$_progress(progressValue) { }; +Promise.prototype._clearFirstHandlerData$Base = +Promise.prototype._clearFirstHandlerData; +Promise.prototype._clearFirstHandlerData = +function Promise$_clearFirstHandlerData() { + this._clearFirstHandlerData$Base(); + this._progressHandler0 = void 0; +}; + Promise.prototype._progressHandlerAt = function Promise$_progressHandlerAt(index) { return index === 0 @@ -2069,11 +2072,24 @@ function Promise(resolver) { if (resolver !== INTERNAL) this._resolveFromResolver(resolver); } +function returnFirstElement(elements) { + return elements[0]; +} + Promise.prototype.bind = function Promise$bind(thisArg) { + var maybePromise = cast(thisArg, void 0); var ret = new Promise(INTERNAL); - ret._follow(this); + if (maybePromise instanceof Promise) { + var binder = maybePromise.then(function(thisArg) { + ret._setBoundTo(thisArg); + }); + var p = Promise.all([this, binder]).then(returnFirstElement); + ret._follow(p); + } else { + ret._follow(this); + ret._setBoundTo(thisArg); + } ret._propagateFrom(this, 2 | 1); - ret._setBoundTo(thisArg); return ret; }; @@ -2098,8 +2114,7 @@ function Promise$catch(fn) { + "or a filter function"); this._attachExtraTrace(catchFilterTypeError); - async.invoke(this._reject, this, catchFilterTypeError); - return; + return Promise.reject(catchFilterTypeError); } } catchInstances.length = j; @@ -2232,10 +2247,19 @@ Promise.defer = Promise.pending = function Promise$Defer() { }; Promise.bind = function Promise$Bind(thisArg) { + var maybePromise = cast(thisArg, void 0); var ret = new Promise(INTERNAL); ret._setTrace(void 0); - ret._setFulfilled(); - ret._setBoundTo(thisArg); + + if (maybePromise instanceof Promise) { + var p = maybePromise.then(function(thisArg) { + ret._setBoundTo(thisArg); + }); + ret._follow(p); + } else { + ret._setBoundTo(thisArg); + ret._setFulfilled(); + } return ret; }; @@ -2535,7 +2559,7 @@ function Promise$_proxyPromiseArray(promiseArray, index) { Promise.prototype._proxyPromise = function Promise$_proxyPromise(promise) { promise._setProxied(); - this._setProxyHandlers(promise, -1); + this._setProxyHandlers(promise, -15); }; Promise.prototype._setBoundTo = function Promise$_setBoundTo(obj) { @@ -2814,7 +2838,7 @@ Promise.prototype._settlePromiseAt = function Promise$_settlePromiseAt(index) { } } - if (index >= 256) { + if (index >= 4) { this._queueGC(); } }; @@ -2850,14 +2874,23 @@ Promise.prototype._queueGC = function Promise$_queueGC() { }; Promise.prototype._gc = function Promise$gc() { - var len = this._length() * 5; + var len = this._length() * 5 - 5; for (var i = 0; i < len; i++) { delete this[i]; } + this._clearFirstHandlerData(); this._setLength(0); this._unsetGcQueued(); }; +Promise.prototype._clearFirstHandlerData = +function Promise$_clearFirstHandlerData() { + this._fulfillmentHandler0 = void 0; + this._rejectionHandler0 = void 0; + this._promise0 = void 0; + this._receiver0 = void 0; +}; + Promise.prototype._queueSettleAt = function Promise$_queueSettleAt(index) { if (this._isRejectionUnhandled()) this._unsetRejectionIsUnhandled(); async.invoke(this._settlePromiseAt, this, index); @@ -3537,26 +3570,26 @@ function makeNodePromisifiedEval(callback, receiver, originalName, fn, suffix) { var ret; if (typeof callback === "string") { ret = " \n\ - this.method(args, fn); \n\ + this.method({{args}}, fn); \n\ break; \n\ ".replace(".method", generatePropertyAccess(callback)); } else if (receiver === THIS) { ret = " \n\ - callback.call(this, args, fn); \n\ + callback.call(this, {{args}}, fn); \n\ break; \n\ "; } else if (receiver !== void 0) { ret = " \n\ - callback.call(receiver, args, fn); \n\ + callback.call(receiver, {{args}}, fn); \n\ break; \n\ "; } else { ret = " \n\ - callback(args, fn); \n\ + callback({{args}}, fn); \n\ break; \n\ "; } - return ret.replace("args", args).replace(", ", comma); + return ret.replace("{{args}}", args).replace(", ", comma); } function generateArgumentSwitchCase() { @@ -4258,7 +4291,7 @@ else if ((typeof MutationObserver !== "undefined" && }); return function Promise$_Scheduler(fn) { queuedFn = fn; - div.setAttribute("class", "foo"); + div.classList.toggle("foo"); }; })(); @@ -4747,7 +4780,7 @@ var _setTimeout = function(fn, ms) { var arg2 = len >= 5 ? arguments[4] : void 0; setTimeout(function() { fn(arg0, arg1, arg2); - }, ms); + }, ms|0); }; module.exports = function(Promise, INTERNAL, cast) { @@ -4854,13 +4887,23 @@ module.exports = function (Promise, apiRejection, cast) { setTimeout(function(){throw e;}, 0); } + function castPreservingDisposable(thenable) { + var maybePromise = cast(thenable, void 0); + if (maybePromise !== thenable && + typeof thenable._isDisposable === "function" && + typeof thenable._getDisposer === "function" && + thenable._isDisposable()) { + maybePromise._setDisposable(thenable._getDisposer()); + } + return maybePromise; + } function dispose(resources, inspection) { var i = 0; var len = resources.length; var ret = Promise.defer(); function iterator() { if (i >= len) return ret.resolve(); - var maybePromise = cast(resources[i++], void 0); + var maybePromise = castPreservingDisposable(resources[i++]); if (maybePromise instanceof Promise && maybePromise._isDisposable()) { try { @@ -4923,6 +4966,12 @@ module.exports = function (Promise, apiRejection, cast) { return ret; }; + Disposer.isDisposer = function Disposer$isDisposer(d) { + return (d != null && + typeof d.resource === "function" && + typeof d.tryDispose === "function"); + }; + function FunctionDisposer(fn, promise) { this.constructor$(fn, promise); } @@ -4943,7 +4992,7 @@ module.exports = function (Promise, apiRejection, cast) { var resources = new Array(len); for (var i = 0; i < len; ++i) { var resource = arguments[i]; - if (resource instanceof Disposer) { + if (Disposer.isDisposer(resource)) { var disposer = resource; resource = resource.promise(); resource._setDisposable(disposer); @@ -5258,4 +5307,4 @@ module.exports = ret; },{"./es5.js":12}]},{},[3]) (3) }); -; \ No newline at end of file +; ;if (typeof window !== 'undefined' && window !== null) { window.P = window.Promise; } else if (typeof self !== 'undefined' && self !== null) { self.P = self.Promise; } \ No newline at end of file