-
-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert 'experimental mode' into a feature flag system
Signed-off-by: Allen Faure <[email protected]>
- Loading branch information
1 parent
6fac322
commit 3724038
Showing
6 changed files
with
129 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
---@type string, Private | ||
local addon, Private = ... | ||
|
||
---@alias BuildType "dev" | "pr" | "alpha" | "beta" | "release" | ||
|
||
---@class feature | ||
---@field id string | ||
---@field autoEnable? BuildType[] | ||
---@field requiredByAura? fun(self: self, aura: auraData): boolean | ||
---@field enabled? boolean | ||
---@field persist? true | ||
---@field sub? SubscribableObject | ||
|
||
---@class Features | ||
local Features = { | ||
---@type table<string, feature> | ||
__feats = {} | ||
} | ||
Private.Features = Features | ||
|
||
---@param id string | ||
function Features:Exists(id) | ||
return self.__feats[id] ~= nil | ||
end | ||
|
||
---@param id string | ||
function Features:Enabled(id) | ||
return self:Exists(id) and self.__feats[id].enabled | ||
end | ||
|
||
---@param id string | ||
function Features:Enable(id) | ||
if self.__feats[id] then | ||
self.__feats[id].enabled = true | ||
if self.__feats[id].persist then | ||
Private.db.features[id] = true | ||
end | ||
self.__feats[id].sub:Notify("ENABLED") | ||
end | ||
end | ||
|
||
---@param id string | ||
function Features:Disable(id) | ||
if self.__feats[id] then | ||
self.__feats[id].enabled = false | ||
if self.__feats[id].persist then | ||
Private.db.features[id] = false | ||
end | ||
self.__feats[id].sub:Notify("DISABLED") | ||
end | ||
end | ||
|
||
---@param feature feature | ||
function Features:Register(feature) | ||
if not self.__feats[feature.id] then | ||
self.__feats[feature.id] = feature | ||
feature.sub = Private.CreateSubscribableObject() | ||
for _, buildType in ipairs(feature.autoEnable or {}) do | ||
if Private.buildType == buildType then | ||
self:Enable(feature.id) | ||
end | ||
end | ||
end | ||
end | ||
|
||
---@param id string | ||
---@param func function | ||
-- hides a function behind a feature flag | ||
function Features:Wrap(id, func) | ||
return function(...) | ||
if self:IsFeatureEnabled(id) then | ||
return func(...) | ||
end | ||
end | ||
end | ||
|
||
---@param data auraData | ||
---@return boolean, table<string, boolean> | ||
function Features:AuraCanFunction(data) | ||
local enabled = true | ||
local reasons = {} | ||
|
||
for _, feature in pairs(self.__feats) do | ||
if feature.requiredByAura and not feature:requiredByAura(data) then | ||
enabled = false | ||
reasons[feature.id] = false | ||
end | ||
end | ||
|
||
return enabled, reasons | ||
end | ||
|
||
-- sample debug feature | ||
Features:Register({ | ||
id = "debug", | ||
autoEnable = {"dev"} | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters