@@ -9,13 +9,13 @@ local NO_ERR = nil
99--
1010--
1111
12- function M .send (cb_id , message_id , message )
13- timer .delay (0.001 , false , function (self )
12+ function M .send (cb_id , arg1 , arg2 )
13+ timer .delay (0 , false , function (self )
1414 local count = # M .listeners
1515 for i = count , 1 , - 1 do
1616 local listener = M .listeners [i ]
1717 if listener .only_id == cb_id then
18- listener .func (self , cb_id , message_id , message )
18+ listener .func (self , cb_id , arg1 , arg2 )
1919 end
2020 end
2121 end )
@@ -25,17 +25,42 @@ function M.add_listener(cb_id, listener)
2525 table.insert (M .listeners , {only_id = cb_id , func = listener })
2626end
2727
28- function M .remove_listener (listener )
28+ function M .remove_listener (listener_fn )
2929 local count = # M .listeners
3030 for i = count , 1 , - 1 do
3131 local listener = M .listeners [i ]
32- if listener .func == listener then
32+ if listener .func == listener_fn then
3333 table.remove (M .listeners , i )
34+ return listener .only_id
35+ end
36+ end
37+ return nil
38+ end
39+
40+ local function dispatch_event (event_name , arg1 , arg2 )
41+ local count = # M .listeners
42+ for i = count , 1 , - 1 do
43+ local listener = M .listeners [i ]
44+ if listener .event_name == event_name then
45+ M .send (listener .only_id , arg1 , arg2 )
3446 break
3547 end
3648 end
3749end
3850
51+ local function sequence_calls (...)
52+ local args = {... }
53+ local handle
54+ handle = timer .delay (0 , true , function (self )
55+ if # args > 0 then
56+ local func = table.remove (args , 1 )
57+ func (self )
58+ else
59+ timer .cancel (handle )
60+ end
61+ end )
62+ end
63+
3964--
4065-- Yandex Games SDK
4166--
@@ -142,7 +167,15 @@ function M.show_fullscreen_adv(cb_id)
142167end
143168
144169function M .show_rewarded_video (cb_id )
145- M .send (cb_id , " close" )
170+ sequence_calls (function (self )
171+ dispatch_event (" game_api_pause" , NO_ERR )
172+ end , function (self )
173+ -- Uncomment this to receive "rewarded" event.
174+ -- M.send(cb_id, "rewarded")
175+ end , function (self )
176+ M .send (cb_id , " close" )
177+ dispatch_event (" game_api_resume" , NO_ERR )
178+ end )
146179end
147180
148181function M .adv_get_banner_adv_status (cb_id )
@@ -599,9 +632,22 @@ function M.storage_length()
599632end
600633
601634function M .event_dispatch (event_name )
635+ -- No need to do anything here.
602636end
603637
604638function M .event_on (event_name , cb_id )
639+ -- Add event name to the listener to be able to find it later.
640+ local count = # M .listeners
641+ for i = count , 1 , - 1 do
642+ local listener = M .listeners [i ]
643+ if listener .only_id == cb_id then
644+ listener .event_name = event_name
645+ end
646+ end
647+ end
648+
649+ function M .event_off (event_name , cb_id )
650+ -- No need to do anything here.
605651end
606652
607653function M .get_flags (cb_id , options )
0 commit comments