Skip to content

Commit 926d78d

Browse files
authored
Fix problem with deploy (#292)
* Added src/blocks/utils/workspaces.ts, which contains functions related to keeping track of blockly workspaces. Modified editor to call workspaces.addWorkspace and workspaces.removeWorkspace Modified existing code that use headless blockly workspaces to call workspaces.createHeadlessWorkspace and workspaces.destroyHeadlessWorkspace. * Pass editor to mrcOnLoad and mrcOnModuleCurrent functions. * Modified mrc_component updateBlock_ function to call workspaces.getModuleTypeForWorkspace instead of trying to get the corresponding editor. If the block is in a headless workspace, there is no corresponding editor. * Removed opt_returnCurrentIfNotFound parameter from getEditorForBlocklyWorkspace.
1 parent 608c186 commit 926d78d

File tree

12 files changed

+257
-199
lines changed

12 files changed

+257
-199
lines changed

src/blocks/mrc_call_python_function.ts

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -549,66 +549,59 @@ const CALL_PYTHON_FUNCTION = {
549549
}
550550
this.updateBlock_();
551551
},
552-
getComponents: function(this: CallPythonFunctionBlock): storageModuleContent.Component[] {
552+
getComponents: function(this: CallPythonFunctionBlock, editor: Editor): storageModuleContent.Component[] {
553553
// Get the list of components whose type matches this.mrcComponentClassName.
554554
const components: storageModuleContent.Component[] = [];
555-
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace, true /* returnCurrentIfNotFound */);
556-
if (editor) {
557-
let componentsToConsider: storageModuleContent.Component[] = [];
558-
if (this.mrcMechanismId) {
559-
// Only consider components that belong to the mechanism.
560-
// this.mrcMechanismId is the mechanismId from the MechanismInRobot.
561-
// We need to get the MechanismInRobot with that id, then get the mechanism, and then get
562-
// the public components defined in that mechanism.
563-
for (const mechanismInRobot of editor.getMechanismsFromRobot()) {
564-
if (mechanismInRobot.mechanismId === this.mrcMechanismId) {
565-
for (const mechanism of editor.getMechanisms()) {
566-
if (mechanism.moduleId === mechanismInRobot.moduleId) {
567-
componentsToConsider = editor.getComponentsFromMechanism(mechanism);
568-
break;
569-
}
555+
let componentsToConsider: storageModuleContent.Component[] = [];
556+
if (this.mrcMechanismId) {
557+
// Only consider components that belong to the mechanism.
558+
// this.mrcMechanismId is the mechanismId from the MechanismInRobot.
559+
// We need to get the MechanismInRobot with that id, then get the mechanism, and then get
560+
// the public components defined in that mechanism.
561+
for (const mechanismInRobot of editor.getMechanismsFromRobot()) {
562+
if (mechanismInRobot.mechanismId === this.mrcMechanismId) {
563+
for (const mechanism of editor.getMechanisms()) {
564+
if (mechanism.moduleId === mechanismInRobot.moduleId) {
565+
componentsToConsider = editor.getComponentsFromMechanism(mechanism);
566+
break;
570567
}
571-
break;
572568
}
569+
break;
573570
}
574-
} else if (editor.getModuleType() === storageModule.ModuleType.MECHANISM) {
575-
// Only consider components (regular and private) in the current workspace.
576-
componentsToConsider = editor.getAllComponentsFromWorkspace();
577-
} else {
578-
// Only consider components in the robot.
579-
componentsToConsider = editor.getComponentsFromRobot();
580571
}
581-
componentsToConsider.forEach(component => {
582-
if (component.className === this.mrcComponentClassName) {
583-
components.push(component);
584-
}
585-
});
572+
} else if (editor.getModuleType() === storageModule.ModuleType.MECHANISM) {
573+
// Only consider components (regular and private) in the current workspace.
574+
componentsToConsider = editor.getAllComponentsFromWorkspace();
575+
} else {
576+
// Only consider components in the robot.
577+
componentsToConsider = editor.getComponentsFromRobot();
586578
}
579+
componentsToConsider.forEach(component => {
580+
if (component.className === this.mrcComponentClassName) {
581+
components.push(component);
582+
}
583+
});
587584
return components;
588585
},
589586

590587
/**
591588
* mrcOnModuleCurrent is called for each CallPythonFunctionBlock when the module becomes the current module.
592589
*/
593-
mrcOnModuleCurrent: function(this: CallPythonFunctionBlock): void {
594-
this.checkFunction();
590+
mrcOnModuleCurrent: function(this: CallPythonFunctionBlock, editor: Editor): void {
591+
this.checkFunction(editor);
595592
},
596593
/**
597594
* mrcOnLoad is called for each CallPythonFunctionBlock when the blocks are loaded in the blockly
598595
* workspace.
599596
*/
600-
mrcOnLoad: function(this: CallPythonFunctionBlock): void {
601-
this.checkFunction();
597+
mrcOnLoad: function(this: CallPythonFunctionBlock, editor: Editor): void {
598+
this.checkFunction(editor);
602599
},
603600
/**
604601
* checkFunction checks the block, updates it, and/or adds a warning balloon if necessary.
605602
* It is called from mrcOnModuleCurrent and mrcOnLoad above.
606603
*/
607-
checkFunction: function(this: CallPythonFunctionBlock): void {
608-
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace, true /* returnCurrentIfNotFound */);
609-
if (!editor) {
610-
return;
611-
}
604+
checkFunction: function(this: CallPythonFunctionBlock, editor: Editor): void {
612605
const warnings: string[] = [];
613606

614607
// If this block is calling a component method, check whether the component
@@ -621,7 +614,7 @@ const CALL_PYTHON_FUNCTION = {
621614
if (this.mrcFunctionKind === FunctionKind.INSTANCE_COMPONENT) {
622615
const componentNames: string[] = [];
623616
this.mrcMapComponentNameToId = {}
624-
this.getComponents().forEach(component => {
617+
this.getComponents(editor).forEach(component => {
625618
componentNames.push(component.name);
626619
this.mrcMapComponentNameToId[component.name] = component.componentId;
627620
});

src/blocks/mrc_component.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { MRC_STYLE_COMPONENTS } from '../themes/styles'
2626
import { createFieldNonEditableText } from '../fields/FieldNonEditableText';
2727
import { Editor } from '../editor/editor';
2828
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
29+
import { getModuleTypeForWorkspace } from './utils/workspaces';
2930
import { getAllowedTypesForSetCheck, getClassData, getSubclassNames } from './utils/python';
3031
import * as toolboxItems from '../toolbox/items';
3132
import * as storageModule from '../storage/module';
@@ -142,8 +143,8 @@ const COMPONENT = {
142143
* Update the block to reflect the newly loaded extra state.
143144
*/
144145
updateBlock_: function (this: ComponentBlock): void {
145-
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace, true /* returnCurrentIfNotFound */);
146-
if (editor && editor.getModuleType() === storageModule.ModuleType.ROBOT) {
146+
const moduleType = getModuleTypeForWorkspace(this.workspace);
147+
if (moduleType === storageModule.ModuleType.ROBOT) {
147148
// Add input sockets for the arguments.
148149
for (let i = 0; i < this.mrcArgs.length; i++) {
149150
const input = this.appendValueInput('ARG' + i)
@@ -192,7 +193,7 @@ const COMPONENT = {
192193
/**
193194
* mrcOnLoad is called for each ComponentBlock when the blocks are loaded in the blockly workspace.
194195
*/
195-
mrcOnLoad: function(this: ComponentBlock): void {
196+
mrcOnLoad: function(this: ComponentBlock, _editor: Editor): void {
196197
this.checkBlockIsInHolder();
197198
},
198199
/**

src/blocks/mrc_event.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import * as Blockly from 'blockly';
2424
import { MRC_STYLE_EVENTS } from '../themes/styles'
2525
import { createFieldNonEditableText } from '../fields/FieldNonEditableText';
2626
import { Parameter } from './mrc_class_method_def';
27+
import { Editor } from '../editor/editor';
2728
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
2829
import * as paramContainer from './mrc_param_container'
2930
import {
@@ -196,7 +197,7 @@ const EVENT = {
196197
/**
197198
* mrcOnLoad is called for each EventBlock when the blocks are loaded in the blockly workspace.
198199
*/
199-
mrcOnLoad: function(this: EventBlock): void {
200+
mrcOnLoad: function(this: EventBlock, _editor: Editor): void {
200201
this.checkBlockIsInHolder();
201202
},
202203
/**

src/blocks/mrc_event_handler.ts

Lines changed: 82 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -170,114 +170,111 @@ const EVENT_HANDLER = {
170170
/**
171171
* mrcOnModuleCurrent is called for each EventHandlerBlock when the module becomes the current module.
172172
*/
173-
mrcOnModuleCurrent: function(this: EventHandlerBlock): void {
174-
this.checkEvent();
173+
mrcOnModuleCurrent: function(this: EventHandlerBlock, editor: Editor): void {
174+
this.checkEvent(editor);
175175
},
176176
/**
177177
* mrcOnLoad is called for each EventHandlerBlock when the blocks are loaded in the blockly
178178
* workspace.
179179
*/
180-
mrcOnLoad: function(this: EventHandlerBlock): void {
181-
this.checkEvent();
180+
mrcOnLoad: function(this: EventHandlerBlock, editor: Editor): void {
181+
this.checkEvent(editor);
182182
},
183183
/**
184184
* checkEvent checks the block, updates it, and/or adds a warning balloon if necessary.
185185
* It is called from mrcOnModuleCurrent and mrcOnLoad above.
186186
*/
187-
checkEvent: function(this: EventHandlerBlock): void {
187+
checkEvent: function(this: EventHandlerBlock, editor: Editor): void {
188188
const warnings: string[] = [];
189189

190-
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace, true /* returnCurrentIfNotFound */);
191-
if (editor) {
192-
if (this.mrcSenderType === SenderType.ROBOT) {
193-
// This block is an event handler for a robot event.
194-
// Check whether the robot event still exists and whether it has been changed.
195-
// If the robot event doesn't exist, put a visible warning on this block.
196-
// If the robot event has changed, update the block if possible or put a
197-
// visible warning on it.
198-
let foundRobotEvent = false;
199-
const robotEvents = editor.getEventsFromRobot();
200-
for (const robotEvent of robotEvents) {
201-
if (robotEvent.eventId === this.mrcEventId) {
202-
foundRobotEvent = true;
203-
if (this.getFieldValue(FIELD_EVENT_NAME) !== robotEvent.name) {
204-
this.setFieldValue(robotEvent.name, FIELD_EVENT_NAME);
205-
}
206-
this.mrcParameters = [];
207-
robotEvent.args.forEach(arg => {
208-
this.mrcParameters.push({
209-
name: arg.name,
210-
type: arg.type,
211-
});
190+
if (this.mrcSenderType === SenderType.ROBOT) {
191+
// This block is an event handler for a robot event.
192+
// Check whether the robot event still exists and whether it has been changed.
193+
// If the robot event doesn't exist, put a visible warning on this block.
194+
// If the robot event has changed, update the block if possible or put a
195+
// visible warning on it.
196+
let foundRobotEvent = false;
197+
const robotEvents = editor.getEventsFromRobot();
198+
for (const robotEvent of robotEvents) {
199+
if (robotEvent.eventId === this.mrcEventId) {
200+
foundRobotEvent = true;
201+
if (this.getFieldValue(FIELD_EVENT_NAME) !== robotEvent.name) {
202+
this.setFieldValue(robotEvent.name, FIELD_EVENT_NAME);
203+
}
204+
this.mrcParameters = [];
205+
robotEvent.args.forEach(arg => {
206+
this.mrcParameters.push({
207+
name: arg.name,
208+
type: arg.type,
212209
});
213-
this.mrcUpdateParams();
210+
});
211+
this.mrcUpdateParams();
214212

215-
// Since we found the robot event, we can break out of the loop.
216-
break;
217-
}
218-
}
219-
if (!foundRobotEvent) {
220-
warnings.push(Blockly.Msg.EVENT_HANDLER_ROBOT_EVENT_NOT_FOUND);
213+
// Since we found the robot event, we can break out of the loop.
214+
break;
221215
}
222216
}
217+
if (!foundRobotEvent) {
218+
warnings.push(Blockly.Msg.EVENT_HANDLER_ROBOT_EVENT_NOT_FOUND);
219+
}
220+
}
223221

224-
if (this.mrcSenderType === SenderType.MECHANISM) {
225-
// This block is an event handler for a mechanism event.
226-
// Check whether the mechanism still exists, whether it has been
227-
// changed, whether the event still exists, and whether the event has
228-
// been changed.
229-
// If the mechanism doesn't exist, put a visible warning on this block.
230-
// If the mechanism has changed, update the block if possible or put a
231-
// visible warning on it.
232-
// If the event doesn't exist, put a visible warning on this block.
233-
// If the event has changed, update the block if possible or put a
234-
// visible warning on it.
235-
let foundMechanism = false;
236-
const mechanismsInRobot = editor.getMechanismsFromRobot();
237-
for (const mechanismInRobot of mechanismsInRobot) {
238-
if (mechanismInRobot.mechanismId === this.mrcMechanismId) {
239-
foundMechanism = true;
240-
241-
// If the mechanism name has changed, we can handle that.
242-
if (this.getFieldValue(FIELD_SENDER) !== mechanismInRobot.name) {
243-
this.setFieldValue(mechanismInRobot.name, FIELD_SENDER);
244-
}
222+
if (this.mrcSenderType === SenderType.MECHANISM) {
223+
// This block is an event handler for a mechanism event.
224+
// Check whether the mechanism still exists, whether it has been
225+
// changed, whether the event still exists, and whether the event has
226+
// been changed.
227+
// If the mechanism doesn't exist, put a visible warning on this block.
228+
// If the mechanism has changed, update the block if possible or put a
229+
// visible warning on it.
230+
// If the event doesn't exist, put a visible warning on this block.
231+
// If the event has changed, update the block if possible or put a
232+
// visible warning on it.
233+
let foundMechanism = false;
234+
const mechanismsInRobot = editor.getMechanismsFromRobot();
235+
for (const mechanismInRobot of mechanismsInRobot) {
236+
if (mechanismInRobot.mechanismId === this.mrcMechanismId) {
237+
foundMechanism = true;
238+
239+
// If the mechanism name has changed, we can handle that.
240+
if (this.getFieldValue(FIELD_SENDER) !== mechanismInRobot.name) {
241+
this.setFieldValue(mechanismInRobot.name, FIELD_SENDER);
242+
}
245243

246-
let foundMechanismEvent = false;
247-
const mechanism = editor.getMechanism(mechanismInRobot);
248-
const mechanismEvents: storageModuleContent.Event[] = mechanism
249-
? editor.getEventsFromMechanism(mechanism) : [];
250-
for (const mechanismEvent of mechanismEvents) {
251-
if (mechanismEvent.eventId === this.mrcEventId) {
252-
foundMechanismEvent = true;
253-
if (this.getFieldValue(FIELD_EVENT_NAME) !== mechanismEvent.name) {
254-
this.setFieldValue(mechanismEvent.name, FIELD_EVENT_NAME);
255-
}
256-
257-
this.mrcParameters = [];
258-
mechanismEvent.args.forEach(arg => {
259-
this.mrcParameters.push({
260-
name: arg.name,
261-
type: arg.type,
262-
});
244+
let foundMechanismEvent = false;
245+
const mechanism = editor.getMechanism(mechanismInRobot);
246+
const mechanismEvents: storageModuleContent.Event[] = mechanism
247+
? editor.getEventsFromMechanism(mechanism) : [];
248+
for (const mechanismEvent of mechanismEvents) {
249+
if (mechanismEvent.eventId === this.mrcEventId) {
250+
foundMechanismEvent = true;
251+
if (this.getFieldValue(FIELD_EVENT_NAME) !== mechanismEvent.name) {
252+
this.setFieldValue(mechanismEvent.name, FIELD_EVENT_NAME);
253+
}
254+
255+
this.mrcParameters = [];
256+
mechanismEvent.args.forEach(arg => {
257+
this.mrcParameters.push({
258+
name: arg.name,
259+
type: arg.type,
263260
});
264-
this.mrcUpdateParams();
261+
});
262+
this.mrcUpdateParams();
265263

266-
// Since we found the mechanism event, we can break out of the loop.
267-
break;
268-
}
264+
// Since we found the mechanism event, we can break out of the loop.
265+
break;
269266
}
270-
if (!foundMechanismEvent) {
271-
warnings.push(Blockly.Msg.EVENT_HANDLER_MECHANISM_EVENT_NOT_FOUND);
272-
}
273-
274-
// Since we found the mechanism, we can break out of the loop.
275-
break;
276267
}
268+
if (!foundMechanismEvent) {
269+
warnings.push(Blockly.Msg.EVENT_HANDLER_MECHANISM_EVENT_NOT_FOUND);
270+
}
271+
272+
// Since we found the mechanism, we can break out of the loop.
273+
break;
277274
}
278-
if (!foundMechanism) {
279-
warnings.push(Blockly.Msg.EVENT_HANDLER_MECHANISM_NOT_FOUND);
280-
}
275+
}
276+
if (!foundMechanism) {
277+
warnings.push(Blockly.Msg.EVENT_HANDLER_MECHANISM_NOT_FOUND);
281278
}
282279
}
283280

0 commit comments

Comments
 (0)