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:


Messages In This Thread
Multi-callback table as class - by NiLSPACE - 07-18-2015, 06:34 AM
RE: Multi-callback table as class - by xoft - 08-23-2016, 06:58 AM
RE: Multi-callback table as class - by NiLSPACE - 08-23-2016, 03:15 PM
RE: Multi-callback table as class - by xoft - 08-23-2016, 04:44 PM
RE: Multi-callback table as class - by NiLSPACE - 08-23-2016, 08:52 PM
RE: Multi-callback table as class - by xoft - 08-23-2016, 10:58 PM
RE: Multi-callback table as class - by NiLSPACE - 08-23-2016, 11:20 PM
RE: Multi-callback table as class - by xoft - 08-24-2016, 01:13 AM



Users browsing this thread: 2 Guest(s)