Top Related Projects
Async utilities for node and the browser
:bird: :zap: Bluebird is a full featured promise library with unmatched performance.
A promise library for JavaScript
The ultimate generator based flow-control goodness for nodejs (supports thunks, promises, etc)
Run multiple promise-returning & async functions with limited concurrency
Promise queue with concurrency control
Quick Overview
EventProxy is a lightweight JavaScript library that implements the publish/subscribe pattern and provides event-driven programming capabilities. It helps manage asynchronous operations and simplifies complex event handling in Node.js and browser environments.
Pros
- Simplifies handling of complex asynchronous operations
- Lightweight and easy to use
- Supports both Node.js and browser environments
- Provides powerful event combination and error handling features
Cons
- May introduce additional complexity for simple use cases
- Limited documentation and examples for advanced usage
- Not actively maintained (last commit was in 2019)
- Might be overkill for small projects or simple event handling needs
Code Examples
- Basic event handling:
const EventProxy = require('eventproxy');
const ep = new EventProxy();
ep.on('data', (data) => {
console.log('Received data:', data);
});
ep.emit('data', 'Hello, EventProxy!');
- Combining multiple events:
const ep = new EventProxy();
ep.all('user', 'articles', (user, articles) => {
console.log('User:', user);
console.log('Articles:', articles);
});
setTimeout(() => ep.emit('user', { name: 'John' }), 100);
setTimeout(() => ep.emit('articles', ['Article 1', 'Article 2']), 200);
- Error handling:
const ep = new EventProxy();
ep.fail((err) => {
console.error('An error occurred:', err);
});
ep.on('data', (data) => {
if (data === 'error') {
ep.emit('error', new Error('Invalid data'));
} else {
console.log('Received data:', data);
}
});
ep.emit('data', 'error');
Getting Started
To use EventProxy in your project, follow these steps:
-
Install the package:
npm install eventproxy
-
Import and create an instance:
const EventProxy = require('eventproxy'); const ep = new EventProxy();
-
Start using EventProxy in your code:
ep.on('myEvent', (data) => { console.log('Event received:', data); }); ep.emit('myEvent', 'Hello, World!');
Competitor Comparisons
Async utilities for node and the browser
Pros of async
- More comprehensive and feature-rich library with a wider range of utility functions
- Better documentation and extensive examples for various use cases
- Actively maintained with regular updates and a larger community
Cons of async
- Larger library size, which may impact performance in some scenarios
- Steeper learning curve due to the extensive API and numerous functions
- May be overkill for simpler asynchronous tasks
Code Comparison
eventproxy example:
var ep = new EventProxy();
ep.all('data1', 'data2', function (data1, data2) {
// Handle data1 and data2
});
async example:
async.parallel({
data1: function(callback) { /* ... */ },
data2: function(callback) { /* ... */ }
}, function(err, results) {
// Handle results.data1 and results.data2
});
Both libraries aim to simplify asynchronous programming in JavaScript, but they approach the problem differently. eventproxy focuses on event-based programming, while async provides a broader set of utilities for managing asynchronous operations. async is generally more popular and widely used, offering more flexibility and features. However, eventproxy may be simpler and more intuitive for developers who prefer event-driven programming patterns.
:bird: :zap: Bluebird is a full featured promise library with unmatched performance.
Pros of Bluebird
- More comprehensive and feature-rich Promise implementation
- Better performance and memory efficiency
- Extensive API with additional utility methods
Cons of Bluebird
- Larger library size, which may impact load times
- Steeper learning curve due to its extensive feature set
- May be overkill for simpler projects with basic async needs
Code Comparison
Bluebird:
Promise.map(items, function(item) {
return processItem(item);
}).then(function(results) {
console.log(results);
});
Eventproxy:
var ep = new EventProxy();
ep.after('item_processed', items.length, function(results) {
console.log(results);
});
items.forEach(function(item) {
processItem(item, ep.done('item_processed'));
});
Bluebird offers a more concise and readable approach to handling asynchronous operations, while Eventproxy uses an event-based pattern. Bluebird's API is more intuitive for developers familiar with Promises, whereas Eventproxy's event-driven approach may be more suitable for certain use cases or developers comfortable with event-based programming.
A promise library for JavaScript
Pros of Q
- More comprehensive promise implementation with advanced features like progress tracking and cancellation
- Better suited for complex asynchronous workflows and large-scale applications
- Extensive documentation and wider community adoption
Cons of Q
- Steeper learning curve due to more complex API
- Larger library size, which may impact performance in smaller projects
- Less focused on event-driven programming compared to EventProxy
Code Comparison
EventProxy:
var ep = new EventProxy();
ep.all('event1', 'event2', function (result1, result2) {
console.log('Both events have occurred.');
});
Q:
Q.all([promise1, promise2])
.then(function ([result1, result2]) {
console.log('Both promises have resolved.');
});
Summary
EventProxy is simpler and more focused on event-driven programming, making it easier to learn and use for smaller projects. Q, on the other hand, offers a more comprehensive promise implementation with advanced features, making it better suited for complex asynchronous workflows in larger applications. The choice between the two depends on the specific needs of your project and your familiarity with promise-based programming.
The ultimate generator based flow-control goodness for nodejs (supports thunks, promises, etc)
Pros of co
- Provides a more elegant and readable way to write asynchronous code using generators
- Offers better error handling and stack traces compared to callback-based approaches
- Supports both promises and thunks, providing flexibility in usage
Cons of co
- Requires ES6 generator support, limiting compatibility with older Node.js versions
- May have a steeper learning curve for developers unfamiliar with generators
- Less suitable for complex event-driven scenarios compared to EventProxy
Code Comparison
co:
co(function* () {
var result = yield someAsyncOperation();
console.log(result);
}).catch(onerror);
EventProxy:
var ep = new EventProxy();
ep.all('event1', 'event2', function (result1, result2) {
console.log(result1, result2);
});
Summary
co focuses on simplifying asynchronous code using generators, while EventProxy is designed for managing complex event-driven scenarios. co offers a more modern approach with better readability and error handling, but requires ES6 support. EventProxy excels in handling multiple asynchronous operations and their dependencies, making it more suitable for certain types of event-driven applications.
Run multiple promise-returning & async functions with limited concurrency
Pros of p-limit
- Lightweight and focused on a single purpose (limiting concurrency)
- Modern JavaScript syntax and Promise-based API
- Actively maintained with recent updates
Cons of p-limit
- Limited functionality compared to eventproxy's broader event handling capabilities
- Requires more setup for complex scenarios involving multiple asynchronous operations
- Less suitable for handling diverse event-driven scenarios
Code Comparison
p-limit:
import pLimit from 'p-limit';
const limit = pLimit(2);
const input = [1, 2, 3, 4, 5];
const promises = input.map(i => limit(() => fetchData(i)));
eventproxy:
var EventProxy = require('eventproxy');
var ep = new EventProxy();
ep.all('data1', 'data2', 'data3', function (data1, data2, data3) {
// All data is ready, do something
});
Summary
p-limit focuses on limiting concurrency for Promise-based operations, offering a simple and modern API. eventproxy provides a more comprehensive event handling system, suitable for complex asynchronous scenarios. p-limit is ideal for straightforward concurrency control, while eventproxy excels in managing multiple interdependent asynchronous operations.
Promise queue with concurrency control
Pros of p-queue
- Supports concurrency control and prioritization of tasks
- Provides a more modern, Promise-based API
- Actively maintained with regular updates and improvements
Cons of p-queue
- More focused on queue management, less on event handling
- May have a steeper learning curve for developers new to Promise-based APIs
- Limited built-in support for complex event patterns
Code Comparison
eventproxy:
var ep = new EventProxy();
ep.all('data1', 'data2', function (data1, data2) {
// Handle multiple events
});
p-queue:
const queue = new PQueue({concurrency: 2});
await queue.add(() => fetchData1());
await queue.add(() => fetchData2());
Summary
p-queue excels in managing asynchronous task queues with concurrency control and prioritization, making it well-suited for scenarios involving multiple API calls or resource-intensive operations. It offers a modern Promise-based interface, which aligns well with current JavaScript best practices.
eventproxy, on the other hand, focuses more on event handling and coordination, particularly useful in scenarios where multiple asynchronous operations need to be synchronized. It provides a simpler API that may be easier to grasp for developers familiar with event-driven programming.
The choice between these libraries depends on the specific requirements of your project, with p-queue being more appropriate for queue management and concurrency control, while eventproxy is better suited for complex event handling scenarios.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
EventProxy
English Doc
è¿ä¸ªä¸çä¸ä¸å卿è°åè°å½æ°æ·±åº¦åµå¥çé®é¢ã ââ Jackson Tian
ä¸ç䏿¬æ²¡æåµå¥åè°ï¼åå¾äººå¤äºï¼ä¹ä¾¿æäº
}}}}}}}}}}}}
ã ââ fengmk2
- APIææ¡£: API Documentation
- jscoverage: 97%
- æºç æ³¨è§£ï¼æ³¨è§£ææ¡£
EventProxy ä» ä» æ¯ä¸ä¸ªå¾è½»éçå·¥å ·ï¼ä½æ¯è½å¤å¸¦æ¥ä¸ç§äºä»¶å¼ç¼ç¨çæç»´ååãæå 个ç¹ç¹ï¼
- å©ç¨äºä»¶æºå¶è§£è¦å¤æä¸å¡é»è¾
- ç§»é¤è¢«å¹¿ä¸ºè¯ç çæ·±åº¦callbackåµå¥é®é¢
- å°ä¸²è¡çå¾ åæå¹¶è¡çå¾ ï¼æåå¤å¼æ¥åä½åºæ¯ä¸çæ§è¡æç
- å好çError handling
- æ å¹³å°ä¾èµï¼éååå端ï¼è½ç¨äºæµè§å¨åNode.js
- å ¼å®¹CMDï¼AMD以åCommonJS模åç¯å¢
ç°å¨çï¼æ 深度åµå¥çï¼å¹¶è¡ç
var ep = EventProxy.create("template", "data", "l10n", function (template, data, l10n) {
_.template(template, data, l10n);
});
$.get("template", function (template) {
// something
ep.emit("template", template);
});
$.get("data", function (data) {
// something
ep.emit("data", data);
});
$.get("l10n", function (l10n) {
// something
ep.emit("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, l10n);
});
});
});
å®è£
Nodeç¨æ·
éè¿NPMå®è£ å³å¯ä½¿ç¨ï¼
$ npm install eventproxy
è°ç¨:
var EventProxy = require('eventproxy');
spm
$ spm install eventproxy
Component
$ component install JacksonTian/eventproxy
åç«¯ç¨æ·
以ä¸ç¤ºä¾åæåGithubçæºæä»¶å°åï¼æ¨ä¹å¯ä»¥ä¸è½½æºæä»¶å°ä½ èªå·±ç项ç®ä¸ãæ´ä¸ªæä»¶æ³¨éå ¨é¢ï¼å¸¦æ³¨éå空è¡ï¼ä¸å ±çº¦500è¡ã为ä¿è¯EventProxyçæåµå ¥ï¼é¡¹ç®æä¸æä¾å缩çãç¨æ·å¯ä»¥èªè¡éç¨UglifyãYUI CompressoræGoogle Closure Complierè¿è¡å缩ã
æ®éç¯å¢
å¨é¡µé¢ä¸åµå ¥èæ¬å³å¯ä½¿ç¨ï¼
<script src="https://raw.github.com/JacksonTian/eventproxy/master/lib/eventproxy.js"></script>
使ç¨ï¼
// EventProxyæ¤æ¶æ¯ä¸ä¸ªå
¨å±åé
var ep = new EventProxy();
SeaJSç¨æ·
SeaJSä¸åªéé
ç½®å«åï¼ç¶årequire
å¼ç¨å³å¯ä½¿ç¨ã
// é
ç½®
seajs.config({
alias: {
eventproxy: 'https://raw.github.com/JacksonTian/eventproxy/master/lib/eventproxy.js'
}
});
// 使ç¨
seajs.use(['eventproxy'], function (EventProxy) {
// TODO
});
// æè
define('test', function (require, exports, modules) {
var EventProxy = require('eventproxy');
});
RequireJSç¨æ·
RequireJSå®ç°çæ¯AMDè§èã
// é
置路å¾
require.config({
paths: {
eventproxy: "https://raw.github.com/JacksonTian/eventproxy/master/lib/eventproxy"
}
});
// 使ç¨
require(["eventproxy"], function (EventProxy) {
// TODO
});
弿¥åä½
å¤ç±»å弿¥åä½
æ¤å¤ä»¥é¡µé¢æ¸²æä¸ºåºæ¯ï¼æ¸²æé¡µé¢éè¦æ¨¡æ¿ãæ°æ®ãå设é½éè¦å¼æ¥è¯»åã
var ep = new EventProxy();
ep.all('tpl', 'data', function (tpl, data) { // or ep.all(['tpl', 'data'], function (tpl, data) {})
// 卿ææå®çäºä»¶è§¦ååï¼å°ä¼è¢«è°ç¨æ§è¡
// 忰坹åºåèªçäºä»¶å
});
fs.readFile('template.tpl', 'utf-8', function (err, content) {
ep.emit('tpl', content);
});
db.get('some sql', function (err, result) {
ep.emit('data', result);
});
all
æ¹æ³å°handler注åå°äºä»¶ç»åä¸ã彿³¨åçå¤ä¸ªäºä»¶é½è§¦ååï¼å°ä¼è°ç¨handleræ§è¡ï¼æ¯ä¸ªäºä»¶ä¼ éçæ°æ®ï¼å°ä¼ä¾ç
§äºä»¶å顺åºï¼ä¼ å
¥handlerä½ä¸ºåæ°ã
å¿«éå建
EventProxyæä¾äºcreate
éææ¹æ³ï¼å¯ä»¥å¿«éå®ææ³¨åall
äºä»¶ã
var ep = EventProxy.create('tpl', 'data', function (tpl, data) {
// TODO
});
以䏿¹æ³çæäº
var ep = new EventProxy();
ep.all('tpl', 'data', function (tpl, data) {
// TODO
});
éå¤å¼æ¥åä½
æ¤å¤ä»¥è¯»åç®å½ä¸çæææä»¶ä¸ºä¾ï¼å¨å¼æ¥æä½ä¸ï¼æä»¬éè¦å¨ææå¼æ¥è°ç¨ç»æåï¼æ§è¡æäºæä½ã
var ep = new EventProxy();
ep.after('got_file', files.length, function (list) {
// 卿ææä»¶ç弿¥æ§è¡ç»æåå°è¢«æ§è¡
// æææä»¶çå
容é½åå¨listæ°ç»ä¸
});
for (var i = 0; i < files.length; i++) {
fs.readFile(files[i], 'utf-8', function (err, content) {
// 触åç»æäºä»¶
ep.emit('got_file', content);
});
}
after
æ¹æ³éåéå¤çæä½ï¼æ¯å¦è¯»å10个æä»¶ï¼è°ç¨5æ¬¡æ°æ®åºçãå°handler注åå°N次ç¸åäºä»¶ç触åä¸ãè¾¾å°æå®çè§¦åæ°ï¼handlerå°ä¼è¢«è°ç¨æ§è¡ï¼æ¯æ¬¡è§¦åçæ°æ®ï¼å°ä¼æè§¦å顺åºï¼å为æ°ç»ä½ä¸ºåæ°ä¼ å
¥ã
æç»å弿¥åä½
æ¤å¤ä»¥è¡ç¥¨ä¸ºä¾ï¼æ°æ®å模æ¿é½æ¯å¼æ¥è·åï¼ä½æ¯æ°æ®ä¼æç»å·æ°ï¼è§å¾ä¼éè¦éæ°å·æ°ã
var ep = new EventProxy();
ep.tail('tpl', 'data', function (tpl, data) {
// 卿ææå®çäºä»¶è§¦ååï¼å°ä¼è¢«è°ç¨æ§è¡
// 忰坹åºåèªçäºä»¶åçææ°æ°æ®
});
fs.readFile('template.tpl', 'utf-8', function (err, content) {
ep.emit('tpl', content);
});
setInterval(function () {
db.get('some sql', function (err, result) {
ep.emit('data', result);
});
}, 2000);
tail
ä¸all
æ¹æ³æ¯è¾ç±»ä¼¼ï¼é½æ¯æ³¨åå°äºä»¶ç»åä¸ãä¸åå¨äºï¼æå®äºä»¶é½è§¦åä¹åï¼å¦æäºä»¶ä¾æ§æç»è§¦åï¼å°ä¼å¨æ¯æ¬¡è§¦åæ¶è°ç¨handlerï¼æå䏿¡å°¾å·´ã
åºæ¬äºä»¶
éè¿äºä»¶å®ç°å¼æ¥å使¯EventProxyç主è¦äº®ç¹ã餿¤ä¹å¤ï¼å®è¿æ¯ä¸ä¸ªåºæ¬çäºä»¶åºãæºå¸¦å¦ä¸åºæ¬API
on
/addListener
ï¼ç»å®äºä»¶çå¬å¨emit
ï¼è§¦åäºä»¶once
ï¼ç»å®åªæ§è¡ä¸æ¬¡çäºä»¶çå¬å¨removeListener
ï¼ç§»é¤äºä»¶ççå¬å¨removeAllListeners
ï¼ç§»é¤å个äºä»¶æè ææäºä»¶ççå¬å¨
为äºç §é¡¾å个ç¯å¢çå¼åè ï¼ä¸é¢çæ¹æ³å¤å ·æå«åã
- YUI3使ç¨è
ï¼
subscribe
åfire
ä½ åºè¯¥ç¥éåå«å¯¹åºçæ¯on
/addListener
åemit
ã - jQuery使ç¨è
ï¼
trigger
对åºçæ¹æ³æ¯emit
ï¼bind
对åºçå°±æ¯on
/addListener
ã removeListener
åremoveAllListeners
å ¶å®é½å¯ä»¥éè¿å«åunbind
宿ã
æä»¥å¨ä½ çç¯å¢ä¸ï¼éç¨ä½ 忬¢çAPIå³å¯ã
æ´å¤APIçæè¿°è¯·è®¿é®API Docsã
å¼å¸¸å¤ç
å¨å¼æ¥æ¹æ³ä¸ï¼å®é
ä¸ï¼å¼å¸¸å¤çéè¦å ç¨ä¸å®æ¯ä¾çç²¾åãå¨è¿å»ä¸æ®µæ¶é´å
ï¼æä»¬é½æ¯éè¿é¢å¤æ·»å error
äºä»¶æ¥è¿è¡å¤ççï¼ä»£ç 大è´å¦ä¸ï¼
exports.getContent = function (callback) {
var ep = new EventProxy();
ep.all('tpl', 'data', function (tpl, data) {
// æååè°
callback(null, {
template: tpl,
data: data
});
});
// 侦å¬erroräºä»¶
ep.bind('error', function (err) {
// å¸è½½æææhandler
ep.unbind();
// å¼å¸¸åè°
callback(err);
});
fs.readFile('template.tpl', 'utf-8', function (err, content) {
if (err) {
// 䏿¦åçå¼å¸¸ï¼ä¸å¾äº¤ç»erroräºä»¶çhandlerå¤ç
return ep.emit('error', err);
}
ep.emit('tpl', content);
});
db.get('some sql', function (err, result) {
if (err) {
// 䏿¦åçå¼å¸¸ï¼ä¸å¾äº¤ç»erroräºä»¶çhandlerå¤ç
return ep.emit('error', err);
}
ep.emit('data', result);
});
};
代ç éå 为å¼å¸¸çå¤çï¼ä¸ä¸åä¸å»äºå¾å¤ãå¨è¿éEventProxyç»è¿å¾å¤å®è·µåï¼æä»¬æ ¹æ®æä»¬çæä½³å®è·µæä¾äºä¼åçé误å¤çæ¹æ¡ã
exports.getContent = function (callback) {
var ep = new EventProxy();
ep.all('tpl', 'data', function (tpl, data) {
// æååè°
callback(null, {
template: tpl,
data: data
});
});
// æ·»å error handler
ep.fail(callback);
fs.readFile('template.tpl', 'utf-8', ep.done('tpl'));
db.get('some sql', ep.done('data'));
};
ä¸è¿°ä»£ç ä¼åä¹åï¼ä¸å¡å¼åè
å ä¹ä¸ç¨å
³å¿å¼å¸¸å¤çäºã代ç ééä½ææææ¾ã
è¿é代ç ç转æ¢ï¼ä¹è®¸æå¼åè
并䏿¾å¿ãå
¶å®ç§è¯å¨fail
æ¹æ³ådone
æ¹æ³ä¸ã
ç¥å¥çfail
ep.fail(callback);
// ç±äºåæ°ä½ç¸åï¼å®å®é
æ¯
ep.fail(function (err) {
callback(err);
});
// çä»·äº
ep.bind('error', function (err) {
// å¸è½½æææhandler
ep.unbind();
// å¼å¸¸åè°
callback(err);
});
fail
æ¹æ³ä¾¦å¬äºerror
äºä»¶ï¼é»è®¤å¤çå¸è½½æææhandlerï¼å¹¶è°ç¨åè°å½æ°ã
ç¥å¥ç throw
throw
æ¯ ep.emit('error', err)
çç®åã
var err = new Error();
ep.throw(err);
// å®é
æ¯
ep.emit('error', err);
ç¥å¥çdone
ep.done('tpl');
// çä»·äº
function (err, content) {
if (err) {
// 䏿¦åçå¼å¸¸ï¼ä¸å¾äº¤ç»erroräºä»¶çhandlerå¤ç
return ep.emit('error', err);
}
ep.emit('tpl', content);
}
å¨Nodeçæä½³å®è·µä¸ï¼åè°å½æ°ç¬¬ä¸ä¸ªåæ°ä¸å®ä¼æ¯ä¸ä¸ªerror
å¯¹è±¡ãæ£æµå°å¼å¸¸åï¼å°ä¼è§¦åerror
äºä»¶ãå©ä¸çåæ°ï¼å°è§¦åäºä»¶ï¼ä¼ éç»å¯¹åºhandlerå¤çã
done乿¥ååè°å½æ°
done
æ¹æ³é¤äºæ¥åäºä»¶åå¤ï¼è¿æ¥ååè°å½æ°ã妿æ¯å½æ°æ¶ï¼å®å°åé¤ç¬¬ä¸ä¸ªerror
对象(æ¤æ¶ä¸ºnull
)åå©ä½çåæ°ï¼ä¼ éç»è¯¥åè°å½æ°ä½ä¸ºåæ°ã该åè°å½æ°æ éèèå¼å¸¸å¤çã
ep.done(function (content) {
// è¿éæ éèèå¼å¸¸
// æå·¥emit
ep.emit('someevent', newcontent);
});
å½ç¶æå·¥emitçæ¹å¼å¹¶ä¸å¤ªå¥½ï¼æä»¬æ´è¿ä¸æ¥ççæ¬ï¼
ep.done('tpl', function (tpl) {
// å°å
å®¹æ´æ¹åï¼è¿åå³å¯
return tpl.trim();
});
注æäºé¡¹
妿emit
éè¦ä¼ éå¤ä¸ªåæ°æ¶ï¼ep.done(event, fn)
çæ¹å¼ä¸è½æ»¡è¶³éæ±ï¼è¿æ¯éè¦ep.done(fn)
ï¼è¿è¡æå·¥emit
å¤ä¸ªåæ°ã
ç¥å¥çgroup
fail
é¤äºç¨äºåå©all
æ¹æ³å®æå¤ï¼ä¹è½åå©after
ä¸çå¼å¸¸å¤çãå¦å¤ï¼å¨after
çåè°å½æ°ä¸ï¼ç»æé¡ºåºæ¯ä¸ç¨æ·emit
çé¡ºåºæå
³ãä¸ºäºæ»¡è¶³è¿åæ°æ®æåèµ·å¼æ¥è°ç¨çé¡ºåºæåï¼EventProxy
æä¾äºgroup
æ¹æ³ã
var ep = new EventProxy();
ep.after('got_file', files.length, function (list) {
// 卿ææä»¶ç弿¥æ§è¡ç»æåå°è¢«æ§è¡
// æææä»¶çå
容é½åå¨listæ°ç»ä¸ï¼æé¡ºåºæå
});
for (var i = 0; i < files.length; i++) {
fs.readFile(files[i], 'utf-8', ep.group('got_file'));
}
group
ç§æ¿done
彿°ç设计ï¼å®å
å«å¼å¸¸çä¼ éãåæ¶å®è¿éå«äºå¯¹è¿åæ°æ®è¿è¡ç¼å·ï¼å¨ç»ææ¶ï¼æé¡ºåºè¿åã
ep.group('got_file');
// 约çä»·äº
function (err, data) {
if (err) {
return ep.emit('error', err);
}
ep.emit('got_file', data);
};
å½åè°å½æ°çæ°æ®è¿éè¦è¿è¡å å·¥æ¶ï¼å¯ä»¥ç»group
带ä¸åè°å½æ°ï¼åªè¦å¨æä½åå°æ°æ®è¿åå³å¯ï¼
ep.group('got_file', function (data) {
// some code
return data;
});
弿¥äºä»¶è§¦å: emitLater && doneLater
å¨nodeä¸ï¼emit
æ¹æ³æ¯åæ¥çï¼EventProxyä¸çemit
ï¼trigger
çè·nodeç飿 ¼ä¸è´ï¼ä¹æ¯åæ¥çãçä¸é¢è¿æ®µä»£ç ï¼å¯è½ç¼å°çåå¦ä¸ä¸å°±åç°äºéèçbug:
var ep = EventProxy.create();
db.check('key', function (err, permission) {
if (err) {
return ep.emit('error', err);
}
ep.emit('check', permission);
});
ep.once('check', function (permission) {
permission && db.get('key', function (err, data) {
if (err) {
return ep.emit('error');
}
ep.emit('get', data);
});
});
ep.once('get', function (err, data) {
if (err) {
return ep.emit('error', err);
}
render(data);
});
ep.on('error', errorHandler);
没éï¼ä¸ä¸db.check
çcallback
è¢«åæ¥æ§è¡äºï¼å¨ep
çå¬check
äºä»¶ä¹åï¼å®å°±å·²ç»è¢«æåºæ¥äºï¼åç»é»è¾æ²¡åæ³ç»§ç»æ§è¡ã尽管nodeççº¦å®æ¯ææçcallback
齿¯éè¦å¼æ¥è¿åçï¼ä½æ¯å¦æè¿ä¸ªæ¹æ³æ¯ç±ç¬¬ä¸æ¹æä¾çï¼æä»¬æ²¡æåæ³ä¿è¯db.check
çcallback
ä¸å®ä¼å¼æ¥æ§è¡ï¼æä»¥æä»¬ç代ç é常就åæäºè¿æ ·:
var ep = EventProxy.create();
ep.once('check', function (permission) {
permission && db.get('key', function (err, data) {
if (err) {
return ep.emit('error');
}
ep.emit('get', data);
});
});
ep.once('get', function (err, data) {
if (err) {
return ep.emit('error', err);
}
render(data);
});
ep.on('error', errorHandler);
db.check('key', function (err, permission) {
if (err) {
return ep.emit('error', err);
}
ep.emit('check', permission);
});
æä»¬è¢«è¿«ædb.check
æªå°æåï¼ä¿è¯äºä»¶å
被çå¬ï¼åæ§è¡db.check
ãcheck
->get
->render
çé»è¾ï¼å¨ä»£ç ä¸çèµ·æ¥åæäºget
->render
->check
ã妿æ´ä¸ªé»è¾æ´å 夿ï¼è¿ç§é£æ ¼å°ä¼è®©ä»£ç å¾é¾è¯»æã
è¿æ¶åï¼æä»¬éè¦çå°±æ¯ __弿¥äºä»¶è§¦å__ï¼
var ep = EventProxy.create();
db.check('key', function (err, permission) {
if (err) {
return ep.emitLater('error', err);
}
ep.emitLater('check', permission);
});
ep.once('check', function (permission) {
permission && db.get('key', function (err, data) {
if (err) {
return ep.emit('error');
}
ep.emit('get', data);
});
});
ep.once('get', function (err, data) {
if (err) {
return ep.emit('error', err);
}
render(data);
});
ep.on('error', errorHandler);
ä¸é¢ä»£ç ä¸ï¼æä»¬ædb.check
çåè°å½æ°ä¸çäºä»¶éè¿emitLater
触åï¼è¿æ ·,å°±ç®db.check
çåè°å½æ°è¢«åæ¥æ§è¡äºï¼äºä»¶ç触åä¹è¿æ¯å¼æ¥çï¼ep
å¨å½åäºä»¶å¾ªç¯ä¸çå¬äºææçäºä»¶ï¼ä¹åçäºä»¶å¾ªç¯ä¸æä¼å»è§¦åcheck
äºä»¶ã代ç 顺åºå°åé»è¾é¡ºåºä¿æä¸è´ã
å½ç¶ï¼è¿ä¹å¤æç代ç ï¼å¿
é¡»å¯ä»¥åep.done()
䏿 ·éè¿doneLater
æ¥è§£å³ï¼
var ep = EventProxy.create();
db.check('key', ep.doneLater('check'));
ep.once('check', function (permission) {
permission && db.get('key', ep.done('get'));
});
ep.once('get', function (data) {
render(data);
});
ep.fail(errorHandler);
æç»åç°åºæ¥çï¼æ¯ä¸æ®µç®æ´ä¸æ¸ æ°ç代ç ã
注æäºé¡¹
- 请å¿ä½¿ç¨
all
ä½ä¸ºä¸å¡ä¸çäºä»¶åã该äºä»¶å为ä¿çäºä»¶ã - å¼å¸¸å¤çé¨åï¼è¯·éµå¾ª Node çæä½³å®è·µ(åè°å½æ°é¦ä¸ªåæ°ä¸ºå¼å¸¸ä¼ éä½)ã
è´¡ç®è 们
谢谢 EventProxy ç使ç¨è 们ï¼äº«å EventProxy çè¿ç¨ï¼ä¹ç» EventProxy åé¦è¯å¤ã
project : eventproxy
repo age : 3 years, 6 months
active : 97 days
commits : 203
files : 24
authors :
177 Jackson Tian 87.2%
9 fengmk2 4.4%
7 dead-horse 3.4%
2 azrael 1.0%
2 rogerz 1.0%
1 Bitdeli Chef 0.5%
1 yaoazhen 0.5%
1 Ivan Yan 0.5%
1 cssmagic 0.5%
1 haoxin 0.5%
1 redky 0.5%
详æ 请åè§https://github.com/JacksonTian/eventproxy/graphs/contributors
License
The MIT Licenseã请èªç±äº«å弿ºã
æèµ
妿æ¨è§å¾æ¬æ¨¡åå¯¹æ¨æå¸®å©ï¼æ¬¢è¿è¯·ä½è 䏿¯åå¡
Top Related Projects
Async utilities for node and the browser
:bird: :zap: Bluebird is a full featured promise library with unmatched performance.
A promise library for JavaScript
The ultimate generator based flow-control goodness for nodejs (supports thunks, promises, etc)
Run multiple promise-returning & async functions with limited concurrency
Promise queue with concurrency control
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot