Skip to content

Commit

Permalink
Merge pull request #3 from enhancv/callback-once
Browse files Browse the repository at this point in the history
Execute callback only once
  • Loading branch information
ivank authored Jun 8, 2017
2 parents 73a7972 + 9b9bf45 commit c3f9d4b
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 11 deletions.
16 changes: 10 additions & 6 deletions src/PopupTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,19 @@ function popupExecute(execute, url, name, options, callback) {
var isMessageSent = false;
var interval;

function popupCallbackOnce(err, data) {
if (!isMessageSent) {
isMessageSent = true;
popupCallback(err, data);
}
}

function onMessage(message) {
var data = message ? message.data : undefined;

if (data) {
isMessageSent = true;
popupCallbackOnce(undefined, data);
window.removeEventListener('message', onMessage);
popupCallback(undefined, data);
}
}

Expand All @@ -155,14 +161,12 @@ function popupExecute(execute, url, name, options, callback) {
if (win == null || win.closed) {
setTimeout(function delayWindowClosing () {
clearInterval(interval);
if (!isMessageSent) {
popupCallback(new Error('Popup closed'));
}
popupCallbackOnce(new Error('Popup closed'));
}, 500);
}
}, 100);
} else {
popupCallback(new Error('Popup blocked'));
popupCallbackOnce(new Error('Popup blocked'));
}

return win;
Expand Down
2 changes: 1 addition & 1 deletion test/specs/centered.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ describe('Centered', function () {

browser.close();

assert.equal(browser.getText('#result'), 'Closed with Popup closed');
browser.waitUntil(() => browser.getText('#result') === 'Closed with Popup closed');
}, 3);
});
2 changes: 1 addition & 1 deletion test/specs/dimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ describe('Dimensions', function () {

browser.close();

assert.equal(browser.getText('#result'), 'Closed with Popup closed');
browser.waitUntil(() => browser.getText('#result') === 'Closed with Popup closed');
}, 3);
});
2 changes: 1 addition & 1 deletion test/specs/position.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ describe('Position', function () {

browser.close();

assert.equal(browser.getText('#result'), 'Closed with Popup closed');
browser.waitUntil(() => browser.getText('#result') === 'Closed with Popup closed');
}, 3);
});
2 changes: 1 addition & 1 deletion test/specs/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ describe('Post', function () {
assert.equal(browser.getText('h1'), '{"msg":"test post"}');
browser.close();

assert.equal(browser.getText('#result'), 'Closed with Popup closed');
browser.waitUntil(() => browser.getText('#result') === 'Closed with Popup closed');
}, 3);
});
2 changes: 1 addition & 1 deletion test/specs/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ describe('Response', function () {
return !browser.getTabIds().includes(popupWindow);
});
browser.switchTab(baseWindow);
assert.equal(browser.getText('#result'), 'Data{"msg1":"Responded Successfully"}');
browser.waitUntil(() => browser.getText('#result') === 'Data{"msg1":"Responded Successfully"}');
}, 3);
});

0 comments on commit c3f9d4b

Please sign in to comment.