Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
czzxueyang
cat
提交
d6d60171
C
cat
项目概览
czzxueyang
/
cat
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
cat
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
d6d60171
编写于
1月 03, 2019
作者:
J
Jialin, Sun
提交者:
GitHub
1月 03, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1518 from withnate/threadmode_190103_nate
add threadmode.
上级
549e5911
743e2cf9
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
65 addition
and
83 deletion
+65
-83
lib/node.js/lib/cat.js
lib/node.js/lib/cat.js
+26
-4
lib/node.js/lib/message/message.js
lib/node.js/lib/message/message.js
+3
-3
lib/node.js/lib/message/org/tree-manager.js
lib/node.js/lib/message/org/tree-manager.js
+29
-73
lib/node.js/lib/message/org/tree.js
lib/node.js/lib/message/org/tree.js
+7
-3
未找到文件。
lib/node.js/lib/cat.js
浏览文件 @
d6d60171
...
...
@@ -10,12 +10,13 @@ const system = require('./system')
let
isInitialized
=
false
;
class
TransactionHandler
{
constructor
(
transactionMessage
,
catInstance
)
{
constructor
(
transactionMessage
,
catInstance
,
threadMode
)
{
this
.
message
=
transactionMessage
this
.
cat
=
catInstance
this
.
treeManager
=
catInstance
.
treeManager
this
.
type
=
transactionMessage
.
type
this
.
name
=
transactionMessage
.
name
this
.
threadMode
=
threadMode
}
/**
...
...
@@ -65,6 +66,20 @@ class TransactionHandler {
}
}
newTransaction
(
type
,
name
)
{
type
=
''
+
type
name
=
''
+
name
let
message
=
new
TransactionMessage
({
type
:
type
,
name
:
name
})
if
(
this
.
threadMode
)
{
this
.
message
.
addChild
(
message
)
}
let
t
=
new
TransactionHandler
(
message
,
this
.
cat
,
this
.
threadMode
)
return
t
}
/**
* logError , 同cat.logError , 但确保挂在此transaction下
*/
...
...
@@ -83,9 +98,10 @@ class TransactionHandler {
* 暴露给用户的API在这边,以这里的参数说明为准
* */
class
Cat
{
constructor
()
{
constructor
(
threadMode
)
{
this
.
STATUS
=
STATUS
this
.
treeManager
=
new
TreeManager
(
implement
)
this
.
treeManager
=
new
TreeManager
(
implement
,
threadMode
)
this
.
threadMode
=
threadMode
}
/**
...
...
@@ -144,9 +160,15 @@ class Cat {
name
:
name
})
this
.
treeManager
.
addMessage
(
message
)
let
t
=
new
TransactionHandler
(
message
,
this
)
let
t
=
new
TransactionHandler
(
message
,
this
,
this
.
threadMode
)
return
t
}
complete
()
{
if
(
this
.
threadMode
)
{
this
.
treeManager
.
complete
()
}
}
}
function
createEvent
(
type
,
name
,
status
,
data
)
{
...
...
lib/node.js/lib/message/message.js
浏览文件 @
d6d60171
...
...
@@ -18,13 +18,13 @@ class Message {
this
.
data
=
options
.
data
||
''
this
.
children
=
options
.
children
||
[]
this
.
parent
=
null
//
this.parent = null
// this.uid = options.uid || undefined;
// this.uid = rand.generate();
this
.
isBegin
=
options
.
isBegin
||
false
this
.
isEnd
=
options
.
isEnd
||
false
this
.
allEnd
=
false
this
.
tree
=
null
// 如果作为tree的根节点,这个属性作为索引
//
this.tree = null // 如果作为tree的根节点,这个属性作为索引
// this.puid = options.puid || undefined;
this
.
messageType
=
'
message
'
// 子类复写
}
...
...
@@ -76,7 +76,7 @@ class Message {
var
self
=
this
Array
.
prototype
.
forEach
.
call
(
arguments
,
message
=>
{
self
.
children
.
push
(
message
)
message
.
parent
=
self
//
message.parent = self
// 如果当前的已经结束,但是加进来的节点没有end
if
(
self
.
allEnd
&&
!
message
.
isAllEnd
())
{
self
.
allEnd
=
false
...
...
lib/node.js/lib/message/org/tree-manager.js
浏览文件 @
d6d60171
...
...
@@ -6,14 +6,16 @@ var Transaction = require('../transaction')
var
Heartbeat
=
require
(
'
../heartbeat
'
)
class
TreeManager
{
constructor
(
sender
)
{
constructor
(
sender
,
threadMode
)
{
// 会出现多个tree的情况
this
.
trees
=
[]
// this.trees = []
this
.
tree
=
null
// 最近一个挂上去的transaction message
this
.
lastNode
=
null
//
this.lastNode = null
this
.
sender
=
sender
this
.
threadMode
=
threadMode
}
/**
...
...
@@ -22,42 +24,32 @@ class TreeManager {
* 如果是Event,挂到transaction下面或者直接发送
*/
addMessage
(
message
)
{
let
lastNode
=
this
.
_findLastNode
()
// Transaction
if
(
message
instanceof
Transaction
)
{
if
(
!
lastNode
)
{
// 没有树或者已经发送掉了
this
.
createTree
(
message
)
this
.
lastNode
=
message
}
else
{
// 已经构建过 tree 了,加入到最后一个transaction的子节点
lastNode
.
addChild
(
message
)
this
.
lastNode
=
message
// 非线程模式下,不用将transaction添加到树
if
(
this
.
threadMode
)
{
if
(
this
.
tree
)
{
this
.
tree
.
addChild
(
message
)
}
else
{
this
.
tree
=
this
.
createTree
(
message
)
}
}
message
.
begin
()
}
else
if
(
message
instanceof
Event
||
message
instanceof
Heartbeat
)
{
// Event or Heartbeat
if
(
!
lastNode
)
{
// 没有构建过树的时候,直接把消息发出去
if
(
!
this
.
threadMode
||
!
this
.
tree
)
{
// 非线程模式,直接把消息发出去
// 线程模式,如果还没有第一个transaction,直接把消息发出去
this
.
sendTree
(
new
Tree
({
root
:
message
}))
}
else
{
lastNod
e
.
addChild
(
message
)
this
.
tre
e
.
addChild
(
message
)
}
}
}
/**
* 某个transaction结束
* 如果是叶子节点:
* 修改状态
* 通知父节点
* 如果是父节点:
* 判断子节点的transaction是否end,
* 如果没结束,说明add的时候挂的节点是不对的,需要修改树结构
* 如果全都结束,通知父节点
* 非线程模式,直接发送消息
*/
endMessage
(
message
,
maxTime
)
{
// 先end自己
...
...
@@ -67,37 +59,21 @@ class TreeManager {
return
}
if
(
message
.
isAllEnd
())
{
// 如果整个子节点都结束了
this
.
notifyParentEnd
(
message
)
}
else
{
// 如果自己结束了,但是子节点没结束,说明子节点中有挂的不对的,不应该挂在自己下面,提到自己并列
let
unEndChildren
=
message
.
children
.
filter
(
child
=>
!
child
.
isAllEnd
())
message
.
removeChild
.
apply
(
message
,
unEndChildren
)
if
(
message
.
parent
)
{
message
.
parent
.
addChild
.
apply
(
message
.
parent
,
unEndChildren
)
}
else
{
// 根节点自己结束了,但是有子节点没结束的,为子节点单独创建树
this
.
sendTree
(
message
.
tree
)
unEndChildren
.
forEach
(
msg
=>
{
this
.
createTree
(
msg
)
})
}
if
(
!
this
.
threadMode
)
{
this
.
sendTree
(
this
.
createTree
(
message
))
}
else
if
(
this
.
tree
&&
message
===
this
.
tree
.
root
)
{
// 线程模式,如果消息为tree的根节点,则发送消息
this
.
complete
()
}
}
notifyParentEnd
(
message
)
{
if
(
message
.
parent
)
{
if
(
message
.
parent
.
isEnd
)
{
// 如果父节点自己已经结束,再end一次,让父节点判断是否全都结束
this
.
endMessage
(
message
.
parent
)
}
else
{
// 什么都不干,等父节点end
complete
()
{
if
(
this
.
tree
)
{
for
(
let
child
of
this
.
tree
.
root
.
children
)
{
child
.
end
()
}
}
else
{
// 自己就是根节点
this
.
sendTree
(
message
.
tree
)
this
.
sendTree
(
this
.
tree
)
this
.
tree
=
null
}
}
...
...
@@ -106,33 +82,13 @@ class TreeManager {
return
}
var
index
=
this
.
trees
.
indexOf
(
tree
)
if
(
index
>
-
1
)
{
this
.
trees
.
splice
(
index
,
1
)
}
this
.
sender
.
sendTree
(
tree
)
}
// 找到最后一个节点,如果this.lastNode是end的,就一直往父节点找
_findLastNode
()
{
if
(
!
this
.
lastNode
)
{
return
null
}
var
last
=
this
.
lastNode
while
(
last
&&
last
.
isEnd
)
{
last
=
last
.
parent
}
return
last
}
createTree
(
rootMessage
)
{
var
tree
=
new
Tree
({
return
new
Tree
({
root
:
rootMessage
})
this
.
trees
.
push
(
tree
)
return
tree
}
}
...
...
lib/node.js/lib/message/org/tree.js
浏览文件 @
d6d60171
...
...
@@ -21,9 +21,13 @@ class Tree {
this
.
sessionToken
=
options
.
sessionToken
||
config
.
sessionToken
this
.
root
=
options
.
root
||
undefined
if
(
this
.
root
)
{
this
.
root
.
tree
=
this
}
// if (this.root) {
// this.root.tree = this
// }
}
addChild
(
message
)
{
this
.
root
.
addChild
(
message
)
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录