зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1536170 - Replace all usage of Async.yieldingIterator with Async.yieldingForEach r=tcsc
Differential Revision: https://phabricator.services.mozilla.com/D26593 --HG-- extra : moz-landing-system : lando
This commit is contained in:
Родитель
7df18942bd
Коммит
48cd738b02
|
@ -161,30 +161,6 @@ var Async = {
|
|||
return false;
|
||||
},
|
||||
|
||||
/**
|
||||
* Turn a synchronous iterator/iterable into an async iterator that yields in
|
||||
* the same manner as Async.yieldingForEach.
|
||||
*
|
||||
* @param iterable {Iterable}
|
||||
* The iterable or iterator that should be wrapped.
|
||||
*
|
||||
* @param yieldEvery {number|object}
|
||||
* Either an existing Async.yieldState to use, or a number to provide as
|
||||
* the argument to async.yieldState.
|
||||
*/
|
||||
async* yieldingIterator(iterable, yieldEvery = 50) {
|
||||
const yieldState = typeof yieldEvery === "number" ? Async.yieldState(yieldEvery) : yieldEvery;
|
||||
|
||||
for (const item of iterable) {
|
||||
yield item;
|
||||
|
||||
if (yieldState.shouldYield()) {
|
||||
await Async.promiseYield();
|
||||
Async.checkAppReady();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
asyncQueueCaller(log) {
|
||||
return new AsyncQueueCaller(log);
|
||||
},
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
/* Any copyright is dedicated to the Public Domain.
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
const {Async} = ChromeUtils.import("resource://services-common/async.js");
|
||||
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
||||
const {sinon} = ChromeUtils.import("resource://testing-common/Sinon.jsm");
|
||||
|
||||
function makeArray(length) {
|
||||
// Start at 1 so that we can just divide by yieldEvery to get the expected
|
||||
// call count. (we exp)
|
||||
return Array.from({ length }, (v, i) => i + 1);
|
||||
}
|
||||
|
||||
// Adjust if we ever change the default
|
||||
const DEFAULT_YIELD_EVERY = 50;
|
||||
|
||||
async function checkIterYields(iterator, yieldEvery = DEFAULT_YIELD_EVERY) {
|
||||
let spy = sinon.spy(Async, "promiseYield");
|
||||
try {
|
||||
for await (let i of iterator) {
|
||||
let expectCount = Math.floor(i / yieldEvery);
|
||||
Assert.equal(spy.callCount, expectCount);
|
||||
}
|
||||
} finally {
|
||||
spy.restore();
|
||||
}
|
||||
}
|
||||
|
||||
add_task(async function testWrapIterable() {
|
||||
let iterator = Async.yieldingIterator(makeArray(DEFAULT_YIELD_EVERY * 2));
|
||||
await checkIterYields(iterator);
|
||||
});
|
||||
|
||||
add_task(async function testWrapIterator() {
|
||||
let innerIter = makeArray(DEFAULT_YIELD_EVERY * 2)[Symbol.iterator]();
|
||||
await checkIterYields(Async.yieldingIterator(innerIter));
|
||||
});
|
||||
|
||||
add_task(async function testNumberArgument() {
|
||||
const yieldEvery = 10;
|
||||
let iterator = Async.yieldingIterator(makeArray(yieldEvery * 2), yieldEvery);
|
||||
await checkIterYields(iterator, yieldEvery);
|
||||
});
|
||||
|
||||
add_task(async function testCustomJankYielder() {
|
||||
let fakeJankYielderSpy = sinon.spy();
|
||||
let iter = Async.yieldingIterator(makeArray(10), fakeJankYielderSpy);
|
||||
for await (let i of iter) {
|
||||
Assert.equal(fakeJankYielderSpy.callCount, i);
|
||||
}
|
||||
});
|
|
@ -43,7 +43,6 @@ tags = remote-settings blocklist
|
|||
|
||||
[test_async_chain.js]
|
||||
[test_async_foreach.js]
|
||||
[test_async_iterator.js]
|
||||
|
||||
[test_hawkclient.js]
|
||||
skip-if = os == "android"
|
||||
|
|
|
@ -180,7 +180,7 @@ HistoryStore.prototype = {
|
|||
let failed = [];
|
||||
let toAdd = [];
|
||||
let toRemove = [];
|
||||
for await (let record of Async.yieldingIterator(records)) {
|
||||
await Async.yieldingForEach(records, async (record) => {
|
||||
if (record.deleted) {
|
||||
toRemove.push(record);
|
||||
} else {
|
||||
|
@ -198,7 +198,7 @@ HistoryStore.prototype = {
|
|||
failed.push(record.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
if (toAdd.length || toRemove.length) {
|
||||
// We want to notify history observers that a batch operation is underway
|
||||
// so they don't do lots of work for each incoming record.
|
||||
|
@ -221,7 +221,7 @@ HistoryStore.prototype = {
|
|||
// but the error semantics are tricky - a single "bad" entry will cause
|
||||
// an exception before anything is removed. So we do remove them one at
|
||||
// a time.
|
||||
for await (let record of Async.yieldingIterator(toRemove)) {
|
||||
await Async.yieldingForEach(toRemove, async (record) => {
|
||||
try {
|
||||
await this.remove(record);
|
||||
} catch (ex) {
|
||||
|
@ -232,7 +232,7 @@ HistoryStore.prototype = {
|
|||
this._log.trace("The record that failed", record);
|
||||
failed.push(record.id);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
for (let chunk of this._generateChunks(toAdd)) {
|
||||
// Per bug 1415560, we ignore any exceptions returned by insertMany
|
||||
|
|
|
@ -128,9 +128,6 @@ const DEFAULT_MAX_FRECENCIES_TO_RECALCULATE = 400;
|
|||
|
||||
// Use a shared jankYielder in these functions
|
||||
XPCOMUtils.defineLazyGetter(this, "yieldState", () => Async.yieldState());
|
||||
function yieldingIterator(collection) {
|
||||
return Async.yieldingIterator(collection, yieldState);
|
||||
}
|
||||
|
||||
/** Adapts a `Log.jsm` logger to a `mozISyncedBookmarksMirrorLogger`. */
|
||||
class MirrorLoggerAdapter {
|
||||
|
@ -404,7 +401,7 @@ class SyncedBookmarksMirror {
|
|||
await this.db.executeBeforeShutdown(
|
||||
"SyncedBookmarksMirror: store",
|
||||
db => db.executeTransaction(async () => {
|
||||
for await (let record of yieldingIterator(records)) {
|
||||
await Async.yieldingForEach(records, async (record) => {
|
||||
let guid = PlacesSyncUtils.bookmarks.recordIdToGuid(record.id);
|
||||
if (guid == PlacesUtils.bookmarks.rootGuid) {
|
||||
// The engine should hard DELETE Places roots from the server.
|
||||
|
@ -414,32 +411,32 @@ class SyncedBookmarksMirror {
|
|||
switch (record.type) {
|
||||
case "bookmark":
|
||||
await this.storeRemoteBookmark(record, options);
|
||||
continue;
|
||||
return;
|
||||
|
||||
case "query":
|
||||
await this.storeRemoteQuery(record, options);
|
||||
continue;
|
||||
return;
|
||||
|
||||
case "folder":
|
||||
await this.storeRemoteFolder(record, options);
|
||||
continue;
|
||||
return;
|
||||
|
||||
case "livemark":
|
||||
await this.storeRemoteLivemark(record, options);
|
||||
continue;
|
||||
return;
|
||||
|
||||
case "separator":
|
||||
await this.storeRemoteSeparator(record, options);
|
||||
continue;
|
||||
return;
|
||||
|
||||
default:
|
||||
if (record.deleted) {
|
||||
await this.storeRemoteTombstone(record, options);
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
}
|
||||
MirrorLog.warn("Ignoring record with unknown type", record.type);
|
||||
}
|
||||
}, yieldState);
|
||||
}
|
||||
));
|
||||
}
|
||||
|
@ -922,7 +919,7 @@ class SyncedBookmarksMirror {
|
|||
ORDER BY guid`,
|
||||
{ rootGuid: PlacesUtils.bookmarks.rootGuid });
|
||||
|
||||
for await (let row of yieldingIterator(orphanRows)) {
|
||||
await Async.yieldingForEach(orphanRows, row => {
|
||||
let guid = row.getResultByName("guid");
|
||||
let missingParent = row.getResultByName("missingParent");
|
||||
if (missingParent) {
|
||||
|
@ -936,7 +933,7 @@ class SyncedBookmarksMirror {
|
|||
if (parentWithGaps) {
|
||||
infos.parentsWithGaps.push(guid);
|
||||
}
|
||||
}
|
||||
}, yieldState);
|
||||
|
||||
return infos;
|
||||
}
|
||||
|
@ -999,7 +996,7 @@ class SyncedBookmarksMirror {
|
|||
{ syncStatus: PlacesUtils.bookmarks.SYNC_STATUS.NORMAL,
|
||||
rootGuid: PlacesUtils.bookmarks.rootGuid });
|
||||
|
||||
for await (let row of yieldingIterator(problemRows)) {
|
||||
await Async.yieldingForEach(problemRows, row => {
|
||||
let guid = row.getResultByName("guid");
|
||||
let missingLocal = row.getResultByName("missingLocal");
|
||||
if (missingLocal) {
|
||||
|
@ -1013,7 +1010,7 @@ class SyncedBookmarksMirror {
|
|||
if (wrongSyncStatus) {
|
||||
infos.wrongSyncStatus.push(guid);
|
||||
}
|
||||
}
|
||||
}, yieldState);
|
||||
|
||||
return infos;
|
||||
}
|
||||
|
@ -1091,7 +1088,7 @@ class SyncedBookmarksMirror {
|
|||
SELECT parentId, guid FROM structureToUpload
|
||||
ORDER BY parentId, position`);
|
||||
|
||||
for await (let row of yieldingIterator(childGuidRows)) {
|
||||
await Async.yieldingForEach(childGuidRows, row => {
|
||||
let localParentId = row.getResultByName("parentId");
|
||||
let childRecordId = PlacesSyncUtils.bookmarks.guidToRecordId(
|
||||
row.getResultByName("guid"));
|
||||
|
@ -1101,12 +1098,12 @@ class SyncedBookmarksMirror {
|
|||
} else {
|
||||
childRecordIdsByLocalParentId.set(localParentId, [childRecordId]);
|
||||
}
|
||||
}
|
||||
}, yieldState);
|
||||
|
||||
let tagRows = await this.db.execute(`
|
||||
SELECT id, tag FROM tagsToUpload`);
|
||||
|
||||
for await (let row of yieldingIterator(tagRows)) {
|
||||
await Async.yieldingForEach(tagRows, row => {
|
||||
let localId = row.getResultByName("id");
|
||||
let tag = row.getResultByName("tag");
|
||||
let tags = tagsByLocalId.get(localId);
|
||||
|
@ -1115,7 +1112,7 @@ class SyncedBookmarksMirror {
|
|||
} else {
|
||||
tagsByLocalId.set(localId, [tag]);
|
||||
}
|
||||
}
|
||||
}, yieldState);
|
||||
|
||||
let itemRows = await this.db.execute(`
|
||||
SELECT id, syncChangeCounter, guid, isDeleted, type, isQuery,
|
||||
|
@ -1124,7 +1121,7 @@ class SyncedBookmarksMirror {
|
|||
IFNULL(parentTitle, "") AS parentTitle, dateAdded
|
||||
FROM itemsToUpload`);
|
||||
|
||||
for await (let row of yieldingIterator(itemRows)) {
|
||||
await Async.yieldingForEach(itemRows, row => {
|
||||
let syncChangeCounter = row.getResultByName("syncChangeCounter");
|
||||
|
||||
let guid = row.getResultByName("guid");
|
||||
|
@ -1137,7 +1134,7 @@ class SyncedBookmarksMirror {
|
|||
id: recordId,
|
||||
deleted: true,
|
||||
});
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
|
||||
let parentGuid = row.getResultByName("parentGuid");
|
||||
|
@ -1172,7 +1169,7 @@ class SyncedBookmarksMirror {
|
|||
};
|
||||
changeRecords[recordId] = new BookmarkChangeRecord(
|
||||
syncChangeCounter, queryCleartext);
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
|
||||
let bookmarkCleartext = {
|
||||
|
@ -1196,7 +1193,7 @@ class SyncedBookmarksMirror {
|
|||
}
|
||||
changeRecords[recordId] = new BookmarkChangeRecord(
|
||||
syncChangeCounter, bookmarkCleartext);
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
|
||||
case PlacesUtils.bookmarks.TYPE_FOLDER: {
|
||||
|
@ -1214,7 +1211,7 @@ class SyncedBookmarksMirror {
|
|||
folderCleartext.children = childRecordIds || [];
|
||||
changeRecords[recordId] = new BookmarkChangeRecord(
|
||||
syncChangeCounter, folderCleartext);
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
|
||||
case PlacesUtils.bookmarks.TYPE_SEPARATOR: {
|
||||
|
@ -1230,13 +1227,13 @@ class SyncedBookmarksMirror {
|
|||
};
|
||||
changeRecords[recordId] = new BookmarkChangeRecord(
|
||||
syncChangeCounter, separatorCleartext);
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
|
||||
default:
|
||||
throw new TypeError("Can't create record for unknown Places item");
|
||||
}
|
||||
}
|
||||
}, yieldState);
|
||||
|
||||
return changeRecords;
|
||||
}
|
||||
|
@ -2133,7 +2130,7 @@ class BookmarkObserverRecorder {
|
|||
FROM itemsRemoved v
|
||||
LEFT JOIN moz_places h ON h.id = v.placeId
|
||||
ORDER BY v.level DESC, v.parentId, v.position`);
|
||||
for await (let row of yieldingIterator(removedItemRows)) {
|
||||
await Async.yieldingForEach(removedItemRows, row => {
|
||||
let info = {
|
||||
id: row.getResultByName("id"),
|
||||
parentId: row.getResultByName("parentId"),
|
||||
|
@ -2145,7 +2142,7 @@ class BookmarkObserverRecorder {
|
|||
isUntagging: row.getResultByName("isUntagging"),
|
||||
};
|
||||
this.noteItemRemoved(info);
|
||||
}
|
||||
}, yieldState);
|
||||
|
||||
MirrorLog.trace("Recording observer notifications for changed GUIDs");
|
||||
let changedGuidRows = await this.db.execute(`
|
||||
|
@ -2155,7 +2152,7 @@ class BookmarkObserverRecorder {
|
|||
JOIN moz_bookmarks b ON b.id = c.itemId
|
||||
JOIN moz_bookmarks p ON p.id = b.parent
|
||||
ORDER BY c.level, p.id, b.position`);
|
||||
for await (let row of yieldingIterator(changedGuidRows)) {
|
||||
await Async.yieldingForEach(changedGuidRows, row => {
|
||||
let info = {
|
||||
id: row.getResultByName("id"),
|
||||
lastModified: row.getResultByName("lastModified"),
|
||||
|
@ -2166,7 +2163,7 @@ class BookmarkObserverRecorder {
|
|||
parentGuid: row.getResultByName("parentGuid"),
|
||||
};
|
||||
this.noteGuidChanged(info);
|
||||
}
|
||||
}, yieldState);
|
||||
|
||||
MirrorLog.trace("Recording observer notifications for new items");
|
||||
let newItemRows = await this.db.execute(`
|
||||
|
@ -2178,7 +2175,7 @@ class BookmarkObserverRecorder {
|
|||
JOIN moz_bookmarks p ON p.id = b.parent
|
||||
LEFT JOIN moz_places h ON h.id = b.fk
|
||||
ORDER BY n.level, p.id, b.position`);
|
||||
for await (let row of yieldingIterator(newItemRows)) {
|
||||
await Async.yieldingForEach(newItemRows, row => {
|
||||
let info = {
|
||||
id: row.getResultByName("id"),
|
||||
parentId: row.getResultByName("parentId"),
|
||||
|
@ -2192,7 +2189,7 @@ class BookmarkObserverRecorder {
|
|||
isTagging: row.getResultByName("isTagging"),
|
||||
};
|
||||
this.noteItemAdded(info);
|
||||
}
|
||||
}, yieldState);
|
||||
|
||||
MirrorLog.trace("Recording observer notifications for moved items");
|
||||
let movedItemRows = await this.db.execute(`
|
||||
|
@ -2204,7 +2201,7 @@ class BookmarkObserverRecorder {
|
|||
JOIN moz_bookmarks p ON p.id = b.parent
|
||||
LEFT JOIN moz_places h ON h.id = b.fk
|
||||
ORDER BY c.level, newParentId, newPosition`);
|
||||
for await (let row of yieldingIterator(movedItemRows)) {
|
||||
await Async.yieldingForEach(movedItemRows, row => {
|
||||
let info = {
|
||||
id: row.getResultByName("id"),
|
||||
guid: row.getResultByName("guid"),
|
||||
|
@ -2218,7 +2215,7 @@ class BookmarkObserverRecorder {
|
|||
urlHref: row.getResultByName("url"),
|
||||
};
|
||||
this.noteItemMoved(info);
|
||||
}
|
||||
}, yieldState);
|
||||
|
||||
MirrorLog.trace("Recording observer notifications for changed items");
|
||||
let changedItemRows = await this.db.execute(`
|
||||
|
@ -2233,7 +2230,7 @@ class BookmarkObserverRecorder {
|
|||
LEFT JOIN moz_places h ON h.id = b.fk
|
||||
LEFT JOIN moz_places i ON i.id = c.oldPlaceId
|
||||
ORDER BY c.level, p.id, b.position`);
|
||||
for await (let row of yieldingIterator(changedItemRows)) {
|
||||
await Async.yieldingForEach(changedItemRows, row => {
|
||||
let info = {
|
||||
id: row.getResultByName("id"),
|
||||
guid: row.getResultByName("guid"),
|
||||
|
@ -2247,7 +2244,7 @@ class BookmarkObserverRecorder {
|
|||
parentGuid: row.getResultByName("parentGuid"),
|
||||
};
|
||||
this.noteItemChanged(info);
|
||||
}
|
||||
}, yieldState);
|
||||
|
||||
MirrorLog.trace("Recording notifications for changed keywords");
|
||||
let keywordsChangedRows = await this.db.execute(`
|
||||
|
@ -2334,18 +2331,18 @@ class BookmarkObserverRecorder {
|
|||
for (let observer of observers) {
|
||||
this.notifyObserver(observer, "onBeginUpdateBatch");
|
||||
}
|
||||
for await (let info of yieldingIterator(this.bookmarkObserverNotifications)) {
|
||||
await Async.yieldingForEach(this.bookmarkObserverNotifications, info => {
|
||||
if (info instanceof PlacesEvent) {
|
||||
PlacesObservers.notifyListeners([info]);
|
||||
} else {
|
||||
for (let observer of observers) {
|
||||
if (info.isTagging && observer.skipTags) {
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
this.notifyObserver(observer, info.name, info.args);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, yieldState);
|
||||
for (let observer of observers) {
|
||||
this.notifyObserver(observer, "onEndUpdateBatch");
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче