1
//
2
//  TypeHelper.swift
3
//  ReSwift
4
//
5
//  Created by Benjamin Encz on 11/27/15.
6
//  Copyright © 2015 ReSwift Community. All rights reserved.
7
//
8

9
/**
10
 Method is only used internally in ReSwift to cast the generic `StateType` to a specific
11
 type expected by reducers / store subscribers.
12

13
 - parameter action: An action that will be passed to `handleAction`.
14
 - parameter state: A generic state type that will be casted to `SpecificStateType`.
15
 - parameter function: The `handleAction` method.
16
 - returns: A `StateType` from `handleAction` or the original `StateType` if it cannot be
17
            casted to `SpecificStateType`.
18
 */
19
@discardableResult
20
func withSpecificTypes<SpecificStateType, Action>(
21
        _ action: Action,
22
        state genericStateType: Any?,
23
        function: (_ action: Action, _ state: SpecificStateType?) -> SpecificStateType
24 5
    ) -> Any {
25 5
        guard let genericStateType = genericStateType else {
26 5
            return function(action, nil) as Any
27
        }
28

29 5
        guard let specificStateType = genericStateType as? SpecificStateType else {
30 5
            return genericStateType
31
        }
32

33 5
        return function(action, specificStateType) as Any
34
}

Read our documentation on viewing source code .

Loading