@thigasdevelopment/luam 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lua/async.lua +118 -118
- package/luam.mjs +363 -96
- package/package.json +1 -1
- package/template/README.md +0 -120
- package/template/config.lua +0 -14
- package/template/env +0 -11
- package/template/framework/bootstrap-client.luam +0 -9
- package/template/framework/bootstrap-server.luam +0 -9
- package/template/framework/command.luam +0 -54
- package/template/framework/core.luam +0 -58
- package/template/framework/event.luam +0 -39
- package/template/framework/listener.luam +0 -40
- package/template/framework/loader.luam +0 -73
- package/template/framework/thread-pool.luam +0 -27
- package/template/gitignore +0 -7
- package/template/src/client/commands/ping-command.luam +0 -8
- package/template/src/client/main.luam +0 -5
- package/template/src/server/commands/hello-command.luam +0 -9
- package/template/src/server/handlers/player-join-listener.luam +0 -9
- package/template/src/server/main.luam +0 -11
- package/template/src/shared/config.luam +0 -5
- package/template/src/shared/framework/command.luam +0 -54
- package/template/src/shared/framework/core.luam +0 -58
- package/template/src/shared/framework/event.luam +0 -39
- package/template/src/shared/framework/listener.luam +0 -40
- package/template/src/shared/framework/loader.luam +0 -73
- package/template/src/shared/framework/thread-pool.luam +0 -27
package/lua/async.lua
CHANGED
|
@@ -1,119 +1,119 @@
|
|
|
1
|
-
---@class Async
|
|
2
|
-
---@field tasks Threads
|
|
3
|
-
---@field interval number
|
|
4
|
-
---@field map fun(self: Async, array: table, func: fun(value: any, index: number): any, callback?: fun(elapsed: number): any): number
|
|
5
|
-
---@field iterate fun(self: Async, from: number, to: number, increment: number, func: fun(i: number): any, callback?: fun(elapsed: number): any): number
|
|
6
|
-
---@field foreach fun(self: Async, object: table<any, any>, func: fun(value: any, key: any): any, callback?: fun(elapsed: number): any): number
|
|
7
|
-
---@field getInterval fun(self: Async): number
|
|
8
|
-
---@field setInterval fun(self: Async, interval: number): boolean
|
|
9
|
-
Async = {
|
|
10
|
-
---@param interval number
|
|
11
|
-
---@return Async
|
|
12
|
-
new = function (interval)
|
|
13
|
-
---@type Async
|
|
14
|
-
local self = setmetatable({ }, { __index = Async });
|
|
15
|
-
self.tasks = Threads.new ('concurrent', 'normal');
|
|
16
|
-
|
|
17
|
-
self.interval = 100;
|
|
18
|
-
self:setInterval (interval);
|
|
19
|
-
return self;
|
|
20
|
-
end,
|
|
21
|
-
|
|
22
|
-
---@param self Async
|
|
23
|
-
---@param array table
|
|
24
|
-
---@param func fun(value: any, index: number): any
|
|
25
|
-
---@param callback? fun(elapsed: number): any
|
|
26
|
-
---@return number
|
|
27
|
-
map = function (self, array, func, callback)
|
|
28
|
-
return self.tasks:add (
|
|
29
|
-
function (self)
|
|
30
|
-
local tick = getTickCount ();
|
|
31
|
-
for index, value in ipairs (array) do
|
|
32
|
-
func (value, index);
|
|
33
|
-
|
|
34
|
-
sleep (self.interval);
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
local callbackType = type (callback);
|
|
38
|
-
if (callbackType == 'function') then
|
|
39
|
-
local elapsed = (getTickCount () - tick);
|
|
40
|
-
callback (elapsed);
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
);
|
|
44
|
-
end,
|
|
45
|
-
|
|
46
|
-
---@param self Async
|
|
47
|
-
---@param from number
|
|
48
|
-
---@param to number
|
|
49
|
-
---@param increment number
|
|
50
|
-
---@param func fun(i: number): any
|
|
51
|
-
---@param callback? fun(elapsed: number): any
|
|
52
|
-
---@return number
|
|
53
|
-
iterate = function (self, from, to, increment, func, callback)
|
|
54
|
-
return self.tasks:add (
|
|
55
|
-
function (self)
|
|
56
|
-
local tick = getTickCount ();
|
|
57
|
-
for i = from, to, increment do
|
|
58
|
-
func (i);
|
|
59
|
-
|
|
60
|
-
sleep (self.interval);
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
local callbackType = type (callback);
|
|
64
|
-
if (callbackType == 'function') then
|
|
65
|
-
local elapsed = (getTickCount () - tick);
|
|
66
|
-
callback (elapsed);
|
|
67
|
-
end
|
|
68
|
-
return true;
|
|
69
|
-
end, { }, from, to, increment, func, callback
|
|
70
|
-
);
|
|
71
|
-
end,
|
|
72
|
-
|
|
73
|
-
---@param self Async
|
|
74
|
-
---@param object table<any, any>
|
|
75
|
-
---@param func fun(value: any, key: any): any
|
|
76
|
-
---@param callback? fun(elapsed: number): any
|
|
77
|
-
---@return number
|
|
78
|
-
foreach = function (self, object, func, callback)
|
|
79
|
-
return self.tasks:add (
|
|
80
|
-
function (self)
|
|
81
|
-
local tick = getTickCount ();
|
|
82
|
-
for key, value in pairs (object) do
|
|
83
|
-
func (value, key);
|
|
84
|
-
|
|
85
|
-
sleep (self.interval);
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
local callbackType = type (callback);
|
|
89
|
-
if (callbackType == 'function') then
|
|
90
|
-
local elapsed = (getTickCount () - tick);
|
|
91
|
-
callback (elapsed);
|
|
92
|
-
end
|
|
93
|
-
end, { }, object, func, callback
|
|
94
|
-
);
|
|
95
|
-
end,
|
|
96
|
-
|
|
97
|
-
---@param self Async
|
|
98
|
-
---@return number
|
|
99
|
-
getInterval = function (self)
|
|
100
|
-
return self.interval;
|
|
101
|
-
end,
|
|
102
|
-
|
|
103
|
-
---@param self Async
|
|
104
|
-
---@param interval number
|
|
105
|
-
---@return boolean
|
|
106
|
-
setInterval = function (self, interval)
|
|
107
|
-
interval = tonumber (interval);
|
|
108
|
-
if (not interval) then
|
|
109
|
-
return false;
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
if (interval < 1) or (interval == self:getInterval ()) then
|
|
113
|
-
return false;
|
|
114
|
-
end
|
|
115
|
-
|
|
116
|
-
self.interval = interval;
|
|
117
|
-
return true;
|
|
118
|
-
end,
|
|
1
|
+
---@class Async
|
|
2
|
+
---@field tasks Threads
|
|
3
|
+
---@field interval number
|
|
4
|
+
---@field map fun(self: Async, array: table, func: fun(value: any, index: number): any, callback?: fun(elapsed: number): any): number
|
|
5
|
+
---@field iterate fun(self: Async, from: number, to: number, increment: number, func: fun(i: number): any, callback?: fun(elapsed: number): any): number
|
|
6
|
+
---@field foreach fun(self: Async, object: table<any, any>, func: fun(value: any, key: any): any, callback?: fun(elapsed: number): any): number
|
|
7
|
+
---@field getInterval fun(self: Async): number
|
|
8
|
+
---@field setInterval fun(self: Async, interval: number): boolean
|
|
9
|
+
Async = {
|
|
10
|
+
---@param interval number
|
|
11
|
+
---@return Async
|
|
12
|
+
new = function (interval)
|
|
13
|
+
---@type Async
|
|
14
|
+
local self = setmetatable({ }, { __index = Async });
|
|
15
|
+
self.tasks = Threads.new ('concurrent', 'normal');
|
|
16
|
+
|
|
17
|
+
self.interval = 100;
|
|
18
|
+
self:setInterval (interval);
|
|
19
|
+
return self;
|
|
20
|
+
end,
|
|
21
|
+
|
|
22
|
+
---@param self Async
|
|
23
|
+
---@param array table
|
|
24
|
+
---@param func fun(value: any, index: number): any
|
|
25
|
+
---@param callback? fun(elapsed: number): any
|
|
26
|
+
---@return number
|
|
27
|
+
map = function (self, array, func, callback)
|
|
28
|
+
return self.tasks:add (
|
|
29
|
+
function (self)
|
|
30
|
+
local tick = getTickCount ();
|
|
31
|
+
for index, value in ipairs (array) do
|
|
32
|
+
func (value, index);
|
|
33
|
+
|
|
34
|
+
sleep (self.interval);
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
local callbackType = type (callback);
|
|
38
|
+
if (callbackType == 'function') then
|
|
39
|
+
local elapsed = (getTickCount () - tick);
|
|
40
|
+
callback (elapsed);
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
);
|
|
44
|
+
end,
|
|
45
|
+
|
|
46
|
+
---@param self Async
|
|
47
|
+
---@param from number
|
|
48
|
+
---@param to number
|
|
49
|
+
---@param increment number
|
|
50
|
+
---@param func fun(i: number): any
|
|
51
|
+
---@param callback? fun(elapsed: number): any
|
|
52
|
+
---@return number
|
|
53
|
+
iterate = function (self, from, to, increment, func, callback)
|
|
54
|
+
return self.tasks:add (
|
|
55
|
+
function (self)
|
|
56
|
+
local tick = getTickCount ();
|
|
57
|
+
for i = from, to, increment do
|
|
58
|
+
func (i);
|
|
59
|
+
|
|
60
|
+
sleep (self.interval);
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
local callbackType = type (callback);
|
|
64
|
+
if (callbackType == 'function') then
|
|
65
|
+
local elapsed = (getTickCount () - tick);
|
|
66
|
+
callback (elapsed);
|
|
67
|
+
end
|
|
68
|
+
return true;
|
|
69
|
+
end, { }, from, to, increment, func, callback
|
|
70
|
+
);
|
|
71
|
+
end,
|
|
72
|
+
|
|
73
|
+
---@param self Async
|
|
74
|
+
---@param object table<any, any>
|
|
75
|
+
---@param func fun(value: any, key: any): any
|
|
76
|
+
---@param callback? fun(elapsed: number): any
|
|
77
|
+
---@return number
|
|
78
|
+
foreach = function (self, object, func, callback)
|
|
79
|
+
return self.tasks:add (
|
|
80
|
+
function (self)
|
|
81
|
+
local tick = getTickCount ();
|
|
82
|
+
for key, value in pairs (object) do
|
|
83
|
+
func (value, key);
|
|
84
|
+
|
|
85
|
+
sleep (self.interval);
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
local callbackType = type (callback);
|
|
89
|
+
if (callbackType == 'function') then
|
|
90
|
+
local elapsed = (getTickCount () - tick);
|
|
91
|
+
callback (elapsed);
|
|
92
|
+
end
|
|
93
|
+
end, { }, object, func, callback
|
|
94
|
+
);
|
|
95
|
+
end,
|
|
96
|
+
|
|
97
|
+
---@param self Async
|
|
98
|
+
---@return number
|
|
99
|
+
getInterval = function (self)
|
|
100
|
+
return self.interval;
|
|
101
|
+
end,
|
|
102
|
+
|
|
103
|
+
---@param self Async
|
|
104
|
+
---@param interval number
|
|
105
|
+
---@return boolean
|
|
106
|
+
setInterval = function (self, interval)
|
|
107
|
+
interval = tonumber (interval);
|
|
108
|
+
if (not interval) then
|
|
109
|
+
return false;
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
if (interval < 1) or (interval == self:getInterval ()) then
|
|
113
|
+
return false;
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
self.interval = interval;
|
|
117
|
+
return true;
|
|
118
|
+
end,
|
|
119
119
|
};
|