Skip to content

Commit

Permalink
Send the event details to Lua
Browse files Browse the repository at this point in the history
  • Loading branch information
britzl committed Oct 14, 2020
1 parent 76d45b9 commit 36a2037
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 13 deletions.
15 changes: 14 additions & 1 deletion README.md
Expand Up @@ -38,7 +38,7 @@ if monetized then
print("The user has an active payment stream")
end
webmonetization.set_listener(function(self, event)
webmonetization.set_listener(function(self, event, details)
if event == webmonetization.EVENT_PENDING then
print("The user is trying to make a first payment")
elseif event == webmonetization.EVENT_START then
Expand All @@ -48,5 +48,18 @@ webmonetization.set_listener(function(self, event)
elseif event == webmonetization.EVENT_STOP then
print("The user has stopped paying")
end
print(details.requestId)
end)
```

The details table contains additional information about the event. Example:

```
{
paymentPointer = "$ilp.uphold.com/QkG86UgXzKq8",
assetScale = 9,
amount = "26009",
requestId = "a1f728aa-21e0-4376-ae99-0ccb22642956",
assetCode = "XRP"
}
```
5 changes: 4 additions & 1 deletion example/example.script
Expand Up @@ -4,7 +4,7 @@ function init(self)
print("The user has an active payment stream")
end

webmonetization.set_listener(function(self, event)
webmonetization.set_listener(function(self, event, details)
if event == webmonetization.EVENT_PENDING then
print("The user is trying to make a first payment")
elseif event == webmonetization.EVENT_START then
Expand All @@ -13,6 +13,9 @@ function init(self)
print("The user is still paying")
elseif event == webmonetization.EVENT_STOP then
print("The user has stopped paying")
else
print("Unknown event", event)
end
pprint(details)
end)
end
2 changes: 1 addition & 1 deletion webmonetization/include/webmonetization.h
Expand Up @@ -4,7 +4,7 @@

#if defined(DM_PLATFORM_HTML5)

typedef void (*OnEventCallback)(const char* event);
typedef void (*OnEventCallback)(const char* event, const char* details);

extern "C" {
void WebMonetization_PlatformSetEventListener(OnEventCallback callback);
Expand Down
24 changes: 18 additions & 6 deletions webmonetization/lib/web/library_webmonetization.js
Expand Up @@ -9,24 +9,36 @@ var WebMonetizationLibrary = {
WebMonetization_PlatformSetEventListener: function(listener) {
Context.listener = listener;

document.monetization.addEventListener("monetizationpending", event => {
dynCall("vi", Context.listener, [allocate(intArrayFromString("monetizationpending"), "i8", ALLOC_STACK)]);
document.monetization.addEventListener("", event => {
dynCall("vii", Context.listener, [
allocate(intArrayFromString("monetizationpending"), "i8", ALLOC_STACK),
allocate(intArrayFromString(JSON.stringify(event.detail !== undefined ? event.detail : {})), "i8", ALLOC_STACK)
]);
});
document.monetization.addEventListener("monetizationstart", event => {
dynCall("vi", Context.listener, [allocate(intArrayFromString("monetizationstart"), "i8", ALLOC_STACK)]);
dynCall("vii", Context.listener, [
allocate(intArrayFromString("monetizationstart"), "i8", ALLOC_STACK),
allocate(intArrayFromString(JSON.stringify(event.detail !== undefined ? event.detail : {})), "i8", ALLOC_STACK)
]);
});
document.monetization.addEventListener("monetizationprogress", event => {
dynCall("vi", Context.listener, [allocate(intArrayFromString("monetizationprogress"), "i8", ALLOC_STACK)]);
dynCall("vii", Context.listener, [
allocate(intArrayFromString("monetizationprogress"), "i8", ALLOC_STACK),
allocate(intArrayFromString(JSON.stringify(event.detail !== undefined ? event.detail : {})), "i8", ALLOC_STACK)
]);
});
document.monetization.addEventListener("monetizationstop", event => {
dynCall("vi", Context.listener, [allocate(intArrayFromString("monetizationstop"), "i8", ALLOC_STACK)]);
dynCall("vii", Context.listener, [
allocate(intArrayFromString("monetizationstop"), "i8", ALLOC_STACK),
allocate(intArrayFromString(JSON.stringify(event.detail !== undefined ? event.detail : {})), "i8", ALLOC_STACK)
]);
});
},

WebMonetization_PlatformIsMonetized: function() {
return document.monetization != undefined && document.monetization.state == "started";
}

};

autoAddDeps(WebMonetizationLibrary, "$Context");
Expand Down
40 changes: 36 additions & 4 deletions webmonetization/src/webmonetization.cpp
Expand Up @@ -27,23 +27,48 @@ static void lua_setfieldstringstring(lua_State* L, const char* key, const char*
}


static void WebMonetization_OnEventListener(const char* event)
static void WebMonetization_OnEventListener(const char* event, const char* details)
{

lua_State* L = dmScript::GetCallbackLuaContext(g_WebMonetization.m_Callback);

DM_LUA_STACK_CHECK(L, 0);

if (!dmScript::IsCallbackValid(g_WebMonetization.m_Callback))
{
dmScript::DestroyCallback(g_WebMonetization.m_Callback);
g_WebMonetization.m_Callback = 0;
return;
}

if (!dmScript::SetupCallback(g_WebMonetization.m_Callback))
{
dmScript::DestroyCallback(g_WebMonetization.m_Callback);
g_WebMonetization.m_Callback = 0;
return;
}

lua_pushstring(L, event);
int ret = lua_pcall(L, 2, 0, 0);
if (ret != 0)

dmJson::Document json;
dmJson::Result r = dmJson::Parse(details, &json);
if (r != dmJson::RESULT_OK)
{
lua_pop(L, 1);
dmLogError("Unable to parse monetization details");
lua_pushnil(L);
}
else
{
char err_str[128];
int convert_r = dmScript::JsonToLua(L, &json, 0, err_str, sizeof(err_str));
if (convert_r < 0)
{
dmLogError("Unable to parse monetization details");
}
dmJson::Free(&json);
}

dmScript::PCall(L, 3, 0);

dmScript::TeardownCallback(g_WebMonetization.m_Callback);
}
Expand All @@ -52,6 +77,11 @@ static int WebMonetization_SetListener(lua_State* L)
{
DM_LUA_STACK_CHECK(L, 0);

if (g_WebMonetization.m_Callback)
{
dmScript::DestroyCallback(g_WebMonetization.m_Callback);
}

g_WebMonetization.m_Callback = dmScript::CreateCallback(L, 1);

WebMonetization_PlatformSetEventListener((OnEventCallback)WebMonetization_OnEventListener);
Expand Down Expand Up @@ -108,11 +138,13 @@ dmExtension::Result InitializeWebMonetizationExtension(dmExtension::Params* para

dmExtension::Result AppFinalizeWebMonetizationExtension(dmExtension::AppParams* params)
{
#if defined(DM_PLATFORM_HTML5)
if (g_WebMonetization.m_Callback != 0)
{
dmScript::DestroyCallback(g_WebMonetization.m_Callback);
g_WebMonetization.m_Callback = 0;
}
#endif
return dmExtension::RESULT_OK;
}

Expand Down

0 comments on commit 36a2037

Please sign in to comment.