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

Add JsonRpc notification support #65

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions src/mopidy.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ class Mopidy extends EventEmitter {
this._handleResponse(data);
} else if (Object.hasOwnProperty.call(data, "event")) {
this._handleEvent(data);
} else if (Object.hasOwnProperty.call(data, "method")) {
this._handleJsonRpcNotification(data);
} else {
this._console.warn(
`Unknown message type received. Message was: ${message.data}`
Expand Down Expand Up @@ -251,6 +253,12 @@ class Mopidy extends EventEmitter {
this.emit(eventName, data);
}

_handleJsonRpcNotification(notificaiton) {
const eventName = `event:${snakeToCamel(notificaiton.method)}`;
this.emit("event", eventName, notificaiton.params);
this.emit(eventName, notificaiton.params);
}

_getApiSpec() {
return this._send({ method: "core.describe" })
.then(this._createApi.bind(this))
Expand Down
32 changes: 32 additions & 0 deletions test/mopidy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,38 @@
});
});

describe("._handleJsonRpcNotification", () => {
test("emits all server side events on 'event' event", () => {
const spy = jest.fn();
this.mopidy.on("event", spy);
const track = {};
const message = {
jsonrpc: "2.0",
method: "track_playback_started",
params:{track},

Check failure on line 799 in test/mopidy.test.js

View workflow job for this annotation

GitHub Actions / Test: Node 18

Replace `{track` with `·{·track·`

Check failure on line 799 in test/mopidy.test.js

View workflow job for this annotation

GitHub Actions / Test: Node 20

Replace `{track` with `·{·track·`
};

this.mopidy._handleJsonRpcNotification(message);

expect(spy).toBeCalledWith("event:trackPlaybackStarted", { track });
});

test("emits server side events on 'event:*' events", () => {
const spy = jest.fn();
this.mopidy.on("event:trackPlaybackStarted", spy);
const track = {};
const message = {
jsonrpc: "2.0",
method: "track_playback_started",
params:{track},

Check failure on line 814 in test/mopidy.test.js

View workflow job for this annotation

GitHub Actions / Test: Node 18

Replace `{track` with `·{·track·`

Check failure on line 814 in test/mopidy.test.js

View workflow job for this annotation

GitHub Actions / Test: Node 20

Replace `{track` with `·{·track·`
};

this.mopidy._handleJsonRpcNotification(message);

expect(spy).toBeCalledWith({ track });
});
});

describe("._getApiSpec", () => {
test("is called on 'websocket:open' event", () => {
const spy = jest.spyOn(this.mopidy, "_getApiSpec");
Expand Down
Loading