GithubHelp home page GithubHelp logo

shakakira / eventproxy Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jacksontian/eventproxy

0.0 0.0 0.0 241 KB

An implementation of task/event based asynchronous pattern.

Home Page: http://weibo.com/shyvo

License: Other

eventproxy's Introduction

EventProxy Build Status

这个世界上不存在所谓回调函数深度嵌套的问题。 —— Jackson Tian

安装EventProxy

npm install eventproxy

API 文档

EventProxy API Documentation

说明

EventProxy.js仅仅是一个很轻量的工具,但是能够带来一种事件式编程的思维变化。有几个特点:

  1. 利用事件机制解耦复杂业务逻辑
  2. 移除被广为诟病的深度callback嵌套问题
  3. 将串行等待变成并行等待,提升多异步场景下的执行效率
  4. 无平台依赖,适合前后端,能用于浏览器和Node.js

现在的,无深度嵌套的,并行的

var proxy = new EventProxy();
var render = function (template, data, l10n){
    _.template(template, data);
};
proxy.assign("template", "data", "l10n", render);
$.get("template", function (template) {
    // something
    proxy.trigger("template", template);
});
$.get("data", function (data) {
    // something
    proxy.trigger("data", data);
});
$.get("l10n", function (l10n) {
    // something
    proxy.trigger("l10n", l10n);
});

过去的,深度嵌套的,串行的。

var render = function (template, data){
    _.template(template, data);
};
$.get("template", function (template) {
    // something
    $.get("data", function (data) {
        // something
        $.get("l10n", function (l10n) {
            // something
            render(template, data);
        });
    });
});

For Frontend user:

Assign once. The callback will be executed once when all event were fired.

<script src="eventproxy.js"></script>
<script>
    var proxy = new EventProxy();
    var render = function (template, data, l10n){
        _.template(template, data);
    };
    proxy.assign("template", "data", "l10n", render);
    $.get("template", function (template) {
        // something
        proxy.trigger("template", template);
    });
    $.get("data", function (data) {
        // something
        proxy.trigger("data", data);
    });
    $.get("l10n", function (l10n) {
        // something
        proxy.trigger("l10n", l10n);
    });
</script>

Assign always. The callback will be executed first time when all event were fired. And after that, any event was fired will trigger callback. It's useful when you need refresh UI with newest data, e.g. stock app.

<script src="eventproxy.js"></script>
<script>
    var proxy = new EventProxy();
    var render = function (template, data, l10n){
        _.template(template, data);
    };
    proxy.assignAll("template", "dataUpdate", "l10n", render);
    $.get("template", function (template) {
        // something
        proxy.trigger("template", template);
    });

    $.get("l10n", function (l10n) {
        // something
        proxy.trigger("l10n", l10n);
    });

    // Need refresh data and UI for some realtime application.
    setInterval(function () {
        $.get("data", function (data) {
            // something
            proxy.trigger("dataUpdate", data);
        });
    }, 1000);
</script>

For Node.js:

var EventProxy = require("eventproxy");

var proxy = new EventProxy();
var render = function (template, data, l10n){
    return _.template(template, data);
};
proxy.assign("template", "data", "l10n", render);
$.get("template", function (template) {
    // something
    proxy.trigger("template", template);
});
$.get("data", function (data) {
    // something
    proxy.trigger("data", data);
});
$.get("l10n", function (l10n) {
    // something
    proxy.trigger("l10n", l10n);
});

注意事项

  • 请勿使用all作为业务中的事件名。该事件名为保留事件。

eventproxy's People

Contributors

dead-horse avatar fengmk2 avatar jacksontian avatar yaoazhen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.