From 6bc27972b0724c236dc7bd8425570767dfde67e0 Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Wed, 5 Jun 2024 21:05:04 -0700 Subject: [PATCH] Create promise_tech.lua My own version of a promise system... WIP and untested :P --- promise_tech.lua | 115 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 promise_tech.lua diff --git a/promise_tech.lua b/promise_tech.lua new file mode 100644 index 0000000..5ee27c7 --- /dev/null +++ b/promise_tech.lua @@ -0,0 +1,115 @@ +--- Javascript Promises, implemented in Lua +-- In other words, a wrapper to manage asynchronous operations. +-- Due to limitations of Lua, while this Promise API is similar it isn't exactly the same as in JS. +-- +-- Also, .then_() does not return a thenable value but the SAME ORIGINAL promise, as we stack up all functions and then execute them in order once you call .run(). This has the subtle implication that Promise.state is not set to "fulfilled" until ALL functions in the chain have been called. +-- +-- Additionally, every .then_(function(...) end) MAY return a promise themselves if they wish. These promises WILL be automatically executed when you call .run() on the PARENT promise, as they are considered required for the parent Promise function chain to run to completion. +-- @class worldeditadditions_core.Promise +local Promise = {} +Promise.__index = Promise +Promise.__name = "Promise" -- A hack to allow identification in wea.inspect + +--- Creates a new Promise instance. +-- @param fn : The function to wrap into a promise. +Promise.new = function(fn) + -- resolve must be a function + if type(fn) ~= "function" then + error("Error (Promise.new): First argument (fn) must be a function") + end + + local result = { + state = "pending", + fn = fn + } + setmetatable(result, Promise) + + return result +end + + +--[[ + ************************* + Instance methods + ************************* +--]] + +--- Then function for promises +-- @param onFulfilled : The function to call if the promise is fulfilled +-- @param onRejected : The function to call if the promise is rejected +-- @return A promise object +Promise.then_ = function(self, onFulfilled, onRejected) + -- onFulfilled must be a function + if type(onFulfilled) ~= "function" and onRejected ~= nil then + error("Error (Promise.then_): First argument (onFulfilled) must be a function or nil") + end + -- onRejected must be a function or nil + if type(onRejected) ~= "function" and onRejected ~= nil then + error("Error (Promise.then_): Second argument (onRejected) must be a function or nil") + end + -- If self.state is not "pending" then error + if self.state ~= "pending" then + error("Error (Promise.then_): Promise is already " .. self.state) + end + + local result = function() + local success, value = pcall(self.fn, onFulfilled, onRejected) + return Promise.new(function(resolve, reject) + if success then + resolve(value) + self.state = "fulfilled" + else + reject(value) + self.state = "rejected" + end + end) + end + + return result() +end + +--- Catch function for promises +-- @param onRejected : The function to call if the promise is rejected +-- @return A promise object +Promise.catch = function(self, onRejected) + -- onRejected must be a function + if type(onRejected) ~= "function" then + error("Error (Promise.catch): First argument (onRejected) must be a function") + end + + return Promise.then_(self, nil, onRejected) +end + +--- Finally function for promises +-- @param onFinally : The function to call if the promise becomes settled +-- @return A promise object +Promise.finally = function(self, onFinally) + onFinally() + return Promise.new(self.fn) +end + +--[[ + ************************* + Static methods + ************************* +--]] + +--- Resolve function for promises +-- @param value : The value to resolve the promise with +-- @return A promise object +Promise.resolve = function(value) + return Promise.new(function(resolve, reject) + resolve(value) + end) +end + +--- Reject function for promises +-- @param value : The value to reject the promise with +-- @return A promise object +Promise.reject = function(value) + return Promise.new(function(resolve, reject) + reject(value) + end) +end + +-- TODO: Implement static methods (all, any, race etc.) \ No newline at end of file