未验证 提交 d6d60171 编写于 作者: J Jialin, Sun 提交者: GitHub

Merge pull request #1518 from withnate/threadmode_190103_nate

add threadmode.
......@@ -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) {
......
......@@ -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
......
......@@ -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 {
lastNode.addChild(message)
this.tree.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
}
}
......
......@@ -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.
先完成此消息的编辑!
想要评论请 注册