Ok, after breaking down how mBlock works it looks like the Worker (Sandbox for the extension) where the extensions is running doesn’t have access to DeviceContext (to call readRaw
or any read for that matter), on the Worker we have the deviceId and AppContext, the sandbox uses this as context
var __context = {
app: self.rpc.remote.app,
getDevice: function (deviceId) {
return new Proxy(
{},
{
get: function get(target, name) {
return "id" == name ?
deviceId :
function () {
for (
var runDevice = __context.app.runDevice,
_len = arguments.length,
args = Array(_len),
_key = 0;
_key < _len;
_key++
)
args[_key] = arguments[_key];
return runDevice.apply(void 0, [deviceId, name].concat(args));
};
},
}
);
},
};
But app.runDevice doesn’t exist on the AppContext, here are the methods for app
["constructor","initDrawable","audioPlayer","initAudio","setXY","_getRenderedDirectionAndScale","setDirection","setDraggable","setVisible","setSize","setEffect","clearEffects","setCostume","addCostume","renameCostume","deleteCostume","addSound","renameSound","deleteSound","setRotationStyle","getCostumeIndexByName","getCurrentCostume","getCostumes","reorderCostume","reorderSound","getSounds","updateAllDrawableProperties","getName","isSprite","getBounds","getBoundsForBubble","isTouchingObject","isTouchingPoint","isTouchingEdge","isTouchingSprite","isTouchingColor","colorIsTouchingColor","getLayerOrder","goToFront","goToBack","goForwardLayers","goBackwardLayers","goBehindOther","keepInFence","makeClone","duplicate","onGreenFlag","onStopAll","postSpriteInfo","startDrag","stopDrag","toJSON","dispose"]
so when onRead is called or any event for that matter device
will always be an empty object instance ({}
), here is the implementation on mBlock side.
//...
runExtension: function (srcMethod, deviceId) {
var method = ExtHandler[srcMethod];
if (!method) return null;
var app = __context.app;
if (deviceId) {
var device = __context.getDevice(deviceId);
return method(app, device);
}
return method(app);
},
//...
onRead: function onRead(app, device) {
this.worker.remote.runExtension('onRead', device.id);
},