mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-06-10 20:41:19 -04:00
Drop worker support (#175)
* Take out worker code * Fix Blockly event listening (minus worker) * Recompile Sept. 13
This commit is contained in:
parent
833c5ed313
commit
08b40b4d1d
8 changed files with 6425 additions and 4638 deletions
|
@ -720,7 +720,7 @@
|
|||
<!-- Renderer -->
|
||||
<script src="../node_modules/scratch-render/render.js"></script>
|
||||
<!-- VM Worker -->
|
||||
<script src="../vm.worker.js"></script>
|
||||
<script src="../vm.js"></script>
|
||||
<!-- Playground -->
|
||||
<script src="./playground.js"></script>
|
||||
<script>
|
||||
|
|
|
@ -35,7 +35,6 @@ window.onload = function() {
|
|||
// Instantiate the renderer and connect it to the VM.
|
||||
var canvas = document.getElementById('scratch-stage');
|
||||
window.renderer = new window.RenderWebGLLocal(canvas);
|
||||
window.renderer.connectWorker(window.vm.vmWorker);
|
||||
|
||||
// Instantiate scratch-blocks and attach it to the DOM.
|
||||
var toolbox = document.getElementById('toolbox');
|
||||
|
|
123
src/index.js
123
src/index.js
|
@ -6,12 +6,6 @@ var sb2import = require('./import/sb2import');
|
|||
var Sprite = require('./sprites/sprite');
|
||||
var Blocks = require('./engine/blocks');
|
||||
|
||||
/**
|
||||
* Whether the environment is a WebWorker.
|
||||
* @const{boolean}
|
||||
*/
|
||||
var ENV_WORKER = typeof importScripts === 'function';
|
||||
|
||||
/**
|
||||
* Handles connections between blocks, stage, and extensions.
|
||||
*
|
||||
|
@ -48,6 +42,8 @@ function VirtualMachine () {
|
|||
instance.runtime.on(Runtime.VISUAL_REPORT, function (id, value) {
|
||||
instance.emit(Runtime.VISUAL_REPORT, {id: id, value: value});
|
||||
});
|
||||
|
||||
this.blockListener = this.blockListener.bind(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -166,6 +162,20 @@ VirtualMachine.prototype.createEmptyProject = function () {
|
|||
this.emitWorkspaceUpdate();
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle a Blockly event for the current editing target.
|
||||
* @param {!Blockly.Event} e Any Blockly event.
|
||||
*/
|
||||
VirtualMachine.prototype.blockListener = function (e) {
|
||||
if (this.editingTarget) {
|
||||
this.editingTarget.blocks.blocklyListen(
|
||||
e,
|
||||
false,
|
||||
this.runtime
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Set an editing target. An editor UI can use this function to switch
|
||||
* between editing different targets, sprites, etc.
|
||||
|
@ -214,107 +224,6 @@ VirtualMachine.prototype.emitWorkspaceUpdate = function () {
|
|||
'xml': this.editingTarget.blocks.toXML()
|
||||
});
|
||||
};
|
||||
|
||||
/*
|
||||
* Worker handlers: for all public methods available above,
|
||||
* we must also provide a message handler in case the VM is run
|
||||
* from a worker environment.
|
||||
*/
|
||||
if (ENV_WORKER) {
|
||||
self.importScripts(
|
||||
'./node_modules/scratch-render/render-worker.js'
|
||||
);
|
||||
self.renderer = new self.RenderWebGLWorker();
|
||||
self.vmInstance = new VirtualMachine();
|
||||
self.onmessage = function (e) {
|
||||
var messageData = e.data;
|
||||
switch (messageData.method) {
|
||||
case 'start':
|
||||
self.vmInstance.runtime.start();
|
||||
break;
|
||||
case 'greenFlag':
|
||||
self.vmInstance.runtime.greenFlag();
|
||||
break;
|
||||
case 'stopAll':
|
||||
self.vmInstance.runtime.stopAll();
|
||||
break;
|
||||
case 'blockListener':
|
||||
if (self.vmInstance.editingTarget) {
|
||||
self.vmInstance.editingTarget.blocks.blocklyListen(
|
||||
messageData.args,
|
||||
false,
|
||||
self.vmInstance.runtime
|
||||
);
|
||||
}
|
||||
break;
|
||||
case 'flyoutBlockListener':
|
||||
if (self.vmInstance.editingTarget) {
|
||||
self.vmInstance.editingTarget.blocks.blocklyListen(
|
||||
messageData.args,
|
||||
true,
|
||||
self.vmInstance.runtime
|
||||
);
|
||||
}
|
||||
break;
|
||||
case 'getPlaygroundData':
|
||||
self.postMessage({
|
||||
method: 'playgroundData',
|
||||
blocks: self.vmInstance.editingTarget.blocks,
|
||||
threads: self.vmInstance.runtime.threads
|
||||
});
|
||||
break;
|
||||
case 'animationFrame':
|
||||
self.vmInstance.animationFrame();
|
||||
break;
|
||||
case 'postIOData':
|
||||
self.vmInstance.postIOData(messageData.device, messageData.data);
|
||||
break;
|
||||
case 'setEditingTarget':
|
||||
self.vmInstance.setEditingTarget(messageData.targetId);
|
||||
break;
|
||||
case 'createEmptyProject':
|
||||
self.vmInstance.createEmptyProject();
|
||||
break;
|
||||
case 'loadProject':
|
||||
self.vmInstance.loadProject(messageData.json);
|
||||
break;
|
||||
default:
|
||||
if (e.data.id == 'RendererConnected') {
|
||||
//initRenderWorker();
|
||||
}
|
||||
self.renderer.onmessage(e);
|
||||
break;
|
||||
}
|
||||
};
|
||||
// Bind runtime's emitted events to postmessages.
|
||||
self.vmInstance.runtime.on(Runtime.SCRIPT_GLOW_ON, function (id) {
|
||||
self.postMessage({method: Runtime.SCRIPT_GLOW_ON, id: id});
|
||||
});
|
||||
self.vmInstance.runtime.on(Runtime.SCRIPT_GLOW_OFF, function (id) {
|
||||
self.postMessage({method: Runtime.SCRIPT_GLOW_OFF, id: id});
|
||||
});
|
||||
self.vmInstance.runtime.on(Runtime.BLOCK_GLOW_ON, function (id) {
|
||||
self.postMessage({method: Runtime.BLOCK_GLOW_ON, id: id});
|
||||
});
|
||||
self.vmInstance.runtime.on(Runtime.BLOCK_GLOW_OFF, function (id) {
|
||||
self.postMessage({method: Runtime.BLOCK_GLOW_OFF, id: id});
|
||||
});
|
||||
self.vmInstance.runtime.on(Runtime.VISUAL_REPORT, function (id, value) {
|
||||
self.postMessage({method: Runtime.VISUAL_REPORT, id: id, value: value});
|
||||
});
|
||||
self.vmInstance.on('workspaceUpdate', function(data) {
|
||||
self.postMessage({method: 'workspaceUpdate',
|
||||
xml: data.xml
|
||||
});
|
||||
});
|
||||
self.vmInstance.on('targetsUpdate', function(data) {
|
||||
self.postMessage({method: 'targetsUpdate',
|
||||
targetList: data.targetList,
|
||||
editingTarget: data.editingTarget
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Export and bind to `window`
|
||||
*/
|
||||
|
|
|
@ -1,95 +0,0 @@
|
|||
var EventEmitter = require('events');
|
||||
var util = require('util');
|
||||
|
||||
function VirtualMachine () {
|
||||
if (!window.Worker) {
|
||||
console.error('WebWorkers not supported in this environment.' +
|
||||
' Please use the non-worker version (vm.js or vm.min.js).');
|
||||
return;
|
||||
}
|
||||
var instance = this;
|
||||
EventEmitter.call(instance);
|
||||
instance.vmWorker = new Worker('../vm.js');
|
||||
|
||||
// onmessage calls are converted into emitted events.
|
||||
instance.vmWorker.onmessage = function (e) {
|
||||
instance.emit(e.data.method, e.data);
|
||||
};
|
||||
|
||||
instance.blockListener = function (e) {
|
||||
// Messages from Blockly are not serializable by default.
|
||||
// Pull out the necessary, serializable components to pass across.
|
||||
var serializableE = {
|
||||
blockId: e.blockId,
|
||||
element: e.element,
|
||||
type: e.type,
|
||||
name: e.name,
|
||||
newValue: e.newValue,
|
||||
oldParentId: e.oldParentId,
|
||||
oldInputName: e.oldInputName,
|
||||
newParentId: e.newParentId,
|
||||
newInputName: e.newInputName,
|
||||
newCoordinate: e.newCoordinate,
|
||||
xml: {
|
||||
outerHTML: (e.xml) ? e.xml.outerHTML : null
|
||||
}
|
||||
};
|
||||
instance.vmWorker.postMessage({
|
||||
method: 'blockListener',
|
||||
args: serializableE
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Inherit from EventEmitter
|
||||
*/
|
||||
util.inherits(VirtualMachine, EventEmitter);
|
||||
|
||||
// For documentation, please see index.js.
|
||||
// These mirror the functionality provided there, with the worker wrapper.
|
||||
VirtualMachine.prototype.getPlaygroundData = function () {
|
||||
this.vmWorker.postMessage({method: 'getPlaygroundData'});
|
||||
};
|
||||
|
||||
VirtualMachine.prototype.postIOData = function (device, data) {
|
||||
this.vmWorker.postMessage({
|
||||
method: 'postIOData',
|
||||
device: device,
|
||||
data: data
|
||||
});
|
||||
};
|
||||
|
||||
VirtualMachine.prototype.start = function () {
|
||||
this.vmWorker.postMessage({method: 'start'});
|
||||
};
|
||||
|
||||
VirtualMachine.prototype.greenFlag = function () {
|
||||
this.vmWorker.postMessage({method: 'greenFlag'});
|
||||
};
|
||||
|
||||
VirtualMachine.prototype.stopAll = function () {
|
||||
this.vmWorker.postMessage({method: 'stopAll'});
|
||||
};
|
||||
|
||||
VirtualMachine.prototype.animationFrame = function () {
|
||||
this.vmWorker.postMessage({method: 'animationFrame'});
|
||||
};
|
||||
|
||||
VirtualMachine.prototype.loadProject = function (json) {
|
||||
this.vmWorker.postMessage({method: 'loadProject', json: json});
|
||||
};
|
||||
|
||||
VirtualMachine.prototype.createEmptyProject = function () {
|
||||
this.vmWorker.postMessage({method: 'createEmptyProject'});
|
||||
};
|
||||
|
||||
VirtualMachine.prototype.setEditingTarget = function (targetId) {
|
||||
this.vmWorker.postMessage({method: 'setEditingTarget', targetId: targetId});
|
||||
};
|
||||
|
||||
/**
|
||||
* Export and bind to `window`
|
||||
*/
|
||||
module.exports = VirtualMachine;
|
||||
if (typeof window !== 'undefined') window.VirtualMachine = module.exports;
|
12
vm.min.js
vendored
12
vm.min.js
vendored
File diff suppressed because one or more lines are too long
1169
vm.worker.js
1169
vm.worker.js
File diff suppressed because it is too large
Load diff
|
@ -3,8 +3,7 @@ var webpack = require('webpack');
|
|||
module.exports = {
|
||||
entry: {
|
||||
'vm': './src/index.js',
|
||||
'vm.min': './src/index.js',
|
||||
'vm.worker': './src/worker.js'
|
||||
'vm.min': './src/index.js'
|
||||
},
|
||||
output: {
|
||||
path: __dirname,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue