1
1
(import "List.ark")
2
2
3
- ###
4
- # @meta Events
5
3
# @brief Allows to register events listeners and emit events
6
4
# =begin
7
5
# (let em (events:manager:make))
8
6
# (em.on "myType" (fun (value) (print "This is a callback")))
9
7
# (em.emit "myType") # => prints "This is a callback" thanks to the registered listener
10
8
# =end
11
9
# @author https://github.com/fabien-zoccola
12
- ##
13
10
(let events:manager:make (fun () {
14
11
# listeners list
15
12
(mut _listeners [])
16
13
17
- ###
18
14
# @brief Checks if a given callback is valid (is a function or a closure)
19
15
# @param callback the callback to check
20
16
# @details Returns true if the callback is a function/closure, false otherwise
24
20
# (closure._check_valid 5) # => false
25
21
# =end
26
22
# @author https://github.com/fabien-zoccola
27
- ##
28
23
(let _check_valid (fun (callback)
29
- (or (= "Function" (type callback)) (= "Closure" (type callback)) )))
24
+ (or (= "Function" (type callback)) (= "Closure" (type callback)))))
30
25
31
- ###
32
26
# @brief Registers an event listener
33
27
# @param typ the type of the event to listen for
34
28
# @param callback the function/closure that will be called when an event is emitted
37
31
# (closure.on "myType" (fun (param) ())
38
32
# =end
39
33
# @author https://github.com/fabien-zoccola
40
- ##
41
34
(let on (fun (typ callback)
42
35
(if (_check_valid callback)
43
36
(set _listeners (append _listeners [typ callback])))))
44
37
45
- ###
46
38
# @brief Emits an event with a value
47
39
# @param val the emitted value
48
40
# @param typ the type of the emitted event
51
43
# (closure.emitWith 5 "myType")
52
44
# =end
53
45
# @author https://github.com/fabien-zoccola
54
- ##
55
46
(let emitWith (fun (val typ) {
56
47
(mut found false)
57
48
(list:forEach _listeners (fun (element)
61
52
})))
62
53
found
63
54
}))
64
-
65
55
66
- ###
67
56
# @brief Emits an event with no value
68
57
# @param typ the type of the emitted event
69
58
# @details Calls emitWith nil <typ>
70
59
# =begin
71
60
# (closure.emit "myType")
72
61
# =end
73
62
# @author https://github.com/fabien-zoccola
74
- ##
75
63
(let emit (fun (typ)
76
- (emitWith nil typ) ))
64
+ (emitWith nil typ)))
77
65
78
- ###
79
66
# @brief Removes all listeners of a given type
80
67
# @param typ the type of event to remove from the list
81
68
# @details Returns if at least one listener has been removed
82
69
# =begin
83
70
# (closure.remove_listeners_of_type "myType")
84
71
# =end
85
72
# @author https://github.com/fabien-zoccola
86
- ##
87
73
(let removeListenersOfType (fun (typ) {
88
- (let newlist (list:filter _listeners (fun (element)
89
- (!= typ (@ element 0)) )))
74
+ (let newlist
75
+ (list:filter _listeners (fun (element) ( != typ (@ element 0)))))
90
76
(let deleted (!= newlist _listeners))
91
77
(set _listeners newlist)
92
78
deleted
98
84
99
85
# hidden methods
100
86
&_check_valid
101
-
87
+
102
88
# methods
103
89
&on
104
90
&emit
105
91
&emitWith
106
92
&removeListenersOfType
107
93
) ())
108
- }))
94
+ }))
0 commit comments