11(import "List.ark")
22
3- ###
4- # @meta Events
53# @brief Allows to register events listeners and emit events
64# =begin
75# (let em (events:manager:make))
86# (em.on "myType" (fun (value) (print "This is a callback")))
97# (em.emit "myType") # => prints "This is a callback" thanks to the registered listener
108# =end
119# @author https://github.com/fabien-zoccola
12- ##
1310(let events:manager:make (fun () {
1411 # listeners list
1512 (mut _listeners [])
1613
17- ###
1814 # @brief Checks if a given callback is valid (is a function or a closure)
1915 # @param callback the callback to check
2016 # @details Returns true if the callback is a function/closure, false otherwise
2420 # (closure._check_valid 5) # => false
2521 # =end
2622 # @author https://github.com/fabien-zoccola
27- ##
2823 (let _check_valid (fun (callback)
29- (or (= "Function" (type callback)) (= "Closure" (type callback)) )))
24+ (or (= "Function" (type callback)) (= "Closure" (type callback)))))
3025
31- ###
3226 # @brief Registers an event listener
3327 # @param typ the type of the event to listen for
3428 # @param callback the function/closure that will be called when an event is emitted
3731 # (closure.on "myType" (fun (param) ())
3832 # =end
3933 # @author https://github.com/fabien-zoccola
40- ##
4134 (let on (fun (typ callback)
4235 (if (_check_valid callback)
4336 (set _listeners (append _listeners [typ callback])))))
4437
45- ###
4638 # @brief Emits an event with a value
4739 # @param val the emitted value
4840 # @param typ the type of the emitted event
5143 # (closure.emitWith 5 "myType")
5244 # =end
5345 # @author https://github.com/fabien-zoccola
54- ##
5546 (let emitWith (fun (val typ) {
5647 (mut found false)
5748 (list:forEach _listeners (fun (element)
6152 })))
6253 found
6354 }))
64-
6555
66- ###
6756 # @brief Emits an event with no value
6857 # @param typ the type of the emitted event
6958 # @details Calls emitWith nil <typ>
7059 # =begin
7160 # (closure.emit "myType")
7261 # =end
7362 # @author https://github.com/fabien-zoccola
74- ##
7563 (let emit (fun (typ)
76- (emitWith nil typ) ))
64+ (emitWith nil typ)))
7765
78- ###
7966 # @brief Removes all listeners of a given type
8067 # @param typ the type of event to remove from the list
8168 # @details Returns if at least one listener has been removed
8269 # =begin
8370 # (closure.remove_listeners_of_type "myType")
8471 # =end
8572 # @author https://github.com/fabien-zoccola
86- ##
8773 (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)))))
9076 (let deleted (!= newlist _listeners))
9177 (set _listeners newlist)
9278 deleted
9884
9985 # hidden methods
10086 &_check_valid
101-
87+
10288 # methods
10389 &on
10490 &emit
10591 &emitWith
10692 &removeListenersOfType
10793 ) ())
108- }))
94+ }))
0 commit comments