4

我正在尝试在 Node.js 中设计一个系统(尝试使用 Node 的并发性来解决我早期的一个问题),但我在弄清楚如何绘制该事物应该如何操作的计划时遇到了麻烦。

我在考虑回调而不是返回值方面非常困惑。流程不是线性的,这真的让我起草它的能力令人难以置信。如何为事件驱动系统绘制操作流程?

我需要一些我可以看到并说“好的,是的,这就是它的工作方式。我会从这里开始,它会在这里给我返回这些结果。”

图片对这个很有帮助。谢谢。

编辑:我正在寻找比UML更细粒度的东西,特别是可以帮助我从我很舒服的阻塞和面向对象的编程结构过渡到我感到舒适的非阻塞和事件驱动结构的东西我不熟悉。

4

2 回答 2

3

基于 http://i.stack.imgur.com/9DDQP.png你需要的是一个好的流库,它允许你在节点中管道同步和异步调用。

一个这样的库是https://github.com/isaacs/slide-flow-control(也请看那里的幻灯片),您需要做的代码大纲如下。

它是自我记录的,如您所见,它非常简洁,不需要纯 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
于 2011-01-14T05:36:58.443 回答
0

UML可能是合适的。我会看看行为图。

于 2011-01-12T04:43:27.470 回答