Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 82 lines (70 sloc) 2.034 kb
c252d10 [feature] stdlib: added an experimental UI library
François-Régis Sinot authored
1 /*
2 Copyright © 2011 MLstate
3
4 This file is part of OPA.
5
6 OPA is free software: you can redistribute it and/or modify it under the
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
10 OPA is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with OPA. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20 * Elvis Events
21 *
22 * @category UI
23 * @author David Rajchenbach-Teller, 2011
24 * @destination PUBLIC
25 * @stability EXPERIMENTAL
26 */
27
28 /**
29 * {1 General definitions}
30 */
31
32 @abstract type Event.source('a) = Network.network('a)
33 @abstract type Event.observer = (-> void)
34
35 Event =
36 {{
37 Source =
38 {{
39 /**
40 * Create a source that won't ever send anything.
41 */
42 empty(): Event.source('a) = (Network.empty())
43 }}
44
45 /**
46 * Register to be informed whenever a source produces a new event
47 */
48 observe(source: Event.source('a), sink: channel('a)): Event.observer =
49 (
50 do Network.add(sink, (source))
51 (-> Network.remove(sink, (source)))
52 )
53 /**
54 * As [observe], but with a function
55 */
56 callback(source: Event.source('a), sink: 'a -> void): Event.observer =
57 (
58 observe(source, Session.make({}, (_, msg -> do sink(msg) {unchanged})))
59 )
60 /**
61 * As [callback], but with the function is automatically unregistered once it has been called
62 */
63 callback_once(source: Event.source('a), sink: 'a -> void): Event.observer =
64 (
65 c = Session.make({}, (_, msg -> do sink(msg) {stop}))
66 observe(source, c)
67 )
68
69 /**
70 * Broadcast an event to all observers
71 */
72 trigger(source: Event.source('a), event:'a): void =
73 (
74 Network.broadcast(event, (source))
75 )
76
77 unobserve(observer: Event.observer): void =
78 (
79 (observer)()
80 )
81 }}
Something went wrong with that request. Please try again.