2011-01-12 43 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转移到事件驱动的代码样式时,我很难想象我需要写什么。 –