forked from cayasso/primus-emitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemitter.js
171 lines (148 loc) · 3.42 KB
/
emitter.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
164
165
166
167
168
169
170
171
module.exports = function emitter() {
'use strict';
var toString = Object.prototype.toString
, slice = Array.prototype.slice;
/**
* Check if the given `value` is an `Array`.
*
* @param {*} value The value to check
* @return {Boolean}
*/
var isArray = Array.isArray || function isArray(value) {
return '[object Array]' === toString.call(value);
};
/**
* Event packets.
*/
var packets = {
EVENT: 0,
ACK: 1
};
/**
* Initialize a new `Emitter`.
*
* @param {Primus|Spark} conn
* @return {Emitter} `Emitter` instance
* @api public
*/
function Emitter(conn) {
if (!(this instanceof Emitter)) return new Emitter(conn);
this.ids = 1;
this.acks = {};
this.conn = conn;
if (this.conn) this.bind();
}
/**
* Bind `Emitter` events.
*
* @return {Emitter} self
* @api private
*/
Emitter.prototype.bind = function bind() {
var em = this;
this.conn.on('data', function ondata(packet) {
em.ondata.call(em, packet);
});
return this;
};
/**
* Called with incoming transport data.
*
* @param {Object} packet
* @return {Emitter} self
* @api private
*/
Emitter.prototype.ondata = function ondata(packet) {
if (!isArray(packet.data) || packet.id && 'number' !== typeof packet.id) {
return this;
}
switch (packet.type) {
case packets.EVENT:
this.onevent(packet);
break;
case packets.ACK:
this.onack(packet);
}
return this;
};
/**
* Send a message to client.
*
* @return {Emitter} self
* @api public
*/
Emitter.prototype.send = function send() {
var args = slice.call(arguments);
this.conn.write(this.packet(args));
return this;
};
/**
* Prepare packet for emitting.
*
* @param {Array} arguments
* @return {Object} packet
* @api private
*/
Emitter.prototype.packet = function pack(args) {
var packet = { type: packets.EVENT, data: args };
// access last argument to see if it's an ACK callback
if ('function' === typeof args[args.length - 1]) {
var id = this.ids++;
this.acks[id] = args.pop();
packet.id = id;
}
return packet;
};
/**
* Called upon event packet.
*
* @param {Object} packet object
* @return {Emitter} self
* @api private
*/
Emitter.prototype.onevent = function onevent(packet) {
var args = packet.data;
if (this.conn.reserved(args[0])) return this;
if (packet.id) args.push(this.ack(packet.id));
this.conn.emit.apply(this.conn, args);
return this;
};
/**
* Produces an ack callback to emit with an event.
*
* @param {Number} packet id
* @return {Function}
* @api private
*/
Emitter.prototype.ack = function ack(id) {
var conn = this.conn
, sent = false;
return function () {
if (sent) return; // prevent double callbacks
sent = true;
conn.write({
id: id,
type: packets.ACK,
data: slice.call(arguments)
});
};
};
/**
* Called upon ack packet.
*
* @param {Object} packet object
* @return {Emitter} self
* @api private
*/
Emitter.prototype.onack = function onack(packet) {
var ack = this.acks[packet.id];
if ('function' === typeof ack) {
ack.apply(this, packet.data);
delete this.acks[packet.id];
}
return this;
};
// Expose packets
Emitter.packets = packets;
return Emitter;
};