ableton-js
Advanced tools
Comparing version 1.3.3 to 1.4.0
/// <reference types="node" /> | ||
import dgram from "dgram"; | ||
import { EventEmitter } from "events"; | ||
import { Song } from "./ns/song"; | ||
export declare class Ableton { | ||
declare type ConnectionEventType = "realtime" | "heartbeat"; | ||
interface ConnectionEventEmitter { | ||
on(e: "connect", l: (t: ConnectionEventType) => void): this; | ||
on(e: "disconnect", l: (t: ConnectionEventType) => void): this; | ||
} | ||
export declare class Ableton extends EventEmitter implements ConnectionEventEmitter { | ||
private host; | ||
@@ -11,6 +17,7 @@ private sendPort; | ||
private eventListeners; | ||
private connectListeners; | ||
private disconnectListeners; | ||
heartbeatInterval: NodeJS.Timeout; | ||
isConnected: boolean; | ||
cancelConnectionEvent: boolean; | ||
song: Song; | ||
constructor(host?: string, sendPort?: number, listenPort?: number); | ||
constructor(host?: string, sendPort?: number, listenPort?: number, heartbeatInterval?: number); | ||
close(): void; | ||
@@ -21,9 +28,8 @@ handleIncoming(msg: Buffer, info: dgram.RemoteInfo): any; | ||
}, timeout?: number): Promise<any>; | ||
addConnectionListener(event: "connect" | "disconnect", listener: () => any): void; | ||
removeConnectionListener(event: "connect" | "disconnect", listener: () => any): void; | ||
getProp(ns: string, nsid: number | undefined, prop: string): Promise<any>; | ||
setProp(ns: string, nsid: number | undefined, prop: string, value: any): Promise<any>; | ||
addListener(ns: string, nsid: number | undefined, prop: string, listener: (data: any) => any): Promise<any>; | ||
removeListener(ns: string, nsid: number | undefined, prop: string, eventId: string): Promise<void>; | ||
addPropListener(ns: string, nsid: number | undefined, prop: string, listener: (data: any) => any): Promise<any>; | ||
removePropListener(ns: string, nsid: number | undefined, prop: string, eventId: string): Promise<void>; | ||
sendRaw(msg: string): void; | ||
} | ||
export {}; |
107
index.js
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -42,20 +55,53 @@ return new (P || (P = Promise))(function (resolve, reject) { | ||
var dgram_1 = __importDefault(require("dgram")); | ||
var events_1 = require("events"); | ||
var node_uuid_1 = __importDefault(require("node-uuid")); | ||
var song_1 = require("./ns/song"); | ||
var Ableton = /** @class */ (function () { | ||
function Ableton(host, sendPort, listenPort) { | ||
var Ableton = /** @class */ (function (_super) { | ||
__extends(Ableton, _super); | ||
function Ableton(host, sendPort, listenPort, heartbeatInterval) { | ||
if (host === void 0) { host = "127.0.0.1"; } | ||
if (sendPort === void 0) { sendPort = 9001; } | ||
if (listenPort === void 0) { listenPort = 9000; } | ||
this.host = host; | ||
this.sendPort = sendPort; | ||
this.listenPort = listenPort; | ||
this.msgMap = new Map(); | ||
this.eventListeners = new Map(); | ||
this.connectListeners = []; | ||
this.disconnectListeners = []; | ||
this.song = new song_1.Song(this); | ||
this.client = dgram_1["default"].createSocket("udp4"); | ||
this.client.bind(this.listenPort, host); | ||
this.client.addListener("message", this.handleIncoming.bind(this)); | ||
if (heartbeatInterval === void 0) { heartbeatInterval = 2000; } | ||
var _this = _super.call(this) || this; | ||
_this.host = host; | ||
_this.sendPort = sendPort; | ||
_this.listenPort = listenPort; | ||
_this.msgMap = new Map(); | ||
_this.eventListeners = new Map(); | ||
_this.isConnected = true; | ||
_this.cancelConnectionEvent = false; | ||
_this.song = new song_1.Song(_this); | ||
_this.client = dgram_1["default"].createSocket("udp4"); | ||
_this.client.bind(_this.listenPort, host); | ||
_this.client.addListener("message", _this.handleIncoming.bind(_this)); | ||
_this.heartbeatInterval = setInterval(function () { return __awaiter(_this, void 0, void 0, function () { | ||
var e_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
this.cancelConnectionEvent = false; | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, this.song.get("current_song_time")]; | ||
case 2: | ||
_a.sent(); | ||
if (!this.isConnected && !this.cancelConnectionEvent) { | ||
this.isConnected = true; | ||
this.emit("connect", "heartbeat"); | ||
} | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
e_1 = _a.sent(); | ||
if (this.isConnected && !this.cancelConnectionEvent) { | ||
this.isConnected = false; | ||
this.emit("disconnect", "heartbeat"); | ||
} | ||
return [3 /*break*/, 4]; | ||
case 4: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }, heartbeatInterval); | ||
return _this; | ||
} | ||
@@ -80,5 +126,6 @@ Ableton.prototype.close = function () { | ||
this.eventListeners.clear(); | ||
for (var _i = 0, _a = this.disconnectListeners; _i < _a.length; _i++) { | ||
var listener = _a[_i]; | ||
listener(); | ||
if (this.isConnected === true) { | ||
this.isConnected = false; | ||
this.cancelConnectionEvent = true; | ||
this.emit("disconnect", "realtime"); | ||
} | ||
@@ -88,5 +135,6 @@ return; | ||
if (data.event === "connect") { | ||
for (var _b = 0, _c = this.connectListeners; _b < _c.length; _b++) { | ||
var listener = _c[_b]; | ||
listener(); | ||
if (this.isConnected === false) { | ||
this.isConnected = true; | ||
this.cancelConnectionEvent = true; | ||
this.emit("connect", "realtime"); | ||
} | ||
@@ -129,17 +177,2 @@ return; | ||
}; | ||
Ableton.prototype.addConnectionListener = function (event, listener) { | ||
(event === "connect" | ||
? this.connectListeners | ||
: this.disconnectListeners).push(listener); | ||
}; | ||
Ableton.prototype.removeConnectionListener = function (event, listener) { | ||
switch (event) { | ||
case "connect": | ||
this.connectListeners = this.connectListeners.filter(function (l) { return l !== listener; }); | ||
break; | ||
case "disconnect": | ||
this.disconnectListeners = this.disconnectListeners.filter(function (l) { return l !== listener; }); | ||
break; | ||
} | ||
}; | ||
Ableton.prototype.getProp = function (ns, nsid, prop) { | ||
@@ -159,3 +192,3 @@ return __awaiter(this, void 0, void 0, function () { | ||
}; | ||
Ableton.prototype.addListener = function (ns, nsid, prop, listener) { | ||
Ableton.prototype.addPropListener = function (ns, nsid, prop, listener) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -181,3 +214,3 @@ var eventId, result; | ||
}; | ||
Ableton.prototype.removeListener = function (ns, nsid, prop, eventId) { | ||
Ableton.prototype.removePropListener = function (ns, nsid, prop, eventId) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -200,3 +233,3 @@ return __generator(this, function (_a) { | ||
return Ableton; | ||
}()); | ||
}(events_1.EventEmitter)); | ||
exports.Ableton = Ableton; |
@@ -54,3 +54,3 @@ "use strict"; | ||
transformer = this.transformers[prop]; | ||
if (transformer) { | ||
if (res !== null && transformer) { | ||
return [2 /*return*/, transformer(res)]; | ||
@@ -78,4 +78,4 @@ } | ||
transformer = this.transformers[prop]; | ||
return [2 /*return*/, this.ableton.addListener(this.ns, this.nsid, String(prop), function (data) { | ||
if (transformer) { | ||
return [2 /*return*/, this.ableton.addPropListener(this.ns, this.nsid, String(prop), function (data) { | ||
if (data !== null && transformer) { | ||
listener(transformer(data)); | ||
@@ -93,3 +93,3 @@ } | ||
return __generator(this, function (_a) { | ||
this.ableton.removeListener(this.ns, this.nsid, String(prop), eventId); | ||
this.ableton.removePropListener(this.ns, this.nsid, String(prop), eventId); | ||
return [2 /*return*/]; | ||
@@ -96,0 +96,0 @@ }); |
{ | ||
"name": "ableton-js", | ||
"version": "1.3.3", | ||
"version": "1.4.0", | ||
"description": "Control Ableton Live from Node", | ||
@@ -33,5 +33,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"commander": "^2.20.0", | ||
"node-uuid": "^1.4.8" | ||
} | ||
} |
@@ -62,3 +62,3 @@ # Ableton.js | ||
```json | ||
```js | ||
{ | ||
@@ -75,3 +75,3 @@ "uuid": "a20f25a0-83e2-11e9-bbe1-bd3a580ef903", // A unique command id | ||
```json | ||
```js | ||
{ | ||
@@ -88,3 +88,3 @@ "data": 0.0, // The command's return value, can be of any JSON-compatible type | ||
```json | ||
```js | ||
{ | ||
@@ -104,3 +104,3 @@ "uuid": "922d54d0-83e3-11e9-ba7c-917478f8b91b", // A unique command id | ||
```json | ||
```js | ||
{ | ||
@@ -116,3 +116,3 @@ "data": "922d2dc0-83e3-11e9-ba7c-917478f8b91b", // The unique event id | ||
```json | ||
```js | ||
{ | ||
@@ -133,3 +133,3 @@ "data": 68.0, // The new value, can be any JSON-compatible type | ||
```json | ||
```js | ||
{ | ||
@@ -136,0 +136,0 @@ "data": null, // Is always null |
73700
1
1674
- Removedcommander@^2.20.0
- Removedcommander@2.20.3(transitive)