freedom-social-xmpp
Advanced tools
Comparing version 0.3.5 to 0.3.6
@@ -8,3 +8,3 @@ /*globals freedom:true,setTimeout,VCardStore,XMPPSocialProvider */ | ||
"http://freedomjs.org/", | ||
//'http://localhost/*', | ||
'http://localhost:8080/' | ||
]; | ||
@@ -11,0 +11,0 @@ XMPPSocialProvider.prototype.oAuthClientId = "746567772449-jkm5q5hjqtpq5m9htg9kn0os8qphra4d.apps.googleusercontent.com"; |
@@ -48,3 +48,5 @@ /*jslint white:true,sloppy:true */ | ||
this.timeOfFirstMessageInBatch = 0; | ||
this.messages = []; | ||
// buffered outbound messages. Arrays of of {message, callback} keyed by | ||
// recipient. | ||
this.messages = {}; | ||
@@ -73,3 +75,3 @@ // Logger | ||
errcode: 'UNKNOWN', | ||
message: 'No login function defined', | ||
message: 'No login function defined' | ||
//message: this.ERRCODE.LOGIN_OAUTHERROR | ||
@@ -238,42 +240,73 @@ }); | ||
// If the destination client is ONLINE (i.e. using the same type of client) | ||
// send this message with type 'normal' so it only reaches that client, | ||
// otherwise use type 'chat' to send to all clients. | ||
// Sending all messages as type 'normal' means we can't communicate across | ||
// different client types, but sending all as type 'chat' means messages | ||
// will be broadcast to all clients. | ||
var messageType = (this.vCardStore.getClient(to).status === 'ONLINE') ? | ||
'normal' : 'chat'; | ||
try { | ||
// After each message is received, reset the timeout to | ||
// wait for at least 100ms to batch other messages received | ||
// in that window. However, if the oldest message in the batch | ||
// was received over 2s ago, don't reset the timeout, and | ||
// just allow the current timeout to execute. | ||
this.messages.push(msg); | ||
if (!this.sendMessagesTimeout) { | ||
this.timeOfFirstMessageInBatch = Date.now(); | ||
} | ||
if ((Date.now() - this.timeOfFirstMessageInBatch < 2000) || | ||
!this.sendMessagesTimeout) { | ||
clearTimeout(this.sendMessagesTimeout); | ||
this.sendMessagesTimeout = setTimeout(function() { | ||
this.client.send(new window.XMPP.Element('message', { | ||
to: to, | ||
type: messageType | ||
}).c('body').t(JSON.stringify(this.messages))); | ||
this.messages = []; | ||
this.sendMessagesTimeout = null; | ||
}.bind(this), 100); | ||
} | ||
} catch(e) { | ||
this.logger.error(e.stack); | ||
continuation(undefined, { | ||
errcode: 'UNKNOWN', | ||
message: e.message | ||
}); | ||
return; | ||
// After each message is received, reset the timeout to | ||
// wait for at least 100ms to batch other messages received | ||
// in that window. However, if the oldest message in the batch | ||
// was received over 2s ago, don't reset the timeout, and | ||
// just allow the current timeout to execute. | ||
if (!this.messages[to]) { | ||
this.messages[to] = []; | ||
} | ||
continuation(); | ||
this.messages[to].push({ | ||
message: msg, | ||
continuation: continuation | ||
}); | ||
if (!this.sendMessagesTimeout) { | ||
this.timeOfFirstMessageInBatch = Date.now(); | ||
} | ||
if ((Date.now() - this.timeOfFirstMessageInBatch < 2000) || | ||
!this.sendMessagesTimeout) { | ||
clearTimeout(this.sendMessagesTimeout); | ||
this.sendMessagesTimeout = setTimeout(function () { | ||
Object.keys(this.messages).forEach(function (to) { | ||
// If the destination client is ONLINE (i.e. using the same type of | ||
// client) send this message with type 'normal' so it only reaches | ||
// that client - and use JSON encoding, which this class on the other | ||
// side will parse. otherwise use type 'chat' to send to all clients - | ||
// in this later case, messages are sent directly, since the goal is to | ||
// be human readable. Sending all messages as type 'normal' means we | ||
// can't communicate across different client types, but sending all as | ||
// type 'chat' means messages will be broadcast to all clients. | ||
var i = 0, | ||
messageType = | ||
(this.vCardStore.getClient(to).status === 'ONLINE') ? | ||
'normal' : 'chat', | ||
message = new window.XMPP.Element('message', { | ||
to: to, | ||
type: messageType | ||
}).c('body'), | ||
body; | ||
if (messageType === 'normal') { | ||
body = []; | ||
for (i = 0; i < this.messages[to].length; i += 1) { | ||
body.push(this.messages[to][i].message); | ||
} | ||
message.t(JSON.stringify(body)); | ||
} else { | ||
body = ''; | ||
for (i = 0; i < this.messages[to].length; i += 1) { | ||
body += this.messages[to][i].message + '\n'; | ||
} | ||
message.t(body); | ||
} | ||
try { | ||
this.client.send(message); | ||
for (i = 0; i < this.messages[to].length; i += 1) { | ||
this.messages[to][i].continuation(); | ||
} | ||
} catch(e) { | ||
for (i = 0; i < this.messages[to].length; i += 1) { | ||
this.messages[to][i].continuation(null, { | ||
errcode: 'UNKNOWN', | ||
message: 'Send Failed: ' + e.message | ||
}); | ||
} | ||
} | ||
}.bind(this)); | ||
this.messages = {}; | ||
this.sendMessagesTimeout = null; | ||
}.bind(this), 100); | ||
} | ||
}; | ||
@@ -280,0 +313,0 @@ |
{ | ||
"name": "freedom-social-xmpp", | ||
"description": "XMPP Social provider for freedomjs", | ||
"version": "0.3.5", | ||
"version": "0.3.6", | ||
"homepage": "http://freedomjs.org", | ||
@@ -6,0 +6,0 @@ "bugs": { |
@@ -51,3 +51,3 @@ describe("Tests for message batching in Social provider", function() { | ||
xmppSocialProvider.sendMessage('Bob', 'Hi', function() {}); | ||
expect(xmppSocialProvider.messages).toEqual(['Hi']); | ||
expect(xmppSocialProvider.messages.Bob[0].message).toEqual('Hi'); | ||
expect(xmppSocialProvider.timeOfFirstMessageInBatch).toEqual(500); | ||
@@ -69,2 +69,13 @@ }); | ||
it("calls callback after send", function() { | ||
var spy = jasmine.createSpy('callback'); | ||
xmppSocialProvider.sendMessage('Bob', 'Hi', spy); | ||
expect(xmppSocialProvider.client.send).not.toHaveBeenCalled(); | ||
expect(spy).not.toHaveBeenCalled(); | ||
jasmine.clock().tick(100); | ||
expect(xmppSocialProvider.client.send).toHaveBeenCalled(); | ||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
it("timeout resets to 100ms after each message", function() { | ||
@@ -77,3 +88,10 @@ xmppSocialProvider.sendMessage('Bob', 'Hi', function() {}); | ||
expect(xmppSocialProvider.client.send).not.toHaveBeenCalled(); | ||
expect(xmppSocialProvider.messages).toEqual(['Hi', 'Hi again']); | ||
expect(xmppSocialProvider.messages.Bob.length).toEqual(2); | ||
expect(xmppSocialProvider.messages.Bob).toEqual([{ | ||
message: 'Hi', | ||
continuation: jasmine.any(Function) | ||
}, { | ||
message: 'Hi again', | ||
continuation: jasmine.any(Function) | ||
}]); | ||
jasmine.clock().tick(50); | ||
@@ -108,2 +126,23 @@ expect(xmppSocialProvider.client.send).toHaveBeenCalled(); | ||
it("sends message to correct destinations", function() { | ||
xmppSocialProvider.sendMessage('Bob', 'Hi', function() {}); | ||
xmppSocialProvider.sendMessage('Alice', 'Hi', function() {}); | ||
expect(xmppSocialProvider.client.send).not.toHaveBeenCalled(); | ||
jasmine.clock().tick(100); | ||
expect(xmppSocialProvider.client.send.calls.count()).toEqual(2); | ||
var dest = xmppSocialProvider.client.send.calls.first().args[0].parent.attrs.to; | ||
// Destination shold be either bob or alice. | ||
if (dest === 'Bob') { | ||
expect(xmppSocialProvider.client.send.calls.mostRecent().args[0].parent.attrs.to) | ||
.toEqual('Alice'); | ||
} else if (dest === 'Alice') { | ||
expect(xmppSocialProvider.client.send.calls.mostRecent().args[0].parent.attrs.to) | ||
.toEqual('Bob'); | ||
} else { | ||
// If it isn't either, this expectation will certainly fail. | ||
expect(dest).toEqual('Bob'); | ||
} | ||
}); | ||
it('sets status to OFFLINE when client disconnected', function() { | ||
@@ -110,0 +149,0 @@ spyOn(window.XMPP, 'Client').and.returnValue(xmppClient); |
@@ -8,3 +8,3 @@ /*globals freedom:true,setTimeout,VCardStore,XMPPSocialProvider */ | ||
"http://freedomjs.org/", | ||
//'http://localhost/*', | ||
'http://localhost:8080/' | ||
]; | ||
@@ -11,0 +11,0 @@ XMPPSocialProvider.prototype.oAuthClientId = "746567772449-jkm5q5hjqtpq5m9htg9kn0os8qphra4d.apps.googleusercontent.com"; |
@@ -48,3 +48,5 @@ /*jslint white:true,sloppy:true */ | ||
this.timeOfFirstMessageInBatch = 0; | ||
this.messages = []; | ||
// buffered outbound messages. Arrays of of {message, callback} keyed by | ||
// recipient. | ||
this.messages = {}; | ||
@@ -73,3 +75,3 @@ // Logger | ||
errcode: 'UNKNOWN', | ||
message: 'No login function defined', | ||
message: 'No login function defined' | ||
//message: this.ERRCODE.LOGIN_OAUTHERROR | ||
@@ -238,42 +240,73 @@ }); | ||
// If the destination client is ONLINE (i.e. using the same type of client) | ||
// send this message with type 'normal' so it only reaches that client, | ||
// otherwise use type 'chat' to send to all clients. | ||
// Sending all messages as type 'normal' means we can't communicate across | ||
// different client types, but sending all as type 'chat' means messages | ||
// will be broadcast to all clients. | ||
var messageType = (this.vCardStore.getClient(to).status === 'ONLINE') ? | ||
'normal' : 'chat'; | ||
try { | ||
// After each message is received, reset the timeout to | ||
// wait for at least 100ms to batch other messages received | ||
// in that window. However, if the oldest message in the batch | ||
// was received over 2s ago, don't reset the timeout, and | ||
// just allow the current timeout to execute. | ||
this.messages.push(msg); | ||
if (!this.sendMessagesTimeout) { | ||
this.timeOfFirstMessageInBatch = Date.now(); | ||
} | ||
if ((Date.now() - this.timeOfFirstMessageInBatch < 2000) || | ||
!this.sendMessagesTimeout) { | ||
clearTimeout(this.sendMessagesTimeout); | ||
this.sendMessagesTimeout = setTimeout(function() { | ||
this.client.send(new window.XMPP.Element('message', { | ||
to: to, | ||
type: messageType | ||
}).c('body').t(JSON.stringify(this.messages))); | ||
this.messages = []; | ||
this.sendMessagesTimeout = null; | ||
}.bind(this), 100); | ||
} | ||
} catch(e) { | ||
this.logger.error(e.stack); | ||
continuation(undefined, { | ||
errcode: 'UNKNOWN', | ||
message: e.message | ||
}); | ||
return; | ||
// After each message is received, reset the timeout to | ||
// wait for at least 100ms to batch other messages received | ||
// in that window. However, if the oldest message in the batch | ||
// was received over 2s ago, don't reset the timeout, and | ||
// just allow the current timeout to execute. | ||
if (!this.messages[to]) { | ||
this.messages[to] = []; | ||
} | ||
continuation(); | ||
this.messages[to].push({ | ||
message: msg, | ||
continuation: continuation | ||
}); | ||
if (!this.sendMessagesTimeout) { | ||
this.timeOfFirstMessageInBatch = Date.now(); | ||
} | ||
if ((Date.now() - this.timeOfFirstMessageInBatch < 2000) || | ||
!this.sendMessagesTimeout) { | ||
clearTimeout(this.sendMessagesTimeout); | ||
this.sendMessagesTimeout = setTimeout(function () { | ||
Object.keys(this.messages).forEach(function (to) { | ||
// If the destination client is ONLINE (i.e. using the same type of | ||
// client) send this message with type 'normal' so it only reaches | ||
// that client - and use JSON encoding, which this class on the other | ||
// side will parse. otherwise use type 'chat' to send to all clients - | ||
// in this later case, messages are sent directly, since the goal is to | ||
// be human readable. Sending all messages as type 'normal' means we | ||
// can't communicate across different client types, but sending all as | ||
// type 'chat' means messages will be broadcast to all clients. | ||
var i = 0, | ||
messageType = | ||
(this.vCardStore.getClient(to).status === 'ONLINE') ? | ||
'normal' : 'chat', | ||
message = new window.XMPP.Element('message', { | ||
to: to, | ||
type: messageType | ||
}).c('body'), | ||
body; | ||
if (messageType === 'normal') { | ||
body = []; | ||
for (i = 0; i < this.messages[to].length; i += 1) { | ||
body.push(this.messages[to][i].message); | ||
} | ||
message.t(JSON.stringify(body)); | ||
} else { | ||
body = ''; | ||
for (i = 0; i < this.messages[to].length; i += 1) { | ||
body += this.messages[to][i].message + '\n'; | ||
} | ||
message.t(body); | ||
} | ||
try { | ||
this.client.send(message); | ||
for (i = 0; i < this.messages[to].length; i += 1) { | ||
this.messages[to][i].continuation(); | ||
} | ||
} catch(e) { | ||
for (i = 0; i < this.messages[to].length; i += 1) { | ||
this.messages[to][i].continuation(null, { | ||
errcode: 'UNKNOWN', | ||
message: 'Send Failed: ' + e.message | ||
}); | ||
} | ||
} | ||
}.bind(this)); | ||
this.messages = {}; | ||
this.sendMessagesTimeout = null; | ||
}.bind(this), 100); | ||
} | ||
}; | ||
@@ -280,0 +313,0 @@ |
@@ -5,3 +5,8 @@ { | ||
"app": { | ||
"script": "demo.js" | ||
"script": "demo.js", | ||
"index": "main.html", | ||
"static": [ | ||
"style.css", | ||
"ux.js" | ||
] | ||
}, | ||
@@ -8,0 +13,0 @@ "dependencies": { |
@@ -5,3 +5,8 @@ { | ||
"app": { | ||
"script": "demo.js" | ||
"script": "demo.js", | ||
"index": "main.html", | ||
"static": [ | ||
"style.css", | ||
"ux.js" | ||
] | ||
}, | ||
@@ -8,0 +13,0 @@ "dependencies": { |
@@ -5,3 +5,8 @@ { | ||
"app": { | ||
"script": "demo.js" | ||
"script": "demo.js", | ||
"index": "main.html", | ||
"static": [ | ||
"style.css", | ||
"ux.js" | ||
] | ||
}, | ||
@@ -8,0 +13,0 @@ "dependencies": { |
@@ -136,3 +136,4 @@ /*globals freedom, console, self*/ | ||
freedom('demo.json').then(start); | ||
} else { // Assume it's Firefox | ||
} else if (typeof port !== 'undefined') { // Firefox | ||
port.emit('test', 'Initializing self.port'); | ||
start(function() { | ||
@@ -152,2 +153,4 @@ return { | ||
}); | ||
} else { | ||
console.error("Error initializing: cannot detect environment"); | ||
} | ||
@@ -157,2 +160,1 @@ //}.bind({}, self.port); | ||
self.port.emit('test', 'Initializing self.port'); |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
872232
26811
12