1
0
Fork 0
mirror of https://github.com/teampoltergeist/poltergeist.git synced 2022-11-09 12:05:00 -05:00

Refactor to use a more formal state machine.

This commit is contained in:
Jon Leighton 2012-02-16 23:47:52 +00:00
parent 8b5541d661
commit a6e4a012ff
2 changed files with 26 additions and 29 deletions

View file

@ -1,19 +1,23 @@
class Poltergeist.Browser
constructor: (@owner) ->
@awaiting_response = false
@state = 'default'
this.resetPage()
resetPage: ->
@page.release() if @page?
@page = new Poltergeist.WebPage
@page.onLoadStarted = =>
@state = 'loading' if @state == 'clicked'
@page.onLoadFinished = (status) =>
if @awaiting_response
if @state == 'loading'
@owner.sendResponse(status)
@awaiting_response = false
@state = 'default'
visit: (url) ->
@awaiting_response = true
@state = 'loading'
@page.open(url)
current_url: ->
@ -88,14 +92,9 @@ class Poltergeist.Browser
@owner.sendResponse(true)
click: (id) ->
load_detected = false
# Detect if the click event triggers a page load. If it does, don't send
# a response here, because the response will be sent once the page has loaded.
@page.onLoadStarted = =>
return if load_detected
@awaiting_response = true
load_detected = true
# If the click event triggers onLoadStarted, we will transition to the 'loading'
# state and wait for onLoadFinished before sending a response.
@state = 'clicked'
@page.get(id).click()
@ -103,8 +102,9 @@ class Poltergeist.Browser
# callback can (possibly) fire, before we decide whether to send a response.
setTimeout(
=>
@page.onLoadStarted = null
@owner.sendResponse(true) unless load_detected
if @state == 'clicked'
@state = 'default'
@owner.sendResponse(true)
,
10
)

View file

@ -2,7 +2,7 @@ var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments)
Poltergeist.Browser = (function() {
function Browser(owner) {
this.owner = owner;
this.awaiting_response = false;
this.state = 'default';
this.resetPage();
}
Browser.prototype.resetPage = function() {
@ -10,15 +10,20 @@ Poltergeist.Browser = (function() {
this.page.release();
}
this.page = new Poltergeist.WebPage;
this.page.onLoadStarted = __bind(function() {
if (this.state === 'clicked') {
return this.state = 'loading';
}
}, this);
return this.page.onLoadFinished = __bind(function(status) {
if (this.awaiting_response) {
if (this.state === 'loading') {
this.owner.sendResponse(status);
return this.awaiting_response = false;
return this.state = 'default';
}
}, this);
};
Browser.prototype.visit = function(url) {
this.awaiting_response = true;
this.state = 'loading';
return this.page.open(url);
};
Browser.prototype.current_url = function() {
@ -86,19 +91,11 @@ Poltergeist.Browser = (function() {
return this.owner.sendResponse(true);
};
Browser.prototype.click = function(id) {
var load_detected;
load_detected = false;
this.page.onLoadStarted = __bind(function() {
if (load_detected) {
return;
}
this.awaiting_response = true;
return load_detected = true;
}, this);
this.state = 'clicked';
this.page.get(id).click();
return setTimeout(__bind(function() {
this.page.onLoadStarted = null;
if (!load_detected) {
if (this.state === 'clicked') {
this.state = 'default';
return this.owner.sendResponse(true);
}
}, this), 10);