Skip to content

Commit

Permalink
Merge pull request #15 from ambers-careware/type-utilities
Browse files Browse the repository at this point in the history
Type Utilities and Critical classComponent fixes
  • Loading branch information
AmberGraceRblx authored Nov 18, 2021
2 parents 1e1a54e + fe81498 commit 7ef8d6d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
6 changes: 4 additions & 2 deletions Pract/Types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type Element = {
[any]: any,
}
export type Component = (props: any) -> Element
export type ComponentTyped<PropsType> = (props: PropsType) -> Element
--export type ComponentTyped<P> = (props: P) -> any
export type ClassState = {[string]: any}
export type ClassStateUpdateThunk = (state: ClassState, props: PropsArgument) -> ClassState
Expand All @@ -28,7 +29,8 @@ export type ClassComponentSelf = {
self: ClassComponentSelf,
partialStateUpdate: ClassStateUpdate
) -> (),
subscribeState: (self: ClassComponentSelf, listener: () -> ()) -> (() -> ())
subscribeState: (self: ClassComponentSelf, listener: () -> ()) -> (() -> ()),
forceUpdate: (self: ClassComponentSelf) -> (),
}
export type ClassComponentMethods = {
[any]: any,
Expand Down Expand Up @@ -97,4 +99,4 @@ export type Reconciler = {
) -> HostContext
}

return nil
return nil
15 changes: 13 additions & 2 deletions Pract/classComponent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
local Types = require(script.Parent.Types)
local withDeferredState = require(script.Parent.withDeferredState)
local withLifecycle = require(script.Parent.withLifecycle)
local Symbols = require(script.Parent.Symbols)

local Symbol_None = Symbols.None

local INIT_EMPTY_STATE = {}
table.freeze(INIT_EMPTY_STATE)
Expand All @@ -28,6 +31,9 @@ local function classComponent(componentMethods: Types.ClassComponentMethods)
if typeof(partialStateUpdate) == 'table' then
local stateChanged = false
for key, newValue in pairs(partialStateUpdate) do
if newValue == Symbol_None then
newValue = nil
end
if saveState[key] ~= newValue then
stateChanged = true
break
Expand All @@ -40,6 +46,9 @@ local function classComponent(componentMethods: Types.ClassComponentMethods)
newState[key] = value
end
for key, value in pairs(partialStateUpdate) do
if value == Symbol_None then
value = nil
end
newState[key] = value
end
table.freeze(newState)
Expand Down Expand Up @@ -91,7 +100,9 @@ local function classComponent(componentMethods: Types.ClassComponentMethods)
end,
init = function(props: Types.PropsArgument)
self.props = props
_init(self)
if _init then
_init(self)
end
self.state = getState()
end,
didMount = wrapOptionalLifecycleMethod 'didMount',
Expand All @@ -100,7 +111,7 @@ local function classComponent(componentMethods: Types.ClassComponentMethods)
shouldUpdate = function(newProps: Types.PropsArgument)
local newState = getState()
if _shouldUpdate then
if _shouldUpdate(newProps, newState) == false then
if _shouldUpdate(self, newProps, newState) == false then
self.state = getState()
self.props = newProps
return false
Expand Down
13 changes: 2 additions & 11 deletions Pract/createReconciler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,6 @@ local function createReconciler(): Types.Reconciler

mountByElementKind[ElementKinds.StateComponent] = function(virtualNode)
local currentState = nil :: any
local deferredNextState = nil :: any
local stateListenerSet = {} :: {[() -> ()]: boolean}
local lastDeferredChangeHeartbeatCount = -1
local function getState()
Expand All @@ -1208,11 +1207,10 @@ local function createReconciler(): Types.Reconciler
return
end

local saveState = currentState
currentState = nextState

local element = virtualNode._currentElement
if element.deferred then
deferredNextState = nextState

if not virtualNode._collateDeferredState then
virtualNode._collateDeferredState = true
task.defer(function()
Expand All @@ -1227,11 +1225,6 @@ local function createReconciler(): Types.Reconciler

-- Resume
virtualNode._collateDeferredState = nil
if virtualNode._wasUnmounted then
currentState = deferredNextState
return
end
currentState = deferredNextState

local listenersToCall = {}
for cb in pairs(stateListenerSet) do
Expand All @@ -1257,8 +1250,6 @@ local function createReconciler(): Types.Reconciler
end)
end
else
currentState = nextState

local listenersToCall = {}
for cb in pairs(stateListenerSet) do
table.insert(listenersToCall, cb)
Expand Down
5 changes: 5 additions & 0 deletions Pract/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ PractGlobalSystems.Run()
-- Public types
export type Tree = Types.PractTree
export type Component = Types.Component
export type ComponentTyped<PropsType> = Types.ComponentTyped<PropsType>
export type Element = Types.Element
export type PropsArgument = Types.PropsArgument
export type ChildrenArgument = Types.ChildrenArgument
Expand All @@ -25,6 +26,10 @@ export type Lifecycle = Types.Lifecycle

-- Base element functions
Pract.create = require(script.create)
Pract.createTyped = (Pract.create :: any) :: <PropsType>(
component: ComponentTyped<PropsType>,
props: PropsType
) -> (Types.Element)
Pract.index = require(script.index)
Pract.stamp = require(script.stamp)
Pract.decorate = require(script.decorate)
Expand Down

0 comments on commit 7ef8d6d

Please sign in to comment.