Skip to content

Commit f6200f0

Browse files
committed
Implement ysdk.on/off
1 parent d1c585d commit f6200f0

File tree

5 files changed

+141
-26
lines changed

5 files changed

+141
-26
lines changed

example/ysdkdebug/playground.gui_script

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,26 @@ local function log_update(self)
3232
end
3333

3434
local function event_test(self)
35-
yagames.event_on("HISTORY_BACK", function (self)
36-
print("yagames.event_on(\"HISTORY_BACK\")")
35+
-- Example of adding an event listener for "game_api_pause" event:
36+
yagames.event_on("game_api_pause", function (self)
37+
print("yagames.event_on(\"game_api_pause\")")
3738
end)
3839

40+
-- Example of adding an event listener for "game_api_resume" event:
41+
local cb = function (self)
42+
print("yagames.event_on(\"game_api_resume\")")
43+
end
44+
yagames.event_on("game_api_resume", cb)
45+
46+
-- Example of removing an event listener:
47+
-- yagames.event_off("game_api_resume", cb)
48+
49+
-- Example of adding an event listener for "HISTORY_BACK" event (used on the TV platform only):
50+
-- yagames.event_on("HISTORY_BACK", function (self)
51+
-- print("yagames.event_on(\"HISTORY_BACK\")")
52+
-- end)
53+
54+
-- Example of dispatching an event:
3955
-- yagames.event_dispatch("EXIT")
4056
end
4157

yagames/helpers/mock.lua

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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})
2626
end
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
3749
end
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)
142167
end
143168

144169
function 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)
146179
end
147180

148181
function M.adv_get_banner_adv_status(cb_id)
@@ -599,9 +632,22 @@ function M.storage_length()
599632
end
600633

601634
function M.event_dispatch(event_name)
635+
-- No need to do anything here.
602636
end
603637

604638
function 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.
605651
end
606652

607653
function M.get_flags(cb_id, options)

yagames/lib/web/lib_yagames.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ var LibYaGamesPrivate = {
1212
_callback_number: null,
1313
_callback_bool: null,
1414

15+
_listeners: {},
16+
1517
toErrStr: function (err) {
1618
return err + "";
1719
},
@@ -93,6 +95,7 @@ var LibYaGamesPrivate = {
9395
self._callback_empty = callback_empty;
9496
self._callback_number = callback_number;
9597
self._callback_bool = callback_bool;
98+
self._listeners = {};
9699

97100
while (typeof YaGamesPrivate_MsgQueue !== "undefined" && YaGamesPrivate_MsgQueue.length) {
98101
var m = YaGamesPrivate_MsgQueue.shift();
@@ -108,6 +111,7 @@ var LibYaGamesPrivate = {
108111
self._callback_empty = null;
109112
self._callback_number = null;
110113
self._callback_bool = null;
114+
self._listeners = {};
111115
},
112116

113117
YaGamesPrivate_IsAvailableMethod: function (cb_id, cname) {
@@ -965,18 +969,20 @@ var LibYaGamesPrivate = {
965969
},
966970

967971
YaGamesPrivate_Event_Dispatch: function (cevent_name) {
968-
var self = YaGamesPrivate;
969-
var event_name = UTF8ToString(cevent_name);
972+
const self = YaGamesPrivate;
973+
const event_name = UTF8ToString(cevent_name);
970974
self._ysdk.dispatchEvent(self._ysdk.EVENTS[event_name]);
971975
},
972976

973977
YaGamesPrivate_Event_On: function (cevent_name, cb_id) {
974-
var self = YaGamesPrivate;
975-
var event_name = UTF8ToString(cevent_name);
978+
const self = YaGamesPrivate;
979+
const event_name = UTF8ToString(cevent_name);
976980
try {
977-
self._ysdk.onEvent(self._ysdk.EVENTS[event_name], () => {
981+
const cb = () => {
978982
self.send(cb_id, null);
979-
});
983+
};
984+
self._listeners[cb_id] = cb;
985+
self._ysdk.on(event_name, cb);
980986

981987
// Uncomment to test the behaviour:
982988
// setInterval(function() { self.send(cb_id, null); }, 3000);
@@ -985,10 +991,20 @@ var LibYaGamesPrivate = {
985991
}
986992
},
987993

994+
YaGamesPrivate_Event_Off: function (cevent_name, cb_id) {
995+
const self = YaGamesPrivate;
996+
const event_name = UTF8ToString(cevent_name);
997+
if (!self._listeners[cb_id]) {
998+
return;
999+
}
1000+
self._ysdk.off(event_name, self._listeners[cb_id]);
1001+
delete self._listeners[cb_id];
1002+
},
1003+
9881004
YaGamesPrivate_GetFlags: function (cb_id, coptions) {
989-
var self = YaGamesPrivate;
1005+
const self = YaGamesPrivate;
9901006
try {
991-
var options = coptions === 0 ? {} : self.parseJson(UTF8ToString(coptions));
1007+
const options = coptions === 0 ? {} : self.parseJson(UTF8ToString(coptions));
9921008
self._ysdk
9931009
.getFlags(options)
9941010
.then((flags) => {
@@ -998,7 +1014,7 @@ var LibYaGamesPrivate = {
9981014
self.send(cb_id, self.toErrStr(err));
9991015
});
10001016
} catch (err) {
1001-
1017+
self.delaySend(cb_id, self.toErrStr(err));
10021018
}
10031019
}
10041020
};

yagames/src/main.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ extern "C"
8080
const int YaGamesPrivate_Storage_Length();
8181
void YaGamesPrivate_Event_Dispatch(const char* cevent_name);
8282
void YaGamesPrivate_Event_On(const char* cevent_name, const int cb_id);
83+
void YaGamesPrivate_Event_Off(const char* cevent_name, const int cb_id);
8384
void YaGamesPrivate_GetFlags(const int cb_id, const char* coptions);
8485
}
8586

@@ -97,7 +98,7 @@ struct YaGamesPrivateListener
9798
int m_OnlyId;
9899
};
99100

100-
static void UnregisterCallback(lua_State* L, YaGamesPrivateListener* cbk);
101+
static int UnregisterCallback(lua_State* L, YaGamesPrivateListener* cbk);
101102
static int GetEqualIndexOfListener(lua_State* L, YaGamesPrivateListener* cbk);
102103

103104
static dmArray<YaGamesPrivateListener> m_Listeners;
@@ -276,6 +277,7 @@ static void SendBoolMessage(const int cb_id, const char* message_id, int message
276277
}
277278
}
278279

280+
// WARNING: Sets m_OnlyId of `cbk` if the callback is found.
279281
static int GetEqualIndexOfListener(lua_State* L, YaGamesPrivateListener* cbk)
280282
{
281283
lua_rawgeti(L, LUA_REGISTRYINDEX, cbk->m_Callback);
@@ -293,6 +295,7 @@ static int GetEqualIndexOfListener(lua_State* L, YaGamesPrivateListener* cbk)
293295
if (lua_equal(L, second, second + 1))
294296
{
295297
lua_pop(L, 3);
298+
cbk->m_OnlyId = cb->m_OnlyId;
296299
return i;
297300
}
298301
lua_pop(L, 2);
@@ -306,7 +309,7 @@ static int GetEqualIndexOfListener(lua_State* L, YaGamesPrivateListener* cbk)
306309
return -1;
307310
}
308311

309-
static void UnregisterCallback(lua_State* L, YaGamesPrivateListener* cbk)
312+
static int UnregisterCallback(lua_State* L, YaGamesPrivateListener* cbk)
310313
{
311314
int index = GetEqualIndexOfListener(L, cbk);
312315
if (index >= 0)
@@ -322,11 +325,13 @@ static void UnregisterCallback(lua_State* L, YaGamesPrivateListener* cbk)
322325
{
323326
YaGamesPrivate_RemoveCallbacks();
324327
}
328+
return cbk->m_OnlyId;
325329
}
326330
else
327331
{
328332
dmLogError("Can't remove a callback that didn't not register.");
329333
}
334+
return -1;
330335
}
331336

332337
static int AddListener(lua_State* L)
@@ -382,8 +387,16 @@ static int RemoveListener(lua_State* L)
382387
dmScript::GetInstance(L);
383388
cbk.m_Self = dmScript::Ref(L, LUA_REGISTRYINDEX);
384389

385-
UnregisterCallback(L, &cbk);
386-
return 0;
390+
int only_id = UnregisterCallback(L, &cbk);
391+
if (only_id != -1)
392+
{
393+
lua_pushinteger(L, only_id);
394+
}
395+
else
396+
{
397+
lua_pushnil(L);
398+
}
399+
return 1;
387400
}
388401

389402
//
@@ -837,6 +850,12 @@ static int Event_On(lua_State* L)
837850
return 0;
838851
}
839852

853+
static int Event_Off(lua_State* L)
854+
{
855+
YaGamesPrivate_Event_Off(luaL_checkstring(L, 1), luaL_checkint(L, 2));
856+
return 0;
857+
}
858+
840859
//
841860
// Flags
842861
//
@@ -931,6 +950,7 @@ static const luaL_reg Module_methods[] = {
931950
// - Events
932951
{ "event_dispatch", Event_Dispatch },
933952
{ "event_on", Event_On },
953+
{ "event_off", Event_Off },
934954
// - Config
935955
{ "get_flags", GetFlags },
936956
{ 0, 0 }

yagames/yagames.lua

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,7 @@ end
727727
--- Initialize the Safe Storage
728728
-- @tparam function callback
729729
function M.storage_init(callback)
730+
assert_ysdk_ready()
730731
assert(type(callback) == "function", "Callback function is required")
731732

732733
yagames_private.get_storage(helper.wrap_for_promise(function(self, err)
@@ -790,7 +791,7 @@ end
790791
-- @tparam string event_name
791792
function M.event_dispatch(event_name)
792793
assert_ysdk_ready()
793-
assert(type(event_name) == "string", "event_name is not a string.")
794+
assert(type(event_name) == "string", "`event_name` is not a string.")
794795

795796
yagames_private.event_dispatch(event_name)
796797
end
@@ -800,16 +801,32 @@ end
800801
-- @tparam function listener
801802
function M.event_on(event_name, listener)
802803
assert_ysdk_ready()
803-
assert(type(event_name) == "string", "event_name is not a string.")
804-
assert(type(listener) == "function", "listener is not a function.")
804+
assert(type(event_name) == "string", "`event_name` is not a string.")
805+
assert(type(listener) == "function", "`listener` is not a function.")
805806

806807
local cb_id = helper.next_cb_id()
807-
yagames_private.add_listener(cb_id, function(self, _cb_id, err)
808-
listener(self, err)
808+
yagames_private.add_listener(cb_id, function(self, _cb_id, err_or_message_id, message)
809+
-- print("*** _CB_ID", _cb_id, " = CB_ID", cb_id, "MESSAGE_ID", err_or_message_id, "MESSAGE", message)
810+
listener(self, err_or_message_id)
809811
end)
810812
yagames_private.event_on(event_name, cb_id)
811813
end
812814

815+
--- Remove an event listener.
816+
-- @tparam string event_name
817+
-- @tparam function listener
818+
function M.event_off(event_name, listener)
819+
assert_ysdk_ready()
820+
assert(type(event_name) == "string", "`event_name` is not a string.")
821+
assert(type(listener) == "function", "`listener` is not a function.")
822+
823+
local cb_id = yagames_private.remove_listener(listener)
824+
if not cb_id then
825+
error("The listener is not found.")
826+
end
827+
yagames_private.event_off(event_name, cb_id)
828+
end
829+
813830
--- Asynchronously get remote config data
814831
-- @tparam[opt] {defaultFlags={},clientFeatures={}} options
815832
-- @tparam function callback

0 commit comments

Comments
 (0)