ableton-js
Advanced tools
Comparing version 3.0.1 to 3.1.0
@@ -7,4 +7,12 @@ ### Changelog | ||
#### [v3.1.0](https://github.com/leolabs/ableton.js/compare/v3.0.1...v3.1.0) | ||
- :art: Extract connect and disconnect handlers into separate functions [`955a6cb`](https://github.com/leolabs/ableton.js/commit/955a6cbf132a38c6616e5e61cb8304507557d32f) | ||
- :sparkles: Add a `waitForConnection` function that returns a Promise that resolves when Live is connected [`3323d3d`](https://github.com/leolabs/ableton.js/commit/3323d3dba256fe86eeb85c0c6931de73337f4369) | ||
- :sparkles: Try a ping request in addition to waiting for the connection event [`c019e68`](https://github.com/leolabs/ableton.js/commit/c019e68b7e9744698e331b9d1fcad92b520d1a95) | ||
#### [v3.0.1](https://github.com/leolabs/ableton.js/compare/v3.0.0...v3.0.1) | ||
> 25 February 2023 | ||
- :memo: Add breaking change reminder to the changelog [`3158328`](https://github.com/leolabs/ableton.js/commit/31583280950ac4c9a4d8d425592c05481ee7b3e7) | ||
@@ -11,0 +19,0 @@ - :sparkles: Don't allow starting the server multiple times [`83a42b1`](https://github.com/leolabs/ableton.js/commit/83a42b1186ede6820efa13c6cec7b599693a1bb5) |
@@ -21,6 +21,7 @@ /// <reference types="node" /> | ||
} | ||
declare type ConnectionEventType = "realtime" | "heartbeat"; | ||
declare type DisconnectEventType = "realtime" | "heartbeat"; | ||
declare type ConnectEventType = DisconnectEventType | "start"; | ||
interface ConnectionEventEmitter { | ||
on(e: "connect", l: (t: ConnectionEventType) => void): this; | ||
on(e: "disconnect", l: (t: ConnectionEventType) => void): this; | ||
on(e: "connect", l: (t: ConnectEventType) => void): this; | ||
on(e: "disconnect", l: (t: DisconnectEventType) => void): this; | ||
on(e: "message", l: (t: any) => void): this; | ||
@@ -54,3 +55,2 @@ on(e: "error", l: (t: Error) => void): this; | ||
private _isConnected; | ||
private cancelConnectionEvent; | ||
private buffer; | ||
@@ -69,3 +69,10 @@ private latency; | ||
constructor(options?: AbletonOptions | undefined); | ||
private handleConnect; | ||
private handleDisconnect; | ||
/** | ||
* If connected, returns immediately. Otherwise, | ||
* it waits for a connection event before returning. | ||
*/ | ||
waitForConnection(): Promise<unknown>; | ||
/** | ||
* Starts the server and waits for a connection with Live to be established. | ||
@@ -72,0 +79,0 @@ * |
94
index.js
@@ -128,3 +128,2 @@ "use strict"; | ||
_this._isConnected = false; | ||
_this.cancelConnectionEvent = false; | ||
_this.buffer = []; | ||
@@ -143,3 +142,39 @@ _this.latency = 0; | ||
} | ||
Ableton.prototype.handleConnect = function (type) { | ||
if (!this._isConnected) { | ||
this._isConnected = true; | ||
this.emit("connect", type); | ||
} | ||
}; | ||
Ableton.prototype.handleDisconnect = function (type) { | ||
if (this._isConnected) { | ||
this._isConnected = false; | ||
this.eventListeners.clear(); | ||
this.msgMap.forEach(function (msg) { return msg.clearTimeout(); }); | ||
this.msgMap.clear(); | ||
this.emit("disconnect", type); | ||
} | ||
}; | ||
/** | ||
* If connected, returns immediately. Otherwise, | ||
* it waits for a connection event before returning. | ||
*/ | ||
Ableton.prototype.waitForConnection = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
if (this._isConnected) { | ||
return [2 /*return*/]; | ||
} | ||
else { | ||
return [2 /*return*/, Promise.race([ | ||
new Promise(function (res) { return _this.once("connect", res); }), | ||
this.internal.get("ping").catch(function () { }), | ||
])]; | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Starts the server and waits for a connection with Live to be established. | ||
@@ -234,3 +269,3 @@ * | ||
(_b = this.logger) === null || _b === void 0 ? void 0 : _b.info("Checking connection..."); | ||
connection = new Promise(function (res) { return _this.once("connect", res); }); | ||
connection = this.waitForConnection(); | ||
if (!timeoutMs) return [3 /*break*/, 3]; | ||
@@ -251,2 +286,3 @@ timeout = new Promise(function (_, rej) { | ||
this.clientState = "started"; | ||
this.handleConnect("start"); | ||
heartbeat = function () { return __awaiter(_this, void 0, void 0, function () { | ||
@@ -257,25 +293,13 @@ var e_2; | ||
case 0: | ||
this.cancelConnectionEvent = false; | ||
_a.label = 1; | ||
_a.trys.push([0, 2, , 3]); | ||
return [4 /*yield*/, this.internal.get("ping")]; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, this.internal.get("ping")]; | ||
_a.sent(); | ||
this.handleConnect("heartbeat"); | ||
return [3 /*break*/, 3]; | ||
case 2: | ||
_a.sent(); | ||
if (!this._isConnected && !this.cancelConnectionEvent) { | ||
this._isConnected = true; | ||
this.emit("connect", "heartbeat"); | ||
} | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
e_2 = _a.sent(); | ||
if (this._isConnected && !this.cancelConnectionEvent) { | ||
this._isConnected = false; | ||
this.eventListeners.clear(); | ||
this.msgMap.forEach(function (msg) { return msg.clearTimeout(); }); | ||
this.msgMap.clear(); | ||
this.emit("disconnect", "heartbeat"); | ||
} | ||
return [3 /*break*/, 4]; | ||
case 4: return [2 /*return*/]; | ||
this.handleDisconnect("heartbeat"); | ||
return [3 /*break*/, 3]; | ||
case 3: return [2 /*return*/]; | ||
} | ||
@@ -309,3 +333,2 @@ }); | ||
case 0: | ||
this.cancelConnectionEvent = true; | ||
fs_1.unwatchFile(this.serverPortFile); | ||
@@ -324,2 +347,3 @@ if (this.heartbeatInterval) { | ||
this.clientState = "closed"; | ||
this._isConnected = false; | ||
return [2 /*return*/]; | ||
@@ -342,2 +366,3 @@ } | ||
Ableton.prototype.handleIncoming = function (msg, info) { | ||
var _a; | ||
try { | ||
@@ -355,2 +380,3 @@ var index = msg[0]; | ||
this.buffer = []; | ||
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.warn("Couldn't handle message:", { error: e }); | ||
this.emit("error", e); | ||
@@ -360,3 +386,3 @@ } | ||
Ableton.prototype.handleUncompressedMessage = function (msg) { | ||
var _a; | ||
var _a, _b; | ||
this.emit("raw_message", msg); | ||
@@ -375,11 +401,3 @@ var data = JSON.parse(msg); | ||
if (data.event === "disconnect") { | ||
this.eventListeners.clear(); | ||
this.msgMap.forEach(function (msg) { return msg.clearTimeout(); }); | ||
this.msgMap.clear(); | ||
if (this._isConnected === true) { | ||
this._isConnected = false; | ||
this.cancelConnectionEvent = true; | ||
this.emit("disconnect", "realtime"); | ||
} | ||
return; | ||
return this.handleDisconnect("realtime"); | ||
} | ||
@@ -393,8 +411,3 @@ if (data.event === "connect") { | ||
} | ||
if (this._isConnected === false) { | ||
this._isConnected = true; | ||
this.cancelConnectionEvent = true; | ||
this.emit("connect", "realtime"); | ||
} | ||
return; | ||
return this.handleConnect(this.clientState === "starting" ? "start" : "realtime"); | ||
} | ||
@@ -407,2 +420,5 @@ var eventCallback = this.eventListeners.get(data.event); | ||
this.emit("error", "Message could not be assigned to any request: " + msg); | ||
(_b = this.logger) === null || _b === void 0 ? void 0 : _b.warn("Message could not be assigned to any request:", { | ||
msg: msg, | ||
}); | ||
} | ||
@@ -409,0 +425,0 @@ }; |
{ | ||
"name": "ableton-js", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "Control Ableton Live from Node", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
250970
4856