Skip to content

Commit

Permalink
0.3 untested
Browse files Browse the repository at this point in the history
  • Loading branch information
jmesrje committed Dec 14, 2024
1 parent 3857577 commit d10ef56
Show file tree
Hide file tree
Showing 14 changed files with 202 additions and 230 deletions.
2 changes: 1 addition & 1 deletion src/Apply.luau
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ return function(instance: Instance, property: any, value: any)
end
elseif type(property) == "string" then
if type(value) == "table" then
(value :: Types.Constructor<any>):_Bind(property, instance)
(value :: Types.Constructor<any, any>):_Bind(property, instance)
else
(instance :: any)[property] = value
end
Expand Down
File renamed without changes.
3 changes: 1 addition & 2 deletions src/Clean.luau
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
local Root = script.Parent
local Action = require(Root.Action)
local Types = require(Root.Types)
local Is = require(Root.Is)
local Debugger = require(Root.Parent.roblox_packages.debugger)

-- Module
Expand All @@ -17,7 +16,7 @@ return function(values: { any }): Types.Action
Debugger.Assert(type(values) == "table", "InvalidType", "table", type(values))
instance.Destroying:Once(function()
for _, value in values do
if Is.Constructor(value) or typeof(value) == "Instance" then
if (type(value) == "table" and value._Bind) or typeof(value) == "Instance" then
value:Destroy()
elseif typeof(value) == "RBXScriptConnection" then
value:Disconnect()
Expand Down
107 changes: 107 additions & 0 deletions src/Computed.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
-- Variables
local Root = script.Parent
local Types = require(Root.Types)
local Debugger = require(Root.Parent.roblox_packages.debugger)

local Class = {}

-- Functions

--[=[
Gets the current value of a value object for use within the compute.
[Learn More](https://luminlabsdev.github.io/ui-framework/api/computed/#use)
]=]
local function Use(value: Types.State | any)
if type(value) == "table" then
return value:Get()
else
return value
end
end

local function Listener(self: Types.Computed)
return function()
self._Value = self._Processor(Use)

if self._Instances ~= nil then
for prop, instance in self._Instances do
(instance :: any)[prop] = self._Value
end
end
end
end

--[=[
Gets the current value of the compute object.
[Learn More](https://luminlabsdev.github.io/ui-framework/api/computed/#get)
]=]
function Class.Get(self: Types.Computed): any
return self._Value
end

function Class._Bind(self: Types.Computed, prop: string, instance: Instance)
if self._Instances == nil then
self._Instances = {}
end

(self._Instances :: {})[prop] = instance;
(instance :: any)[prop] = self._Value
end

--[=[
Destroys the compute object.
[Learn More](https://luminlabsdev.github.io/ui-framework/api/computed/#destroy)
]=]
function Class.Destroy(self: Types.Computed)
if self._Dependencies then
for dependency: any, disconnect: any in self._Dependencies do
disconnect()
self._Dependencies[dependency] = nil
end
end

table.clear(self :: any)
setmetatable(self :: any, nil)
end

-- Module

--[=[
Creates a new computed value, which changes the final value when a dependency is changed.
[Learn More](https://luminlabsdev.github.io/ui-framework/api/#computed)
]=]
return function(
processor: (use: (value: Types.State | any) -> ()) -> (),
dependencies: { Types.State | Types.Spring }?
): Types.ComputedExport
local self = setmetatable({}, { __index = Class })

self._Type = "Computed"
self._Processor = processor
self._Value = processor(Use)
self._Instances = {}
self._Dependencies = nil

if dependencies then
local Type = type(dependencies)
Debugger.Assert(Type == "table", "InvalidType", "table", Type)
self._Dependencies = {} :: any
for _, dependency in dependencies do
if type(dependency) == "table" then
if dependency._Type == "Spring" then
(self._Dependencies :: any)[dependency] = (dependency :: any)._Goal:Listen(Listener(self :: any))
elseif dependency._Type == "State" then
(self._Dependencies :: any)[dependency] = (dependency :: any):Listen(Listener(self :: any))
else
Debugger.Fatal("InvalidType", "state or spring", type(dependency))
end
end
end
end

return self :: any
end
119 changes: 0 additions & 119 deletions src/Effect.luau

This file was deleted.

17 changes: 0 additions & 17 deletions src/Is.luau

This file was deleted.

19 changes: 14 additions & 5 deletions src/New.luau
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,24 @@ local Defaults = require(Root.Defaults)
-- Module

--[=[
Creates a new instance or component with the provided children and properties.
Clones or creates a new isntance with the provided properties. Default properties are not
set on clones.
[Learn More](https://luminlabsdev.github.io/ui-framework/api/#new)
]=]
return function(class: string, props: { any }?): Instance
local Success, New = pcall(Instance.new, class) -- Create the instance wrapped inside pcall
return function(class: string | Instance, props: { any }?): Instance
Debugger.Assert(type(class) == "string" or typeof(class) == "Instance", "FailedCreation", class, "")

local Success, New

if type(class) == "string" then
Success, New = pcall(Instance.new, class)
else
Success, New = true, class:Clone()
end

if Success then
if Defaults[class] then
if type(class) == "string" and Defaults[class] then
for prop, value in Defaults[class] do
(New :: any)[prop] = value
end
Expand All @@ -33,7 +42,7 @@ return function(class: string, props: { any }?): Instance
New.Parent = (props :: any).Parent
end
end
else -- If it wasn't successful, error it because instance creation has to be complete.
else
Debugger.Fatal("FailedCreation", class, New)
end
return New
Expand Down
33 changes: 21 additions & 12 deletions src/Spring.luau
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
-- Variables
local Root = script.Parent.Parent
local Packages = script.Parent.Parent.Parent.roblox_packages

local Debugger = require(Packages.debugger)
local Utils = require(Root.Utility)
local Is = require(Root.Is)
local Root = script.Parent
local Debugger = require(Root.Parent.roblox_packages.debugger)
local Types = require(Root.Types)

local Spr = require(script.Parent.Spr)
Expand All @@ -28,7 +24,7 @@ local Animatable = {

function Class._Bind(self: Types.Spring, prop: string, instance: Instance)
(instance :: any)[prop] = self._Goal:Get()
self._Animated = instance
table.insert(self._Instances, instance)
self._Goal:Listen(function(new)
Spr.target(instance, self._Damping, self._Frequency, {
[prop] = new,
Expand All @@ -37,12 +33,25 @@ function Class._Bind(self: Types.Spring, prop: string, instance: Instance)
end

--[[
Gets the current value that the spring is moving.
Stops the current spring from moving, leaving it at its current position.
[Learn More](https://luminlabsdev.github.io/ui-framework/api/spring/#stop)
]]
function Class.Stop(self: Types.Spring)
if self._Instances then
for _, instance in self._Instances do
Spr.stop(instance)
end
end
end

--[[
Gets the end goal of the spring.
[Learn More](https://luminlabsdev.github.io/ui-framework/api/spring/#get)
]]
function Class.Get(self: Types.Spring): Types.Animatable
return 1
return self._Goal:Get()
end

--[[
Expand All @@ -63,14 +72,14 @@ end
]=]
return function(goal: Types.State, damping: number?, frequency: number?): Types.SpringExport
Debugger.Assert(table.find(Animatable, typeof((goal :: any):Get())), "NotAnimatable", typeof((goal :: any):Get()))
Debugger.Assert(Is.Constructor(goal :: any), "InvalidType", "State", type(goal))
Debugger.Assert(type(goal) == "table" and goal._Type == "State", "InvalidType", "State", type(goal))

local self = setmetatable({}, { __index = Class })

self._Type = "spring"
self._Type = "Spring"
self._Damping = damping or 1
self._Frequency = frequency or 1
self._Animated = nil
self._Instances = {}
self._Goal = goal

return self :: any
Expand Down
Loading

0 comments on commit d10ef56

Please sign in to comment.