-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
163 lines (136 loc) · 3.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var Bus = (function () {
'use strict';
var headCount = 0;
var historyMax = 9999;
function Ring(max) {
var list = [];
var end = 0;
var start = 0;
this.push = function (item) {
if (end - start >= max) {
start++;
if (start >= max) {
start = 0;
end = max - 1;
}
}
var index = end % max;
list[index] = item;
end++;
};
this.asArray = function () {
var first = list.slice(start, Math.min(end, max));
var second = list.slice(0, Math.max(end - max, 0));
return first.concat(second);
};
this.list = list;
}
function add(topic, graph) {
var cursor = graph;
for (var i = 0; i < topic.length; i++) {
var word = topic[i];
var right = cursor.r;
if (!right[word]) {
right[word] = { w: word, r: {}, i: headCount++ };
}
cursor = right[word];
}
return cursor;
}
function getList(topic, graph) {
var routes = [[graph, 0]];
var finalNodes = {}; // track found nodes, no duplicates should be returned
var finalRoutes = []; // remember found functions to call
while (routes.length) {
var route = routes.shift();
var cursor = route[0];
var index = route[1];
var right = cursor.r;
var word = topic[index];
if (word === undefined && cursor.fn && !finalNodes[cursor.i]) {
finalNodes[cursor.i] = 1; // remember that we've seen this node
finalRoutes.push(cursor.fn); // remember functions
} else if (right[word]) {
routes.push([right[word], index + 1]);
}
if (right['#']) {
for (var i = index; i <= topic.length; i++) {
routes.push([right['#'], i]);
}
}
if (word && right['*']) {
routes.push([right['*'], index + 1]);
}
}
return finalRoutes;
}
function getCachedList(topicStr, graph, cache) {
var list;
if (cache[topicStr]) { // use previous work if available
list = cache[topicStr];
} else {
list = getList(topicStr.split('.'), graph);
cache[topicStr] = list; // remember previous work
}
return list;
}
var Bus = function Bus() {
var head = { w: '', r: {}, i: headCount++ };
var emitCache = {}; // memoize graph lookups
var historyCache = new Ring(historyMax);
function getHistory(topicStr) {
var partialGraph = { w: '', r: {}, i: headCount++ };
var lastNode = add(topicStr.split('.'), partialGraph);
lastNode.fn = 1;
var timeline = [];
var cache = {};
var log = historyCache.asArray();
for (var i = 0; i < log.length; i++) {
var entry = log[i];
if(getCachedList(entry[0], partialGraph, cache).length) {
timeline.push(entry);
}
}
return timeline;
}
function on(topicStr, fn) {
var lastNode = add(topicStr.split('.'), head);
var fnList = lastNode.fn || [];
fnList.push(fn);
lastNode.fn = fnList;
emitCache = {}; // forget graph lookups because everything has changed
// return off() function to unsubscribe
return function () {
var index = fnList.indexOf(fn);
if (index > -1) {
fnList.splice(index, 1);
}
}
}
function emit(topicStr, message) {
var ts = Date.now();
historyCache.push([topicStr, ts]);
var list = getCachedList(topicStr, head, emitCache);
var meta = {topic: topicStr};
for (var i = 0; i < list.length; i++) {
var fn = list[i];
for (var j = 0; j < fn.length; j++) {
fn[j](message, meta);
}
}
}
// public methods
this.emit = emit;
this.on = on;
this.history = getHistory;
// alias
this.publish = emit;
this.subscribe = on;
};
// public classes
Bus.Ring = Ring;
return Bus;
}());
if (typeof exports !== 'undefined') {
module.exports = Bus;
}