2011-01-12 39 views
4

我想在Node.js中設計一個系統(嘗試使用Node的併發性來解決my earlier problems之一),但是我很難找出如何繪製應該如何操作的計劃。如何勾畫事件驅動系統?

我越來越覺得在回調方面而不是返回的值。流量不是線性的,我的草稿能力真的讓人難以置信。如何爲事件驅動系統繪製操作流程?

我需要一些東西,我可以看看並說「好的,是的,這就是它的工作原理,我會在這裏開始,它會讓我回到這裏。

圖片將是這個非常有幫助。謝謝。

編輯:我正在尋找的東西更精細的比UML,具體的東西,這將有助於我從過渡阻塞和麪向對象的編程結構,在這裏我很舒服,到非阻塞和事件驅動結構,我不熟悉。

回答

3

基於http://i.stack.imgur.com/9DDQP.png您需要的是一個很好的流程庫,它允許您在節點中管道同步和異步調用。

一個這樣的庫是https://github.com/isaacs/slide-flow-control(在那裏也請看slide preso),你需要做的代碼大綱如下。

這是自我記錄,你看它很簡潔,純粹的nodejs,uml,img等不需要。

var chain = require("slide/chain") 
    , asyncMap = require("slide/async-map") 
    ; 

// start processing 
main_loop(function() { 
    console.log("its done"); // when finished 
}); 

function main_loop(cb) { 
    var res = []; 
    // each entry in chain below fires sequentially i.e. after 
    // the previous function completes 
    chain 
     ([ [start_update_q, "user-foo"] 
      , [get_followed_users, chain.last] 
      , [get_favorites, chain.last] 
      , [calc_new_q] 
      , [push_results, chain.last] 
      ] 
      , res 
      , cb 
     ) 
} 

function get_favorites(users, cb) { 
    function fn(user, cb_) { 
     get_one_users_favorites(user, cb_); 
    } 
    // this will run thru get_favorites in parallel 
    // and after all user favorites are gotten it will fire 
    // callback cb 
    asyncMap(users, fn, cb); 
} 

// code in the various functions in chain here, 
// remember to either return the callback on completion. 
// or pass it as an arg to the async call you make within the 
// function as above i.e. asyncMap will fire cb on completion 
+0

這正是我正在尋找的。我已經開始使用Seq,但後來打破了我的Node/npm安裝:http://goo.gl/fhilo –

0

UML可能是合適的。我會看看行爲圖。

+0

一個良好的開端,我已經有沿着這些線路圖:http://i.imgur.com/9DDQP.png但我尋找的東西會更直接地幫助我從去想法/圖 - >縮短代碼。當我從OOP轉移到事件驅動的代碼樣式時,我很難想象我需要寫什麼。 –