forked from phoffer/crystalized_ruby
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathticker.cr
27 lines (25 loc) · 940 Bytes
/
ticker.cr
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
# I think this was to help figuring out callbacks for parsing hashes?
lib LibTicker
fun on_tick(callback : (Int32, Void* ->), data : Void*)
end
module Ticker
# The callback for the user doesn't have a Void*
def self.on_tick(&callback : Int32 ->)
# We must save this in Crystal-land so the GC doesn't collect it (*)
@@callback = callback
# Since Proc is a {Void*, Void*}, we can't turn that into a Void*, so we
# "box" it: we allocate memory and store the Proc there
boxed_data = Box.box(callback)
# We pass a callback that doesn't form a closure, and pass the boxed_data as
# the callback data
LibTicker.on_tick(->(tick, data) {
# Now we turn data back into the Proc, using Box.unbox
data_as_callback = Box(typeof(callback)).unbox(data)
# And finally invoke the user's callback
data_as_callback.call(tick)
}, boxed_data)
end
end
Ticker.on_tick do |tick|
puts tick
end