-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
85 lines (79 loc) · 2.98 KB
/
init.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
--[[lit-meta
name = "alphafantomu/kagura-mea"
version = "0.0.4"
description = "A lightweight async function binder meant to mimick libuv async functions"
tags = { "luvit", "reference", "async", "callbacks", "lua", "work", "thread" }
license = "MIT"
author = { name = "Ari Kumikaeru"}
homepage = "https://github.com/alphafantomu/kagura-mea"
files = {"**.lua"}
]]
local require, type = require, type;
local uv = require('uv');
local getCallback = function(a, b, c, d, e, f)
return type(f) == 'function' and f
or type(e) == 'function' and e
or type(d) == 'function' and d
or type(c) == 'function' and c
or type(b) == 'function' and b
or type(a) == 'function' and a
or nil;
end;
---@param fx function the synchronous function to wrap around
---@return function AsyncFx the asynchronous version of `fx` as a wrapper
---Wraps `fx` as a thread based asynchronous function, there is a 6 argument limit on functions.
---
---In order to call the function asynchronously, pass a callback function at the end of the arguments, note that this will create a new thread. Otherwise the function runs synchronously.
local thread_async = function(fx)
return function(a, b, c, d, e, f)
local callback = getCallback(a, b, c, d, e, f);
local ta, tb, tc, td, te, tf = type(a), type(b), type(c), type(d), type(e), type(f);
a, b, c, d, e, f =
ta ~= 'function' and a or nil,
tb ~= 'function' and b or nil,
tc ~= 'function' and c or nil,
td ~= 'function' and d or nil,
te ~= 'function' and e or nil,
tf ~= 'function' and f or nil;
if (callback) then
local async_handler; async_handler = uv.new_async(function(ca, cb, cc, cd, ce, cf)
callback(ca, cb, cc, cd, ce, cf);
async_handler:close();
end);
uv.new_thread(fx, async_handler, a, b, c, d, e, f);
else fx(a, b, c, d, e, f);
end;
end;
end;
---@param fx function the synchronous function to wrap around
---@return function AsyncFx the asynchronous version of `fx` as a wrapper
---Wraps `fx` as a thread-pool based asynchronous function, there is a 6 argument limit on functions.
---
---In order to call the function asynchronously, pass a callback function at the end of the arguments. Otherwise the function runs synchronously.
local async = function(fx)
return function(a, b, c, d, e, f)
local callback = getCallback(a, b, c, d, e, f);
local ta, tb, tc, td, te, tf = type(a), type(b), type(c), type(d), type(e), type(f);
a, b, c, d, e, f =
ta ~= 'function' and a or nil,
tb ~= 'function' and b or nil,
tc ~= 'function' and c or nil,
td ~= 'function' and d or nil,
te ~= 'function' and e or nil,
tf ~= 'function' and f or nil;
if (callback) then
local async_handler; async_handler = uv.new_async(function(ca, cb, cc, cd, ce, cf)
callback(ca, cb, cc, cd, ce, cf);
async_handler:close();
end);
uv.new_work(fx, function(...)
async_handler:send(...);
end):queue(a, b, c, d, e, f);
else fx(a, b, c, d, e, f);
end;
end;
end;
return {
thread_async = thread_async;
async = async;
};