ableton-js 3.0.1 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,8 +4,23 @@ All notable changes to this project will be documented in this file. Dates are d
4
4
 
5
5
  Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
6
6
 
7
+ #### [v3.1.1](https://github.com/leolabs/ableton.js/compare/v3.1.0...v3.1.1)
8
+
9
+ - :loud_sound: Log connect and disconnect events [`e69f610`](https://github.com/leolabs/ableton.js/commit/e69f61098d61fd4027db4e94aad8155db5dbb504)
10
+ - :bug: Don't count a failed ping as a successful connection event [`217e91e`](https://github.com/leolabs/ableton.js/commit/217e91ed936326c29a391ace9dcbe831125f4fa2)
11
+
12
+ #### [v3.1.0](https://github.com/leolabs/ableton.js/compare/v3.0.1...v3.1.0)
13
+
14
+ > 25 February 2023
15
+
16
+ - :art: Extract connect and disconnect handlers into separate functions [`955a6cb`](https://github.com/leolabs/ableton.js/commit/955a6cbf132a38c6616e5e61cb8304507557d32f)
17
+ - :sparkles: Add a `waitForConnection` function that returns a Promise that resolves when Live is connected [`3323d3d`](https://github.com/leolabs/ableton.js/commit/3323d3dba256fe86eeb85c0c6931de73337f4369)
18
+ - :sparkles: Try a ping request in addition to waiting for the connection event [`c019e68`](https://github.com/leolabs/ableton.js/commit/c019e68b7e9744698e331b9d1fcad92b520d1a95)
19
+
7
20
  #### [v3.0.1](https://github.com/leolabs/ableton.js/compare/v3.0.0...v3.0.1)
8
21
 
22
+ > 25 February 2023
23
+
9
24
  - :memo: Add breaking change reminder to the changelog [`3158328`](https://github.com/leolabs/ableton.js/commit/31583280950ac4c9a4d8d425592c05481ee7b3e7)
10
25
  - :sparkles: Don't allow starting the server multiple times [`83a42b1`](https://github.com/leolabs/ableton.js/commit/83a42b1186ede6820efa13c6cec7b599693a1bb5)
11
26
 
package/index.d.ts CHANGED
@@ -18,10 +18,11 @@ interface Command {
18
18
  [k: string]: any;
19
19
  };
20
20
  }
21
- declare type ConnectionEventType = "realtime" | "heartbeat";
21
+ declare type DisconnectEventType = "realtime" | "heartbeat";
22
+ declare type ConnectEventType = DisconnectEventType | "start";
22
23
  interface ConnectionEventEmitter {
23
- on(e: "connect", l: (t: ConnectionEventType) => void): this;
24
- on(e: "disconnect", l: (t: ConnectionEventType) => void): this;
24
+ on(e: "connect", l: (t: ConnectEventType) => void): this;
25
+ on(e: "disconnect", l: (t: DisconnectEventType) => void): this;
25
26
  on(e: "message", l: (t: any) => void): this;
26
27
  on(e: "error", l: (t: Error) => void): this;
27
28
  on(e: "ping", l: (t: number) => void): this;
@@ -50,7 +51,6 @@ export declare class Ableton extends EventEmitter implements ConnectionEventEmit
50
51
  private eventListeners;
51
52
  private heartbeatInterval;
52
53
  private _isConnected;
53
- private cancelConnectionEvent;
54
54
  private buffer;
55
55
  private latency;
56
56
  private serverPort;
@@ -64,6 +64,13 @@ export declare class Ableton extends EventEmitter implements ConnectionEventEmit
64
64
  private logger;
65
65
  private clientState;
66
66
  constructor(options?: AbletonOptions | undefined);
67
+ private handleConnect;
68
+ private handleDisconnect;
69
+ /**
70
+ * If connected, returns immediately. Otherwise,
71
+ * it waits for a connection event before returning.
72
+ */
73
+ waitForConnection(): Promise<unknown>;
67
74
  /**
68
75
  * Starts the server and waits for a connection with Live to be established.
69
76
  *
package/index.js CHANGED
@@ -125,7 +125,6 @@ var Ableton = /** @class */ (function (_super) {
125
125
  _this.msgMap = new Map();
126
126
  _this.eventListeners = new Map();
127
127
  _this._isConnected = false;
128
- _this.cancelConnectionEvent = false;
129
128
  _this.buffer = [];
130
129
  _this.latency = 0;
131
130
  _this.song = new song_1.Song(_this);
@@ -139,6 +138,46 @@ var Ableton = /** @class */ (function (_super) {
139
138
  _this.serverPortFile = path_1.default.join(os_1.default.tmpdir(), (_d = (_c = _this.options) === null || _c === void 0 ? void 0 : _c.serverPortFile) !== null && _d !== void 0 ? _d : SERVER_PORT_FILE);
140
139
  return _this;
141
140
  }
141
+ Ableton.prototype.handleConnect = function (type) {
142
+ var _a;
143
+ if (!this._isConnected) {
144
+ this._isConnected = true;
145
+ (_a = this.logger) === null || _a === void 0 ? void 0 : _a.info("Live connected", { type: type });
146
+ this.emit("connect", type);
147
+ }
148
+ };
149
+ Ableton.prototype.handleDisconnect = function (type) {
150
+ var _a;
151
+ if (this._isConnected) {
152
+ this._isConnected = false;
153
+ this.eventListeners.clear();
154
+ this.msgMap.forEach(function (msg) { return msg.clearTimeout(); });
155
+ this.msgMap.clear();
156
+ (_a = this.logger) === null || _a === void 0 ? void 0 : _a.info("Live disconnected", { type: type });
157
+ this.emit("disconnect", type);
158
+ }
159
+ };
160
+ /**
161
+ * If connected, returns immediately. Otherwise,
162
+ * it waits for a connection event before returning.
163
+ */
164
+ Ableton.prototype.waitForConnection = function () {
165
+ return __awaiter(this, void 0, void 0, function () {
166
+ var _this = this;
167
+ return __generator(this, function (_a) {
168
+ if (this._isConnected) {
169
+ return [2 /*return*/];
170
+ }
171
+ else {
172
+ return [2 /*return*/, Promise.race([
173
+ new Promise(function (res) { return _this.once("connect", res); }),
174
+ this.internal.get("ping").catch(function () { return new Promise(function () { }); }),
175
+ ])];
176
+ }
177
+ return [2 /*return*/];
178
+ });
179
+ });
180
+ };
142
181
  /**
143
182
  * Starts the server and waits for a connection with Live to be established.
144
183
  *
@@ -229,7 +268,7 @@ var Ableton = /** @class */ (function (_super) {
229
268
  // Wait for the server port file to exist
230
269
  _f.sent();
231
270
  (_b = this.logger) === null || _b === void 0 ? void 0 : _b.info("Checking connection...");
232
- connection = new Promise(function (res) { return _this.once("connect", res); });
271
+ connection = this.waitForConnection();
233
272
  if (!timeoutMs) return [3 /*break*/, 3];
234
273
  timeout = new Promise(function (_, rej) {
235
274
  return setTimeout(function () { return rej("Connection timed out."); }, timeoutMs);
@@ -245,34 +284,23 @@ var Ableton = /** @class */ (function (_super) {
245
284
  case 5:
246
285
  (_c = this.logger) === null || _c === void 0 ? void 0 : _c.info("Got connection!");
247
286
  this.clientState = "started";
287
+ this.handleConnect("start");
248
288
  heartbeat = function () { return __awaiter(_this, void 0, void 0, function () {
249
289
  var e_2;
250
290
  return __generator(this, function (_a) {
251
291
  switch (_a.label) {
252
292
  case 0:
253
- this.cancelConnectionEvent = false;
254
- _a.label = 1;
255
- case 1:
256
- _a.trys.push([1, 3, , 4]);
293
+ _a.trys.push([0, 2, , 3]);
257
294
  return [4 /*yield*/, this.internal.get("ping")];
258
- case 2:
295
+ case 1:
259
296
  _a.sent();
260
- if (!this._isConnected && !this.cancelConnectionEvent) {
261
- this._isConnected = true;
262
- this.emit("connect", "heartbeat");
263
- }
264
- return [3 /*break*/, 4];
265
- case 3:
297
+ this.handleConnect("heartbeat");
298
+ return [3 /*break*/, 3];
299
+ case 2:
266
300
  e_2 = _a.sent();
267
- if (this._isConnected && !this.cancelConnectionEvent) {
268
- this._isConnected = false;
269
- this.eventListeners.clear();
270
- this.msgMap.forEach(function (msg) { return msg.clearTimeout(); });
271
- this.msgMap.clear();
272
- this.emit("disconnect", "heartbeat");
273
- }
274
- return [3 /*break*/, 4];
275
- case 4: return [2 /*return*/];
301
+ this.handleDisconnect("heartbeat");
302
+ return [3 /*break*/, 3];
303
+ case 3: return [2 /*return*/];
276
304
  }
277
305
  });
278
306
  }); };
@@ -301,7 +329,6 @@ var Ableton = /** @class */ (function (_super) {
301
329
  return __generator(this, function (_a) {
302
330
  switch (_a.label) {
303
331
  case 0:
304
- this.cancelConnectionEvent = true;
305
332
  fs_1.unwatchFile(this.serverPortFile);
306
333
  if (this.heartbeatInterval) {
307
334
  clearInterval(this.heartbeatInterval);
@@ -315,6 +342,7 @@ var Ableton = /** @class */ (function (_super) {
315
342
  _a.label = 2;
316
343
  case 2:
317
344
  this.clientState = "closed";
345
+ this._isConnected = false;
318
346
  return [2 /*return*/];
319
347
  }
320
348
  });
@@ -332,6 +360,7 @@ var Ableton = /** @class */ (function (_super) {
332
360
  this.emit("ping", this.latency);
333
361
  };
334
362
  Ableton.prototype.handleIncoming = function (msg, info) {
363
+ var _a;
335
364
  try {
336
365
  var index = msg[0];
337
366
  var message = msg.slice(1);
@@ -344,11 +373,12 @@ var Ableton = /** @class */ (function (_super) {
344
373
  }
345
374
  catch (e) {
346
375
  this.buffer = [];
376
+ (_a = this.logger) === null || _a === void 0 ? void 0 : _a.warn("Couldn't handle message:", { error: e });
347
377
  this.emit("error", e);
348
378
  }
349
379
  };
350
380
  Ableton.prototype.handleUncompressedMessage = function (msg) {
351
- var _a;
381
+ var _a, _b;
352
382
  this.emit("raw_message", msg);
353
383
  var data = JSON.parse(msg);
354
384
  var functionCallback = this.msgMap.get(data.uuid);
@@ -362,15 +392,7 @@ var Ableton = /** @class */ (function (_super) {
362
392
  return functionCallback.rej(new Error(data.data));
363
393
  }
364
394
  if (data.event === "disconnect") {
365
- this.eventListeners.clear();
366
- this.msgMap.forEach(function (msg) { return msg.clearTimeout(); });
367
- this.msgMap.clear();
368
- if (this._isConnected === true) {
369
- this._isConnected = false;
370
- this.cancelConnectionEvent = true;
371
- this.emit("disconnect", "realtime");
372
- }
373
- return;
395
+ return this.handleDisconnect("realtime");
374
396
  }
375
397
  if (data.event === "connect") {
376
398
  if (data.data.port && data.data.port !== this.serverPort) {
@@ -379,12 +401,7 @@ var Ableton = /** @class */ (function (_super) {
379
401
  });
380
402
  this.serverPort = data.data.port;
381
403
  }
382
- if (this._isConnected === false) {
383
- this._isConnected = true;
384
- this.cancelConnectionEvent = true;
385
- this.emit("connect", "realtime");
386
- }
387
- return;
404
+ return this.handleConnect(this.clientState === "starting" ? "start" : "realtime");
388
405
  }
389
406
  var eventCallback = this.eventListeners.get(data.event);
390
407
  if (eventCallback) {
@@ -392,6 +409,9 @@ var Ableton = /** @class */ (function (_super) {
392
409
  }
393
410
  if (data.uuid) {
394
411
  this.emit("error", "Message could not be assigned to any request: " + msg);
412
+ (_b = this.logger) === null || _b === void 0 ? void 0 : _b.warn("Message could not be assigned to any request:", {
413
+ msg: msg,
414
+ });
395
415
  }
396
416
  };
397
417
  /**
@@ -13,4 +13,4 @@ class Internal(Interface):
13
13
  return True
14
14
 
15
15
  def get_version(self, ns):
16
- return "3.0.1"
16
+ return "3.1.1"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ableton-js",
3
- "version": "3.0.1",
3
+ "version": "3.1.1",
4
4
  "description": "Control Ableton Live from Node",
5
5
  "main": "index.js",
6
6
  "author": "Leo Bernard <admin@leolabs.org>",