node.js - 如何草拟一个事件驱动系统?

标签 node.js project-planning event-driven-design

我正在尝试在 Node.js 中设计一个系统(试图解决 my earlier problems 之一,使用 Node 的并发性)但是我在弄清楚如何制定一个关于事物应该如何运作的计划时遇到了麻烦.

我对回调而不是返回值的思考感到非常困惑。流程不是线性的,这真的让我难以起草它。如何绘制事件驱动系统的操作流程?

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

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

编辑:我正在寻找比 UML 更细化的内容,特别是,它可以帮助我从我熟悉的阻塞和面向对象的编程结构过渡到我不熟悉的非阻塞和事件驱动的结构。

最佳答案

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

一个这样的图书馆是https://github.com/isaacs/slide-flow-control (也请查看那里的 slide preso),您需要执行的操作的代码大纲如下。

它是 self 记录的,如您所见,它非常简洁,不需要纯 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

关于node.js - 如何草拟一个事件驱动系统?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4665357/

相关文章:

javascript - node.js multipart/form-data 本地文件上传到api

javascript - Nodejs/Javascript 获取任何进程的进程内存

language-agnostic - 通过估算一年编写的代码来计算可以节省多少时间

event-driven-design - 对于 Windows 服务,这是一种不错的事件驱动方法吗?

node.js - Node Express 说 EADDRINUSE 但端口是免费的

node.js - 从 nodejs 中的现有模型 Sequelize 的 Mirations

visual-studio - 用于测量在解决方案/项目上花费的时间的 Visual Studio 插件

project-planning - 对象规划或用户流规划——哪个应该先来?

ruby - 在 ruby​​ 中使用 ActiveMQ + activemessaging gem 的问题