Skip to content

Commit

Permalink
Replace When.js with ES6 Promise
Browse files Browse the repository at this point in the history
This shaves the minified browser build down from 42 kB to 12 kB.
  • Loading branch information
jodal committed Oct 9, 2018
1 parent 4d8fa63 commit 23ecc22
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 37 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,13 @@ yarn build
const Mopidy = new Mopidy(...);
```

- **Backwards incompatible:** The `Mopidy` class no longer reexports When.js
as `Mopidy.when()`. To upgrade existing code, either migrate to standard
ES6 `Promise` or add When.js as a dependency to your project.

- Modernized dependencies:

- The `Promise` object standardized in ES6 has replaced When.js.
- `isomorphic-ws` and `ws` has replaced our own wrapper around the browser's
`WebSocket` API and `faye-websocket` on Node.
Expand Down
29 changes: 11 additions & 18 deletions __tests__/mopidy.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const when = require("when");
const Mopidy = require("../src/mopidy");

const warn = jest.spyOn(global.console, "warn").mockImplementation(() => {});
Expand Down Expand Up @@ -192,16 +191,16 @@ describe("._cleanup", () => {
this.mopidy._cleanup(closeEvent);

expect(Object.keys(this.mopidy._pendingRequests).length).toBe(0);
when
.settle([promise1, promise2])
.then(descriptors => {
expect(descriptors.length).toBe(2);
descriptors.forEach(d => {
expect(d.state).toBe("rejected");
expect(d.reason).toBeInstanceOf(Error);
expect(d.reason).toBeInstanceOf(Mopidy.ConnectionError);
expect(d.reason.message).toBe("WebSocket closed");
expect(d.reason.closeEvent).toBe(closeEvent);
Promise.all([
promise1.catch(error => error),
promise2.catch(error => error),
])
.then(errors => {
errors.forEach(error => {
expect(error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(Mopidy.ConnectionError);
expect(error.message).toBe("WebSocket closed");
expect(error.closeEvent).toBe(closeEvent);
});
})
.then(done);
Expand Down Expand Up @@ -697,7 +696,7 @@ describe("._getApiSpec", () => {
const methods = {};
const sendStub = jest
.spyOn(this.mopidy, "_send")
.mockReturnValue(when.resolve(methods));
.mockReturnValue(Promise.resolve(methods));
const createApiStub = jest
.spyOn(this.mopidy, "_createApi")
.mockImplementation(() => {});
Expand Down Expand Up @@ -910,9 +909,3 @@ describe("._createApi", () => {
});
});
});

describe("Reexports", () => {
test("Reexports When.js", () => {
expect(Mopidy.when()).toEqual(when());
});
});
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"dependencies": {
"bane": "~1.1.0",
"isomorphic-ws": "^4.0.1",
"when": "~3.7.2",
"ws": "^6.1.0"
},
"devDependencies": {
Expand Down
27 changes: 14 additions & 13 deletions src/mopidy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const bane = require("bane");
const when = require("when");
const WebSocket = require("isomorphic-ws");

class Mopidy {
Expand Down Expand Up @@ -130,25 +129,29 @@ class Mopidy {
_send(message) {
switch (this._webSocket.readyState) {
case Mopidy.WebSocket.CONNECTING:
return when.reject(
return Promise.reject(
new Mopidy.ConnectionError("WebSocket is still connecting")
);
case Mopidy.WebSocket.CLOSING:
return when.reject(new Mopidy.ConnectionError("WebSocket is closing"));
return Promise.reject(
new Mopidy.ConnectionError("WebSocket is closing")
);
case Mopidy.WebSocket.CLOSED:
return when.reject(new Mopidy.ConnectionError("WebSocket is closed"));
return Promise.reject(
new Mopidy.ConnectionError("WebSocket is closed")
);
default:
}
const jsonRpcMessage = {
...message,
jsonrpc: "2.0",
id: this._nextRequestId(),
};
const deferred = when.defer();
this._pendingRequests[jsonRpcMessage.id] = deferred.resolver;
this._webSocket.send(JSON.stringify(jsonRpcMessage));
this.emit("websocket:outgoingMessage", jsonRpcMessage);
return deferred.promise;
return new Promise((resolve, reject) => {
this._pendingRequests[jsonRpcMessage.id] = { resolve, reject };
this._webSocket.send(JSON.stringify(jsonRpcMessage));
this.emit("websocket:outgoingMessage", jsonRpcMessage);
});
}

_handleMessage(message) {
Expand Down Expand Up @@ -233,14 +236,14 @@ class Mopidy {
return this._send(message);
}
if (args.length > 1) {
return when.reject(
return Promise.reject(
new Error(
"Expected zero arguments, a single array, or a single object."
)
);
}
if (!Array.isArray(args[0]) && args[0] !== Object(args[0])) {
return when.reject(new TypeError("Expected an array or an object."));
return Promise.reject(new TypeError("Expected an array or an object."));
}
[message.params] = args;
return this._send(message);
Expand Down Expand Up @@ -314,8 +317,6 @@ Mopidy.ServerError = ServerError;

Mopidy.WebSocket = WebSocket;

Mopidy.when = when;

Mopidy.prototype._nextRequestId = (() => {
let lastUsed = -1;
return () => {
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7334,11 +7334,6 @@ whatwg-url@^7.0.0:
tr46 "^1.0.1"
webidl-conversions "^4.0.2"

when@~3.7.2:
version "3.7.8"
resolved "https://registry.yarnpkg.com/when/-/when-3.7.8.tgz#c7130b6a7ea04693e842cdc9e7a1f2aa39a39f82"
integrity sha1-xxMLan6gRpPoQs3J56Hyqjmjn4I=

whet.extend@~0.9.9:
version "0.9.9"
resolved "https://registry.yarnpkg.com/whet.extend/-/whet.extend-0.9.9.tgz#f877d5bf648c97e5aa542fadc16d6a259b9c11a1"
Expand Down

0 comments on commit 23ecc22

Please sign in to comment.