Skip to content

Commit

Permalink
Added caching for faster style construction
Browse files Browse the repository at this point in the history
  • Loading branch information
nightcycle committed Jan 22, 2024
1 parent 00eb9ab commit f800955
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 31 deletions.
2 changes: 1 addition & 1 deletion sourcemap.json

Large diffs are not rendered by default.

27 changes: 26 additions & 1 deletion src/Style/Theme/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ local _Package = script.Parent.Parent
local _Packages = _Package.Parent
-- Services
-- Packages
local HashUtil = require(_Packages:WaitForChild("HashUtil"))
local TableUtil = require(_Packages:WaitForChild("TableUtil"))

-- Modules
local ThemeUtil = require(script:WaitForChild("ThemeUtil"))
local HCT = require(script:WaitForChild("HCT"))
Expand Down Expand Up @@ -34,6 +37,7 @@ export type Theme = {
_CustomColors: {
[string]: CustomColorGroup,
},
_Hash: string,
Get: (self: Theme, role: Enums.ColorRoleType, schemeType: Enums.SchemeType, elevation: number?) -> Color3,
GetCustom: (
self: Theme,
Expand All @@ -48,14 +52,33 @@ export type Theme = {
-- Constants
local ELEVATION_TINTS = { 0, 0.05, 0.08, 0.11, 0.12, 0.14 }
-- Variables
local Cache: {[string]: Theme} = {}
-- References
-- Private Functions

-- Class
local Theme = {} :: Theme
Theme.__index = Theme

function Theme.new(source: Color3, custom: { [string]: Color3 }?): Theme
function Theme.new(source: Color3, custom: { [string]: Color3 }?): Theme

local hash = ""
do
local hashInput = `{source:ToHex()}+`.."{"
if custom then
local keys = TableUtil.keys(custom)
table.sort(keys)
for i, k in ipairs(keys) do
hashInput..= `{k}={custom[k]:ToHex()},`
end
end
hashInput ..= "}"
hash = HashUtil.md5(hashInput)
end
if Cache[hash] then
return Cache[hash]
end

local googleTheme: GoogleTheme
do
local customColors: { [number]: GoogleCustomColor } = {}
Expand Down Expand Up @@ -164,6 +187,7 @@ function Theme.new(source: Color3, custom: { [string]: Color3 }?): Theme
end
end


local theme: Theme = setmetatable({
Source = source,
_Schemes = table.freeze({
Expand All @@ -181,6 +205,7 @@ function Theme.new(source: Color3, custom: { [string]: Color3 }?): Theme
CustomColors = table.freeze(customColors),
}, Theme) :: any
table.freeze(theme)
Cache[hash] = theme
return theme
end

Expand Down
26 changes: 15 additions & 11 deletions src/Style/Typography.luau
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ local Enums = require(_Package:WaitForChild("Enums"))
local Types = require(_Package:WaitForChild("Types"))

-- Types
export type FontData = Types.FontData
export type Typography = {
__index: Typography,
_Styles: { [Enums.FontType]: FontData },
Get: (self: Typography, style: Enums.FontType) -> FontData,
new: (fontFamily: string?) -> Typography,
}
-- Constants
local DEFAULT_FONT_FAMILY = "Source Sans"
local WEIGHT = {
Expand All @@ -22,17 +29,9 @@ local WEIGHT = {
[900] = Enum.FontWeight.Heavy,
}
-- Variables
local Cache: {[string]: Typography} = {}
-- References
-- Private Functions
-- Class
-- Types
export type FontData = Types.FontData
export type Typography = {
__index: Typography,
_Styles: { [Enums.FontType]: FontData },
Get: (self: Typography, style: Enums.FontType) -> FontData,
new: (fontFamily: string?) -> Typography,
}

-- Class
local Typography = {} :: Typography
Expand All @@ -42,9 +41,14 @@ function Typography:Get(style: Enums.FontType): FontData
return self._Styles[style]
end

function Typography.new(fontFamily: string?)
function Typography.new(fontFamily: string?): Typography
fontFamily = fontFamily or DEFAULT_FONT_FAMILY
assert(fontFamily)

if Cache[fontFamily] then
return Cache[fontFamily]
end

local self: Typography = setmetatable({}, Typography) :: any
self._Styles = table.freeze({
[Enums.FontType.DisplayLarge] = table.freeze({
Expand Down Expand Up @@ -140,7 +144,7 @@ function Typography.new(fontFamily: string?)
})

table.freeze(self)

Cache[fontFamily] = self
return self
end

Expand Down
36 changes: 29 additions & 7 deletions src/Style/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ local _Package = script.Parent
local _Packages = _Package.Parent
-- Services
-- Packages
local HashUtil = require(_Packages:WaitForChild("HashUtil"))
local TableUtil = require(_Packages:WaitForChild("TableUtil"))

-- Modules
local Typography = require(script:WaitForChild("Typography"))
local Theme = require(script:WaitForChild("Theme"))
Expand All @@ -12,12 +15,6 @@ local Enums = require(_Package:WaitForChild("Enums"))
export type Theme = Theme.Theme
export type Typography = Typography.Typography
export type FontData = Typography.FontData
-- Constants
-- Variables
-- References
-- Private Functions
-- Class
-- Types
export type Style = {
__index: Style,
Scale: number,
Expand All @@ -29,7 +26,6 @@ export type Style = {
GetCustomColor: (self: Style, name: string, customType: Enums.CustomColorType, elevation: number?) -> Color3,
GetFont: (self: Style, fontType: Enums.FontType) -> Font,
GetFontData: (self: Style, fontType: Enums.FontType) -> FontData,

GetTextSize: (self: Style, fontType: Enums.FontType) -> number,
GetLineHeight: (self: Style, fontType: Enums.FontType) -> number,
new: (
Expand All @@ -41,6 +37,13 @@ export type Style = {
) -> Style,
}

-- Constants
-- Variables
local Cache: {[string]: Style} = {}

-- References
-- Private Functions

-- Class
local Style = {} :: Style
Style.__index = Style
Expand Down Expand Up @@ -80,13 +83,32 @@ function Style.new(
source: Color3,
additions: { [string]: Color3 }?
): Style
local hash = ""
do
local hashInput = `{scale}+{fontFamily}+{schemeType}+{source:ToHex()}+`.."{"
if additions then
local keys = TableUtil.keys(additions)
table.sort(keys)
for i, k in ipairs(keys) do
hashInput..= `{k}={additions[k]:ToHex()},`
end
end
hashInput ..= "}"
hash = HashUtil.md5(hashInput)
end
if Cache[hash] then
return Cache[hash]
end

local self: Style = setmetatable({}, Style) :: any
self.Scale = scale
self.SchemeType = schemeType
self._Theme = Theme.new(source, additions)
self._Typography = Typography.new(fontFamily)

table.freeze(self)
Cache[hash] = self

return self
end

Expand Down
17 changes: 6 additions & 11 deletions wally.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
# It is not intended for manual editing.
registry = "test"

[[package]]
name = "boatbomber/hashlib"
version = "1.0.0"
dependencies = []

[[package]]
name = "core-packages/boolean"
version = "0.0.1"
Expand Down Expand Up @@ -72,16 +77,6 @@ name = "elttob/fusion"
version = "0.2.0"
dependencies = []

[[package]]
name = "evaera/promise"
version = "4.0.0"
dependencies = []

[[package]]
name = "littensy/ripple"
version = "0.7.1"
dependencies = [["Promise", "evaera/[email protected]"]]

[[package]]
name = "nightcycle/cold-fusion"
version = "9.0.7"
Expand Down Expand Up @@ -135,7 +130,7 @@ dependencies = []
[[package]]
name = "nightcycle/synthetic"
version = "5.0.0"
dependencies = [["ColdFusion", "nightcycle/[email protected]"], ["CurveUtil", "nightcycle/[email protected]"], ["Format", "nightcycle/[email protected]"], ["Fusion", "elttob/[email protected]"], ["Maid", "nightcycle/[email protected]"], ["MaterialIcons", "nightcycle/[email protected]"], ["MeshUtil", "nightcycle/[email protected]"], ["React", "core-packages/[email protected].1"], ["Ripple", "littensy/[email protected].1"], ["Roact", "roblox/[email protected]"], ["Signal", "nightcycle/[email protected]"], ["TableUtil", "nightcycle/[email protected]"]]
dependencies = [["ColdFusion", "nightcycle/[email protected]"], ["CurveUtil", "nightcycle/[email protected]"], ["Format", "nightcycle/[email protected]"], ["Fusion", "elttob/[email protected]"], ["HashUtil", "boatbomber/[email protected]"], ["Maid", "nightcycle/[email protected]"], ["MaterialIcons", "nightcycle/[email protected]"], ["MeshUtil", "nightcycle/[email protected]"], ["React", "core-packages/[email protected]"], ["Roact", "roblox/[email protected]"], ["Signal", "nightcycle/[email protected]"], ["TableUtil", "nightcycle/[email protected]"]]

[[package]]
name = "nightcycle/table-util"
Expand Down
1 change: 1 addition & 0 deletions wally.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ React = "core-packages/[email protected]"
MaterialIcons = "nightcycle/[email protected]"
CurveUtil = "nightcycle/[email protected]"
TableUtil = "nightcycle/[email protected]"
HashUtil = "boatbomber/[email protected]"

0 comments on commit f800955

Please sign in to comment.