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
|
#pragma once
#include "scripting/lua/LuaTypes.h"
namespace scripting {
namespace api {
/**
* @brief A context that manages an asynchronous operation
*
* This is the interface used by a LuaPromise for running pending promises. It gets passed a callback that should be
* called when a promise has resolved.
*/
class resolve_context {
public:
virtual ~resolve_context();
using Resolver = std::function<void(bool error, const luacpp::LuaValueList& resolveVals)>;
/**
* @brief Sets the function which is called when an asynchronous operation finishes
*
* @note For future compatibility, the operation represented by a subclass of this should not start executing until
* this function is called.
*
* @param resolver The function to call to resolve a coroutine
*/
virtual void setResolver(Resolver resolver) = 0;
};
class LuaPromise {
public:
using ContinuationFunction = std::function<luacpp::LuaValueList(const luacpp::LuaValueList& resolveVals)>;
/**
* @brief Initializes an invalid promise
*/
LuaPromise();
virtual ~LuaPromise();
/**
* @brief Creates a pending promise
* @param resolveContext The context to register on
*/
explicit LuaPromise(const std::shared_ptr<resolve_context>& resolveContext);
LuaPromise(const LuaPromise&);
LuaPromise& operator=(const LuaPromise&);
LuaPromise(LuaPromise&&) noexcept;
LuaPromise& operator=(LuaPromise&&) noexcept;
LuaPromise then(ContinuationFunction continuation);
LuaPromise catchError(ContinuationFunction continuation);
bool isValid() const;
bool isResolved() const;
bool isErrored() const;
const luacpp::LuaValueList& resolveValue() const;
const luacpp::LuaValueList& errorValue() const;
static LuaPromise resolved(luacpp::LuaValueList resolveValue);
static LuaPromise errored(luacpp::LuaValueList resolveValue);
private:
LuaPromise registerContinuation(bool catchErrors, ContinuationFunction continuation);
struct internal_state;
std::shared_ptr<internal_state> m_state;
};
} // namespace api
} // namespace scripting
|