-
Notifications
You must be signed in to change notification settings - Fork 3
/
js.lua
158 lines (140 loc) · 4.8 KB
/
js.lua
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
__requestQueue = {}
_requestCount = 0
_Request =
{
command = "",
currentTime = 0,
timeOut = 2,
id = '0'
}
local os = love.system.getOS()
local __defaultErrorFunction = nil
local isDebugActive = false
JS = {}
function JS.callJS(funcToCall)
if(os == "Web") then
print("callJavascriptFunction " .. funcToCall)
end
end
--You can pass a set of commands here and, it is a syntactic sugar for executing many commands inside callJS, as it only calls a function
--If you pass arguments to the func beyond the string, it will perform automatically string.format
--Return statement is possible inside this structure
--This will return a string containing a function to be called by JS.callJS
local _unpack
if(_VERSION == "Lua 5.1" or _VERSION == "LuaJIT") then
_unpack = unpack
else
_unpack = table.unpack
end
function JS.stringFunc(str, ...)
str = "(function(){"..str.."})()"
if(#arg > 0) then
str = str:format(_unpack(arg))
end
str = str:gsub("[\n\t]", "")
return str
end
--The call will store in the webDB the return value from the function passed
--it timeouts
local function retrieveJS(funcToCall, id)
--Used for retrieveData function
JS.callJS("FS.writeFile('"..love.filesystem.getSaveDirectory().."/__temp"..id.."', "..funcToCall..");")
end
--Call JS.newRequest instead
function _Request:new(isPromise, command, onDataLoaded, onError, timeout, id)
local obj = {}
setmetatable(obj, self)
obj.command = command
obj.onError = onError or __defaultErrorFunction
if not isPromise then
retrieveJS(command, id)
else
JS.callJS(command)
end
obj.onDataLoaded = onDataLoaded
obj.timeOut = (timeout == nil) and obj.timeOut or timeout
obj.id = id
function obj:getData()
--Try to read from webdb
return love.filesystem.read("__temp"..self.id)
end
function obj:purgeData()
--Data must be purged for not allowing old data to be retrieved
love.filesystem.remove("__temp"..self.id)
end
function obj:update(dt)
self.timeOut = self.timeOut - dt
local retData = self:getData()
if((retData ~= nil and retData ~= "nil") or self.timeOut <= 0) then
if(retData ~= nil and retData:match("ERROR") == nil) then
if isDebugActive then
print("Data has been retrieved "..retData)
end
self.onDataLoaded(retData)
else
self.onError(self.id, retData)
end
self:purgeData()
return false
else
return true
end
end
return obj
end
--Place this function on love.update and set it to return if it returns false (This API is synchronous)
function JS.retrieveData(dt)
local isRetrieving = #__requestQueue ~= 0
local deadRequests = {}
for i = 1, #__requestQueue do
local isUpdating =__requestQueue[i]:update(dt)
if not isUpdating then
table.insert(deadRequests, i)
end
end
for i = 1, #deadRequests do
if(isDebugActive) then
print("Request died: "..deadRequests[i])
end
table.remove(__requestQueue, deadRequests[i])
end
return isRetrieving
end
--May only be used for functions that don't return a promise
function JS.newRequest(funcToCall, onDataLoaded, onError, timeout, optionalId)
if(os ~= "Web") then
return
end
table.insert(__requestQueue, _Request:new(false, funcToCall, onDataLoaded, onError, timeout or 5, optionalId or _requestCount))
end
--This function can be handled manually (in JS code)
--How to: add the function call when your events resolve: FS.writeFile("Put love.filesystem.getSaveDirectory here", "Pass a string here (NUMBER DONT WORK"))
--Or it can be handled by Lua, it auto sets your data if you write the following command:
-- _$_(yourStringOrFunctionHere)
function JS.newPromiseRequest(funcToCall, onDataLoaded, onError, timeout, optionalId)
if(os ~= "Web") then
return
end
optionalId = optionalId or _requestCount
funcToCall = funcToCall:gsub("_$_%(", "FS.writeFile('"..love.filesystem.getSaveDirectory().."/__temp"..optionalId.."', ")
table.insert(__requestQueue, _Request:new(true, funcToCall, onDataLoaded, onError, timeout or 5, optionalId))
end
--It receives the ID from ther request
--Don't try printing the request.command, as it will execute the javascript command
function JS.setDefaultErrorFunction(func)
__defaultErrorFunction = func
end
JS.setDefaultErrorFunction(function(id, error)
if( isDebugActive ) then
local msg = "Data could not be loaded for id:'"..id.."'"
if(error)then
msg = msg.."\nError: "..error
end
print(msg)
end
end)
JS.callJS(JS.stringFunc(
[[
__getWebDB("%s");
]]
, "__LuaJSDB"))