Skip to content

Commit

Permalink
environment promises wrapped with vow (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
jifeon authored and maZahaca committed Oct 18, 2017
1 parent fd59786 commit 3e07cfa
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 35 deletions.
6 changes: 3 additions & 3 deletions lib/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ Actions.prototype = {
return vow.reject(new Error('Unknown action type: ' + action.type));
}

actionPromise = actionInstance.perform();
actionPromise = vow.resolve(actionInstance.perform());

if (action.transform) {
actionPromise = actionPromise.then(result => this._parser.transform(result, action.transform));
Expand Down Expand Up @@ -272,9 +272,9 @@ Actions.prototype = {
*/
scroll: function(interval) {
debug('scroll %s px', interval);
return this._env.evaluateJs(interval, /* @covignore */ function(interval) {
return vow.resolve(this._env.evaluateJs(interval, /* @covignore */ function(interval) {
document.body.scrollTop += interval;
});
}));
}
};

Expand Down
17 changes: 8 additions & 9 deletions lib/Paginator.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,9 @@ Paginator.prototype = {
*/
_paginateScroll: function () {
var previousHeight;
return this._env
.evaluateJs(/* @covignore */ function () {
return vow.resolve(this._env.evaluateJs(/* @covignore */ function () {
return document.body.scrollHeight;
})
}))
.then(function (height) {
previousHeight = height;
})
Expand Down Expand Up @@ -213,12 +212,12 @@ Paginator.prototype = {
* @private
*/
_getRealNextPageIndex: function () {
return this._env.evaluateJs(this._scope, /* @covignore */ function (selector) {
return vow.resolve(this._env.evaluateJs(this._scope, /* @covignore */ function (selector) {
var nodes = Sizzle(selector);
return nodes.map(function (node) {
return parseInt(node.textContent);
});
}).then(function (pages) {
})).then(function (pages) {
var pageIndex;
if (!Array.isArray(pages) || (pageIndex = pages.indexOf(this._currentPage + 1)) === -1) {
throw new Error('Cannot detect page index for pagination');
Expand All @@ -241,7 +240,7 @@ Paginator.prototype = {
selector = this._scope + ':eq(' + realPageIndex + ')';
}, this)
.then(function () {
return this._env.evaluateJs(this._pageScope, this._getPaginatePageHtml);
return vow.resolve(this._env.evaluateJs(this._pageScope, this._getPaginatePageHtml));
}, this)
.then(function (html) {
previousPageHtml = html;
Expand All @@ -250,14 +249,14 @@ Paginator.prototype = {
var paginationPromise;
switch (this._strategy) {
case this.STRATEGY.NEW_PAGE:
paginationPromise = this._env.waitForPage(this._timeout);
paginationPromise = vow.resolve(this._env.waitForPage(this._timeout));
break;
case this.STRATEGY.AJAX:
paginationPromise = this._hasPageDataChanged(previousPageHtml);
break;
case null:
paginationPromise = vow.any([
this._env.waitForPage(this._timeout).then(function () {
vow.resolve(this._env.waitForPage(this._timeout)).then(function () {
this._strategy = this.STRATEGY.NEW_PAGE;
debug('Set strategy to %s', this._strategy);
}, this),
Expand Down Expand Up @@ -293,7 +292,7 @@ Paginator.prototype = {
_paginateShowMore: function () {
var selector = this._scope;
var previousPageHtml;
return this._env.evaluateJs(this._pageScope, this._getPaginatePageHtml)
return vow.resolve(this._env.evaluateJs(this._pageScope, this._getPaginatePageHtml))
.then(html => {
previousPageHtml = html;
})
Expand Down
33 changes: 14 additions & 19 deletions lib/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Parser.prototype = {
this._preActions = options.actions || null;
this._paginator && this._paginator.reset();
var results;
return this._env.prepare()
return vow.resolve(this._env.prepare())
.then(() => {
if (this._preActions) {
return this._actions.performActions(this._preActions);
Expand All @@ -169,9 +169,8 @@ Parser.prototype = {
}
})
.then(
() => this._env.tearDown(),
e => this._env
.snapshot('error')
() => vow.resolve(this._env.tearDown()),
e => vow.resolve(this._env.snapshot('error'))
.always(this._env.tearDown, this._env)
.then(() => {
throw e
Expand All @@ -193,16 +192,14 @@ Parser.prototype = {
const milestones = stages.milestones;
const edgeCases = stages.edgeCases;

return this._env
.prepare()
return vow.resolve(this._env.prepare())
.then(() => milestones.reduce((promise, milestone) => {
return promise.then(() => this.passMilestone(milestone, edgeCases));
}, Promise.resolve()))
.then(
() => this._env.tearDown(),
e => this._env
.snapshot('error')
.always(() => this._env.tearDown())
() => vow.resolve(this._env.tearDown()),
e => vow.resolve(this._env.snapshot('error'))
.always(() => vow.resolve(this._env.tearDown()))
.then(() => {
throw e
})
Expand Down Expand Up @@ -576,10 +573,9 @@ Parser.prototype = {
offset = offset || 0;
var maxItems = rule.maxItems || null;
var collection = rule.collection[0];
return this._env
.evaluateJs(this._getSelector(), /* @covignore */ function(selector) {
return vow.resolve(this._env.evaluateJs(this._getSelector(), /* @covignore */ function(selector) {
return Sizzle(selector).length;
})
}))
.then((nodesCount) => {
debug('parsing %s nodes', nodesCount);
if (!nodesCount) {
Expand Down Expand Up @@ -639,7 +635,7 @@ Parser.prototype = {
.then(function(results) {
if (this.clearDom) {
debug('clear parsed dom for %s', selector);
return this._env.evaluateJs(selector, /* @covignore */ function(selector) {
return vow.resolve(this._env.evaluateJs(selector, /* @covignore */ function(selector) {
var parsedElement = Sizzle(selector)[0];
if (!parsedElement) {
return;
Expand All @@ -649,7 +645,7 @@ Parser.prototype = {
//parsedElement.setAttribute('class', '');
parsedElement.style.height = boundingRect.height + 'px';
parsedElement.style.width = boundingRect.width + 'px';
}).then(function() {
})).then(function() {
return results;
});
}
Expand All @@ -660,8 +656,7 @@ Parser.prototype = {
_injectBrowserRule: function(rule, offset, actionsResults) {
debug('._injectBrowserRule()');
let internalGooseResults, internalGooseError;
return this._env
.injectBrowserEnv()
return vow.resolve(this._env.injectBrowserEnv())
.then(() => this._env.evaluateJs(rule, offset, this._scopes, function(rule, offset, scopes) {
__gooseParse(rule, offset, scopes);
}))
Expand Down Expand Up @@ -747,7 +742,7 @@ Parser.prototype = {
filter.value = rule.prop;
}
debug('._parseSimpleRule() has called for selector %s with filter %o', selector, filter);
return this._env.evaluateJs(selector, filter, /* @covignore */ function(selector, filter) {
return vow.resolve(this._env.evaluateJs(selector, filter, /* @covignore */ function(selector, filter) {
var nodes = Sizzle(selector);
return nodes.map(function(node) {
switch (filter.type) {
Expand All @@ -768,7 +763,7 @@ Parser.prototype = {
return node.textContent;
}
});
}).then(function(results) {
})).then(function(results) {
if (!results) {
throw new Error('Error during querying selector: ' + selector);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/tools/wait.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const Promise = require('vow').Promise;
const vow = require('vow');
const Promise = vow.Promise;

/**
* Wait until function evalFunction expected in checkerFunction result
Expand Down Expand Up @@ -39,8 +40,7 @@ module.exports = function wait (env, evalFunction, checkerFunction, args, timeou
const evalArgs = args.slice(0);
evalArgs.push(evalFunction);
var intervalId = setInterval(() => {
env.evaluateJs
.apply(env, evalArgs)
vow.resolve(env.evaluateJs.apply(env, evalArgs))
.then(function() {
if (checkerFunction.apply(null, arguments)) {
clearTimeout(timeoutId);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "goose-parser",
"version": "0.3.0-alpha.2",
"version": "0.3.0-alpha.3",
"description": "PhantomJS/Browser lib which allows to parse a webpage",
"author": {
"name": "Andrew Reddikh",
Expand Down

0 comments on commit 3e07cfa

Please sign in to comment.