mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-01-12 18:24:54 -05:00
d09c3f0418
* First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
112 lines
3.1 KiB
JavaScript
112 lines
3.1 KiB
JavaScript
class JSONRPC {
|
|
constructor () {
|
|
this._requestID = 0;
|
|
this._openRequests = {};
|
|
}
|
|
|
|
/**
|
|
* Make an RPC request and retrieve the result.
|
|
* @param {string} method - the remote method to call.
|
|
* @param {object} params - the parameters to pass to the remote method.
|
|
* @returns {Promise} - a promise for the result of the call.
|
|
*/
|
|
sendRemoteRequest (method, params) {
|
|
const requestID = this._requestID++;
|
|
|
|
const promise = new Promise((resolve, reject) => {
|
|
this._openRequests[requestID] = {resolve, reject};
|
|
});
|
|
|
|
this._sendRequest(method, params, requestID);
|
|
|
|
return promise;
|
|
}
|
|
|
|
/**
|
|
* Make an RPC notification with no expectation of a result or callback.
|
|
* @param {string} method - the remote method to call.
|
|
* @param {object} params - the parameters to pass to the remote method.
|
|
*/
|
|
sendRemoteNotification (method, params) {
|
|
this._sendRequest(method, params);
|
|
}
|
|
|
|
/**
|
|
* Handle an RPC request from remote, should return a result or Promise for result, if appropriate.
|
|
* @param {string} method - the method requested by the remote caller.
|
|
* @param {object} params - the parameters sent with the remote caller's request.
|
|
*/
|
|
didReceiveCall (/* method , params */) {
|
|
throw new Error('Must override didReceiveCall');
|
|
}
|
|
|
|
_sendMessage (/* jsonMessageObject */) {
|
|
throw new Error('Must override _sendMessage');
|
|
}
|
|
|
|
_sendRequest (method, params, id) {
|
|
const request = {
|
|
jsonrpc: '2.0',
|
|
method,
|
|
params
|
|
};
|
|
|
|
if (id !== null) {
|
|
request.id = id;
|
|
}
|
|
|
|
this._sendMessage(request);
|
|
}
|
|
|
|
_handleMessage (json) {
|
|
if (json.jsonrpc !== '2.0') {
|
|
throw new Error(`Bad or missing JSON-RPC version in message: ${json}`);
|
|
}
|
|
if (json.hasOwnProperty('method')) {
|
|
this._handleRequest(json);
|
|
} else {
|
|
this._handleResponse(json);
|
|
}
|
|
}
|
|
|
|
_sendResponse (id, result, error) {
|
|
const response = {
|
|
jsonrpc: '2.0',
|
|
id
|
|
};
|
|
if (error) {
|
|
response.error = error;
|
|
} else {
|
|
response.result = result || null;
|
|
}
|
|
this._sendMessage(response);
|
|
}
|
|
|
|
_handleResponse (json) {
|
|
const {result, error, id} = json;
|
|
const openRequest = this._openRequests[id];
|
|
delete this._openRequests[id];
|
|
if (error) {
|
|
openRequest.reject(error);
|
|
} else {
|
|
openRequest.resolve(result);
|
|
}
|
|
}
|
|
|
|
_handleRequest (json) {
|
|
const {method, params, id} = json;
|
|
const rawResult = this.didReceiveCall(method, params);
|
|
if (id) {
|
|
Promise.resolve(rawResult).then(
|
|
result => {
|
|
this._sendResponse(id, result);
|
|
},
|
|
error => {
|
|
this._sendResponse(id, null, error);
|
|
}
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = JSONRPC;
|