-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cr
53 lines (50 loc) · 1.9 KB
/
utils.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
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
53
module Api::Utils
macro generate_subscriber(facade, topic, message, drop, key_check = nil)
# Subscribe to {{topic.id}} events.
def subscribe_{{topic.id}}(*extra_args, once = false, &callback : {{message}} -> Void)
@subscriber_sync.synchronize do
%key = {{ topic }}
subscriber = uninitialized Void* -> Void
subscriber = ->(c_message: Void*) {
if Thread.current.event_base.nil?
pp Thread.current
raise "Subscriber is not running in a Crystal thread…"
end
message = {{message}}.new c_message.as(LibHermes::C{{message}}*).value
{% if key_check %}
unless {{ key_check }}
return
end
{% end %}
callback.call(message)
@subscriber_sync.synchronize do
@subscriptions[%key].delete(subscriber) if once
end
}
unless @subscriptions.has_key? %key
@subscriptions[%key] = [] of Void* -> Void
dispatcher = ->(message: LibHermes::C{{message}}*, user_data : Void*) {
GC.disable # Disabling GC because this callback runs inside a thread spawned by the hermes code.
cleanup = -> {
LibHermes.{{drop}}(message)
nil
}
Box((Void*, String, -> Void) -> Void).unbox(user_data).call(message.as(Void*), {{ topic }}, cleanup)
GC.enable # Re-enabling GC.
Process.kill(Signal::USR1, Process.pid)
}
call! LibHermes.hermes_{{ facade }}_subscribe_{{topic.id}}(@facade, *extra_args, dispatcher)
end
@subscriptions[%key] << subscriber
subscriber
end
end
# Unsubscribe to {{topic.id}} events.
def unsubscribe_{{topic.id}}(callback_ref, *extra_args)
@subscriber_sync.synchronize do
%key = {{ topic }}
@subscriptions[%key].delete(callback_ref)
end
end
end
end