-
Notifications
You must be signed in to change notification settings - Fork 0
/
faye.js
171 lines (147 loc) · 4.51 KB
/
faye.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
if (!this.Faye) Faye = {};
Faye.extend = function(dest, source, overwrite) {
if (!source) return dest;
for (var key in source) {
if (!source.hasOwnProperty(key)) continue;
if (dest.hasOwnProperty(key) && overwrite === false) continue;
if (dest[key] !== source[key])
dest[key] = source[key];
}
return dest;
};
Faye.extend(Faye, {
VERSION: '<%= Faye::VERSION %>',
BAYEUX_VERSION: '<%= Faye::BAYEUX_VERSION %>',
ID_LENGTH: <%= Faye::ID_LENGTH %>,
JSONP_CALLBACK: '<%= Faye::JSONP_CALLBACK %>',
CONNECTION_TYPES: ['long-polling', 'cross-origin-long-polling', 'callback-polling', 'websocket', 'in-process'],
MANDATORY_CONNECTION_TYPES: ['long-polling', 'callback-polling', 'in-process'],
ENV: (function() { return this })(),
random: function(bitlength) {
bitlength = bitlength || this.ID_LENGTH;
if (bitlength > 32) {
var parts = Math.ceil(bitlength / 32),
string = '';
while (parts--) string += this.random(32);
return string;
}
var limit = Math.pow(2, bitlength) - 1,
maxSize = limit.toString(36).length,
string = Math.floor(Math.random() * limit).toString(36);
while (string.length < maxSize) string = '0' + string;
return string;
},
commonElement: function(lista, listb) {
for (var i = 0, n = lista.length; i < n; i++) {
if (this.indexOf(listb, lista[i]) !== -1)
return lista[i];
}
return null;
},
indexOf: function(list, needle) {
for (var i = 0, n = list.length; i < n; i++) {
if (list[i] === needle) return i;
}
return -1;
},
each: function(object, callback, scope) {
if (object instanceof Array) {
for (var i = 0, n = object.length; i < n; i++) {
if (object[i] !== undefined)
callback.call(scope || null, object[i], i);
}
} else {
for (var key in object) {
if (object.hasOwnProperty(key))
callback.call(scope || null, key, object[key]);
}
}
},
map: function(object, callback, scope) {
if (object.map) return object.map(callback, scope);
var result = [];
this.each(object, function() {
result.push(callback.apply(scope || null, arguments));
});
return result;
},
filter: function(array, callback, scope) {
var result = [];
this.each(array, function() {
if (callback.apply(scope, arguments))
result.push(arguments[0]);
});
return result;
},
size: function(object) {
var size = 0;
this.each(object, function() { size += 1 });
return size;
},
enumEqual: function(actual, expected) {
if (expected instanceof Array) {
if (!(actual instanceof Array)) return false;
var i = actual.length;
if (i !== expected.length) return false;
while (i--) {
if (actual[i] !== expected[i]) return false;
}
return true;
} else {
if (!(actual instanceof Object)) return false;
if (this.size(expected) !== this.size(actual)) return false;
var result = true;
this.each(actual, function(key, value) {
result = result && (expected[key] === value);
});
return result;
}
},
asyncEach: function(list, iterator, callback, scope) {
var n = list.length,
i = -1,
calls = 0,
looping = false;
var iterate = function() {
calls -= 1;
i += 1;
if (i === n) return callback && callback.call(scope);
iterator(list[i], resume);
};
var loop = function() {
if (looping) return;
looping = true;
while (calls > 0) iterate();
looping = false;
};
var resume = function() {
calls += 1;
loop();
};
resume();
},
// http://assanka.net/content/tech/2009/09/02/json2-js-vs-prototype/
toJSON: function(object) {
if (this.stringify)
return this.stringify(object, function(key, value) {
return (this[key] instanceof Array)
? this[key]
: value;
});
return JSON.stringify(object);
},
timestamp: function() {
var date = new Date(),
year = date.getFullYear(),
month = date.getMonth() + 1,
day = date.getDate(),
hour = date.getHours(),
minute = date.getMinutes(),
second = date.getSeconds();
var pad = function(n) {
return n < 10 ? '0' + n : String(n);
};
return pad(year) + '-' + pad(month) + '-' + pad(day) + ' ' +
pad(hour) + ':' + pad(minute) + ':' + pad(second);
}
});