Status Data

有狀態了,然後呢?

Status是一個物件,而不是一個export出某些對外的API,這意味著你可以嘗試解讀輸出的狀態,而Status本身也有幾個API可以幫助你建立介面或直接產生介面。


get

get會歷遍當下status對象以下的所有子節點,回傳一個結果物件。

var gene = Nucleoid.createGene('look status')

gene.template('add status', (base, enzy, next) => {
    enzy.setStatusAttr('show', 'hello.')
    next()
})

gene.transcription().then((messenger) => {
    console.log(JSON.stringify(messenger.status.get(), null, 4))
})

以下是輸出root status的資料

{
    "name": "look status",
    "type": "root",
    "detail": {
        "operationTime": 12
    },
    "message": "",
    "success": true,
    "attributes": {},
    "children": [
        {
            "name": "add status",
            "type": "template",
            "detail": {
                "operationTime": 0
            },
            "message": "",
            "success": true,
            "attributes": {
                "say": "hello."
            },
            "children": []
        }
    ]
}

json

json()是取得get之後轉換成json字串,但與直接stringify不同的是,該function會檢查是否有迴圈結構。

var gene = Nucleoid.createGene('look json')

gene.template('add status', (base, enzy, next) => {
    let loop = {}
    loop.target = loop
    enzy.setStatusAttr('show', loop)
    next()
})

gene.transcription().then((messenger) => {
    console.log(messenger.status.json())
})

輸出:

{
    "name": "look json",
    "type": "root",
    "detail": {
        "operationTime": 12
    },
    "message": "",
    "success": true,
    "attributes": {},
    "children": [
        {
            "name": "add status",
            "type": "template",
            "detail": {
                "operationTime": 0
            },
            "message": "",
            "success": true,
            "attributes": {
                "show": {
                    "target": "Circular structure object." // 這個循環結構被阻擋掉了
                }
            },
            "children": []
        }
    ]
}

html

看這函數名就知道尊爵不凡,它能產生一組簡單的HTML,提升最終狀態的可視度。

var gene = Nucleoid.createGene('look html')

gene.template('add status', (base, enzy, next) => {
    let loop = {}
    loop.target = loop
    enzy.setStatusAttr('show', loop)
    next()
})

gene.transcription().then((messenger) => {
    document.body.innerHTML = messenger.status.html()
})

輸出:

type : root
name : look json
detail :
{
    "operationTime": 13
}
type : template
name : add status
detail :
{
    "operationTime": 1
}
attributes(show) :
{
    "target": "Circular structure object."
}

Error Status

如果該模板執行fail或宣告錯誤,該模板會呈現錯誤狀態。

當節點有錯誤

var gene = Nucleoid.createGene('look error')

gene.template('success', (base, enzy, next, exit, fail) => {
    next()
})

gene.template('fail', (base, enzy, next, exit, fail) => {
    // 宣告fail會擲出錯誤狀態
    fail('fail test')
    // 在try-catch-mode底下這樣也會擲出錯誤狀態
    let a = 5
    a()
})

gene.transcription().catch((messenger) => {
    document.body.innerHTML = messenger.status.html()
})

輸出:

type : root
name : look json
message :
fail test
detail :
{
    "operationTime": 11
}
type : template
name : success
detail :
{
    "operationTime": 0
}
type : template
name : fail

只取得錯誤狀態

有時候會發現整包status太大包了,有時候我們只需要post一些訊息給slack之類的推播系統。

// 該函數可以得到平面化的錯誤資料
var gene = Nucleoid.createGene('look error')

gene.template('success', (base, enzy, next, exit, fail) => {
    next()
})

gene.template('fail', (base, enzy, next, exit, fail) => {
    fail('fail test')
})

gene.transcription().catch((messenger) => {
    // status[0]是root,抓它形同抓整組出來
    console.log(status.getErrorStatus().slice(1))
})

輸出:

[
    {
        "name": "fail",
        "type": "template",
        "detail": null,
        "message": "",
        "success": false,
        "attributes": {},
        "children": []
    }
]