eventproxy
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -1,1 +0,1 @@ | ||
module.exports = process.env.EVENTPROXY_COV ? require('./lib-cov/eventproxy') : require('./lib/eventproxy'); | ||
module.exports = require('./lib/eventproxy'); |
@@ -13,7 +13,10 @@ /*global exports */ | ||
if (hasDefine) { // AMD Module or CMD Module | ||
if (hasDefine) { | ||
// AMD Module or CMD Module | ||
define(definition); | ||
} else if (hasExports) { // Node.js Module | ||
} else if (hasExports) { | ||
// Node.js Module | ||
module.exports = definition(); | ||
} else { // Assign to common namespaces or simply the global object (window) | ||
} else { | ||
// Assign to common namespaces or simply the global object (window) | ||
this[name] = definition(); | ||
@@ -29,3 +32,3 @@ } | ||
* Examples: | ||
* ``` | ||
* ```js | ||
* var render = function (template, resources) {}; | ||
@@ -59,11 +62,11 @@ * var proxy = new EventProxy(); | ||
/** | ||
* `addListener` alias | ||
* `addListener` alias, bind | ||
*/ | ||
EventProxy.prototype.bind = EventProxy.prototype.addListener; | ||
/** | ||
* `addListener` alias | ||
* `addListener` alias, on | ||
*/ | ||
EventProxy.prototype.on = EventProxy.prototype.addListener; | ||
/** | ||
* `addListener` alias | ||
* `addListener` alias, subscribe | ||
*/ | ||
@@ -103,3 +106,3 @@ EventProxy.prototype.subscribe = EventProxy.prototype.addListener; | ||
/** | ||
* `removeListener` alias | ||
* `removeListener` alias, unbind | ||
*/ | ||
@@ -242,3 +245,3 @@ EventProxy.prototype.unbind = EventProxy.prototype.removeListener; | ||
* Examples: | ||
* ``` | ||
* ```js | ||
* proxy.all(ev1, ev2, callback); | ||
@@ -280,3 +283,3 @@ * proxy.all([ev1, ev2], callback); | ||
* Examples: | ||
* ``` | ||
* ```js | ||
* proxy.tail(ev1, ev2, callback); | ||
@@ -297,7 +300,7 @@ * proxy.tail([ev1, ev2], callback); | ||
/** | ||
* `tail` alias | ||
* `tail` alias, assignAll | ||
*/ | ||
EventProxy.prototype.assignAll = EventProxy.prototype.tail; | ||
/** | ||
* `tail` alias | ||
* `tail` alias, assignAlways | ||
*/ | ||
@@ -350,3 +353,13 @@ EventProxy.prototype.assignAlways = EventProxy.prototype.tail; | ||
* The `after` method's helper. Use it will return ordered results. | ||
* If you need manipulate result, you need callback. | ||
* If you need manipulate result, you need callback | ||
* Examples: | ||
* ```js | ||
* var ep = new EventProxy(); | ||
* ep.after('file', files.length, function (list) { | ||
* // Ordered results | ||
* }); | ||
* for (var i = 0; i < files.length; i++) { | ||
* fs.readFile(files[i], 'utf-8', ep.group('file')); | ||
* } | ||
* ``` | ||
* @param {String} eventName Event name, shoule keep consistent with `after`. | ||
@@ -364,3 +377,3 @@ * @param {Function} callback Callback function, should return the final result. | ||
} | ||
that.fire(group, { | ||
that.emit(group, { | ||
index: index, | ||
@@ -431,6 +444,20 @@ result: callback ? callback(data) : data | ||
* | ||
* ```js | ||
* fs.readFile('foo.txt', ep.done('content', function (content) { | ||
* return content.trim(); | ||
* })); | ||
* | ||
* // equal to => | ||
* | ||
* fs.readFile('foo.txt', function (err, content) { | ||
* if (err) { | ||
* return ep.emit('error', err); | ||
* } | ||
* ep.emit('content', content.trim()); | ||
* }); | ||
* ``` | ||
* @param {Function|String} handler, success callback or event name will be emit after callback. | ||
* @return {Function} | ||
*/ | ||
EventProxy.prototype.done = function (handler) { | ||
EventProxy.prototype.done = function (handler, callback) { | ||
var that = this; | ||
@@ -442,5 +469,9 @@ return function (err, data) { | ||
// getAsync(query, ep.done('query')); | ||
if (typeof handler === 'string') { | ||
return that.emit(handler, data); | ||
// getAsync(query, ep.done('query')); | ||
// or | ||
// getAsync(query, ep.done('query', function (data) { | ||
// return data.trim(); | ||
// })); | ||
return that.emit(handler, callback ? callback.call(data) : data); | ||
} | ||
@@ -462,3 +493,3 @@ | ||
* Examples: | ||
* ``` | ||
* ```js | ||
* var ep = EventProxy.create(); | ||
@@ -475,3 +506,2 @@ * ep.assign('user', 'articles', function(user, articles) { | ||
*/ | ||
EventProxy.create = function () { | ||
@@ -478,0 +508,0 @@ var ep = new EventProxy(); |
@@ -19,8 +19,18 @@ { | ||
"mocha": "*", | ||
"jscover": "*", | ||
"blanket": "*", | ||
"pedding": "*", | ||
"chai": "*" | ||
"chai": "*", | ||
"travis-cov": "*" | ||
}, | ||
"scripts": { | ||
"test": "make test" | ||
"test": "make test-all", | ||
"blanket": { | ||
"pattern": "eventproxy/lib", | ||
"data-cover-flags": { | ||
"debug": false | ||
} | ||
}, | ||
"travis-cov": { | ||
"threshold": 70 | ||
} | ||
}, | ||
@@ -31,3 +41,3 @@ "repository": { | ||
}, | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"main": "index.js", | ||
@@ -34,0 +44,0 @@ "directories": { |
@@ -1,2 +0,2 @@ | ||
EventProxy [![Build Status](https://secure.travis-ci.org/JacksonTian/eventproxy.png)](http://travis-ci.org/JacksonTian/eventproxy) | ||
EventProxy [![Build Status](https://secure.travis-ci.org/JacksonTian/eventproxy.png)](http://travis-ci.org/JacksonTian/eventproxy) [English Doc](https://github.com/JacksonTian/eventproxy/blob/master/README_en.md) | ||
====== | ||
@@ -9,4 +9,6 @@ | ||
* API文档: [API Documentation](http://html5ify.com/eventproxy/api.html) | ||
* jscoverage: [95%](http://fengmk2.github.com/coverage/eventproxy.html) | ||
* jscoverage: [97%](http://html5ify.com/eventproxy/coverage.html) | ||
* 源码注解:[注解文档](http://html5ify.com/eventproxy/eventproxy.html) | ||
EventProxy 仅仅是一个很轻量的工具,但是能够带来一种事件式编程的思维变化。有几个特点: | ||
@@ -73,3 +75,3 @@ | ||
### 前端用户 | ||
以下示例均指向Github的源文件地址,您也可以[下载源文件](https://raw.github.com/JacksonTian/eventproxy/master/lib/eventproxy.js)到你自己的项目中。整个文件注释全面,带注释和空行,一共400多行。为保证EventProxy的易嵌入,项目暂不提供压缩版。用户可以自行采用Uglify、YUI Compressor或Google Closure Complier进行压缩。 | ||
以下示例均指向Github的源文件地址,您也可以[下载源文件](https://raw.github.com/JacksonTian/eventproxy/master/lib/eventproxy.js)到你自己的项目中。整个文件注释全面,带注释和空行,一共约500行。为保证EventProxy的易嵌入,项目暂不提供压缩版。用户可以自行采用Uglify、YUI Compressor或Google Closure Complier进行压缩。 | ||
@@ -181,3 +183,3 @@ #### 普通环境 | ||
### 持续型异步协作 | ||
此处以股票为例,数据和模板都是异步获取,但是数据会是刷新,视图会重新刷新。 | ||
此处以股票为例,数据和模板都是异步获取,但是数据会持续刷新,视图会需要重新刷新。 | ||
@@ -319,3 +321,3 @@ ```js | ||
#### done也接受回调函数 | ||
done方法除了接受事件名外,还接受回调函数。如果是函数时,它将剔除第一个`error`对象(此时为`null`)后剩余的参数,传递给该回调函数作为参数。该回调函数无需考虑异常处理。 | ||
`done`方法除了接受事件名外,还接受回调函数。如果是函数时,它将剔除第一个`error`对象(此时为`null`)后剩余的参数,传递给该回调函数作为参数。该回调函数无需考虑异常处理。 | ||
@@ -325,7 +327,21 @@ ```js | ||
// 这里无需考虑异常 | ||
// 手工emit | ||
ep.emit('someevent', newcontent); | ||
}); | ||
``` | ||
当然手工emit的方式并不太好,我们更进一步的版本: | ||
```js | ||
ep.done('tpl', function (tpl) { | ||
// 将内容更改后,返回即可 | ||
return tpl.trim(); | ||
}); | ||
``` | ||
#### 注意事项 | ||
如果`emit`需要传递多个参数时,`ep.done(event, fn)`的方式不能满足需求,还是需要`ep.done(fn)`,进行手工`emit`多个参数。 | ||
### 神奇的group | ||
在`after`的回调函数中,结果顺序是与用户`emit`的顺序有关。为了满足返回数据按发起异步调用的顺序排列,`EventProxy`提供了`group`方法。 | ||
`fail`除了用于协助`all`方法完成外,也能协助`after`中的异常处理。另外,在`after`的回调函数中,结果顺序是与用户`emit`的顺序有关。为了满足返回数据按发起异步调用的顺序排列,`EventProxy`提供了`group`方法。 | ||
@@ -365,5 +381,4 @@ ```js | ||
## 注意事项 | ||
- 请勿使用`all`作为业务中的事件名。该事件名为保留事件。 | ||
- 异常处理部分,请遵循Node的最佳实践。 | ||
- 异常处理部分,请遵循Node的最佳实践(回调函数首个参数为异常传递位)。 | ||
@@ -370,0 +385,0 @@ ## [贡献者们](https://github.com/JacksonTian/eventproxy/graphs/contributors) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
41174
7
482
401
0
5