API: Memoized Selectors
-
Higher order function that creates a composable, optimized selector function. A selector function is a pure function used for obtaining slices of store state.
Example: The following shows a composable, memoized
selectVisibleBooks
selector function.Composable: We depend on the
selectUser
andselectAllBooks
selectors to filter only the books belonging to the selectedUser, if one exists.Memoized: By using the default
memoized = true
parameter, if theallMovies
section slice of the state changes, thetransformation
inselectVisibleBooks
would not recompute.struct AppState { var selectedUser: User? var allBooks: [Book] = [] var allMovies: [Movie] = [] } let selectUser = { (state: AppState) in state.selectedUser } let selectAllBooks = { (state: AppState) in state.allBooks }
let selectVisibleBooks = createSelector( selectUser, selectAllBooks, transformation: { (selectedUser, allBooks) -> [Book] in if let selectedUser = selectedUser { return allBooks.filter { (book: Book) in book.userId == selectedUser.id } } else { return allBooks } } )
Declaration
Swift
public func createSelector< S: Equatable, V: Equatable, T >(_ selectorFn: @escaping SelectorFn<S, V>, transformation: @escaping (V) -> T, memoized: Bool = true ) -> SelectorFn<S, T>
Parameters
selectorFn
Up to nine selector functions. The result of each selector function will be used as input to the
transformation
parameter’s closure.transformation
A projector function used to derive, transform or combine data from the state slices passed to it and return this modified data as a single object - typically for direct view consumption.
memoized
True by default. If true, will cache the result from the last input. Prevents unnecessary recomputation if dependent state slices do not change.
-
See documentation in
createSelector(_:transformation:memoized:)
.Declaration
Swift
public func createSelector< S: Equatable, V1: Equatable, V2: Equatable, T >(_ selectorFn1: @escaping SelectorFn<S, V1>, _ selectorFn2: @escaping SelectorFn<S, V2>, transformation: @escaping (V1, V2) -> T, memoized: Bool = true ) -> SelectorFn<S, T>
-
See documentation in
createSelector(_:transformation:memoized:)
.Declaration
Swift
public func createSelector< S: Equatable, V1: Equatable, V2: Equatable, V3: Equatable, T >(_ selectorFn1: @escaping SelectorFn<S, V1>, _ selectorFn2: @escaping SelectorFn<S, V2>, _ selectorFn3: @escaping SelectorFn<S, V3>, transformation: @escaping (V1, V2, V3) -> T, memoized: Bool = true ) -> SelectorFn<S, T>
-
See documentation in
createSelector(_:transformation:memoized:)
.Declaration
Swift
public func createSelector< S: Equatable, V1: Equatable, V2: Equatable, V3: Equatable, V4: Equatable, T >(_ selectorFn1: @escaping SelectorFn<S, V1>, _ selectorFn2: @escaping SelectorFn<S, V2>, _ selectorFn3: @escaping SelectorFn<S, V3>, _ selectorFn4: @escaping SelectorFn<S, V4>, transformation: @escaping (V1, V2, V3, V4) -> T, memoized: Bool = true ) -> SelectorFn<S, T>
-
See documentation in
createSelector(_:transformation:memoized:)
.Declaration
Swift
public func createSelector< S: Equatable, V1: Equatable, V2: Equatable, V3: Equatable, V4: Equatable, V5: Equatable, T >(_ selectorFn1: @escaping SelectorFn<S, V1>, _ selectorFn2: @escaping SelectorFn<S, V2>, _ selectorFn3: @escaping SelectorFn<S, V3>, _ selectorFn4: @escaping SelectorFn<S, V4>, _ selectorFn5: @escaping SelectorFn<S, V5>, transformation: @escaping (V1, V2, V3, V4, V5) -> T, memoized: Bool = true ) -> SelectorFn<S, T>
-
See documentation in
createSelector(_:transformation:memoized:)
.Declaration
Swift
public func createSelector< S: Equatable, V1: Equatable, V2: Equatable, V3: Equatable, V4: Equatable, V5: Equatable, V6: Equatable, T >(_ selectorFn1: @escaping SelectorFn<S, V1>, _ selectorFn2: @escaping SelectorFn<S, V2>, _ selectorFn3: @escaping SelectorFn<S, V3>, _ selectorFn4: @escaping SelectorFn<S, V4>, _ selectorFn5: @escaping SelectorFn<S, V5>, _ selectorFn6: @escaping SelectorFn<S, V6>, transformation: @escaping (V1, V2, V3, V4, V5, V6) -> T, memoized: Bool = true ) -> SelectorFn<S, T>
-
See documentation in
createSelector(_:transformation:memoized:)
.Declaration
Swift
public func createSelector< S: Equatable, V1: Equatable, V2: Equatable, V3: Equatable, V4: Equatable, V5: Equatable, V6: Equatable, V7: Equatable, T >(_ selectorFn1: @escaping SelectorFn<S, V1>, _ selectorFn2: @escaping SelectorFn<S, V2>, _ selectorFn3: @escaping SelectorFn<S, V3>, _ selectorFn4: @escaping SelectorFn<S, V4>, _ selectorFn5: @escaping SelectorFn<S, V5>, _ selectorFn6: @escaping SelectorFn<S, V6>, _ selectorFn7: @escaping SelectorFn<S, V7>, transformation: @escaping (V1, V2, V3, V4, V5, V6, V7) -> T, memoized: Bool = true ) -> SelectorFn<S, T>
-
See documentation in
createSelector(_:transformation:memoized:)
.Declaration
Swift
public func createSelector< S: Equatable, V1: Equatable, V2: Equatable, V3: Equatable, V4: Equatable, V5: Equatable, V6: Equatable, V7: Equatable, V8: Equatable, T >(_ selectorFn1: @escaping SelectorFn<S, V1>, _ selectorFn2: @escaping SelectorFn<S, V2>, _ selectorFn3: @escaping SelectorFn<S, V3>, _ selectorFn4: @escaping SelectorFn<S, V4>, _ selectorFn5: @escaping SelectorFn<S, V5>, _ selectorFn6: @escaping SelectorFn<S, V6>, _ selectorFn7: @escaping SelectorFn<S, V7>, _ selectorFn8: @escaping SelectorFn<S, V8>, transformation: @escaping (V1, V2, V3, V4, V5, V6, V7, V8) -> T, memoized: Bool = true ) -> SelectorFn<S, T>
-
See documentation in
createSelector(_:transformation:memoized:)
.Declaration
Swift
public func createSelector< S: Equatable, V1: Equatable, V2: Equatable, V3: Equatable, V4: Equatable, V5: Equatable, V6: Equatable, V7: Equatable, V8: Equatable, V9: Equatable, T >(_ selectorFn1: @escaping SelectorFn<S, V1>, _ selectorFn2: @escaping SelectorFn<S, V2>, _ selectorFn3: @escaping SelectorFn<S, V3>, _ selectorFn4: @escaping SelectorFn<S, V4>, _ selectorFn5: @escaping SelectorFn<S, V5>, _ selectorFn6: @escaping SelectorFn<S, V6>, _ selectorFn7: @escaping SelectorFn<S, V7>, _ selectorFn8: @escaping SelectorFn<S, V8>, _ selectorFn9: @escaping SelectorFn<S, V9>, transformation: @escaping (V1, V2, V3, V4, V5, V6, V7, V8, V9) -> T, memoized: Bool = true ) -> SelectorFn<S, T>