-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.rkt
404 lines (346 loc) · 14.8 KB
/
commands.rkt
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#lang racket
;; Commands - abstract commands & persistent undo system (requires links to be equal across runs!)
(require racket/serialize
anaphoric
(prefix-in db: db)
"err.rkt"
"links.rkt"
"storage.rkt")
(provide make-command command%
result% open-undo-system current-undo-manager command-table close-undo-system
undo-target<%> undo-mixin can-undo? undo can-redo? redo get-undo-command)
(define current-undo-manager (make-parameter #f))
(define command-table (make-parameter (make-hash)))
(define (can-undo?)
(if (current-undo-manager)
(send (current-undo-manager) can-undo?)
#f))
(define (undo)
(precondition:undo-manager-open?)
(send (current-undo-manager) undo))
(define (can-redo?)
(if (current-undo-manager)
(send (current-undo-manager) can-redo?)
#f))
(define (redo)
(precondition:undo-manager-open?)
(send (current-undo-manager) redo))
(define (get-undo-command)
(precondition:undo-manager-open?)
(send (current-undo-manager) get-undo-command))
(define undo-target<%>
(interface () execute can-execute? undo can-undo? redo can-redo?))
(define undo-mixin
(mixin () (undo-target<%> link-addressable<%>)
(init-field link)
(define/public (get-link)
link)
(super-new)
(associate link this)))
(define/contract command%
(class/c
[init-field (numeric exact-integer?)]
[init-field (symbolic symbol?)]
[init-field (name string?)]
[init-field (help (or/c (->* () () #:rest (listof string?) string?)
string?))]
[init-field (shortcuts (listof (or/c symbol?
string?
char?)))]
[execute (->*m ((is-a?/c link%)) () #:rest (listof any/c) any)]
[can-execute? (->*m ((is-a?/c link%)) () #:rest (listof any/c) any)])
(class* object% (equal<%>)
(init-field numeric)
(init-field symbolic)
(init-field name)
(init-field help)
(init-field shortcuts)
(define/public (execute target-link . args)
(aif (resolve/maybe 'execute target-link)
(let ((result (send it execute this args)))
(cond
(result
(db:call-with-transaction
(get-field db (current-undo-manager))
(lambda ()
(send (current-undo-manager) clear-redo-chain)
(send (current-undo-manager) add-result result)))
result)
(else #f)))
#f))
(define/public (can-execute? target-link . args)
(aif (resolve/maybe 'can-execute? target-link)
(send it can-execute? this args)
#f))
(define/public (equal-to? other recur)
(= numeric (get-field numeric other)))
(define/public (equal-hash-code-of hash-code)
(hash-code numeric))
(define/public (equal-secondary-hash-code-of hash-code)
(hash-code numeric))
(super-new)
(when (hash-has-key? (command-table) numeric)
(fail 'make-command -103 numeric))
(hash-set! (command-table) numeric this)))
(define make-command
(let ((counter 0))
(lambda (symbolic name help keyboard-shortcuts)
(set! counter (add1 counter))
(make-object command% counter symbolic name help keyboard-shortcuts))))
(define/contract result%
(class/c
[init-field (command (is-a?/c command%))]
[init-field (result serializable?)]
[init-field (target-link (is-a?/c link%))]
[init-field (undo-data serializable?)]
[init-field (redo-data serializable?)])
(class object%
(init-field command)
(init-field result)
(init-field target-link)
(init-field undo-data)
(init-field redo-data)
(define/public (can-undo?)
(aif (resolve/maybe 'can-undo? (get-field target-link this))
(send it can-undo? this)
#f))
(define/public (undo)
(aif (resolve/maybe 'undo (get-field target-link this))
(send it undo this)
#f))
(define/public (can-redo?)
(aif (resolve/maybe 'can-redo? (get-field target-link this))
(send it can-redo? this)
#f))
(define/public (redo)
(aif (resolve/maybe 'redo (get-field target-link this))
(let ((result (send it redo this)))
(send (current-undo-manager) add-result result)
result)
#f))
(super-new)))
(define/contract undo-manager%
(class/c
[inherit-field (db (or/c db:connection? #f))]
[init (->m any/c)]
[add-result (->m (is-a?/c result%) any)]
[can-undo? (->m any)]
[get-undo-command (->m any)]
[undo (->m any)]
[can-redo? (->m any)]
[redo (->m any)]
[clear-redo-chain (->m any)])
(class storage%
(inherit open set-component new-item set get get-latest-item delete-item)
(inherit-field db)
;; Public API
(define/public (init)
(open)
(set-component 'UndoChain '(TargetLink blob) '(Command integer) '(UndoData blob) '(RedoData blob) '(Result blob))
(set-component 'RedoChain '(TargetLink blob) '(Command integer) '(UndoData blob) '(RedoData blob) '(Result blob)))
(define/public (add-result result)
(internal-add 'UndoChain result))
(define/public (can-undo?)
(if (get-latest-item 'UndoChain) #t #f))
(define/public (get-undo-command)
(aif (get-latest-item 'UndoChain)
(hash-ref (command-table) (get 'UndoChain it 'Command) #f)
#f))
(define/public (undo)
(aif (get-latest-item 'UndoChain)
(begin0 (send (item->result 'UndoChain it) undo)
(shift-item-to-redo it))
#f))
(define/public (can-redo?)
(if (get-latest-item 'RedoChain) #t #f))
(define/public (redo)
(aif (get-latest-item 'RedoChain)
(begin0 (send (item->result 'RedoChain it) redo)
(delete-item 'RedoChain it))
#f))
(define/public (clear-redo-chain)
(db:query-exec db "DELETE FROM RedoChain;"))
;; Private part
(define (shift-item-to-redo item)
(define result (item->result 'UndoChain item))
(db:call-with-transaction
db
(lambda ()
(delete-item 'UndoChain item)
(internal-add 'RedoChain result))))
(define (item->result chain item)
(letrec ((command (hash-ref (command-table) (get chain item 'Command) #f))
(TargetLinkBytes (get chain item 'TargetLink))
(UndoDataBytes (get chain item 'UndoData))
(RedoDataBytes (get chain item 'RedoData))
(ResultBytes (get chain item 'Result)))
(if (and command (bytes? TargetLinkBytes) (bytes? UndoDataBytes) (bytes? RedoDataBytes) (bytes? ResultBytes)
(> (bytes-length TargetLinkBytes) 0)
(> (bytes-length TargetLinkBytes) 0)
(> (bytes-length UndoDataBytes) 0)
(> (bytes-length RedoDataBytes) 0)
(> (bytes-length ResultBytes) 0))
(new result%
[command command]
[target-link (list->link (with-input-from-bytes TargetLinkBytes (lambda () (deserialize (read)))))]
[result (with-input-from-bytes ResultBytes (lambda () (deserialize (read))))]
[undo-data (with-input-from-bytes UndoDataBytes (lambda () (deserialize (read))))]
[redo-data (with-input-from-bytes RedoDataBytes (lambda () (deserialize (read))))])
(if command (fail 'undo-manager -102) (fail 'undo-manager -104 (get chain item 'Command #f))))))
(define (internal-add chain result)
(when result
(let ((item (new-item chain)))
(db:call-with-transaction
db
(lambda ()
(set chain item 'Command (get-field numeric (get-field command result)))
(set chain item 'TargetLink (with-output-to-bytes (lambda ()
(write (serialize (link->list (get-field target-link result)))))))
(set chain item 'UndoData (with-output-to-bytes (lambda () (write (serialize (get-field undo-data result))))))
(set chain item 'RedoData (with-output-to-bytes (lambda () (write (serialize (get-field redo-data result))))))
(set chain item 'Result (with-output-to-bytes (lambda () (write (serialize (get-field result result))))))))
item)))
(super-new)))
(define/contract (open-undo-system persistent-path #:maintenance-start-proc [maintenance-start (lambda (msg) (void))]
#:maintenance-end-proc [maintenance-end (lambda (msg) (void))]
#:maintenance-start-message [start-message "Compacting undo storage..."]
#:maintenance-end-message [end-message "Compacting undo storage... done."])
(->* ((or/c path? path-string?)) (#:maintenance-start-proc (-> string? any)
#:maintenance-end-proc(-> string? any)
#:maintenance-start-message string?
#:maintenance-end-message string?)
any)
(precondition:undo-manager-closed?)
(current-undo-manager (new undo-manager% [file persistent-path]))
(send (current-undo-manager) init)
(when (send (current-undo-manager) needs-maintenance?)
(maintenance-start start-message)
(send (current-undo-manager) start-maintenance (lambda () (maintenance-end end-message)))))
(define (close-undo-system)
(define manager (current-undo-manager))
(current-undo-manager #f)
(when (and manager (send manager is-open?))
(send manager close)))
(define (resolve/maybe caller target)
(resolve target (lambda () (warn caller -100 (link->list target)) #f)))
;; preconditions
(define (precondition:undo-manager-closed?)
(when (is-a? (current-undo-manager) undo-manager%)
(fail 'undo-manager -105)))
(define (precondition:undo-manager-open?)
(unless (is-a? (current-undo-manager) undo-manager%)
(fail 'undo-manager -106)))
;; error messages
(register-errors
'((-100 appy-command-target-not-found)
(-101 appy-command-empty-data-field)
(-102 appy-undo-empty-data)
(-103 appy-command-numeric-twice)
(-104 appy-undo-version-err)
(-105 appy-undo-manager-already-open)
(-106 appy-undo-manager-closed)))
(module+ test
(require rackunit)
(define undo-tester%
(undo-mixin
(class* object% (undo-target<%>)
(define/public (execute cmd args)
(make-object result% cmd 'test-result (send this get-link) '(undo data) '(some redo data)))
(define/public (can-execute? cmd args)
#t)
(define/public (undo result)
#t)
(define/public (can-undo? result)
#t)
(define/public (redo result)
(make-object result% (get-field command result) 'test-result (send this get-link) '(undo data) '(some redo data)))
(define/public (can-redo? result)
#t)
(super-new))))
(define undoable-stack%
(undo-mixin
(class* object% (undo-target<%>)
(define data '())
(define/public (push arg)
(set! data (cons arg data)))
(define/public (pop)
(let ((result (first data)))
(set! data (rest data))
result))
(define/public (execute cmd args)
(case (get-field symbolic cmd)
((push) (let ((previous data))
(make-object result% cmd (push (first args)) (send this get-link) previous data)))
((pop) (let ((previous data))
(make-object result% cmd (pop) (send this get-link) previous data)))
(else (error 'undoable-stack% "unknown command, I only understand 'push and 'pop, given ~s" (get-field symbolic cmd)))))
(define/public (can-execute? cmd args)
(case (get-field symbolic cmd)
((push) (and (list? args) (not (null? args))))
((pop) (not (null? data)))
(else #f)))
(define/public (can-undo? result)
#t)
(define/public (undo result)
(set! data (get-field undo-data result)) #t)
(define/public (can-redo? result)
#t)
(define/public (redo result)
(let ((previous data))
(set! data (get-field redo-data result))
(make-object result% (get-field command result) (void) (send this get-link) previous data)))
(super-new))))
(define (setup)
(when (file-exists? "private/testing/undo-tests") (delete-file "private/testing/undo-tests"))
(when (file-exists? "private/testing/undo-tests-shm") (delete-file "private/testing/undo-tests-shm"))
(when (file-exists? "private/testing/undo-tests-wal") (delete-file "private/testing/undo-tests-wal"))
(open-undo-system "private/testing/undo-tests"))
(define a (make-object undo-tester% (make-link '(this is a testlink))))
(define teststack (make-object undoable-stack% (make-link '(link to the stack))))
(define (teardown)
(close-undo-system))
;; basic functionality
(check-exn exn:fail? (lambda () (undo)))
(check-false (can-undo?))
(check-false (can-redo?))
(check-not-exn (lambda () (setup)))
(check-false (can-undo?))
(check-false (can-redo?))
(define cmd1 (make-command 'cmd1 "Command 1" "just a test command" '()))
(define cmd2 (make-command 'cmd2 "Command 2" "another test command" '()))
(check-equal? (send a get-link) (make-link '(this is a testlink)))
(check-true (send cmd1 can-execute? (send a get-link)))
(check-equal? (get-field result (send cmd1 execute (send a get-link))) 'test-result)
(check-true (can-undo?))
(check-equal? (get-field symbolic (get-undo-command)) 'cmd1)
(check-true (not (false? (undo))))
(check-true (can-redo?))
(check-true (not (false? (redo))))
(check-false (can-redo?))
(check-true (can-undo?))
;; test the undoable stack example
(define push (make-command 'push "Stack Push" "push a value on the stack" '()))
(define pop (make-command 'pop "Stack Pop" "pop a value from the stack" '()))
(define s (make-link '(link to the stack)))
(check-true (send teststack can-execute? push '(test)))
(check-false (send teststack can-execute? cmd1 '()))
(check-not-exn (lambda () (send teststack execute push '(a))))
(check-not-exn (lambda () (send teststack execute push '(b))))
(check-true (can-undo?))
(check-true (not (false? (undo))))
(check-true (can-redo?))
(check-true (not (false? (send push execute s 'c))))
(check-equal? (get-field result (send pop execute s)) 'c)
(check-true (not (false? (undo))))
(check-equal? (get-field result (send pop execute s)) 'c)
(check-equal? (get-field result (send pop execute s)) 'b)
(check-equal? (get-field result (send pop execute s)) 'a)
(check-false (can-redo?))
(check-true (not (false? (undo))))
(check-true (not (false? (undo))))
(check-true (can-redo?))
(check-not-exn (lambda () (redo)))
(check-true (can-redo?))
(check-not-exn (lambda () (redo)))
(check-not-exn (lambda () (teardown)))
)