Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pubsub falsy message #297

Merged
merged 2 commits into from
Aug 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -114,8 +114,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