Skip to content
Permalink
Browse files

Replace When.js with ES6 Promise

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 23ecc224d642bac0ab5b411acc370906af510348
Showing with 30 additions and 37 deletions.
  1. +5 −0 README.md
  2. +11 −18 __tests__/mopidy.test.js
  3. +0 −1 package.json
  4. +14 −13 src/mopidy.js
  5. +0 −5 yarn.lock
@@ -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.

@@ -1,4 +1,3 @@
const when = require("when");
const Mopidy = require("../src/mopidy");

const warn = jest.spyOn(global.console, "warn").mockImplementation(() => {});
@@ -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);
@@ -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(() => {});
@@ -910,9 +909,3 @@ describe("._createApi", () => {
});
});
});

describe("Reexports", () => {
test("Reexports When.js", () => {
expect(Mopidy.when()).toEqual(when());
});
});
@@ -40,7 +40,6 @@
"dependencies": {
"bane": "~1.1.0",
"isomorphic-ws": "^4.0.1",
"when": "~3.7.2",
"ws": "^6.1.0"
},
"devDependencies": {
@@ -1,5 +1,4 @@
const bane = require("bane");
const when = require("when");
const WebSocket = require("isomorphic-ws");

class Mopidy {
@@ -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) {
@@ -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);
@@ -314,8 +317,6 @@ Mopidy.ServerError = ServerError;

Mopidy.WebSocket = WebSocket;

Mopidy.when = when;

Mopidy.prototype._nextRequestId = (() => {
let lastUsed = -1;
return () => {
@@ -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"

0 comments on commit 23ecc22

Please sign in to comment.
You can’t perform that action at this time.