Skip to content

Commit

Permalink
refactor(widget): improve building part 5 - remove widget factory (#4742
Browse files Browse the repository at this point in the history
)

* refactor(widget): improve building part 5 - remove widget factory

* remove imports

* these changes were for part 6
  • Loading branch information
Rathoz authored Sep 17, 2024
1 parent 45b692c commit cbebb22
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 52 deletions.
5 changes: 1 addition & 4 deletions components/infobox/commons/infobox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
local Array = require('Module:Array')
local Class = require('Module:Class')
local Logic = require('Module:Logic')
local Lua = require('Module:Lua')
local Variables = require('Module:Variables')
local WarningBox = require('Module:WarningBox')

local WidgetFactory = Lua.import('Module:Widget/Factory')

---@class Infobox
---@field frame Frame?
---@field root Html?
Expand Down Expand Up @@ -86,7 +83,7 @@ function Infobox:build(widgets)
error('Infobox:build can only accept Widgets')
end

self.content:node(WidgetFactory.work(widget, self.injector))
self.content:node(widget:tryMake(self.injector))
end

self.root:node(self.content)
Expand Down
3 changes: 1 addition & 2 deletions components/prize_pool/commons/prize_pool_base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ local Opponent = OpponentLibraries.Opponent
local OpponentDisplay = OpponentLibraries.OpponentDisplay

local Widgets = require('Module:Widget/All')
local WidgetFactory = Lua.import('Module:Widget/Factory')
local WidgetTable = Widgets.Table
local TableRow = Widgets.TableRow
local TableCell = Widgets.TableCell
Expand Down Expand Up @@ -609,7 +608,7 @@ function BasePrizePool:_buildTable(isAward)
end

local tableNode = mw.html.create('div'):css('overflow-x', 'auto')
tableNode:node(WidgetFactory.work(tbl, self._widgetInjector))
tableNode:node(tbl:tryMake(self._widgetInjector))

return tableNode
end
Expand Down
3 changes: 1 addition & 2 deletions components/squad/commons/squad.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ local String = require('Module:StringUtils')

local SquadUtils = Lua.import('Module:Squad/Utils')
local Widget = Lua.import('Module:Widget/All')
local WidgetFactory = Lua.import('Module:Widget/Factory')

---@class Squad
---@operator call:Squad
Expand Down Expand Up @@ -108,7 +107,7 @@ function Squad:create()
classes = {'wikitable-striped', 'roster-card'},
children = self.rows,
}
return WidgetFactory.work(dataTable, self.injector)
return dataTable:tryMake(self.injector) or ''
end

return Squad
17 changes: 7 additions & 10 deletions components/widget/widget.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ local Array = require('Module:Array')
local Class = require('Module:Class')
local ErrorDisplay = require('Module:Error/Display')
local Logic = require('Module:Logic')
local Lua = require('Module:Lua')
local String = require('Module:StringUtils')

local WidgetFactory = Lua.import('Module:Widget/Factory')

---@class Widget: BaseClass
---@operator call({children: Widget[]?}?): Widget
---@field children (Widget|Html|string|number)[]
Expand All @@ -31,18 +28,18 @@ end

---@param injector WidgetInjector?
---@param children string[]
---@return Widget[]|string|nil
---@return string|nil
function Widget:make(injector, children)
error('A Widget must override the make() function!')
end

---@param injector WidgetInjector?
---@return Widget[]|string|nil
---@return string|nil
function Widget:tryMake(injector)
local processedChildren = self:tryChildren(injector)
return Logic.tryOrElseLog(
function() return self:make(injector, processedChildren) end,
function(error) return {ErrorDisplay.InlineError(error)} end,
function(error) return tostring(ErrorDisplay.InlineError(error)) end,
function(error)
error.header = 'Error occured in widget: (caught by Widget:tryMake)'
return error
Expand All @@ -57,20 +54,20 @@ function Widget:tryChildren(injector)
if self.makeChildren then
children = self:makeChildren(injector) or {}
end
return Array.flatMap(children, function(child)
return Array.map(children, function(child)
if type(child) == 'table' and type(child['is_a']) == 'function' and child:is_a(Widget) then
---@cast child Widget
return Logic.tryOrElseLog(
function() return WidgetFactory.work(child, injector) end,
function(error) return {ErrorDisplay.InlineError(error)} end,
function() return child:tryMake(injector) end,
function(error) return tostring(ErrorDisplay.InlineError(error)) end,
function(error)
error.header = 'Error occured in widget: (caught by Widget:tryChildren)'
return error
end
)
end
---@cast child -Widget
return {tostring(child)}
return tostring(child)
end)
end

Expand Down
34 changes: 0 additions & 34 deletions components/widget/widget_factory.lua

This file was deleted.

0 comments on commit cbebb22

Please sign in to comment.