Task lib
I can't seem to find any task lib.
For example you can't use the function Wait() or task.Wait()
Managed to implement promises
Adding await to the call function is a better solution
const call = async (f, ...args) => {
if (f instanceof Function)
return ensureArray(await f(...args));
const mm = f instanceof Table && f.getMetaMethod('__call');
if (mm)
return ensureArray(await mm(f, ...args));
throw new LuaError(`attempt to call an uncallable type`);
};
Aswell as doing this
const exec = new Function('__lua', 'return (async () => {' + chunk + '})();');
And making all lua functions async
return `async (${argsStr}) => {\n${body}${returnStr}\n}`;
Implemented a task lib
const taskLib = new Table({
wait: async (time: LuaType) => {
const TIME = utils.coerceArgToNumber(time, "wait", 1);
return new Promise((resolve) => setTimeout(resolve, TIME * 1000));
},
spawn: async (func: LuaType) => {
const FUNC = utils.coerceArgToFunction(func, "spawn", 1);
return setImmediate(FUNC as any);
},
delay: async (time: LuaType, func: LuaType) => {
const TIME = utils.coerceArgToNumber(time, "delay", 1);
const FUNC = utils.coerceArgToFunction(func, "delayFunc", 2);
const timeout = setTimeout(FUNC as any, TIME * 1000);
return new Table({
timeout: () => timeout,
type: "timeout",
});
},
cancel: async (id: LuaType) => {
const ID = utils.coerceArgToTable(id, "cancelTimeout", 1);
if (ID.get("type") === "timeout") {
((ID.get("timeout") as Function)() as NodeJS.Timeout).unref();
}
},
});
Hi, I can't seem to find this functionality in the standard library that Lua provides. I don't think it's a good idea to convert all functions to async functions as that can have unintended performance consequences.
The closest thing Lua has to this is the coroutine library which lua-in-js doesn't currently support.
I'd be open to adding support for the coroutine library rather than a non-standard task library.
task and task.wait is from roblox