Skip to content

Commit 02662fe

Browse files
authored
Breakup common storage (#194)
* Moved constants and functions related to names and paths from common_storage.ts to names.ts. * Moved constants and functions related to modules from common_storage.ts to module.ts. * Moved constants and functions related to projects from common_storage.ts to project.ts. * Moved types and functions related to module content from common_storage.ts to module_content.ts. * Update names.ts * Update module.ts * Update project.ts * Update module_content.ts
1 parent fedb96d commit 02662fe

30 files changed

+1019
-933
lines changed

src/App.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import * as editor from './editor/editor';
3838
import { extendedPythonGenerator } from './editor/extended_python_generator';
3939

4040
import * as commonStorage from './storage/common_storage';
41+
import * as storageModule from './storage/module';
42+
import * as storageProject from './storage/project';
4143
import * as clientSideStorage from './storage/client_side_storage';
4244

4345
import * as CustomBlocks from './blocks/setup_custom_blocks';
@@ -129,7 +131,7 @@ const App: React.FC = (): React.JSX.Element => {
129131
* App wrapper that manages project state and provides it to UserSettingsProvider.
130132
*/
131133
const AppWithUserSettings: React.FC<{ storage: commonStorage.Storage }> = ({ storage }) => {
132-
const [project, setProject] = React.useState<commonStorage.Project | null>(null);
134+
const [project, setProject] = React.useState<storageProject.Project | null>(null);
133135

134136
return (
135137
<UserSettingsProvider
@@ -145,16 +147,16 @@ const AppWithUserSettings: React.FC<{ storage: commonStorage.Storage }> = ({ sto
145147
* Inner application content component that has access to UserSettings context.
146148
*/
147149
interface AppContentProps {
148-
project: commonStorage.Project | null;
149-
setProject: React.Dispatch<React.SetStateAction<commonStorage.Project | null>>;
150+
project: storageProject.Project | null;
151+
setProject: React.Dispatch<React.SetStateAction<storageProject.Project | null>>;
150152
}
151153

152154
const AppContent: React.FC<AppContentProps> = ({ project, setProject }): React.JSX.Element => {
153155
const { t, i18n } = useTranslation();
154156
const { settings, updateLanguage, updateTheme, storage, isLoading } = useUserSettings();
155157

156158
const [alertErrorMessage, setAlertErrorMessage] = React.useState('');
157-
const [currentModule, setCurrentModule] = React.useState<commonStorage.Module | null>(null);
159+
const [currentModule, setCurrentModule] = React.useState<storageModule.Module | null>(null);
158160
const [messageApi, contextHolder] = Antd.message.useMessage();
159161
const [generatedCode, setGeneratedCode] = React.useState<string>('');
160162
const [toolboxSettingsModalIsOpen, setToolboxSettingsModalIsOpen] = React.useState(false);
@@ -340,7 +342,7 @@ const AppContent: React.FC<AppContentProps> = ({ project, setProject }): React.J
340342
};
341343

342344
/** Changes current module with automatic saving if modified. */
343-
const changeModule = async (module: commonStorage.Module | null): Promise<void> => {
345+
const changeModule = async (module: storageModule.Module | null): Promise<void> => {
344346
if (currentModule && areBlocksModified()) {
345347
await saveBlocks();
346348
}
@@ -359,7 +361,7 @@ const AppContent: React.FC<AppContentProps> = ({ project, setProject }): React.J
359361
};
360362

361363
/** Creates tab items from project data. */
362-
const createTabItemsFromProject = (projectData: commonStorage.Project): Tabs.TabItem[] => {
364+
const createTabItemsFromProject = (projectData: storageProject.Project): Tabs.TabItem[] => {
363365
const tabs: Tabs.TabItem[] = [
364366
{
365367
key: projectData.robot.modulePath,

src/blocks/mrc_call_python_function.ts

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ import { createFieldDropdown } from '../fields/FieldDropdown';
3333
import { createFieldNonEditableText } from '../fields/FieldNonEditableText';
3434
import { MRC_STYLE_FUNCTIONS } from '../themes/styles'
3535
import * as toolboxItems from '../toolbox/items';
36-
import * as commonStorage from '../storage/common_storage';
36+
import * as storageModule from '../storage/module';
37+
import * as storageModuleContent from '../storage/module_content';
3738

3839

3940
// A block to call a python function.
@@ -496,11 +497,11 @@ const CALL_PYTHON_FUNCTION = {
496497
},
497498
mutateMethodCaller: function(
498499
this: CallPythonFunctionBlock,
499-
methodOrEvent: commonStorage.Method | commonStorage.Event
500+
methodOrEvent: storageModuleContent.Method | storageModuleContent.Event
500501
): void {
501502
// mutateMethodCaller is called when the method or event definition block in the same module is modified.
502503
if (this.mrcFunctionKind == FunctionKind.EVENT) {
503-
const event = methodOrEvent as commonStorage.Event;
504+
const event = methodOrEvent as storageModuleContent.Event;
504505
this.mrcArgs = [];
505506
event.args.forEach((arg) => {
506507
this.mrcArgs.push({
@@ -509,7 +510,7 @@ const CALL_PYTHON_FUNCTION = {
509510
});
510511
});
511512
} else if (this.mrcFunctionKind == FunctionKind.INSTANCE_WITHIN) {
512-
const method = methodOrEvent as commonStorage.Method;
513+
const method = methodOrEvent as storageModuleContent.Method;
513514
this.mrcReturnType = method.returnType;
514515
this.mrcArgs = [];
515516
// We don't include the arg for the self argument because we don't need a socket for it.
@@ -522,9 +523,9 @@ const CALL_PYTHON_FUNCTION = {
522523
}
523524
this.updateBlock_();
524525
},
525-
getComponentsFromRobot: function(this: CallPythonFunctionBlock): commonStorage.Component[] {
526+
getComponentsFromRobot: function(this: CallPythonFunctionBlock): storageModuleContent.Component[] {
526527
// Get the list of components whose type matches this.mrcComponentClassName.
527-
const components: commonStorage.Component[] = [];
528+
const components: storageModuleContent.Component[] = [];
528529
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace);
529530
if (editor) {
530531
editor.getComponentsFromRobot().forEach(component => {
@@ -550,9 +551,9 @@ const CALL_PYTHON_FUNCTION = {
550551
// visible warning on it.
551552
if (this.mrcFunctionKind === FunctionKind.INSTANCE_COMPONENT) {
552553
let foundComponent = false;
553-
const componentsInScope: commonStorage.Component[] = [];
554+
const componentsInScope: storageModuleContent.Component[] = [];
554555
componentsInScope.push(...this.getComponentsFromRobot());
555-
if (editor.getCurrentModuleType() === commonStorage.MODULE_TYPE_MECHANISM) {
556+
if (editor.getCurrentModuleType() === storageModule.MODULE_TYPE_MECHANISM) {
556557
componentsInScope.push(...editor.getComponentsFromWorkspace());
557558
}
558559
for (const component of componentsInScope) {
@@ -599,7 +600,7 @@ const CALL_PYTHON_FUNCTION = {
599600
// If the robot method has changed, update the block if possible or put a
600601
// visible warning on it.
601602
if (this.mrcFunctionKind === FunctionKind.INSTANCE_ROBOT) {
602-
if (editor.getCurrentModuleType() === commonStorage.MODULE_TYPE_MECHANISM) {
603+
if (editor.getCurrentModuleType() === storageModule.MODULE_TYPE_MECHANISM) {
603604
warnings.push('This block is not allowed to be used inside a mechanism.');
604605
} else {
605606
let foundRobotMethod = false;
@@ -645,7 +646,7 @@ const CALL_PYTHON_FUNCTION = {
645646
// If the method has changed, update the block if possible or put a
646647
// visible warning on it.
647648
if (this.mrcFunctionKind === FunctionKind.INSTANCE_MECHANISM) {
648-
if (editor.getCurrentModuleType() === commonStorage.MODULE_TYPE_MECHANISM) {
649+
if (editor.getCurrentModuleType() === storageModule.MODULE_TYPE_MECHANISM) {
649650
warnings.push('This block is not allowed to be used inside a mechanism.');
650651
} else {
651652
let foundMechanism = false;
@@ -661,7 +662,7 @@ const CALL_PYTHON_FUNCTION = {
661662

662663
let foundMechanismMethod = false;
663664
const mechanism = editor.getMechanism(mechanismInRobot);
664-
const mechanismMethods: commonStorage.Method[] = mechanism
665+
const mechanismMethods: storageModuleContent.Method[] = mechanism
665666
? editor.getMethodsFromMechanism(mechanism) : [];
666667
for (const mechanismMethod of mechanismMethods) {
667668
if (mechanismMethod.blockId === this.mrcOtherBlockId) {
@@ -790,11 +791,11 @@ export const pythonFromBlock = function(
790791
: block.getFieldValue(FIELD_FUNCTION_NAME);
791792
// Generate the correct code depending on the module type.
792793
switch (generator.getModuleType()) {
793-
case commonStorage.MODULE_TYPE_ROBOT:
794-
case commonStorage.MODULE_TYPE_MECHANISM:
794+
case storageModule.MODULE_TYPE_ROBOT:
795+
case storageModule.MODULE_TYPE_MECHANISM:
795796
code = 'self.';
796797
break;
797-
case commonStorage.MODULE_TYPE_OPMODE:
798+
case storageModule.MODULE_TYPE_OPMODE:
798799
code = 'self.robot.';
799800
break;
800801
}
@@ -815,13 +816,13 @@ export const pythonFromBlock = function(
815816
: block.getFieldValue(FIELD_FUNCTION_NAME);
816817
// Generate the correct code depending on the module type.
817818
switch (generator.getModuleType()) {
818-
case commonStorage.MODULE_TYPE_ROBOT:
819+
case storageModule.MODULE_TYPE_ROBOT:
819820
code = 'self.' + mechanismName;
820821
break;
821-
case commonStorage.MODULE_TYPE_OPMODE:
822+
case storageModule.MODULE_TYPE_OPMODE:
822823
code = 'self.robot.' + mechanismName;
823824
break;
824-
case commonStorage.MODULE_TYPE_MECHANISM:
825+
case storageModule.MODULE_TYPE_MECHANISM:
825826
// The INSTANCE_MECHANISM version should not be used in a mechanism.
826827
// TODO(lizlooney): What if the user copies a block from an robot or opmode and pastes
827828
// it into a mechanism?
@@ -882,7 +883,7 @@ export function renameMethodCallers(workspace: Blockly.Workspace, otherBlockId:
882883
}
883884

884885
export function mutateMethodCallers(
885-
workspace: Blockly.Workspace, otherBlockId: string, methodOrEvent: commonStorage.Method | commonStorage.Event) {
886+
workspace: Blockly.Workspace, otherBlockId: string, methodOrEvent: storageModuleContent.Method | storageModuleContent.Event) {
886887
const oldRecordUndo = Blockly.Events.getRecordUndo();
887888

888889
getMethodCallers(workspace, otherBlockId).forEach(block => {
@@ -1070,14 +1071,14 @@ function createInstanceMethodBlock(
10701071
}
10711072

10721073
export function addInstanceWithinBlocks(
1073-
methods: commonStorage.Method[],
1074+
methods: storageModuleContent.Method[],
10741075
contents: toolboxItems.ContentsType[]) {
10751076
methods.forEach(method => {
10761077
contents.push(createInstanceWithinBlock(method));
10771078
});
10781079
}
10791080

1080-
function createInstanceWithinBlock(method: commonStorage.Method): toolboxItems.Block {
1081+
function createInstanceWithinBlock(method: storageModuleContent.Method): toolboxItems.Block {
10811082
const extraState: CallPythonFunctionExtraState = {
10821083
functionKind: FunctionKind.INSTANCE_WITHIN,
10831084
returnType: method.returnType,
@@ -1088,7 +1089,7 @@ function createInstanceWithinBlock(method: commonStorage.Method): toolboxItems.B
10881089
const fields: {[key: string]: any} = {};
10891090
fields[FIELD_FUNCTION_NAME] = method.visibleName;
10901091
const inputs: {[key: string]: any} = {};
1091-
// Convert method.args from commonStorage.MethodArg[] to ArgData[].
1092+
// Convert method.args from storageModuleContent.MethodArg[] to ArgData[].
10921093
const args: ArgData[] = [];
10931094
// We don't include the arg for the self argument because we don't need a socket for it.
10941095
for (let i = 1; i < method.args.length; i++) {
@@ -1103,7 +1104,7 @@ function createInstanceWithinBlock(method: commonStorage.Method): toolboxItems.B
11031104
}
11041105

11051106
export function getInstanceComponentBlocks(
1106-
component: commonStorage.Component): toolboxItems.ContentsType[] {
1107+
component: storageModuleContent.Component): toolboxItems.ContentsType[] {
11071108
const contents: toolboxItems.ContentsType[] = [];
11081109

11091110
const classData = getClassData(component.className);
@@ -1131,7 +1132,7 @@ export function getInstanceComponentBlocks(
11311132
}
11321133

11331134
function createInstanceComponentBlock(
1134-
component: commonStorage.Component, functionData: FunctionData): toolboxItems.Block {
1135+
component: storageModuleContent.Component, functionData: FunctionData): toolboxItems.Block {
11351136
const extraState: CallPythonFunctionExtraState = {
11361137
functionKind: FunctionKind.INSTANCE_COMPONENT,
11371138
returnType: functionData.returnType,
@@ -1155,14 +1156,14 @@ function createInstanceComponentBlock(
11551156
}
11561157

11571158
export function addInstanceRobotBlocks(
1158-
methods: commonStorage.Method[],
1159+
methods: storageModuleContent.Method[],
11591160
contents: toolboxItems.ContentsType[]) {
11601161
methods.forEach(method => {
11611162
contents.push(createInstanceRobotBlock(method));
11621163
});
11631164
}
11641165

1165-
function createInstanceRobotBlock(method: commonStorage.Method): toolboxItems.Block {
1166+
function createInstanceRobotBlock(method: storageModuleContent.Method): toolboxItems.Block {
11661167
const extraState: CallPythonFunctionExtraState = {
11671168
functionKind: FunctionKind.INSTANCE_ROBOT,
11681169
returnType: method.returnType,
@@ -1173,7 +1174,7 @@ function createInstanceRobotBlock(method: commonStorage.Method): toolboxItems.Bl
11731174
const fields: {[key: string]: any} = {};
11741175
fields[FIELD_FUNCTION_NAME] = method.visibleName;
11751176
const inputs: {[key: string]: any} = {};
1176-
// Convert method.args from commonStorage.MethodArg[] to ArgData[].
1177+
// Convert method.args from storageModuleContent.MethodArg[] to ArgData[].
11771178
const args: ArgData[] = [];
11781179
// We don't include the arg for the self argument because we don't need a socket for it.
11791180
for (let i = 1; i < method.args.length; i++) {
@@ -1188,17 +1189,17 @@ function createInstanceRobotBlock(method: commonStorage.Method): toolboxItems.Bl
11881189
}
11891190

11901191
export function addInstanceMechanismBlocks(
1191-
mechanismInRobot: commonStorage.MechanismInRobot,
1192-
methods: commonStorage.Method[],
1192+
mechanismInRobot: storageModuleContent.MechanismInRobot,
1193+
methods: storageModuleContent.Method[],
11931194
contents: toolboxItems.ContentsType[]) {
11941195
methods.forEach(method => {
11951196
contents.push(createInstanceMechanismBlock(mechanismInRobot, method));
11961197
});
11971198
}
11981199

11991200
function createInstanceMechanismBlock(
1200-
mechanismInRobot: commonStorage.MechanismInRobot,
1201-
method: commonStorage.Method): toolboxItems.Block {
1201+
mechanismInRobot: storageModuleContent.MechanismInRobot,
1202+
method: storageModuleContent.Method): toolboxItems.Block {
12021203
const extraState: CallPythonFunctionExtraState = {
12031204
functionKind: FunctionKind.INSTANCE_MECHANISM,
12041205
returnType: method.returnType,
@@ -1212,7 +1213,7 @@ function createInstanceMechanismBlock(
12121213
fields[FIELD_MECHANISM_NAME] = mechanismInRobot.name;
12131214
fields[FIELD_FUNCTION_NAME] = method.visibleName;
12141215
const inputs: {[key: string]: any} = {};
1215-
// Convert method.args from commonStorage.MethodArg[] to ArgData[].
1216+
// Convert method.args from storageModuleContent.MethodArg[] to ArgData[].
12161217
const args: ArgData[] = [];
12171218
// For INSTANCE_MECHANISM functions, the 0 argument is 'self', but
12181219
// self is represented by the FIELD_MECHANISM_NAME field.
@@ -1229,14 +1230,14 @@ function createInstanceMechanismBlock(
12291230
}
12301231

12311232
export function addFireEventBlocks(
1232-
events: commonStorage.Event[],
1233+
events: storageModuleContent.Event[],
12331234
contents: toolboxItems.ContentsType[]) {
12341235
events.forEach(event => {
12351236
contents.push(createFireEventBlock(event));
12361237
});
12371238
}
12381239

1239-
function createFireEventBlock(event: commonStorage.Event): toolboxItems.Block {
1240+
function createFireEventBlock(event: storageModuleContent.Event): toolboxItems.Block {
12401241
const extraState: CallPythonFunctionExtraState = {
12411242
functionKind: FunctionKind.EVENT,
12421243
returnType: RETURN_TYPE_NONE,
@@ -1246,7 +1247,7 @@ function createFireEventBlock(event: commonStorage.Event): toolboxItems.Block {
12461247
const fields: {[key: string]: any} = {};
12471248
fields[FIELD_EVENT_NAME] = event.name;
12481249
const inputs: {[key: string]: any} = {};
1249-
// Convert event.args from commonStorage.MethodArg[] to ArgData[].
1250+
// Convert event.args from storageModuleContent.MethodArg[] to ArgData[].
12501251
const args: ArgData[] = [];
12511252
event.args.forEach(methodArg => {
12521253
args.push({

src/blocks/mrc_class_method_def.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import { createFieldNonEditableText } from '../fields/FieldNonEditableText'
2525
import { createFieldFlydown } from '../fields/field_flydown';
2626
import { Order } from 'blockly/python';
2727
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
28-
import * as commonStorage from '../storage/common_storage';
28+
import * as storageModule from '../storage/module';
29+
import * as storageModuleContent from '../storage/module_content';
2930
import { renameMethodCallers, mutateMethodCallers } from './mrc_call_python_function'
3031
import * as toolboxItems from '../toolbox/items';
3132
import { getClassData } from './utils/python';
@@ -266,8 +267,8 @@ const CLASS_METHOD_DEF = {
266267
}
267268
return legalName;
268269
},
269-
getMethod: function (this: ClassMethodDefBlock): commonStorage.Method | null {
270-
const method: commonStorage.Method = {
270+
getMethod: function (this: ClassMethodDefBlock): storageModuleContent.Method | null {
271+
const method: storageModuleContent.Method = {
271272
blockId: this.id,
272273
visibleName: this.getFieldValue(FIELD_METHOD_NAME),
273274
pythonName: this.mrcFuncName ? this.mrcFuncName : '',
@@ -288,13 +289,13 @@ const CLASS_METHOD_DEF = {
288289
});
289290
return method;
290291
},
291-
getMethodForWithin: function (this: ClassMethodDefBlock): commonStorage.Method | null {
292+
getMethodForWithin: function (this: ClassMethodDefBlock): storageModuleContent.Method | null {
292293
if (this.mrcCanBeCalledWithinClass) {
293294
return this.getMethod();
294295
}
295296
return null;
296297
},
297-
getMethodForOutside: function (this: ClassMethodDefBlock): commonStorage.Method | null {
298+
getMethodForOutside: function (this: ClassMethodDefBlock): storageModuleContent.Method | null {
298299
if (this.mrcCanBeCalledOutsideClass) {
299300
return this.getMethod();
300301
}
@@ -424,7 +425,7 @@ export const pythonFromBlock = function (
424425

425426
let params = block.mrcParameters;
426427
let paramString = "self";
427-
if (generator.getModuleType() == commonStorage.MODULE_TYPE_MECHANISM && block.mrcPythonMethodName == '__init__') {
428+
if (generator.getModuleType() == storageModule.MODULE_TYPE_MECHANISM && block.mrcPythonMethodName == '__init__') {
428429
const ports: string[] = generator.getComponentPortParameters();
429430
if (ports.length) {
430431
paramString += ', ' + ports.join(', ');
@@ -452,7 +453,7 @@ export const pythonFromBlock = function (
452453
code = generator.scrub_(block, code);
453454
generator.addClassMethodDefinition(funcName, code);
454455

455-
// Save the name of the function we just generated so we can use it to create the commonStorage.Method.
456+
// Save the name of the function we just generated so we can use it to create the storageModuleContent.Method.
456457
// in the getMethod function.
457458
block.mrcFuncName = funcName;
458459

@@ -518,7 +519,7 @@ function createClassMethodDefBlock(
518519

519520
export function getMethodsForWithin(
520521
workspace: Blockly.Workspace,
521-
methods: commonStorage.Method[]): void {
522+
methods: storageModuleContent.Method[]): void {
522523
workspace.getBlocksByType(BLOCK_NAME).forEach(block => {
523524
const method = (block as ClassMethodDefBlock).getMethodForWithin();
524525
if (method) {
@@ -529,7 +530,7 @@ export function getMethodsForWithin(
529530

530531
export function getMethodsForOutside(
531532
workspace: Blockly.Workspace,
532-
methods: commonStorage.Method[]): void {
533+
methods: storageModuleContent.Method[]): void {
533534
workspace.getBlocksByType(BLOCK_NAME).forEach(block => {
534535
const method = (block as ClassMethodDefBlock).getMethodForOutside();
535536
if (method) {

0 commit comments

Comments
 (0)