Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Add Luau types #36

Merged
merged 10 commits into from
Aug 6, 2022
Merged
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: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Added Luau types to Hooks API ([#36](https://github.com/Kampfkarren/roact-hooks/pull/36))

## [0.4.1]
### Fixed
- Fixed `useMemo` not working correctly with nil dependencies ([#35](https://github.com/Kampfkarren/roact-hooks/issues/35))
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Defines default values for props to ensure props will have values even if they w
## Implemented Hooks

### useState
`useState<T>(defaultValue: T) -> (T, update: (newValue: T) -> void)`
`useState<T>(defaultValue: T | (() -> T)) -> (T, update: (value: T | ((prevState: T) -> T)) -> ())`

Used to store a stateful value. Returns the current value, and a function that can be used to set the value.

Expand Down
2 changes: 1 addition & 1 deletion src/createUseBinding.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local function createUseBinding(roact, useValue)
return function(defaultValue)
return function<T>(defaultValue: T): (any, (newValue: T) -> ())
Kampfkarren marked this conversation as resolved.
Show resolved Hide resolved
return unpack(useValue({
roact.createBinding(defaultValue)
}).value)
Expand Down
5 changes: 4 additions & 1 deletion src/createUseCallback.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
type DependencyList = { unknown }
type Function<Args..., Rets...> = (Args...) -> Rets...

local function createUseCallback(useMemo)
return function(callback, dependencies)
return function<Args..., Rets...>(callback: Function<Args..., Rets...>, dependencies: DependencyList?): Function<Args..., Rets...>
return useMemo(function()
return callback
end, dependencies)
Expand Down
6 changes: 5 additions & 1 deletion src/createUseEffect.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
type Destructor = () -> ()
type EffectCallback = (() -> Destructor) | (() -> ())
type DependencyList = { unknown }

local function createUseEffect(component)
return function(callback, dependsOn)
return function(callback: EffectCallback, dependsOn: DependencyList?)
assert(typeof(callback) == "function", "useEffect callback is not a function")

component.hookCounter += 1
Expand Down
4 changes: 3 additions & 1 deletion src/createUseMemo.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
local dependenciesDifferent = require(script.Parent.dependenciesDifferent)

type DependencyList = { unknown }

local function createUseMemo(useValue)
return function(createValue, dependencies)
return function<T...>(createValue: () -> T..., dependencies: DependencyList?): T...
local currentValue = useValue(nil)

local needToRecalculate = dependencies == nil
Expand Down
4 changes: 3 additions & 1 deletion src/createUseReducer.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
type Reducer<S, A> = (state: S, action: A) -> S

local function createUseReducer(useCallback, useState)
return function(reducer, initialState)
return function<S, A>(reducer: Reducer<S, A>, initialState: S): (S, (action: A) -> ())
local state, setState = useState(initialState)
local dispatch = useCallback(function(action)
setState(reducer(state, action))
Expand Down
5 changes: 4 additions & 1 deletion src/createUseState.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ local function extractValue(valueOrCallback, currentValue)
end
end

type SetStateAction<S> = S | ((prevState: S) -> S)
type Dispatch<T> = (value: T) -> ()

local function createUseState(component)
local setValues = {}

return function(defaultValue)
return function<T>(defaultValue: T | (() -> T)): (T, Dispatch<SetStateAction<T>>)
component.hookCounter += 1
local hookCount = component.hookCounter
local value = component.state[hookCount]
Expand Down
14 changes: 12 additions & 2 deletions src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,18 @@ local function createHooks(roact, component)
}
end

function Hooks.new(roact)
return function(render, options)
export type Hooks = typeof(createHooks())
export type HookOptions<Props> = {
name: string?,
defaultProps: { [string]: any }?,
componentType: "Component" | "PureComponent" | nil,
validateProps: ((props: Props) -> (boolean, string?))?,
[string]: { NO_EXTRA_ARGS: never },
}
export type RenderFunction<Props> = (props: Props, hooks: Hooks) -> any

function Hooks.new<Props>(roact)
return function(render: RenderFunction<Props>, options: HookOptions<Props>?)
assert(typeof(render) == "function", "Hooked components must be functions.")

if options == nil then
Expand Down