Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ local effect = require("@self/reactive/effect")
local for_keys = require("@self/instances/for_keys")
local for_values = require("@self/instances/for_values")
local graph = require("@self/reactive/graph")
local instance_types = require("@self/instances/types")
local interval = require("@self/utils/interval")
local lerp = require("@self/anim/lerp")
local mount = require("@self/instances/mount")
Expand All @@ -29,7 +30,8 @@ local untrack = require("@self/reactive/untrack")
export type UsedAs<T> = types.UsedAs<T>
export type Source<T> = types.Source<T>
export type Action = action.Identity
export type PropertiesFor<T> = create.PropertiesFor<T>
export type PropertiesFor<T> = instance_types.PropertiesFor<T>
export type GetInstance<T> = instance_types.GetInstance<T>

local scheduler_thread: thread? = nil

Expand Down
25 changes: 13 additions & 12 deletions src/instances/bind.luau
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local action = require("./action")
local graph = require("../reactive/graph")
local types = require("../reactive/types")
local instance_types = require("./types")
local reactive_types = require("../reactive/types")

local function set_property(inst: Instance, property: string, value: any)
(inst :: any)[property] = value
Expand All @@ -10,7 +11,7 @@ local function get_property(inst: Instance, property: string): any
return (inst :: any)[property]
end

local function bind_property<T>(inst: Instance, property: string, value: types.Readable<T>)
local function bind_property<T>(inst: Instance, property: string, value: reactive_types.Readable<T>)
local property_node = graph.create_reactive_node(graph.assert_stable_parent(), function()
set_property(inst, property, value())
return nil
Expand Down Expand Up @@ -101,17 +102,17 @@ local function bind_reactive_child(inst: Instance, func: () -> Result)
graph.evaluate_node(property_node)
end

local function bind(inst: Instance, props: { [unknown & number]: unknown })
for key: any, value: any in props do
local function bind<I>(instance: I & Instance, props: instance_types.PropertiesFor<I>): I
for key, value in props :: any do
local key_type, value_type = type(key), typeof(value)

if key_type == "string" then
local property_value = get_property(inst, key)
local property_value = get_property(instance, key)
if typeof(property_value) ~= "RBXScriptSignal" then
if value_type == "function" then
bind_property(inst, key, value)
bind_property(instance, key, value)
else
set_property(inst, key, value)
set_property(instance, key, value)
end
elseif value_type == "function" then
local connection = property_value:Connect(value)
Expand All @@ -122,19 +123,19 @@ local function bind(inst: Instance, props: { [unknown & number]: unknown })
end
else
if value_type == "Instance" then
value.Parent = inst
value.Parent = instance
elseif value_type == "table" then
if value.__ACTION_SYMBOL == action.symbol then
value.callback(inst)
value.callback(instance)
else
bind(inst, value)
bind(instance, value)
end
elseif value_type == "function" then
bind_reactive_child(inst, value)
bind_reactive_child(instance, value)
end
end
end
return inst
return instance
end

return bind
216 changes: 4 additions & 212 deletions src/instances/create.luau
Original file line number Diff line number Diff line change
@@ -1,214 +1,5 @@
local action = require("./action")
local bind = require("./bind")

type Instances = {
PyramidHandleAdornment: PyramidHandleAdornment,
AudioChannelSplitter: AudioChannelSplitter,
AudioPitchShifter: AudioPitchShifter,
AudioDeviceOutput: AudioDeviceOutput,
AudioChannelMixer: AudioChannelMixer,
AudioTextToSpeech: AudioTextToSpeech,
AudioDeviceInput: AudioDeviceInput,
AudioDistortion: AudioDistortion,
AudioCompressor: AudioCompressor,
AudioEqualizer: AudioEqualizer,
AudioAnalyzer: AudioAnalyzer,
AudioListener: AudioListener,
AudioRecorder: AudioRecorder,
AudioLimiter: AudioLimiter,
AudioFlanger: AudioFlanger,
AudioEmitter: AudioEmitter,
AudioPlayer: AudioPlayer,
AudioReverb: AudioReverb,
AudioFilter: AudioFilter,
ArcHandles: ArcHandles,
AudioFader: AudioFader,
AudioEcho: AudioEcho,
AdGui: AdGui,
BoxHandleAdornment: BoxHandleAdornment,
BillboardGui: BillboardGui,
BlurEffect: BlurEffect,
CylinderHandleAdornment: CylinderHandleAdornment,
ConeHandleAdornment: ConeHandleAdornment,
CanvasGroup: CanvasGroup,
Camera: Camera,
DialogChoice: DialogChoice,
DragDetector: DragDetector,
Dialog: Dialog,
Folder: Folder,
Frame: Frame,
Highlight: Highlight,
Handles: Handles,
ImageHandLeAdornment: ImageHandleAdornment,
InputActionLabel: InputActionLabel,
ImageButton: ImageButton,
ImageLabel: ImageLabel,
LineHandleAdornment: LineHandleAdornment,
ProximityPrompt: ProximityPrompt,
Path3D: Path3D,
Path2D: Path2D,
Part: Part,
SphereHandLeAdornment: SphereHandleAdornment,
SelectionSphere: SelectionSphere,
ScrollingFrame: ScrollingFrame,
SelectionBox: SelectionBox,
StyleDerive: StyleDerive,
StyleQuery: StyleQuery,
SurfaceGui: SurfaceGui,
StyleSheet: StyleSheet,
SoundGroup: SoundGroup,
StyleLink: StyleLink,
ScreenGui: ScreenGui,
StyleBase: StyleBase,
StyleRule: StyleRule,
Sound: Sound,
TextButton: TextButton,
TextLabel: TextLabel,
TextBox: TextBox,
UIAspectRatioConstraint: UIAspectRatioConstraint,
UITextSizeConstraint: UITextSizeConstraint,
UISizeConstraint: UISizeConstraint,
UIDragDetector: UIDragDetector,
UITableLayout: UITableLayout,
UIPageLayout: UIPageLayout,
UIGridLayout: UIGridLayout,
UIListLayout: UIListLayout,
UIGradient: UIGradient,
UIFlexItem: UIFlexItem,
UIPadding: UIPadding,
UIStroke: UIStroke,
UICorner: UICorner,
UIShadow: UIShadow,
UIScale: UIScale,
ViewportCamera: ViewportCamera,
ViewportFrame: ViewportFrame,
VideoDisplay: VideoDisplay,
VideoFrame: VideoFrame,
WireframeHandleAdornment: WireframeHandleAdornment,
WorldModel: WorldModel,
Wire: Wire,
}

type UnknownConnectCallback = (...any) -> ()

type Signal = RBXScriptSignal

type function get_connect(v: type): type?
local connect = v:readproperty(types.singleton("Connect"))

if connect and connect:is("function") then
local head = connect:parameters().head

if not head then
return nil
end
local callback = head[2]

if callback and callback:is("function") then
return callback
end
end

return nil
end

type function is_signal(v: type): type?
if v == Signal and v.tag == "extern" then
return UnknownConnectCallback
-- luau lsp types RBXScriptSignals as tables
elseif v.tag == "table" then
local connect = get_connect(v)

if connect then
return connect
end

-- i cba to test if this is needed or not
local metatable = v:metatable()

if metatable then
connect = get_connect(metatable)

if connect then
return connect
end
end
end

return nil
end

type instance = Instance

type Action = action.Identity

type function GetInstance(class_name: type, instances: type): type
if not class_name:is("singleton") then
return instance
end

return instances:readproperty(class_name) or instance
end

export type function PropertiesFor(class_name: type): type
local object = GetInstance(class_name, Instances)
local head_table = {}
local args_table = { head = head_table }
local properties = types.newtable()
local current = object
local empty_table = {}

local read_only_properties = table.freeze({
types.singleton("RobloxLocked"),
types.singleton("ClassName"),
types.singleton("UniqueId"),
})

while current do
for property, value in current:properties() do
if table.find(read_only_properties, property) then
continue
end

local value_read = value.read
local callback = is_signal(value_read)

if callback then
properties:setproperty(property, types.optional(callback))
elseif value_read.tag == "function" or value_read.tag == "intersection" then
continue
elseif value.write then
head_table[1] = value_read

properties:setproperty(
property,
types.optional(
types.unionof(types.newfunction(empty_table :: any, args_table :: any), value.write)
)
)
end
end

current = current:readparent()
end

-- Functions returning a child can return an array of children, or alike functions, nested to any level
local function_returns_child = types.newfunction()
local child_only_union = types.unionof(function_returns_child, instance)

-- TODO: Use a read only indexer when released to properly accept children which are subtypes of `Instance`
-- TODO: Nesting throws a type error, seemingly a solver bug caused whenever the keys are a union instead of direct type
-- Happens even with a union between `Instance` and `any`, but disappears when it's just `Instance`
local nested_table_or_function_or_child = types.newtable()
local nested_child_union = types.unionof(child_only_union, nested_table_or_function_or_child)
nested_table_or_function_or_child:setindexer(types.number, nested_child_union)
function_returns_child:setreturns({ nested_child_union })

local full_union = types.unionof(child_only_union, Action, properties)
properties:setindexer(types.number, types.optional(full_union))

return properties
end
local types = require("./types")

local function try_create_instance(class_name: string): Instance
local success, call_result = pcall(Instance.new, class_name)
Expand All @@ -217,9 +8,10 @@ local function try_create_instance(class_name: string): Instance
return call_result
end

--stylua: ignore
local function create<ClassName>(
class_name: ClassName | keyof<Instances> | ""
): (props: PropertiesFor<ClassName>) -> GetInstance<ClassName, Instances>
class_name: ClassName | keyof<types.Instances> | ""
): (props: types.PropertiesFor<ClassName>) -> types.GetInstance<ClassName, types.Instances>
return function(properties)
return bind(try_create_instance(class_name), properties) :: any
end
Expand Down
Loading
Loading