Skip to content

Commit

Permalink
perf: migrate learn/unlearn logic from state to view model
Browse files Browse the repository at this point in the history
  • Loading branch information
Bamdad Sabbagh committed Nov 10, 2021
1 parent 6d6b484 commit d09d9b7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/app/devices/controller.device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { neuronCardUi } from '../ui/neuron-card.ui';
import { networkState } from '../state/network.state';
import { mappingState } from '../state/mapping.state';
import { parametersUi } from '../ui/parameters.ui';
import { modalUi } from '../ui/modal.ui';

/**
* Controller is a unique device that controls the playground.
Expand Down Expand Up @@ -86,7 +87,7 @@ controllerDevice.setDefaultMode = function () {
});

if (isLearning && learningParameter) {
mappingState.learn ({
modalUi.learn ({
parameter: learningParameter,
control: note,
type: 'range',
Expand Down Expand Up @@ -145,7 +146,7 @@ controllerDevice.attachButtons = function () {
});

if (isLearning && learningParameter) {
mappingState.learn ({
modalUi.learn ({
parameter: learningParameter,
control: note,
type: 'button',
Expand Down
2 changes: 2 additions & 0 deletions src/app/state/mapping.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ mappingState.setParameterMaps = function ({
} else {
this.parametersByControl[control].push (parameter);
}

this.disableLearningMode ();
};

/**
Expand Down
46 changes: 45 additions & 1 deletion src/app/ui/modal.ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getSetting } from '../../coolearning/utils/get-setting';
import { SettingsActions, SettingsPositions } from '../../coolearning/enums';
import { createSettingsActionButton } from '../../coolearning/utils/create-settings-action-button';
import { mappingState } from '../state/mapping.state';
import { notificationsUi } from './notifications.ui';

/**
* View model for the modal component.
Expand Down Expand Up @@ -124,7 +125,7 @@ modalUi.updateMapping = function (parameter, control = undefined, type = undefin
});
child.onclick = () => {
if (learned) {
mappingState.unlearn (parameter);
this.unlearn (parameter);
} else {
mappingState.enableLearningMode (parameter);
}
Expand All @@ -135,3 +136,46 @@ modalUi.updateMapping = function (parameter, control = undefined, type = undefin
}
});
};

type LearnOptions = {
parameter: string;
control: number;
type: string;
}

/**
* Learn a mapping between a parameter and a control.
*
* @param {LearnOptions} options - The options for learning.
*/
modalUi.learn = function ({
parameter,
control,
type,
}: LearnOptions) {
if (mappingState.isMapped (parameter)) {
return;
}

mappingState.setParameterMaps ({ parameter, control, type });
modalUi.updateMapping (parameter, control, type);
notificationsUi.notify (
`Learn: control ${control} for ${parameter} (${type})`,
);
};

/**
* Unlearn a mapping between a parameter and a control.
*
* @param {string} parameter - The parameter to unlearn.
*/
modalUi.unlearn = function (parameter: string) {
if (!mappingState.isMapped (parameter)) {
return;
}
mappingState.unsetParameterMaps (parameter);
modalUi.updateMapping (parameter);
notificationsUi.notify (`${parameter} unlearned`);

// saveState ();
};

0 comments on commit d09d9b7

Please sign in to comment.