Skip to content

Commit

Permalink
Allow pubsub to publish falsy message
Browse files Browse the repository at this point in the history
refs #228
  • Loading branch information
rickmak committed Aug 17, 2017
2 parents 3e94222 + 6d21022 commit 2ca9626
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
5 changes: 3 additions & 2 deletions example/pubsub.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,15 @@ <h4>Publish Message</h4>
function publishMessage () {
var _channel = document.getElementById("publish-channel").value;
var _data = document.getElementById("publish-data").value;
skygear.pubsub.publish(_channel, _data);
skygear.pubsub.publish(_channel, JSON.parse(_data));
}

function subscribeChannel() {
var _channel = document.getElementById("subscribe-channel").value;
var _data = document.getElementById("subscribe-data");
skygear.pubsub.on(_channel, function(data) {
_data.value += 'Channel "' + _channel + '": ' + data + '\n';
var stringifyData = JSON.stringify(data);
_data.value += 'Channel "' + _channel + '": ' + stringifyData + '\n';
});
}

Expand Down
5 changes: 3 additions & 2 deletions packages/skygear-core/lib/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,9 @@ export class Pubsub {
throw new Error('Missing channel to publish');
}

if (!data) {
throw new Error('Missing data to publish');
const dataType = typeof data;
if (dataType !== 'object' || data === null || _.isArray(data)) {
throw new Error('Data must be object');
}

let publishData = {
Expand Down
18 changes: 12 additions & 6 deletions packages/skygear-core/test/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ describe('Pubsub', function () {
expect(JSON.parse(data)).to.deep.equal({
action: 'pub',
channel: 'CHANNEL',
data: 'DATA'
data: {
content: 'DATA'
}
});
});
pubsub.publish('CHANNEL', 'DATA');
pubsub.publish('CHANNEL', {content: 'DATA'});
expect(ws.send).to.be.calledOnce();
});

Expand Down Expand Up @@ -164,12 +166,14 @@ describe('Pubsub', function () {

it('resend queued messages on open when no websocket', function () {
pubsub._setWebSocket(null);
pubsub.publish('CHANNEL', 'MESSAGE');
pubsub.publish('CHANNEL', {content: 'MESSAGE'});
ws.send = sinon.spy(function (data) {
expect(JSON.parse(data)).to.deep.equal({
action: 'pub',
channel: 'CHANNEL',
data: 'MESSAGE'
data: {
content: 'MESSAGE'
}
});
});
pubsub._setWebSocket(ws);
Expand All @@ -180,14 +184,16 @@ describe('Pubsub', function () {
it('resend queued messages on open after disconnected', function () {
ws.readyState = 2;
ws.send = sinon.spy();
pubsub.publish('CHANNEL', 'MESSAGE');
pubsub.publish('CHANNEL', {content: 'MESSAGE'});
expect(ws.send).not.to.be.called();
ws.readyState = 1;
ws.send = sinon.spy(function (data) {
expect(JSON.parse(data)).to.deep.equal({
action: 'pub',
channel: 'CHANNEL',
data: 'MESSAGE'
data: {
content: 'MESSAGE'
}
});
});
ws.onopen();
Expand Down

0 comments on commit 2ca9626

Please sign in to comment.