2017-07-14 15:37:59 -04:00
|
|
|
/* eslint-env worker */
|
|
|
|
|
2017-07-21 16:46:08 -04:00
|
|
|
const ArgumentType = require('../extension-support/argument-type');
|
|
|
|
const BlockType = require('../extension-support/block-type');
|
2017-07-14 15:37:59 -04:00
|
|
|
const dispatch = require('../dispatch/worker-dispatch');
|
|
|
|
|
|
|
|
class ExtensionWorker {
|
|
|
|
constructor () {
|
|
|
|
this.nextExtensionId = 0;
|
|
|
|
|
|
|
|
dispatch.waitForConnection.then(() => {
|
|
|
|
dispatch.call('extensions', 'allocateWorker').then(x => {
|
|
|
|
const [id, extension] = x;
|
|
|
|
this.workerId = id;
|
|
|
|
|
|
|
|
// TODO: catch and report any exceptions here
|
|
|
|
importScripts(extension);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.extensions = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
register (extensionObject) {
|
|
|
|
const extensionId = this.nextExtensionId++;
|
|
|
|
this.extensions.push(extensionObject);
|
2017-07-21 16:46:08 -04:00
|
|
|
const serviceName = `extension.${this.workerId}.${extensionId}`;
|
|
|
|
return dispatch.setService(serviceName, extensionObject)
|
|
|
|
.then(() => dispatch.call('extensions', 'registerExtensionService', serviceName));
|
2017-07-14 15:37:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
global.Scratch = global.Scratch || {};
|
2017-07-21 16:46:08 -04:00
|
|
|
global.Scratch.ArgumentType = ArgumentType;
|
|
|
|
global.Scratch.BlockType = BlockType;
|
2017-07-14 15:37:59 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Expose only specific parts of the worker to extensions.
|
|
|
|
*/
|
2017-07-21 16:46:08 -04:00
|
|
|
const extensionWorker = new ExtensionWorker();
|
2017-07-14 15:37:59 -04:00
|
|
|
global.Scratch.extensions = {
|
|
|
|
register: extensionWorker.register.bind(extensionWorker)
|
|
|
|
};
|