Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.www.
wechaty
提交
a46b1a7e
W
wechaty
项目概览
.www.
/
wechaty
与 Fork 源项目一致
Fork自
wechaty / wechaty
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
wechaty
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a46b1a7e
编写于
9月 06, 2016
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add targetState() and currentState() to puppet-web compoments
上级
8d90a5ec
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
112 addition
and
42 deletion
+112
-42
src/puppet-web/browser.js
src/puppet-web/browser.js
+67
-9
src/puppet-web/event.js
src/puppet-web/event.js
+12
-4
src/puppet-web/puppet-web.js
src/puppet-web/puppet-web.js
+25
-26
src/puppet-web/watchdog.js
src/puppet-web/watchdog.js
+8
-3
未找到文件。
src/puppet-web/browser.js
浏览文件 @
a46b1a7e
...
...
@@ -32,16 +32,52 @@ class Browser extends EventEmitter {
this
.
head
=
head
this
.
sessionFile
=
sessionFile
// a file to save session cookies
this
.
live
=
false
// this.live = false
this
.
targetState
(
'
close
'
)
this
.
currentState
(
'
close
'
)
}
// targetState : 'open' | 'close'
targetState
(
newState
)
{
if
(
newState
)
{
log
.
verbose
(
'
Browser
'
,
'
targetState(%s)
'
,
newState
)
this
.
_targetState
=
newState
}
return
this
.
_targetState
}
// currentState : 'opening' | 'open' | 'closing' | 'close'
currentState
(
newState
)
{
if
(
newState
)
{
log
.
verbose
(
'
Browser
'
,
'
currentState(%s)
'
,
newState
)
this
.
_currentState
=
newState
}
return
this
.
_currentState
}
toString
()
{
return
`Browser({head:
${
this
.
head
}
)`
}
init
()
{
return
this
.
initDriver
()
.
then
(()
=>
{
this
.
live
=
true
this
.
targetState
(
'
open
'
)
this
.
currentState
(
'
opening
'
)
// fastUrl is used to open in browser for we can set cookies.
// backup: 'https://res.wx.qq.com/zh_CN/htmledition/v2/images/icon/ico_loading28a2f7.gif'
const
fastUrl
=
'
https://wx.qq.com/zh_CN/htmledition/v2/images/webwxgeticon.jpg
'
return
co
.
call
(
this
,
function
*
()
{
yield
this
.
initDriver
()
// this.live = true
yield
this
.
open
(
fastUrl
)
yield
this
.
loadSession
()
.
catch
(
e
=>
{
// fail safe
log
.
verbose
(
'
PuppetWeb
'
,
'
browser.loadSession(%s) exception: %s
'
,
this
.
profile
,
e
.
message
||
e
)
})
yield
this
.
open
()
this
.
currentState
(
'
open
'
)
return
this
})
.
catch
(
e
=>
{
...
...
@@ -50,6 +86,12 @@ class Browser extends EventEmitter {
// with selenium-webdriver v2.53.2
// XXX: https://github.com/SeleniumHQ/selenium/issues/2233
log
.
error
(
'
PuppetWebBrowser
'
,
'
init() exception: %s
'
,
e
.
message
)
this
.
currentState
(
'
closing
'
)
this
.
quit
().
then
(
_
=>
{
this
.
currentState
(
'
close
'
)
})
throw
e
})
}
...
...
@@ -164,7 +206,7 @@ class Browser extends EventEmitter {
.
build
()
/**
* ISSUE #21 - https://github.com/zixia/wechaty/issues/21
*
FIXME:
ISSUE #21 - https://github.com/zixia/wechaty/issues/21
*
* http://phantomjs.org/api/webpage/handler/on-resource-requested.html
* http://stackoverflow.com/a/29544970/1123955
...
...
@@ -190,13 +232,20 @@ this.onResourceRequested = function(request, net) {
quit
()
{
log
.
verbose
(
'
PuppetWebBrowser
'
,
'
quit()
'
)
this
.
live
=
false
this
.
targetState
(
'
close
'
)
this
.
currentState
(
'
closing
'
)
// this.live = false
if
(
!
this
.
driver
)
{
log
.
verbose
(
'
PuppetWebBrowser
'
,
'
driver.quit() skipped because no driver
'
)
this
.
currentState
(
'
close
'
)
return
Promise
.
resolve
(
'
no driver
'
)
}
else
if
(
!
this
.
driver
.
getSession
())
{
this
.
driver
=
null
log
.
verbose
(
'
PuppetWebBrowser
'
,
'
driver.quit() skipped because no driver session
'
)
this
.
currentState
(
'
close
'
)
return
Promise
.
resolve
(
'
no driver session
'
)
}
...
...
@@ -217,6 +266,7 @@ this.onResourceRequested = function(request, net) {
*/
yield
this
.
clean
()
this
.
currentState
(
'
close
'
)
log
.
silly
(
'
PuppetWebBrowser
'
,
'
quit() co() end
'
)
}).
catch
(
e
=>
{
// console.log(e)
...
...
@@ -232,6 +282,9 @@ this.onResourceRequested = function(request, net) {
if
(
crashRegex
.
test
(
e
.
message
))
{
log
.
warn
(
'
PuppetWebBrowser
'
,
'
driver.quit() browser crashed
'
)
}
else
{
log
.
warn
(
'
PuppetWebBrowser
'
,
'
driver.quit() exception: %s
'
,
e
.
message
)
}
// XXX fail safe to `close` ?
this
.
currentState
(
'
close
'
)
})
}
...
...
@@ -399,9 +452,11 @@ this.onResourceRequested = function(request, net) {
if
(
forceReason
)
{
dead
=
true
errMsg
=
forceReason
}
else
if
(
!
this
.
live
)
{
// } else if (!this.live) {
}
else
if
(
this
.
targetState
()
!==
'
open
'
)
{
dead
=
true
errMsg
=
'
browser not live
'
// errMsg = 'browser not live'
errMsg
=
'
targetState not open
'
}
else
if
(
!
this
.
driver
||
!
this
.
driver
.
getSession
())
{
dead
=
true
errMsg
=
'
no driver or session
'
...
...
@@ -409,7 +464,10 @@ this.onResourceRequested = function(request, net) {
if
(
dead
)
{
log
.
warn
(
'
PuppetWebBrowser
'
,
'
dead() because %s
'
,
errMsg
)
this
.
live
=
false
// this.live = false
this
.
currentState
(
'
closing
'
)
this
.
quit
().
then
(
_
=>
this
.
currentState
(
'
close
'
))
// must use nextTick here, or promise will hang... 2016/6/10
process
.
nextTick
(
_
=>
{
log
.
verbose
(
'
PuppetWebBrowser
'
,
'
dead() emit a `dead` event because %s
'
,
errMsg
)
...
...
src/puppet-web/event.js
浏览文件 @
a46b1a7e
...
...
@@ -157,8 +157,12 @@ function onServerDisconnect(data) {
this
.
user
=
null
}
if
(
this
.
readyState
()
===
'
disconnecting
'
)
{
log
.
verbose
(
'
PuppetWebEvent
'
,
'
onServerDisconnect() be called when readyState is `disconnecting`
'
)
// if (this.readyState() === 'disconnecting') {
// log.verbose('PuppetWebEvent', 'onServerDisconnect() be called when readyState is `disconnecting`')
// return
// }
if
(
this
.
currentState
()
===
'
killing
'
)
{
log
.
verbose
(
'
PuppetWebEvent
'
,
'
onServerDisconnect() be called when currentState is `killing`
'
)
return
}
...
...
@@ -217,8 +221,12 @@ function onServerUnload(data) {
log
.
warn
(
'
PuppetWebEvent
'
,
'
onServerUnload(%s)
'
,
data
)
// onServerLogout.call(this, data) // XXX: should emit event[logout] from browser
if
(
this
.
readyState
()
===
'
disconnecting
'
)
{
log
.
verbose
(
'
PuppetWebEvent
'
,
'
onServerUnload() will return because readyState is `disconnecting`
'
)
// if (this.readyState() === 'disconnecting') {
// log.verbose('PuppetWebEvent', 'onServerUnload() will return because readyState is `disconnecting`')
// return
// }
if
(
this
.
currentState
()
===
'
killing
'
)
{
log
.
verbose
(
'
PuppetWebEvent
'
,
'
onServerUnload() will return because currentState is `killing`
'
)
return
}
...
...
src/puppet-web/puppet-web.js
浏览文件 @
a46b1a7e
...
...
@@ -56,7 +56,9 @@ class PuppetWeb extends Puppet {
init
()
{
log
.
verbose
(
'
PuppetWeb
'
,
`init() with head:
${
this
.
head
}
, profile:
${
this
.
profile
}
`
)
this
.
readyState
(
'
connecting
'
)
// this.readyState('connecting')
this
.
targetState
(
'
live
'
)
this
.
currentState
(
'
birthing
'
)
return
co
.
call
(
this
,
function
*
()
{
...
...
@@ -66,13 +68,13 @@ class PuppetWeb extends Puppet {
yield
this
.
initAttach
(
this
)
log
.
verbose
(
'
PuppetWeb
'
,
'
initAttach() done
'
)
this
.
server
=
yield
this
.
initServer
()
yield
this
.
initServer
()
log
.
verbose
(
'
PuppetWeb
'
,
'
initServer() done
'
)
this
.
browser
=
yield
this
.
initBrowser
()
yield
this
.
initBrowser
()
log
.
verbose
(
'
PuppetWeb
'
,
'
initBrowser() done
'
)
this
.
bridge
=
yield
this
.
initBridge
()
yield
this
.
initBridge
()
log
.
verbose
(
'
PuppetWeb
'
,
'
initBridge() done
'
)
this
.
emit
(
'
watchdog
'
,
{
data
:
'
inited
'
})
...
...
@@ -84,16 +86,20 @@ class PuppetWeb extends Puppet {
})
.
then
(()
=>
{
// Finally
log
.
verbose
(
'
PuppetWeb
'
,
'
init() done
'
)
this
.
readyState
(
'
connected
'
)
// this.readyState('connected')
this
.
currentState
(
'
live
'
)
return
this
// for Chaining
})
}
quit
()
{
log
.
verbose
(
'
PuppetWeb
'
,
'
quit()
'
)
this
.
targetState
(
'
dead
'
)
if
(
this
.
readyState
()
===
'
disconnecting
'
)
{
log
.
warn
(
'
PuppetWeb
'
,
'
quit() is called but readyState is `disconnecting`?
'
)
// if (this.readyState() === 'disconnecting') {
if
(
this
.
currentState
()
===
'
killing
'
)
{
// log.warn('PuppetWeb', 'quit() is called but readyState is `disconnecting`?')
log
.
warn
(
'
PuppetWeb
'
,
'
quit() is called but currentState is `killing`?
'
)
throw
new
Error
(
'
do not call quit again when quiting
'
)
}
...
...
@@ -103,7 +109,8 @@ class PuppetWeb extends Puppet {
type
:
'
POISON
'
})
this
.
readyState
(
'
disconnecting
'
)
// this.readyState('disconnecting')
this
.
currentState
(
'
killing
'
)
return
co
.
call
(
this
,
function
*
()
{
...
...
@@ -133,14 +140,18 @@ class PuppetWeb extends Puppet {
log
.
verbose
(
'
PuppetWeb
'
,
'
quit() server.quit() this.initAttach(null)
'
)
yield
this
.
initAttach
(
null
)
this
.
currentState
(
'
dead
'
)
})
.
catch
(
e
=>
{
// Reject
log
.
error
(
'
PuppetWeb
'
,
'
quit() exception: %s
'
,
e
.
message
)
this
.
currentState
(
'
dead
'
)
throw
e
})
.
then
(()
=>
{
// Finally, Fail Safe
log
.
verbose
(
'
PuppetWeb
'
,
'
quit() done
'
)
this
.
readyState
(
'
disconnected
'
)
// this.readyState('disconnected')
this
.
currentState
(
'
dead
'
)
return
this
// for Chaining
})
}
...
...
@@ -162,18 +173,10 @@ class PuppetWeb extends Puppet {
browser
.
on
(
'
dead
'
,
Event
.
onBrowserDead
.
bind
(
this
))
// fastUrl is used to open in browser for we can set cookies.
// backup: 'https://res.wx.qq.com/zh_CN/htmledition/v2/images/icon/ico_loading28a2f7.gif'
const
fastUrl
=
'
https://wx.qq.com/zh_CN/htmledition/v2/images/webwxgeticon.jpg
'
this
.
browser
=
browser
return
co
.
call
(
this
,
function
*
()
{
yield
browser
.
init
()
yield
browser
.
open
(
fastUrl
)
yield
browser
.
loadSession
()
.
catch
(
e
=>
{
// fail safe
log
.
verbose
(
'
PuppetWeb
'
,
'
browser.loadSession(%s) exception: %s
'
,
this
.
profile
,
e
.
message
||
e
)
})
yield
browser
.
open
()
return
browser
// follow func name meaning
}).
catch
(
e
=>
{
log
.
error
(
'
PuppetWeb
'
,
'
initBrowser() exception: %s
'
,
e
.
message
)
...
...
@@ -188,6 +191,8 @@ class PuppetWeb extends Puppet {
,
port
:
this
.
port
})
this
.
bridge
=
bridge
return
bridge
.
init
()
.
catch
(
e
=>
{
if
(
this
.
browser
.
dead
())
{
...
...
@@ -220,6 +225,8 @@ class PuppetWeb extends Puppet {
server
.
on
(
'
log
'
,
Event
.
onServerLog
.
bind
(
this
))
server
.
on
(
'
ding
'
,
Event
.
onServerDing
.
bind
(
this
))
this
.
server
=
server
return
server
.
init
()
.
catch
(
e
=>
{
log
.
error
(
'
PuppetWeb
'
,
'
initServer() exception: %s
'
,
e
.
message
)
...
...
@@ -316,12 +323,4 @@ class PuppetWeb extends Puppet {
}
}
// Object.assign(PuppetWeb, {
// default: PuppetWeb
// , PuppetWeb
// , Server
// , Browser
// , Bridge
// })
module
.
exports
=
PuppetWeb
.
default
=
PuppetWeb
.
PuppetWeb
=
PuppetWeb
src/puppet-web/watchdog.js
浏览文件 @
a46b1a7e
...
...
@@ -43,12 +43,17 @@ function onFeed({
const
feed
=
`
${
type
}
:[
${
data
}
]`
log
.
verbose
(
'
PuppetWebWatchdog
'
,
'
onFeed: %d, %s
'
,
timeout
,
feed
)
if
(
this
.
readyState
()
===
'
disconnecting
'
// || this.readyState() === 'disconnected'
if
(
this
.
currentState
()
===
'
killing
'
)
{
log
.
warn
(
'
PuppetWebWatchdog
'
,
'
onFeed() is disabled because
readyState is `disconnect
ing`
'
)
log
.
warn
(
'
PuppetWebWatchdog
'
,
'
onFeed() is disabled because
currentState is `kill
ing`
'
)
return
}
// if (this.readyState() === 'disconnecting'
// // || this.readyState() === 'disconnected'
// ) {
// log.warn('PuppetWebWatchdog', 'onFeed() is disabled because readyState is `disconnecting`')
// return
// }
setWatchDogTimer
.
call
(
this
,
timeout
,
feed
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录