-
Notifications
You must be signed in to change notification settings - Fork 0
/
samrocket.lpr
287 lines (251 loc) · 7.07 KB
/
samrocket.lpr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// SAM pattern rocket launcher demo
// Copyright (c) 2023 Pierce Ng.
// Code released under MIT license.
program samrocket;
{$mode objfpc}
uses
browserapp, JS, Classes, SysUtils, web;
const
TEXT_ELT = 'text';
BUTTON_ELT = 'button';
COUNTER_MAX = 10;
type
TControlState = (Invalid, Ready, Counting, Launched, Aborted);
TStateRepresentation = class(TObject)
private
FControlState: TControlState;
FCounter: Integer;
public
property ControlState: TControlState read FControlState;
property Counter: Integer read FCounter write FCounter;
end;
TView = class(TObject)
private
procedure Ready(sr: TStateRepresentation);
procedure Counting(sr: TStateRepresentation);
procedure Launched;
procedure Aborted(sr: TStateRepresentation);
procedure Invalid;
public
procedure Render(sr: TStateRepresentation);
end;
TModel = class(TObject)
private
FCounter: Integer;
FStarted: Boolean;
FLaunched: Boolean;
FAborted: Boolean;
FView: TView;
// Below are domain-specific operations.
procedure Decrement(CurrentCounter: Integer);
procedure Launch;
// Below are SAM operations.
function NextActionPredicate(state: TStateRepresentation): Boolean;
function StateRepresentation: TStateRepresentation;
public
constructor Create;
procedure Present(Proposal: TJSObject);
function IsReady: Boolean;
function IsCounting: Boolean;
function IsLaunched: Boolean;
function IsAborted: Boolean;
end;
TMyApplication = class(TBrowserApplication)
public
procedure doRun; override;
end;
var
Model: TModel;
Application : TMyApplication;
function SAM_Action_Ready(anEvent: TJSMouseEvent): Boolean;
begin
Model.Present(new(['ready', true]));
result := false;
end;
function SAM_Action_Start(anEvent: TJSMouseEvent): Boolean;
begin
Model.Present(new(['start', true]));
result := false;
end;
function SAM_Action_Abort(anEvent: TJSMouseEvent): Boolean;
begin
Model.Present(new(['abort', true]));
result := false;
end;
constructor TModel.Create;
begin
inherited;
FCounter := COUNTER_MAX;
FStarted := false;
FLaunched := false;
FAborted := false;
FView := TView.Create;
end;
procedure TModel.Decrement(CurrentCounter: Integer);
procedure countdown;
begin
self.Present(new(['counter', CurrentCounter - 1]));
end;
begin
window.SetTimeOut(@countdown, 1000);
end;
procedure TModel.Launch;
procedure launching;
begin
self.Present(new(['launch', true]));
end;
begin
window.SetTimeOut(@launching, 500);
end;
function TModel.StateRepresentation: TStateRepresentation;
begin
result := TStateRepresentation.Create;
result.FControlState := TControlState.Invalid;
if self.IsReady then result.FControlState := TControlState.Ready;
if self.IsCounting then result.FControlState := TControlState.Counting;
if self.IsLaunched then result.FControlState := TControlState.Launched;
if self.IsAborted then result.FControlState := TControlState.Aborted;
result.Counter := self.FCounter;
end;
function TModel.NextActionPredicate(state: TStateRepresentation): Boolean;
begin
if state.ControlState = Counting then
begin
if state.Counter > 0 then
begin
self.Decrement(self.FCounter);
exit(true);
end;
if state.Counter = 0 then
begin
self.Launch;
exit(true);
end;
end;
exit(false);
end;
procedure TModel.Present(Proposal: TJSObject);
function b2s(v: Boolean): String;
begin
result := BoolToStr(v, 't', 'f');
end;
var
sr: TStateRepresentation;
begin
if Proposal.hasOwnProperty('ready') then
if Boolean(Proposal['ready']) then
begin
self.FCounter := COUNTER_MAX;
self.FStarted := false;
self.FLaunched := false;
self.FAborted := false;
end;
if Proposal.hasOwnProperty('counter') then
self.FCounter := Integer(Proposal['counter']);
if Proposal.hasOwnProperty('start') then
self.FStarted := Boolean(Proposal['start']);
if Proposal.hasOwnProperty('launch') then
self.FLaunched := Boolean(Proposal['launch']);
if Proposal.hasOwnProperty('abort') then
self.FAborted := Boolean(Proposal['abort']);
sr := self.StateRepresentation;
//writeln(Format('counter=%d; started=%s; launched=%s; aborted=%s', [FCounter, b2s(FStarted), b2s(FLaunched), b2s(FAborted)]));
self.NextActionPredicate(sr);
self.FView.Render(sr);
end;
function TModel.IsReady: Boolean;
begin
result := (FCounter = COUNTER_MAX)
and not FStarted
and not FLaunched
and not FAborted;
end;
function TModel.IsCounting: Boolean;
begin
result := (FCounter <= COUNTER_MAX)
and (FCounter >= 0)
and FStarted
and not FLaunched
and not FAborted;
end;
function TModel.IsLaunched: Boolean;
begin
result := (FCounter = 0)
and FStarted
and FLaunched
and not FAborted;
end;
function TModel.IsAborted: Boolean;
begin
result := (FCounter <= COUNTER_MAX)
and (FCounter >= 0)
and FStarted
and not FLaunched
and FAborted;
end;
procedure TView.Ready(sr: TStateRepresentation);
var
btn: TJSHtmlButtonElement;
begin
Document.GetElementById(TEXT_ELT).innerHtml := Format('Counter = %d', [sr.Counter]);
btn := TJSHtmlButtonElement(Document.GetElementById(BUTTON_ELT));
btn.OnClick := @SAM_Action_Start;
btn.innerHtml := 'Start';
end;
procedure TView.Counting(sr: TStateRepresentation);
var
btn: TJSHtmlButtonElement;
begin
Document.GetElementById(TEXT_ELT).innerHtml := Format('Countdown %d...', [sr.Counter]);
btn := TJSHtmlButtonElement(Document.GetElementById(BUTTON_ELT));
btn.OnClick := @SAM_Action_Abort;
btn.innerHtml := 'Abort';
end;
procedure TView.Launched;
var
btn: TJSHtmlButtonElement;
begin
Document.GetElementById(TEXT_ELT).innerHtml := 'Launched!';
btn := TJSHtmlButtonElement(Document.GetElementById(BUTTON_ELT));
btn.OnClick := @SAM_Action_Ready;
btn.innerHtml := 'Next Rocket';
end;
procedure TView.Aborted(sr: TStateRepresentation);
var
btn: TJSHtmlButtonElement;
begin
Document.GetElementById(TEXT_ELT).innerHtml := Format('Aborted when countdown at %d', [sr.Counter]);
btn := TJSHtmlButtonElement(Document.GetElementById(BUTTON_ELT));
btn.OnClick := @SAM_Action_Ready;
btn.innerHtml := 'New Rocket';
end;
procedure TView.Invalid;
var
btn: TJSHtmlButtonElement;
begin
Document.GetElementById(TEXT_ELT).innerHtml := 'SHOULD NOT HAPPEN: Model state is invalid';
btn := TJSHtmlButtonElement(Document.GetElementById(BUTTON_ELT));
btn.OnClick := @SAM_Action_Ready;
btn.innerHtml := 'Start Over';
end;
procedure TView.Render(sr: TStateRepresentation);
begin
case sr.ControlState of
TControlState.Ready: self.Ready(sr);
TControlState.Counting: self.Counting(sr);
TControlState.Launched: self.Launched;
TControlState.Aborted: self.Aborted(sr);
TControlState.Invalid: self.Invalid;
end;
end;
procedure TMyApplication.doRun;
begin
Model.Present(new(['ready', true]));
end;
begin
Model := TModel.Create;
Application := TMyApplication.Create(nil);
Application.Initialize;
Application.Run;
Application.Free;
end.