or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

development.mdindex.mdmemoization.mdselector-creation.mdselector-creator.mdstructured-selectors.md

selector-creator.mddocs/

0

# Selector Creator Customization

1

2

Advanced functionality for creating customized selector creators with specific memoization strategies and configuration options.

3

4

## Capabilities

5

6

### createSelectorCreator

7

8

Creates a customized `createSelector` function with pre-configured memoization and options.

9

10

```typescript { .api }

11

/**

12

* Creates a customized createSelector function with specific memoization options

13

* @param options - Configuration object with memoize function and options

14

* @returns Customized CreateSelectorFunction instance

15

*/

16

function createSelectorCreator<MemoizeFunction>(

17

options: {

18

memoize: MemoizeFunction;

19

memoizeOptions?: ExtractMemoizerFields<MemoizeFunction>;

20

}

21

): CreateSelectorFunction<MemoizeFunction>;

22

23

/**

24

* Legacy overload - creates a selector creator with memoize function and options

25

* @param memoizeFunc - Memoization function to use

26

* @param memoizeOptions - Options passed to the memoization function

27

* @returns Customized CreateSelectorFunction instance

28

*/

29

function createSelectorCreator<MemoizeFunction>(

30

memoizeFunc: MemoizeFunction,

31

...memoizeOptions: Parameters<MemoizeFunction>

32

): CreateSelectorFunction<MemoizeFunction>;

33

```

34

35

**Basic Usage:**

36

37

```typescript

38

import { createSelectorCreator, lruMemoize } from "reselect";

39

40

// Create a selector creator with LRU memoization

41

const createSelectorWithLRU = createSelectorCreator({

42

memoize: lruMemoize,

43

memoizeOptions: { maxSize: 5 }

44

});

45

46

// Use the custom selector creator

47

const selectExpensiveData = createSelectorWithLRU(

48

[selectLargeDataset],

49

(data) => performExpensiveComputation(data)

50

);

51

```

52

53

**Legacy Syntax:**

54

55

```typescript

56

import { createSelectorCreator, lruMemoize } from "reselect";

57

58

// Legacy syntax (still supported)

59

const createSelectorWithLRU = createSelectorCreator(

60

lruMemoize,

61

{ maxSize: 5 }

62

);

63

```

64

65

**Auto-tracking Example:**

66

67

```typescript

68

import { createSelectorCreator, unstable_autotrackMemoize } from "reselect";

69

70

// Create selector creator with auto-tracking memoization

71

const createAutotrackSelector = createSelectorCreator({

72

memoize: unstable_autotrackMemoize

73

});

74

75

const selectNestedData = createAutotrackSelector(

76

[state => state.users],

77

(users) => users.map(user => user.profile.name) // Only recomputes if profile.name changes

78

);

79

```

80

81

### CreateSelectorFunction Interface

82

83

The type of customized selector creator functions returned by `createSelectorCreator`.

84

85

```typescript { .api }

86

interface CreateSelectorFunction<MemoizeFunction = typeof weakMapMemoize> {

87

/** Create selector with array of input selectors */

88

<State, Result>(

89

selectors: SelectorArray<State>,

90

combiner: Combiner<Result>

91

): OutputSelector<State, Result>;

92

93

/** Create selector with array of input selectors and options */

94

<State, Result>(

95

selectors: SelectorArray<State>,

96

combiner: Combiner<Result>,

97

options: CreateSelectorOptions<MemoizeFunction>

98

): OutputSelector<State, Result>;

99

100

/** Variadic overloads for inline selectors (1-12 selectors) */

101

<State, Res1, Result>(

102

selector1: Selector<State, Res1>,

103

combiner: (res1: Res1) => Result

104

): OutputSelector<State, Result>;

105

106

<State, Res1, Res2, Result>(

107

selector1: Selector<State, Res1>,

108

selector2: Selector<State, Res2>,

109

combiner: (res1: Res1, res2: Res2) => Result

110

): OutputSelector<State, Result>;

111

112

// Additional overloads for 3-12 input selectors...

113

}

114

```

115

116

### Custom Memoization Examples

117

118

**WeakMap Memoization (Default):**

119

120

```typescript

121

import { createSelectorCreator, weakMapMemoize } from "reselect";

122

123

const createWeakMapSelector = createSelectorCreator({

124

memoize: weakMapMemoize,

125

memoizeOptions: {

126

equalityCheck: (a, b) => a.id === b.id // Custom equality check

127

}

128

});

129

130

const selectUserData = createWeakMapSelector(

131

[selectUser],

132

(user) => processUserData(user)

133

);

134

```

135

136

**LRU Memoization with Custom Size:**

137

138

```typescript

139

import { createSelectorCreator, lruMemoize } from "reselect";

140

141

const createLRUSelector = createSelectorCreator({

142

memoize: lruMemoize,

143

memoizeOptions: {

144

maxSize: 50, // Cache up to 50 results

145

equalityCheck: (a, b) => JSON.stringify(a) === JSON.stringify(b)

146

}

147

});

148

149

const selectProcessedItems = createLRUSelector(

150

[selectItems, selectFilters],

151

(items, filters) => applyFiltersAndSort(items, filters)

152

);

153

```

154

155

**Custom Memoization Function:**

156

157

```typescript

158

import { createSelectorCreator } from "reselect";

159

160

// Custom memoization that only caches the last result

161

function simpleMemoize(func) {

162

let lastArgs;

163

let lastResult;

164

165

return (...args) => {

166

if (!lastArgs || !shallowEqual(args, lastArgs)) {

167

lastResult = func(...args);

168

lastArgs = args;

169

}

170

return lastResult;

171

};

172

}

173

174

const createSimpleSelector = createSelectorCreator({

175

memoize: simpleMemoize

176

});

177

178

const selectSimpleData = createSimpleSelector(

179

[selectInput],

180

(input) => transformInput(input)

181

);

182

```

183

184

### Advanced Configuration

185

186

```typescript

187

import {

188

createSelectorCreator,

189

lruMemoize,

190

type CreateSelectorOptions

191

} from "reselect";

192

193

// Selector creator with both memoization and dev mode configuration

194

const createDebugSelector = createSelectorCreator({

195

memoize: lruMemoize,

196

memoizeOptions: { maxSize: 10 }

197

});

198

199

// Override dev mode checks when creating specific selectors

200

const selectDebugData = createDebugSelector(

201

[selectComplexState],

202

(state) => processComplexState(state),

203

{

204

// Override memoization for this specific selector

205

memoize: weakMapMemoize,

206

devModeChecks: {

207

inputStabilityCheck: 'always',

208

identityFunctionCheck: 'never'

209

}

210

}

211

);

212

```

213

214

## Types

215

216

```typescript { .api }

217

type ExtractMemoizerFields<MemoizeFunction> =

218

MemoizeFunction extends (func: any, ...options: infer Options) => any

219

? Options

220

: never;

221

222

interface CreateSelectorOptions<MemoizeFunction = typeof weakMapMemoize> {

223

memoize?: MemoizeFunction;

224

memoizeOptions?: ExtractMemoizerFields<MemoizeFunction>;

225

devModeChecks?: Partial<DevModeChecks>;

226

argsMemoize?: UnknownMemoizer;

227

argsMemoizeOptions?: unknown[];

228

}

229

230

type MemoizeOptionsFromParameters<MemoizeParameters> =

231

MemoizeParameters extends readonly [any, ...infer Rest]

232

? Rest

233

: never;

234

235

interface OverrideMemoizeOptions<MemoizeFunction = UnknownMemoizer> {

236

memoize: MemoizeFunction;

237

argsMemoize?: UnknownMemoizer;

238

}

239

```