@@ -32,6 +32,15 @@ local function noop()
32
32
return nil
33
33
end
34
34
35
+ --[[
36
+ Returns `true` if the value can be called i.e. you can write `value(...)`.
37
+ ]]
38
+ local function isCallable (value : any ): boolean
39
+ return type (value ) == " function"
40
+ or (type (value ) == " table" and getmetatable (value ) and getmetatable (value ).__call ~= nil )
41
+ or false
42
+ end
43
+
35
44
--[[
36
45
The stateUpdater accepts props when they update and computes the
37
46
complete set of props that should be passed to the wrapped component.
@@ -77,7 +86,7 @@ local function connect<StoreState, Props, MappedStatePartialProps, MappedDispatc
77
86
>?
78
87
)
79
88
if mapStateToPropsOrThunk ~= nil then
80
- assert (typeof (mapStateToPropsOrThunk ) == " function " , " mapStateToProps must be a function or nil!" )
89
+ assert (isCallable (mapStateToPropsOrThunk ), " mapStateToProps must be a function or nil!" )
81
90
else
82
91
mapStateToPropsOrThunk = noop
83
92
end
@@ -142,8 +151,8 @@ local function connect<StoreState, Props, MappedStatePartialProps, MappedDispatc
142
151
-- value. In this variant, we keep that value as mapStateToProps
143
152
-- instead of the original mapStateToProps. This matches react-redux
144
153
-- and enables connectors to keep instance-level state.
145
- if typeof (mappedStoreState ) == " function " then
146
- mapStateToProps = mappedStoreState
154
+ if isCallable (mappedStoreState ) then
155
+ mapStateToProps = mappedStoreState :: any
147
156
mappedStoreState = mapStateToProps (storeState , self .props .innerProps )
148
157
end
149
158
@@ -164,20 +173,20 @@ local function connect<StoreState, Props, MappedStatePartialProps, MappedDispatc
164
173
end
165
174
166
175
local mappedStoreDispatch : any
167
- if mapDispatchType == " table" then
176
+ if isCallable (mapDispatchToProps ) then
177
+ mappedStoreDispatch = (mapDispatchToProps :: MapDispatchToProps < StoreState , MappedDispatchPartialProps > )(
178
+ (dispatch :: any ) :: ThunkfulDispatchProp <StoreState>
179
+ )
180
+ elseif mapDispatchType == " table" then
168
181
mappedStoreDispatch = {}
169
182
170
183
for key , actionCreator in pairs (mapDispatchToProps :: ActionCreatorMap ) do
171
- assert (typeof (actionCreator ) == " function " , " mapDispatchToProps must contain function values" )
184
+ assert (isCallable (actionCreator ), " mapDispatchToProps must contain function values" )
172
185
173
186
mappedStoreDispatch [key ] = function (...)
174
187
dispatch (actionCreator (... ))
175
188
end
176
189
end
177
- elseif mapDispatchType == " function" then
178
- mappedStoreDispatch = (mapDispatchToProps :: MapDispatchToProps < StoreState , MappedDispatchPartialProps > )(
179
- (dispatch :: any ) :: ThunkfulDispatchProp <StoreState>
180
- )
181
190
end
182
191
183
192
local stateUpdater = makeStateUpdater (self .store )
0 commit comments