Multi-callback table as class
#1
Can plugins currently create classes instead of simple tables for multi callbacks like the network callbacks. An example is the nether portal scanner in the source code (though currently chunkstay doesn't use a table, but simply a parameter for each callback). If we'd want the same the callbacks would have to give their own table as a parameter I believe.

After creating the class you could simply give the object as a parameter for a function.

I'll describe a use-case. Currently when doing network request where you need to have the complete return body you append the data to a variable. Take for example the WorldEdit update class. (local variable, append data). This means you can't do multiple requests at once as the from multiple requests would be mixed together in some Frankensteins monster.

If instead you could have a class with a member variable that would make this allot easier. The updater callbacks from WorldEdit would look a little like this:

local CheckForNewerVersionCallbacks = {}

function CheckForNewerVersionCallbacks:new()
	local obj = {}
	setmetatable(obj, CheckForNewerVersionCallbacks)
	self.__index = self
	
	-- Member variable containing all the received data
	obj.m_ReceivedPluginInfo = ""
	
	return obj
end

function CheckForNewerVersionCallbacks:OnConnected(a_TCPLink)
	local res, msg = a_TCPLink:StartTLSClient()
	a_TCPLink:Send("GET /cuberite/WorldEdit/master/Info.lua HTTP/1.0\r\nHost: raw.githubusercontent.com\r\n\r\n")
end
	
function CheckForNewerVersionCallbacks:OnError(a_TCPLink, a_ErrorCode, a_ErrorMsg) 
	LOGWARNING("Error while checking for newer WorldEdit version: " .. a_ErrorMsg)
end
	
function CheckForNewerVersionCallbacks:OnReceivedData(a_TCPLink, a_Data)
	self.m_ReceivedPluginInfo = self.m_ReceivedPluginInfo .. a_Data
end
	
function CheckForNewerVersionCallbacks:OnRemoteClosed(a_TCPLink)
	-- Do something with the data now that we\'ve received everything.
	cUpdater:ParseResults(self.m_ReceivedPluginInfo)
end



-- Start a network request
cNetwork:Connect("raw.githubusercontent.com", 443, CheckForNewerVersionCallbacks:new())
Reply
Thanks given by:
#2
This will be implemented for the cUrlClient callbacks.

Not sure if I should change the cNetwork bindings to do that as well, because it would break existing code (the functions need an invisible extra parameter, "self").
Reply
Thanks given by:
#3
Perhaps it's possible to detect if a function was created with a colon?
Reply
Thanks given by:
#4
No, unfortunately there's no way to distinguish those cases.
Reply
Thanks given by:
#5
How many plugins currently use the network API? All I can think of from the top of my head is my WorldEdit plugin (which will use the cUrlClient instead), your GalExport plugin and the DockerCraft plugin (though I don't think they've continued working on their implementation)
Reply
Thanks given by:
#6
There's always a possibility of having multiple API function signatures and decide based upon those. Such as "If there's an extra bool param, use the object-oriented callbacks".
Reply
Thanks given by:
#7
I guess that could work. We could also log a warning to try to get people over to OO callbacks.
Reply
Thanks given by:
#8
Heh, even easier than that. We could just swap the current parameters - each of the cNetwork functions takes *something* and callbacks. So if it receives callbacks and *something* instead, it will assume the OO callbacks.
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)