This repository has been archived by the owner on Jul 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ampersand-io.js
147 lines (134 loc) · 3.77 KB
/
ampersand-io.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
/*$AMPERSAND_VERSION*/
var extend = require('ampersand-class-extend');
var io = require ('socket.io-client');
var AmpersandIO = function(socket, options){
options || (options = {});
this.events = options.events || this.events || {};
if(socket){
this.socket = {};
if(typeof socket === 'string'){
socket = io(socket);
}
this.socket = socket;
}
else{
// this.socket = clone(this.socket, true);
this.socket = this.socket.connect();
}
if(options.listeners){
this.listeners = {};
this.addListeners(options.listeners);
}
else{
this.listeners = this.listeners || {};
if(options.initListeners){
this.setListeners();
}
}
};
AmpersandIO.prototype = {
socket: io,
_setCallback: function(fn){
var self = this;
return function(){
fn.apply(self, arguments);
};
},
_setListener: function(event, listener){
this.listeners[listener]._callbacks[event] = this._setCallback(this.listeners[listener].fn);
this.socket.on(event, this.listeners[listener]._callbacks[event]);
},
_removeListener: function(event, listener){
this.socket.removeListener(event, this.listeners[listener]._callbacks[event]);
},
addListeners: function(listeners){
for(var listenerID in listeners){
if(!listeners.hasOwnProperty(listenerID)){
continue;
}
if(this.listeners[listenerID] && this.listeners[listenerID].active){
continue;
}
var listener = listeners[listenerID], events = listenerID;
this.listeners[listenerID] = listener;
this.listeners[listenerID]._callbacks = {};
if(this.events[listenerID]){
events = this.events[listenerID];
}
if(typeof events === 'string'){
events = [events];
}
for(var i = 0; i < events.length; i++){
var id = events[i];
if(listener.active){
this._setListener(id, listenerID);
}
}
}
},
setListeners: function (listeners){
if(!listeners){
listeners = Object.keys(this.listeners);
}
for(var i = 0; i < listeners.length; i++){
var listenerID = listeners[i];
var listener = this.listeners[listenerID], events = listenerID;
if(!listener.active){
listener.active = true;
listener._callbacks = {};
events = this.events[listenerID] || events;
if(typeof events === 'string'){
events = [events];
}
for(var j = 0; j < events.length; j++){
var id = events[j];
this._setListener(id, listenerID);
}
}
}
},
removeListeners: function (listeners){
if(!listeners){
listeners = Object.keys(this.listeners);
}
for(var i = 0; i < listeners.length; i++){
var listenerID = listeners[i];
var listener = this.listeners[listenerID], events = listenerID;
if(listener.active){
listener.active = false;
events = this.events[listenerID] || events;
if(typeof events === 'string'){
events = [events];
}
for(var j = 0; j < events.length; j++){
var id = events[j];
this._removeListener(id, listenerID);
}
}
}
},
emit: function (event, data, options, cb){
options || (options = {});
if(typeof options === 'function'){
cb = options;
options = {};
}
cb || (cb = options.callback);
if(this.events[event]){
event = this.events[event];
}
if(typeof event === 'string'){
event = [event];
}
for(var i = 0; i < event.length; i++){
if(options.room){
io.to(options.room).emit(event[i], {data: data, options: options}, cb);
}
else{
this.socket.emit(event[i], {data: data, options: options}, cb);
}
}
}
};
AmpersandIO.extend = extend;
module.exports = AmpersandIO;