Skip to content

Commit 8cd9b69

Browse files
committed
Update mrc_call_python_function mrcOnLoad method to check whether a mechanism component is now a private component.
Removed field mrcComponentNames. It only needs to be a local variable in mrcOnLoad. Added method Editor.getPrivateComponentsFromMechanism.
1 parent 6a1a2f5 commit 8cd9b69

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

src/blocks/mrc_call_python_function.ts

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ interface CallPythonFunctionMixin extends CallPythonFunctionMixinType {
8585
mrcComponentClassName: string,
8686
mrcOriginalComponentName: string,
8787
mrcMechanismClassName: string,
88-
mrcComponentNames: string[],
8988
mrcMapComponentNameToId: {[componentName: string]: string},
9089
}
9190
type CallPythonFunctionMixinType = typeof CALL_PYTHON_FUNCTION;
@@ -318,8 +317,7 @@ const CALL_PYTHON_FUNCTION = {
318317
this.mrcMechanismId = extraState.mechanismId ? extraState.mechanismId : '';
319318
this.mrcComponentClassName = extraState.componentClassName ? extraState.componentClassName : '';
320319
this.mrcMechanismClassName = extraState.mechanismClassName ? extraState.mechanismClassName : '';
321-
// Initialize mrcComponentNames and mrcMapComponentNameToId here. They will be filled during mrcOnLoad.
322-
this.mrcComponentNames = [];
320+
// Initialize mrcMapComponentNameToId here. It will be filled during mrcOnLoad.
323321
this.mrcMapComponentNameToId = {};
324322
this.updateBlock_();
325323
},
@@ -505,7 +503,7 @@ const CALL_PYTHON_FUNCTION = {
505503
methodOrEvent: storageModuleContent.Method | storageModuleContent.Event
506504
): void {
507505
// mutateMethodCaller is called when the method or event definition block in the same module is modified.
508-
if (this.mrcFunctionKind == FunctionKind.EVENT) {
506+
if (this.mrcFunctionKind === FunctionKind.EVENT) {
509507
const event = methodOrEvent as storageModuleContent.Event;
510508
this.mrcArgs = [];
511509
event.args.forEach((arg) => {
@@ -514,7 +512,7 @@ const CALL_PYTHON_FUNCTION = {
514512
type: arg.type,
515513
});
516514
});
517-
} else if (this.mrcFunctionKind == FunctionKind.INSTANCE_WITHIN) {
515+
} else if (this.mrcFunctionKind === FunctionKind.INSTANCE_WITHIN) {
518516
const method = methodOrEvent as storageModuleContent.Method;
519517
this.mrcReturnType = method.returnType;
520518
this.mrcArgs = [];
@@ -581,12 +579,14 @@ const CALL_PYTHON_FUNCTION = {
581579
// If the component belongs to a mechanism, also check whether the mechanism
582580
// still exists and whether it has been changed.
583581
if (this.mrcFunctionKind === FunctionKind.INSTANCE_COMPONENT) {
582+
const componentNames: string[] = [];
583+
this.mrcMapComponentNameToId = {}
584584
this.getComponents().forEach(component => {
585-
this.mrcComponentNames.push(component.name);
585+
componentNames.push(component.name);
586586
this.mrcMapComponentNameToId[component.name] = component.componentId;
587587
});
588588
let foundComponent = false;
589-
for (const componentName of this.mrcComponentNames) {
589+
for (const componentName of componentNames) {
590590
const componentId = this.mrcMapComponentNameToId[componentName];
591591
if (componentId === this.mrcComponentId) {
592592
foundComponent = true;
@@ -605,12 +605,12 @@ const CALL_PYTHON_FUNCTION = {
605605
break;
606606
}
607607
}
608-
if (indexOfComponentNameField == -1) {
608+
if (indexOfComponentNameField === -1) {
609609
throw new Error('Could not find the component name field');
610610
}
611611
titleInput.removeField(FIELD_COMPONENT_NAME);
612612
titleInput.insertFieldAt(indexOfComponentNameField,
613-
createFieldDropdown(this.mrcComponentNames), FIELD_COMPONENT_NAME);
613+
createFieldDropdown(componentNames), FIELD_COMPONENT_NAME);
614614
// TODO(lizlooney): If the current module is the robot or a mechanism, we need to update the
615615
// items in the dropdown if the user adds or removes a component.
616616

@@ -620,6 +620,39 @@ const CALL_PYTHON_FUNCTION = {
620620
break;
621621
}
622622
}
623+
if (!foundComponent) {
624+
if (this.mrcMechanismId) {
625+
// Check whether the the component still exists, but is a private component in the mechanism.
626+
for (const mechanismInRobot of editor.getMechanismsFromRobot()) {
627+
if (mechanismInRobot.mechanismId === this.mrcMechanismId) {
628+
for (const mechanism of editor.getMechanisms()) {
629+
if (mechanism.moduleId === mechanismInRobot.moduleId) {
630+
for (const privateComponent of editor.getPrivateComponentsFromMechanism(mechanism)) {
631+
if (privateComponent.className === this.mrcComponentClassName &&
632+
privateComponent.componentId === this.mrcComponentId) {
633+
foundComponent = true;
634+
warnings.push(
635+
'This blocks calls a method on a private component in the ' +
636+
mechanism.className + ' mechanism.'
637+
);
638+
break
639+
}
640+
}
641+
break;
642+
}
643+
if (foundComponent) {
644+
break;
645+
}
646+
}
647+
break;
648+
}
649+
if (foundComponent) {
650+
break;
651+
}
652+
}
653+
}
654+
}
655+
623656
if (!foundComponent) {
624657
warnings.push('This block calls a method on a component that no longer exists.');
625658
}

src/editor/editor.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ export class Editor {
441441
}
442442

443443
/**
444-
* Returns the components defined in the given mechanism.
444+
* Returns the regular components defined in the given mechanism.
445445
*/
446446
public getComponentsFromMechanism(mechanism: storageModule.Mechanism): storageModuleContent.Component[] {
447447
if (this.module.modulePath === mechanism.modulePath) {
@@ -453,6 +453,19 @@ export class Editor {
453453
throw new Error('getComponentsFromMechanism: mechanism not found: ' + mechanism.className);
454454
}
455455

456+
/**
457+
* Returns the private components defined in the given mechanism.
458+
*/
459+
public getPrivateComponentsFromMechanism(mechanism: storageModule.Mechanism): storageModuleContent.Component[] {
460+
if (this.module.modulePath === mechanism.modulePath) {
461+
return this.getPrivateComponentsFromWorkspace();
462+
}
463+
if (mechanism.className in this.mechanismClassNameToModuleContent) {
464+
return this.mechanismClassNameToModuleContent[mechanism.className].getPrivateComponents();
465+
}
466+
throw new Error('getPrivateComponentsFromMechanism: mechanism not found: ' + mechanism.className);
467+
}
468+
456469
/**
457470
* Returns ALL components (including private components) defined in the given mechanism.
458471
* This is used when creating mechanism blocks that need all components for port parameters.

0 commit comments

Comments
 (0)