The problem
- One data source generating data in format
{key, value} - Multiple receivers each waiting for different key
Example
Getting data is run in loop. Sometimes I will want to get next value labelled with key by using
Value = MyClass:GetNextValue(Key)
I want my code to stop there until the value is ready (making some sort of future(?) value). I’ve tried using simple coroutines, but they work only when waiting for any data.
So the question I want to ask is something like How to implement async values in lua using coroutines or similar concept (without threads)?
Side notes
The main processing function will, apart from returning values to waiting consumers, process some of incoming data (say, labeled with special key) itself.
The full usage context should look something like:
-- in loop
ReceiveData()
ProcessSpecialData()
--
-- Called outside the loop:
V = RequestDataWithGivenKey(Key)
You start by not implementing async values. You implement async functions: you don’t get the value back until has been retrieved.
First, your code must be in a Lua coroutine. I’ll assume you understand the care and feeding of coroutines. I’ll focus on how to implement
RequestDataWithGivenKey:FunctionThatStartsAsyncGettingreturns arequestback to the function. Therequestis an object that stores all of the data needs to process the specific request. It represents asking for the value. This should be a C-function that starts the actual async getting.The
requestwill be either a userdata or an encapsulated Lua table that stores enough information to communicate with the C-code that’s doing the async fetching.IsCompleteuses the internalrequestdata to see if that request has completed.GetReturnedValuecan only be called whenIsCompletereturns true; it puts the value on the Lua stack, so that this function can return it.Your external code simply needs to handle the async stuff internally. Between resumes of these Lua coroutines, you’ll need to pump whatever async stuff is doing the fetching, if there are outstanding requests.