Merge pull request #1565 from cwillisf/extension-monitors

Extension monitors
This commit is contained in:
Chris Willis-Ford 2018-09-12 18:10:12 -04:00 committed by GitHub
commit 8977ce1e93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 3 deletions

View file

@ -805,8 +805,12 @@ class Runtime extends EventEmitter {
}
}
// Add icon to the bottom right of a loop block
if (blockInfo.blockType === BlockType.LOOP) {
if (blockInfo.blockType === BlockType.REPORTER) {
if (!blockInfo.disableMonitor && context.inputList.length === 0) {
blockJSON.checkboxInFlyout = true;
}
} else if (blockInfo.blockType === BlockType.LOOP) {
// Add icon to the bottom right of a loop block
blockJSON[`lastDummyAlign${outLineNum}`] = 'RIGHT';
blockJSON[`message${outLineNum}`] = '%1';
blockJSON[`args${outLineNum}`] = [{
@ -1851,6 +1855,31 @@ class Runtime extends EventEmitter {
return varNames;
}
/**
* Get the label or label function for an opcode
* @param {string} extendedOpcode - the opcode you want a label for
* @return {object} - object with label and category
* @property {string} category - the category for this opcode
* @property {Function} [labelFn] - function to generate the label for this opcode
* @property {string} [label] - the label for this opcode if `labelFn` is absent
*/
getLabelForOpcode (extendedOpcode) {
const [category, opcode] = StringUtil.splitFirst(extendedOpcode, '_');
if (!(category && opcode)) return;
const categoryInfo = this._blockInfo.find(ci => ci.id === category);
if (!categoryInfo) return;
const block = categoryInfo.blocks.find(b => b.info.opcode === opcode);
if (!block) return;
// TODO: should this use some other category? Also, we may want to format the label in a locale-specific way.
return {
category: 'data',
label: `${categoryInfo.name}: ${block.info.text}`
};
}
/**
* Create a new global variable avoiding conflicts with other variable names.
* @param {string} variableName The desired variable name for the new global variable.

View file

@ -19,6 +19,7 @@
* @property {string} text - the text on the block, with [PLACEHOLDERS] for arguments.
* @property {Boolean} [hideFromPalette] - true if this block should not appear in the block palette.
* @property {Boolean} [isTerminal] - true if the block ends a stack - no blocks can be connected after it.
* @property {Boolean} [disableMonitor] - true if this block is a reporter but should not allow a monitor.
* @property {ReporterScope} [reporterScope] - if this block is a reporter, this is the scope/context for its value.
* @property {Boolean} [isEdgeActivated] - sets whether a hat block is edge-activated.
* @property {Boolean} [shouldRestartExistingThreads] - sets whether a hat/event block should restart existing threads.

View file

@ -50,7 +50,7 @@ test('monitorStateDoesNotEqual', t => {
});
r.requestAddMonitor(prevMonitorState);
r.requestUpdateMonitor(newMonitorDelta);
t.equals(false, prevMonitorState.equals(r._monitorState.get(id)));
t.equals(String(24), r._monitorState.get(id).get('value'));
@ -67,3 +67,44 @@ test('monitorStateDoesNotEqual', t => {
t.end();
});
test('getLabelForOpcode', t => {
const r = new Runtime();
const fakeExtension = {
id: 'fakeExtension',
name: 'Fake Extension',
blocks: [
{
info: {
opcode: 'foo',
json: {},
text: 'Foo',
xml: ''
}
},
{
info: {
opcode: 'foo_2',
json: {},
text: 'Foo 2',
xml: ''
}
}
]
};
r._blockInfo.push(fakeExtension);
const result1 = r.getLabelForOpcode('fakeExtension_foo');
t.type(result1.category, 'string');
t.type(result1.label, 'string');
t.equals(result1.label, 'Fake Extension: Foo');
const result2 = r.getLabelForOpcode('fakeExtension_foo_2');
t.type(result2.category, 'string');
t.type(result2.label, 'string');
t.equals(result2.label, 'Fake Extension: Foo 2');
t.end();
});