快速公交车
一个带有AMQP和Postal.js等功能的小型事件总线,意在在客户端使用.
自豪地建造了:
这是什么?
这是一个简单的事件总线,在大约80行代码(小于773字节)中复制了Postal.js的基本内容
它不像Postal一样使用regex列表,而是使用定向图,即: 多数 快点.
它增加了自动公尺收集的历史,使行数达到大约150行代码(小于1258字节)
它没有依赖关系.
如何使用
使用 emit 财务报告和审定财务报表 on 方法,就像一个常规事件发射器.
var channel = new Bus();
channel.on('metrics.#', function (msg) {
console.log(msg);
});
channel.emit('metrics.page.loaded', 'hello world');
在模仿Postal.js时,使用返回的功能来阻止事件.
var off = channel.on('page.load.justOnce', function (msg) {
console.log(msg);
off();
});
同样在模仿Postal.js时,使用第二个参数来得到引发事件的主题.
var off = channel.on('page.ad.*.filled', function (msg, meta) {
console.log(meta.topic + 'just happened');
});
如果你更喜欢酒吧和子公司模特儿 我们就别名了 subscribe 财务报告和审定财务报表 publish 还:
var channel = new Bus();
channel.subscribe('metrics.#', function (msg) {
console.log(msg);
});
channel.publish('metrics.page.loaded', 'hello world');
当然要取消订阅, 请使用返回的函数 :
var unsubscribe = channel.subscribe('#', function (msg) {
console.log(Date.now(), msg);
});
for (var i = 0; i < 10; i++) {
channel.publish('welcome.' + i, 'hello world!');
}
unsubscribe();
如何运作
A型客车绘制了直线订阅图. 随着事件主题的发布,图中发现了所有订阅者以通知,然后缓存结果,以在未来更快地发布消息. 当新增订阅者时,会修改图表并重置订阅者缓存.
通关卡订阅
支持与Postal.js相同的通配符,例如:
使用零或更多单词 #
channel.subscribe('#.changed', function (msg) {
// ...
});
channel.emit('what.has.changed', event);
channel.subscribe('metrics.#.changed', function (msg) {
// ...
});
channel.emit('metrics.something.important.has.changed', event);
使用单词 *
channel.subscribe('ads.slot.*.filled', function (msg) {
// ...
});
channel.emit('ads.slot.post-nav.filled', {data, msg});
历史
我们保存着已发生事件的历史。 他们可以被问到 history 方法 :
var channel = new Bus();
channel.emit('ads.slot.post-nav.filled', {data, msg});
channel.emit('ads.slot.side-rail.filled', {thing, stuff});
channel.emit('ads.slot.instream-banner-0.filled', {a, b});
channel.emit('metrics.component.absdf2324.render.start', {etc, ie});
channel.emit('metrics.component.absdf2324.render.end', {etc, ie});
// gets the ad slots that were filled
var history = channel.history('ads.slot.*.filled');
// gets history of components rendering
var history = channel.history('metrics.component.*.render.*');
格式是数组。 例如:
[
["ads.slot.post-nav.filled", 1515714470550],
["ads.slot.side-rail.filled", 1515714470559],
["ads.slot.instream-banner-0.filled", 1515714500268],
["metrics.component.absdf2324.render.start", 1515714782584],
["metrics.component.absdf2324.render.end", 1515714790507],
]
只存储每个事件的话题和时间戳. 我们不会在历史中存储信息/负载,以防止潜在的内存泄露和范围界定问题.
这些事件被储存在一个环缓冲器中,所以一旦达到一定的大小,旧事件就会从历史中消失. 历史规模为当前设置,最多9999个事件.
请注意,这一特性是为下列目的设计的: 计量,而且人们对衡量标准感兴趣的信息往往可以作为专题的一部分。 例如,如果你有一个订阅者 on('components.*.render.start'),然后可以拥有无限数量的组件编号,例如: emit('components.abcdef.render.start') 财务报告和审定财务报表 emit('components.someRandomId.render.start') 不给活动总线增加任何复杂性或性能处罚。 如果你试图获取更多无法作为话题一部分包含的信息,那么你应该让一个订阅者登录这些信息.
看看我们 实例 页次 对于一些常见的代码模式与事件总线.
待执行
- 确定是否应该包括Postals.js的"meta"字段,或者是否只是承诺API讨厌的重量
- 我们应披露时间信息还是信息编号?
- 一个收集、缓冲或节流消息的同步机制是否有用? 是否使用事件队列 ?
- 我们实际使用的邮政还有其他特征吗?
未来/相关架构
- 创建专门从 React 生成事件的助手库
- 创建库以链接到浏览器事件( Boomeram)
- 创建库来收集事件并转发到Sparrow,GA或BooCatch等服务器.
