Skip to content

Commit ad1e071

Browse files
committed
Changed controller to set run state immediately on run
1 parent d828f2c commit ad1e071

File tree

1 file changed

+62
-39
lines changed

1 file changed

+62
-39
lines changed

src/main/matlab/+symphonyui/+core/Controller.m

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,37 @@ function runProtocol(obj, protocol, persistor)
4848
if obj.state.isPaused()
4949
error('Controller is paused');
5050
end
51-
52-
onRunCompleted = onCleanup(@()obj.completeRun());
53-
obj.prepareRun(protocol, persistor);
54-
55-
obj.run();
51+
52+
try
53+
obj.prepareRun(protocol, persistor);
54+
obj.run();
55+
catch x
56+
obj.completeRun();
57+
rethrow(x);
58+
end
59+
60+
obj.completeRun();
5661
end
5762

5863
function resume(obj)
5964
if ~obj.state.isPaused()
6065
error('Controller not paused');
6166
end
62-
63-
onRunCompleted = onCleanup(@()obj.completeRun());
64-
65-
obj.run();
67+
68+
if isempty(obj.state.isViewingPaused())
69+
obj.state = symphonyui.core.ControllerState.VIEWING;
70+
else
71+
obj.state = symphonyui.core.ControllerState.RECORDING;
72+
end
73+
74+
try
75+
obj.run();
76+
catch x
77+
obj.completeRun();
78+
rethrow(x);
79+
end
80+
81+
obj.completeRun();
6682
end
6783

6884
function requestPause(obj)
@@ -100,15 +116,23 @@ function requestStop(obj)
100116
methods (Access = protected)
101117

102118
function prepareRun(obj, protocol, persistor)
119+
import symphonyui.core.ControllerState;
120+
103121
obj.currentProtocol = protocol;
104122
obj.currentPersistor = persistor;
123+
124+
if isempty(persistor)
125+
obj.state = ControllerState.VIEWING;
126+
else
127+
obj.state = ControllerState.RECORDING;
128+
end
105129

106130
obj.clearEpochQueue();
107131

108132
protocol.prepareRun();
109133

110134
if ~isempty(persistor)
111-
map = protocol.getPropertyDescriptors().toMap();
135+
map = protocol.getProperties();
112136
keys = map.keys;
113137
for i = 1:numel(keys)
114138
map(keys{i}) = obj.propertyValueFromValue(map(keys{i}));
@@ -117,21 +141,37 @@ function prepareRun(obj, protocol, persistor)
117141
end
118142
end
119143

120-
function completeRun(obj)
121-
if obj.state.isPaused()
122-
return;
123-
end
124-
144+
function completeRun(obj)
145+
import symphonyui.core.ControllerState;
146+
125147
protocol = obj.currentProtocol;
126148
persistor = obj.currentPersistor;
127-
obj.currentProtocol = [];
128-
obj.currentPersistor = [];
129-
149+
150+
if obj.state.isPausing()
151+
if isempty(persistor)
152+
obj.state = ControllerState.VIEWING_PAUSED;
153+
else
154+
obj.state = ControllerState.RECORDING_PAUSED;
155+
end
156+
return;
157+
end
158+
130159
if ~isempty(persistor) && ~isempty(persistor.currentEpochBlock)
131160
persistor.endEpochBlock();
132161
end
133-
134-
protocol.completeRun();
162+
163+
try
164+
protocol.completeRun();
165+
catch x
166+
obj.state = ControllerState.STOPPED;
167+
obj.currentProtocol = [];
168+
obj.currentPersistor = [];
169+
rethrow(x);
170+
end
171+
172+
obj.state = ControllerState.STOPPED;
173+
obj.currentProtocol = [];
174+
obj.currentPersistor = [];
135175
end
136176

137177
end
@@ -140,7 +180,6 @@ function completeRun(obj)
140180

141181
function run(obj)
142182
import symphonyui.core.util.NetListener;
143-
import symphonyui.core.ControllerState;
144183

145184
listeners = NetListener.empty(0, 1);
146185

@@ -192,31 +231,15 @@ function onCompletedEpoch(obj, ~, event)
192231
function onDiscardedEpoch(obj, ~, ~)
193232
obj.requestStop();
194233
end
195-
196-
if isempty(obj.currentPersistor)
197-
obj.state = ControllerState.VIEWING;
198-
else
199-
obj.state = ControllerState.RECORDING;
200-
end
201-
234+
202235
try
203236
obj.process();
204237
catch x
205238
delete(listeners);
206-
obj.state = ControllerState.STOPPED;
207239
rethrow(x);
208240
end
209-
241+
210242
delete(listeners);
211-
if obj.state.isPausing()
212-
if isempty(obj.currentPersistor)
213-
obj.state = ControllerState.VIEWING_PAUSED;
214-
else
215-
obj.state = ControllerState.RECORDING_PAUSED;
216-
end
217-
else
218-
obj.state = ControllerState.STOPPED;
219-
end
220243
end
221244

222245
function process(obj)

0 commit comments

Comments
 (0)