mirror of
https://github.com/centau/vide.git
synced 2026-08-20 23:01:37 +00:00
Merge branch 'centau:main' into main
This commit is contained in:
commit
92cf612cef
7 changed files with 221 additions and 45 deletions
|
|
@ -23,6 +23,9 @@ type Cache = {
|
|||
Array<(Instance) -> ()> -- action callbacks
|
||||
>,
|
||||
|
||||
-- what to parent the instance to after running actions
|
||||
parent: unknown,
|
||||
|
||||
-- cache to detect duplicate property setting at same nesting depth
|
||||
nested_debug: Map<
|
||||
number, -- depth
|
||||
|
|
@ -47,6 +50,7 @@ local function borrow_cache(): Cache
|
|||
actions = setmetatable({} :: any, { -- lazy init
|
||||
__index = function(self, i) self[i] = {}; return self[i] end
|
||||
}),
|
||||
parent = nil,
|
||||
nested_debug = setmetatable({} :: any, {
|
||||
__index = function(self, i: number) self[i] = {}; return self[i] end
|
||||
}),
|
||||
|
|
@ -61,8 +65,6 @@ end
|
|||
|
||||
local function process_properties(properties: Map<unknown, unknown>, instance: Instance, cache: Cache, depth: number)
|
||||
for property, value in properties do
|
||||
if property == "Parent" then continue end
|
||||
|
||||
if type(property) == "string" then
|
||||
if flags.strict then -- check for duplicate property assignment at nesting depth
|
||||
if cache.nested_debug[depth][property] then
|
||||
|
|
@ -71,6 +73,11 @@ local function process_properties(properties: Map<unknown, unknown>, instance: I
|
|||
cache.nested_debug[depth][property] = true
|
||||
end
|
||||
|
||||
if property == "Parent" then
|
||||
cache.parent = value
|
||||
continue
|
||||
end
|
||||
|
||||
if type(value) == "function" then
|
||||
if typeof((instance :: any)[property]) == "RBXScriptSignal" then
|
||||
table.insert(cache.events, property) -- add event name to buffer
|
||||
|
|
@ -93,7 +100,7 @@ local function process_properties(properties: Map<unknown, unknown>, instance: I
|
|||
else
|
||||
process_properties(value :: Map<unknown, unknown>, instance, cache, depth + 1)
|
||||
end
|
||||
else
|
||||
elseif type(value) == "userdata" then
|
||||
(value :: Instance).Parent = instance -- parent child
|
||||
end
|
||||
end
|
||||
|
|
@ -106,9 +113,6 @@ local function apply<T>(instance: T & Instance, properties: { [unknown]: unknown
|
|||
error "attempt to call a constructor returned by create() with no properties"
|
||||
end
|
||||
|
||||
-- queue parent assignment if any for last
|
||||
local parent: unknown = properties.Parent
|
||||
|
||||
local caches = borrow_cache()
|
||||
local events = caches.events
|
||||
local actions = caches.actions
|
||||
|
|
@ -135,6 +139,7 @@ local function apply<T>(instance: T & Instance, properties: { [unknown]: unknown
|
|||
end
|
||||
end
|
||||
|
||||
local parent = caches.parent
|
||||
if parent then
|
||||
if type(parent) == "function" then
|
||||
implicit_effect.parent(instance, parent :: () -> Instance)
|
||||
|
|
@ -145,6 +150,7 @@ local function apply<T>(instance: T & Instance, properties: { [unknown]: unknown
|
|||
|
||||
table.clear(events)
|
||||
for _, queued in next, actions do table.clear(queued) end
|
||||
caches.parent = nil
|
||||
if flags.strict then table.clear(nested_debug) end
|
||||
table.clear(nested_stack)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,16 +1,33 @@
|
|||
local switch = require "./switch"
|
||||
local source = require "./source"
|
||||
local derive = require "./derive"
|
||||
local effect = require "./effect"
|
||||
local untrack = require "./untrack"
|
||||
|
||||
local function show<T>(source: () -> any, component: () -> T, fallback: (() -> T)?): () -> T?
|
||||
local function truthy()
|
||||
return not not source()
|
||||
end
|
||||
local function show<T, U>(input: () -> T?, component: (() -> T) -> U, fallback: (() -> U)?): () -> U?
|
||||
local filtered_input = source()
|
||||
|
||||
return switch(truthy) {
|
||||
[true] = component,
|
||||
[false] = fallback,
|
||||
}
|
||||
effect(function()
|
||||
local v = input()
|
||||
if v then
|
||||
filtered_input(v)
|
||||
end
|
||||
end)
|
||||
|
||||
local input_is_truthy = derive(function()
|
||||
return not not input()
|
||||
end)
|
||||
|
||||
-- todo: is this needed?
|
||||
-- local filtered_input_is_truthy = derive(function()
|
||||
-- return not not filtered_input()
|
||||
-- end)
|
||||
|
||||
return derive(function()
|
||||
return
|
||||
if input_is_truthy() then untrack(function() return component(filtered_input :: () -> T) end)
|
||||
elseif fallback then untrack(fallback)
|
||||
else nil
|
||||
end)
|
||||
end
|
||||
|
||||
return show ::
|
||||
(<T>(source: () -> any, component: () -> T) -> () -> T?) &
|
||||
(<T, U>(source: () -> any, component: () -> T, fallback: () -> U) -> () -> (T | U)?)
|
||||
return show
|
||||
|
|
|
|||
|
|
@ -22,4 +22,4 @@ local function untrack<T>(source: () -> T): T
|
|||
end
|
||||
end
|
||||
|
||||
return untrack
|
||||
return untrack :: ( <T>(fn: () -> T) -> T ) & ( (fn: () -> ()) -> () )
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue