Skip to content

Instantly share code, notes, and snippets.

@samuelmaddock
Created April 22, 2014 16:25
Show Gist options
  • Save samuelmaddock/11185488 to your computer and use it in GitHub Desktop.
Save samuelmaddock/11185488 to your computer and use it in GitHub Desktop.
Awesomium Pool Interface
if browserpool then return end -- ignore Lua refresh
local table = table
local vgui = vgui
_G.browserpool = {}
--
-- Debug variable which will allow outputting messages if enabled.
-- @type boolean
--
local DEBUG = true
--
-- Array of available, pooled browsers
-- @type table
--
local available = {}
--
-- Array of active, pooled browsers.
-- @type table
--
local active = {}
--
-- Minimum number of active browsers to be pooled.
-- @type Number
--
local numMin = 2
--
-- Number of currently active browsers.
-- @type Number
--
local numActive = 0
--
-- Default URL to set browsers on setup/teardown.
-- @type String
--
local defaultUrl = "about:blank"
--
-- JavaScript code to remove an object's property.
-- @type String
--
local JS_RemoveProp = "delete %s.%s;"
--
-- Helper function to setup/teardown a browser panel.
--
-- @param panel? Browser panel to be cleaned up.
-- @return Panel DMediaPlayerHTML panel instance.
--
local function setupPanel( panel )
-- Create a new panel if it wasn't passed in
if panel then
panel:Stop()
else
panel = vgui.Create("DMediaPlayerHTML")
end
-- Hide panel
-- panel:SetSize(0, 0)
panel:SetPos(0, 0)
-- Disable input
panel:SetKeyBoardInputEnabled(false)
panel:SetMouseInputEnabled(false)
-- Browser panels are usually manually drawn, use a regular panel if not
panel:SetPaintedManually(true)
-- Set default URL
panel:OpenURL( defaultUrl )
-- Remove any added function callbacks
for obj, tbl in pairs(panel.Callbacks) do
if obj ~= "console" then
for funcname, _ in pairs(tbl) do
panel:QueueJavascript(JS_RemoveProp:format(obj, funcname))
end
end
end
return panel
end
--
-- Retrieves an available browser panel from the pool. Otherwise, a new panel
-- will be created.
--
-- @return Panel DMediaPlayerHTML panel instance.
--
function browserpool.get()
local panel
-- Check if there's an available panel
if #available > 0 then
panel = table.remove( available )
else
-- TODO: impose limit on number of active browsers
-- Otherwise, create a new panel
panel = setupPanel()
numActive = numActive + 1
if DEBUG then
print( "browserpool: Spawned new browser [Active: "..numActive.."]" )
end
end
table.insert( active, panel )
return panel
end
--
-- Releases the given browser panel from the active pool.
--
-- @param panel Browser panel to be released.
-- @return boolean Whether the panel was successfully removed.
--
function browserpool.release( panel )
if not panel then return end
local key = table.RemoveByValue( active, panel )
-- Unable to find active browser panel
if not key then
ErrorNoHalt( "browserpool: Attempted to release unactive browser.\n" )
debug.Trace()
return false
end
-- Remove panel if there are more active than the minimum pool size
if numActive > numMin then
panel:Remove()
numActive = numActive - 1
if DEBUG then
print( "browserpool: Destroyed browser [Active: "..numActive.."]" )
end
else
-- Cleanup panel
setupPanel( panel )
-- Add to the pool
table.insert( available, panel )
end
return true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment