WebSockets: improve support for callbacks in JS client lib
This commit is contained in:
parent
1f3b40f4bd
commit
da670aa186
@ -38,26 +38,28 @@ import { Switch, DiscreteSlider, ContinuousSlider, LogarithmicSlider,
|
|||||||
main();
|
main();
|
||||||
|
|
||||||
function main () {
|
function main () {
|
||||||
ardour.messageCallback = (msg) => {
|
ardour.addCallback({
|
||||||
log(`↙ ${msg}`, 'message-in');
|
onMessage: (msg) => {
|
||||||
|
log(`↙ ${msg}`, 'message-in');
|
||||||
|
|
||||||
if (msg.node == 'strip_desc') {
|
if (msg.node == 'strip_desc') {
|
||||||
createStrip (msg.addr, ...msg.val);
|
createStrip (msg.addr, ...msg.val);
|
||||||
} else if (msg.node == 'strip_plugin_desc') {
|
} else if (msg.node == 'strip_plugin_desc') {
|
||||||
createStripPlugin (msg.addr, ...msg.val);
|
createStripPlugin (msg.addr, ...msg.val);
|
||||||
} else if (msg.node == 'strip_plugin_param_desc') {
|
} else if (msg.node == 'strip_plugin_param_desc') {
|
||||||
createStripPluginParam (msg.addr, ...msg.val);
|
createStripPluginParam (msg.addr, ...msg.val);
|
||||||
} else if (FEEDBACK_NODES.includes(msg.node)) {
|
} else if (FEEDBACK_NODES.includes(msg.node)) {
|
||||||
if (widgets[msg.hash]) {
|
if (widgets[msg.hash]) {
|
||||||
widgets[msg.hash].value = msg.val[0];
|
widgets[msg.hash].value = msg.val[0];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
onError: () => {
|
||||||
|
log('Client error', 'error');
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
|
||||||
ardour.errorCallback = () => {
|
|
||||||
log('Client error', 'error');
|
|
||||||
};
|
|
||||||
|
|
||||||
ardour.open();
|
ardour.open();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,36 +22,39 @@ import { MessageChannel } from './channel.js';
|
|||||||
export class Ardour {
|
export class Ardour {
|
||||||
|
|
||||||
constructor () {
|
constructor () {
|
||||||
this._channel = new MessageChannel(location.host);
|
this._callbacks = [];
|
||||||
this._channel.errorCallback = (error) => this.errorCallback();
|
|
||||||
this._channel.messageCallback = (msg) => this._onChannelMessage(msg);
|
|
||||||
this._pendingRequest = null;
|
this._pendingRequest = null;
|
||||||
|
this._channel = new MessageChannel(location.host);
|
||||||
|
|
||||||
|
this._channel.onError = (error) => {
|
||||||
|
this._fireCallbacks('error', error);
|
||||||
|
};
|
||||||
|
|
||||||
|
this._channel.onMessage = (msg) => {
|
||||||
|
this._onChannelMessage(msg);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
addCallback (callback) {
|
||||||
|
this._callbacks.push(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
async open () {
|
async open () {
|
||||||
this._channel.closeCallback = () => {
|
this._channel.onClose = () => {
|
||||||
this.errorCallback(new Error('Message channel unexpectedly closed'));
|
this._fireCallbacks('error', new Error('Message channel unexpectedly closed'));
|
||||||
};
|
};
|
||||||
|
|
||||||
await this._channel.open();
|
await this._channel.open();
|
||||||
}
|
}
|
||||||
|
|
||||||
close () {
|
close () {
|
||||||
this._channel.closeCallback = () => {};
|
this._channel.onClose = () => {};
|
||||||
this._channel.close();
|
this._channel.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
send (msg) {
|
send (msg) {
|
||||||
this._channel.send(msg);
|
this._channel.send(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
errorCallback (error) {
|
|
||||||
// empty
|
|
||||||
}
|
|
||||||
|
|
||||||
messageCallback (msg) {
|
|
||||||
// empty
|
|
||||||
}
|
|
||||||
|
|
||||||
// Surface metadata API over HTTP
|
// Surface metadata API over HTTP
|
||||||
|
|
||||||
@ -73,7 +76,8 @@ export class Ardour {
|
|||||||
const xmlDoc = new DOMParser().parseFromString(xmlText, 'text/xml');
|
const xmlDoc = new DOMParser().parseFromString(xmlText, 'text/xml');
|
||||||
return {
|
return {
|
||||||
name: xmlDoc.getElementsByTagName('Name')[0].getAttribute('value'),
|
name: xmlDoc.getElementsByTagName('Name')[0].getAttribute('value'),
|
||||||
description: xmlDoc.getElementsByTagName('Description')[0].getAttribute('value')
|
description: xmlDoc.getElementsByTagName('Description')[0].getAttribute('value'),
|
||||||
|
version: xmlDoc.getElementsByTagName('Version')[0].getAttribute('value')
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw this._fetchResponseStatusError(response.status);
|
throw this._fetchResponseStatusError(response.status);
|
||||||
@ -151,7 +155,21 @@ export class Ardour {
|
|||||||
this._pendingRequest.resolve(msg.val);
|
this._pendingRequest.resolve(msg.val);
|
||||||
this._pendingRequest = null;
|
this._pendingRequest = null;
|
||||||
} else {
|
} else {
|
||||||
this.messageCallback(msg);
|
this._fireCallbacks('message', msg);
|
||||||
|
this._fireCallbacks(msg.node, ...msg.addr, ...msg.val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_fireCallbacks (name, ...args) {
|
||||||
|
// name_with_underscores -> onNameWithUnderscores
|
||||||
|
const method = 'on' + name.split('_').map((s) => {
|
||||||
|
return s[0].toUpperCase() + s.slice(1).toLowerCase();
|
||||||
|
}).join('');
|
||||||
|
|
||||||
|
for (const callback of this._callbacks) {
|
||||||
|
if (method in callback) {
|
||||||
|
callback[method](...args)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
31
share/web_surfaces/shared/callback.js
Normal file
31
share/web_surfaces/shared/callback.js
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Copyright © 2020 Luciano Iam <lucianito@gmail.com>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export class ArdourCallback {
|
||||||
|
|
||||||
|
onTempo (bpm) {}
|
||||||
|
onStripGain (stripId, db) {}
|
||||||
|
onStripPan (stripId, value) {}
|
||||||
|
onStripMute (stripId, value) {}
|
||||||
|
onStripPluginEnable (stripId, pluginId, value) {}
|
||||||
|
onStripPluginParamValue (stripId, pluginId, paramId, value) {}
|
||||||
|
|
||||||
|
onMessage (msg) {}
|
||||||
|
onError (error) {}
|
||||||
|
|
||||||
|
}
|
@ -29,19 +29,19 @@ export class MessageChannel {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this._socket = new WebSocket(`ws://${this._host}`);
|
this._socket = new WebSocket(`ws://${this._host}`);
|
||||||
|
|
||||||
this._socket.onclose = () => this.closeCallback();
|
this._socket.onclose = () => this.onClose();
|
||||||
|
|
||||||
this._socket.onerror = (error) => this.errorCallback(error);
|
this._socket.onerror = (error) => this.onError(error);
|
||||||
|
|
||||||
this._socket.onmessage = (event) => {
|
this._socket.onmessage = (event) => {
|
||||||
this.messageCallback (Message.fromJsonText(event.data));
|
this.onMessage (Message.fromJsonText(event.data));
|
||||||
};
|
};
|
||||||
|
|
||||||
this._socket.onopen = resolve;
|
this._socket.onopen = resolve;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async close () {
|
close () {
|
||||||
this._socket.close();
|
this._socket.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,16 +53,8 @@ export class MessageChannel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
closeCallback () {
|
onClose () {}
|
||||||
// empty
|
onError (error) {}
|
||||||
}
|
onMessage (msg) {}
|
||||||
|
|
||||||
errorCallback (error) {
|
|
||||||
// empty
|
|
||||||
}
|
|
||||||
|
|
||||||
messageCallback (msg) {
|
|
||||||
// empty
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user