Skip to content

Commit

Permalink
hopefully fix customizable
Browse files Browse the repository at this point in the history
  • Loading branch information
Rathoz committed Oct 14, 2024
1 parent 3216bee commit 43c87cb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 66 deletions.
76 changes: 20 additions & 56 deletions components/widget/widget.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ local Table = require('Module:Table')
---@field children (Widget|Html|string|number)[] @deprecated
---@field context Widget[]
---@field props table<string, any>
---@field makeChildren? fun(self:Widget, injector: WidgetInjector?): Widget[]?
---@field injector WidgetInjector?
local Widget = Class.new(function(self, props)
self.props = Table.copy(props) or {}
self.props.children = self.props.children or {}
self.context = {} -- Populated by the parent
---@deprecated To be removed when CustomizableWidget is removed
self.injector = nil -- Populated by the parent
end)

---Asserts the existence of a value and copies it
Expand All @@ -37,44 +39,28 @@ function Widget:render()
error('A Widget must override the render() function!')
end

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

---@param injector WidgetInjector?
---@return string
function Widget:tryMake(injector)
local renderComponent
if self.render == Widget.render then
-- Widget v1 backwards compability
renderComponent = function()
local processedChildren = self:tryChildren(injector)
local ret = self:make(processedChildren)
return ret ~= nil and ret or ''
local renderComponent = function()
self.injector = injector
local ret = self:render()
if not Array.isArray(ret) then
ret = {ret}
end
else
renderComponent = function()
local ret = self:render()
if not Array.isArray(ret) then
ret = {ret}
end

---@cast ret (string|Widget|Html|nil)[]
return Array.reduce(ret, function(acc, val)
if Class.instanceOf(val, Widget) then
---@cast val Widget
val.context = self:_nextContext()
return acc .. val:tryMake(injector)
end
if val ~= nil then
return acc .. tostring(val)
end
return acc
end, '')
end
---@cast ret (string|Widget|Html|nil)[]
return Array.reduce(ret, function(acc, val)
if Class.instanceOf(val, Widget) then
---@cast val Widget
val.context = self:_nextContext()
return acc .. val:tryMake(injector)
end
if val ~= nil then
return acc .. tostring(val)
end
return acc
end, '')
end

return Logic.tryOrElseLog(
Expand All @@ -84,28 +70,6 @@ function Widget:tryMake(injector)
)
end

---@deprecated
---@param injector WidgetInjector?
---@return string[]
function Widget:tryChildren(injector)
local children = self.props.children
if self.makeChildren then
children = self:makeChildren(injector) or {}
end
return Array.map(children, function(child)
if Class.instanceOf(child, Widget) then
---@cast child Widget
return Logic.tryOrElseLog(
function() return child:tryMake(injector) end,
FnUtil.curry(self.getDerivedStateFromError, self),
Widget._updateErrorHeader
)
end
---@cast child -Widget
return tostring(child)
end)
end

---@param widget WidgetContext
---@param default any
---@return any
Expand Down
13 changes: 3 additions & 10 deletions components/widget/widget_customizable.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,12 @@ local Customizable = Class.new(
end
)

---@param children string[]
---@return string
function Customizable:make(children)
return table.concat(children)
end

---@param injector WidgetInjector?
---@return Widget[]?
function Customizable:makeChildren(injector)
if injector == nil then
function Customizable:render()
if self.injector == nil then
return self.props.children
end
return injector:parse(self.id, self.props.children)
return self.injector:parse(self.id, self.props.children)
end

return Customizable

0 comments on commit 43c87cb

Please sign in to comment.