Skip to content

Commit

Permalink
V1 upgrade (API change)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebv committed Oct 26, 2012
1 parent 3ed2fd9 commit 4e395bc
Show file tree
Hide file tree
Showing 25 changed files with 521 additions and 1,438 deletions.
21 changes: 1 addition & 20 deletions Cakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
DEV_DIRS = ['lib','test']
COFFEE_PATHS = DEV_DIRS.concat ['index.coffee']
JS_PATHS = DEV_DIRS.concat ['index.js']
TEST_ENV = ['test/testing-env.coffee']

u = require 'sv-cake-utils'

Expand All @@ -13,28 +12,10 @@ task 'compile:watch', 'Compile All coffee files and watch for changes', ->

task 'clean', 'Remove all js files', ->
u.js.clean JS_PATHS
u.coffee.compile TEST_ENV

task 'test:async', 'Run all async tests (using vows)', ->
u.coffee.compile TEST_ENV
u.vows.test 'test/async'

task 'test:sync', 'Run all async tests (using vows)', ->
u.coffee.compile TEST_ENV
task 'test', 'Run all async tests (using vows)', ->
u.mocha.test 'test/sync'

task 'test', 'Run all sync tests (using mocha)', ->
u.coffee.compile TEST_ENV
u.mocha.test 'test/sync', (status)->
if status is 0
u.vows.test 'test/async', (status) ->
if status == 0
console.log "\n\nAll Tests Succesful.\n\n"
else
console.warn "\n\n!!! Async tests failed.\n\n"
else
console.warn "\n\n!!! Sync tests failed.\n\n"

task 'grep:dirty', 'Lookup for debugger and console.log in code', ->
u.grep.debug()
u.grep.log()
Expand Down
74 changes: 30 additions & 44 deletions lib/phantom-sync.coffee
Original file line number Diff line number Diff line change
@@ -1,59 +1,45 @@
{Sync, MakeSync} = require('make-sync')
{sync, makeSync} = require('make-sync')
phantom = require 'phantom'
_ = require 'underscore'

buildObjectOptions = (options) ->
objectOptions =
create:
mode: options.mode
mode: 'sync'
'sync-return': 'res'
ph:
mode: options.mode
mode: 'sync'
exclude: [/^_/, 'exit']
'sync-return': 'res'
num_of_args:
set: 2
page:
mode: options.mode
mode: 'sync'
exclude: [/^_/, 'sendEvent']
'sync-return': 'res'
num_of_args:
set: 2

# Builds replacement for the phantom.create method
createReplacement = (options) ->
objectOptions = buildObjectOptions options
replacement = (args..., done) ->
# disassembling and rebuilding child object structure
# with sync version of objects
phantom.create args..., (ph) ->
_createPage = ph.createPage
ph.createPage = (args..., done) ->
_createPage args..., (page) ->
_evaluate = page.evaluate
_evaluateSync = MakeSync _evaluate ,
mode:'sync',
'sync-return': 'res'
MakeSync page, objectOptions.page
if _.isEqual options.mode, ['mixed','args']
page.evaluate = (args...) ->
[f,fargs...,cb] = args
if (typeof cb) isnt 'function'
fargs.push cb
cb = null

unless (typeof cb) is 'function'
return _evaluateSync.apply page, args
else
return _evaluate.apply page, args
done page
MakeSync ph, objectOptions.ph
done(ph)
MakeSync replacement, objectOptions.create

class Phantom
constructor: (options) ->
options = {mode: 'sync'} unless options?
@create = createReplacement options

exports.Phantom = Phantom
exports.Sync= Sync
_create = (args..., done) ->
# disassembling and rebuilding child object structure
# with sync version of objects
phantom.create args..., (ph) ->
_createPage = ph.createPage
ph.createPage = (args..., done) ->
_createPage args..., (page) ->
_evaluate = page.evaluate
_evaluateSync = makeSync _evaluate ,
mode:'sync',
'sync-return': 'res'
makeSync page, objectOptions.page
done page
makeSync ph, objectOptions.ph
done(ph)

_create = makeSync _create, objectOptions.create

phantomSync =
phantom:
create: _create
sync: sync

module.exports = phantomSync

139 changes: 53 additions & 86 deletions lib/phantom-sync.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "phantom-sync"
, "version": "0.1.4"
, "version": "1.0.0"
, "description": "sync version of phantom"
, "keywords": ["phantom", "phantomjs", "sync", "synchronous"]
, "author": "Seb Vincent <seb.vincent@gmail.com>"
Expand All @@ -13,12 +13,10 @@
, "dependencies": {
"phantom": "git://github.com/sebv/phantomjs-node.git"
,"make-sync" : "0.1.x"
, "underscore": "1.4.x"
}
, "devDependencies" : {
"coffee-script" : "1.3.x"
, "sv-cake-utils" : "0.1.x"
, "vows": "0.6.x"
, "mocha": "1.6.x"
, "should": "1.2.x"
, "ps-tree": "0.0.x"
Expand Down
28 changes: 11 additions & 17 deletions test/sync/adv.coffee → test/adv.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
should = require 'should'
express = require 'express'
{Phantom, Sync} = require '../../lib/phantom-sync'
{phantom, sync} = require '../lib/phantom-sync'

test = (options) ->
{app, server, phantom, p, page} = []
describe "phantom-sync", -> \
describe "sync", -> \
describe "adv", ->
{app, server, ph, page} = []

before (done) ->
app = express()
Expand All @@ -22,39 +24,31 @@ test = (options) ->
"""

server = app.listen()
phantom = new Phantom options
done()

after (done)->
p?.exit()
ph?.exit()
server?.close()
done()

describe "phantom instance with --load-images=no", ->
it "opening a page", (done) ->
Sync ->
p = phantom.create '--load-images=no'
page = p.createPage()
sync ->
ph = phantom.create '--load-images=no'
page = ph.createPage()
status = page.open "http://127.0.0.1:#{server.address().port}/"
status.should.be.ok
done()
it "checking that loadImages is not set", (done) ->
Sync ->
sync ->
s = page.get 'settings'
s.loadImages.should.be.false
done()

it "checking a test image", (done) ->
Sync ->
sync ->
img = page.evaluate -> document.getElementsByTagName('img')[0]
img.width.should.equal 0
img.height.should.equal 0
done()

describe "phantom-sync", -> \
describe "sync", -> \
describe "adv", ->

for mode in [undefined,'sync',['mixed','args'],['mixed','fibers']]
describe "#{mode} mode", ->
test mode:mode
Loading

0 comments on commit 4e395bc

Please sign in to comment.