Simplify waitFor code (#92)

This commit is contained in:
Pavel Feldman 2017-07-18 12:16:19 -07:00 committed by Andrey Lushnikov
parent 5ca92ffb71
commit bef9982687

View File

@ -178,25 +178,28 @@ class FrameManager extends EventEmitter {
* @return {!Promise<undefined>} * @return {!Promise<undefined>}
*/ */
async _waitForInFrame(selector, frame) { async _waitForInFrame(selector, frame) {
let code = selector => {
let promise = new Promise(fulfill => { function code(selector) {
if (document.querySelector(selector)) { if (document.querySelector(selector))
fulfill(); return Promise.resolve();
return;
} let callback;
new MutationObserver((mutations, observer) => { const result = new Promise(fulfill => callback = fulfill);
const mo = new MutationObserver((mutations, observer) => {
if (document.querySelector(selector)) { if (document.querySelector(selector)) {
observer.disconnect(); observer.disconnect();
fulfill(); callback();
return; return;
} }
}).observe(document.documentElement, { });
mo.observe(document.documentElement, {
childList: true, childList: true,
subtree: true subtree: true
}); });
}); return result;
return promise; }
};
let contextId = undefined; let contextId = undefined;
if (!frame.isMainFrame()) { if (!frame.isMainFrame()) {
contextId = this._frameIdToExecutionContextId.get(frame._id); contextId = this._frameIdToExecutionContextId.get(frame._id);