-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.ss
executable file
·52 lines (44 loc) · 1.49 KB
/
example.ss
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
#! /usr/bin/env -S chez-scheme --script
;; SPDX-License-Identifier: Unlicense
;; Small example of using ev library under Chez Scheme.
;; It demonstrates:
;; - ev-io waiting on a file (stdin)
;; - ev-prepare as a one-shot way to initialise ev-timer
;; - ev-timer to countdown to the end
;; - ev-async to display timer events and how useful this watcher would be for custom user events (via ev-async-send)
;; - rec as a way of defining self-contained watchers that cleanup after themselves without adding to any namespace.
(import
(ev)
(rnrs)
(only (chezscheme) rec))
(define j 0)
(define tw #f)
(define asyncw
(ev-async
(lambda (w rev)
(display "timer called ")(display j)(newline)
(display "async after timer: pending ")(display (ev-async-pending? w))(newline))))
(define stdinw
(ev-io 0 (evmask 'READ)
(lambda (w rev)
(display "key pressed: ")(display (read-char))(newline)
(ev-io-stop w)
(ev-break (evbreak 'ALL)))))
(rec prepw
(ev-prepare
(lambda (w rev)
(display "enabling timer watcher..")(newline)
(set! tw
(ev-timer 1 5
(lambda (timer i)
(set! j (+ 1 j))
(ev-async-send asyncw)
(display "async before timer: pending ")(display (ev-async-pending? asyncw))(newline)
(when (> j 4)
(ev-break (evbreak 'ONE))))))
;; once initialised, clear away the prepare watcher.
(ev-prepare-free prepw))))
(ev-run)
(ev-async-free asyncw)
(ev-io-free stdinw)
(ev-timer-free tw)