1/** 2 * KB changes: 3 * All actions typed 4 * State is TypedState always 5 */ 6import {TypedActions} from '../../actions/typed-actions-gen' 7import {TypedState as _TypedState} from '../../constants/reducer' 8export type Action = TypedActions 9export type AnyAction = TypedActions 10export type TypedState = _TypedState 11 12/** 13 * An *action* is a plain object that represents an intention to change the 14 * state. Actions are the only way to get data into the store. Any data, 15 * whether from UI events, network callbacks, or other sources such as 16 * WebSockets needs to eventually be dispatched as actions. 17 * 18 * Actions must have a `type` field that indicates the type of action being 19 * performed. Types can be defined as constants and imported from another 20 * module. It's better to use strings for `type` than Symbols because strings 21 * are serializable. 22 * 23 * Other than `type`, the structure of an action object is really up to you. 24 * If you're interested, check out Flux Standard Action for recommendations on 25 * how actions should be constructed. 26 * 27 * @template T the type of the action's `type` tag. 28 */ 29// export interface Action<T = any> { 30// type: T 31// } 32 33/** 34 * An Action type which accepts any other properties. 35 * This is mainly for the use of the `Reducer` type. 36 * This is not part of `Action` itself to prevent users who are extending `Action. 37 */ 38// export interface AnyAction extends Action { 39// // Allows any extra properties to be defined in an action. 40// [extraProps: string]: any 41// } 42 43/* reducers */ 44 45/** 46 * A *reducer* (also called a *reducing function*) is a function that accepts 47 * an accumulation and a value and returns a new accumulation. They are used 48 * to reduce a collection of values down to a single value 49 * 50 * Reducers are not unique to Redux—they are a fundamental concept in 51 * functional programming. Even most non-functional languages, like 52 * JavaScript, have a built-in API for reducing. In JavaScript, it's 53 * `Array.prototype.reduce()`. 54 * 55 * In Redux, the accumulated value is the state object, and the values being 56 * accumulated are actions. Reducers calculate a new state given the previous 57 * state and an action. They must be *pure functions*—functions that return 58 * the exact same output for given inputs. They should also be free of 59 * side-effects. This is what enables exciting features like hot reloading and 60 * time travel. 61 * 62 * Reducers are the most important concept in Redux. 63 * 64 * *Do not put API calls into reducers.* 65 * 66 * @template S The type of state consumed and produced by this reducer. 67 * @template A The type of actions the reducer can potentially respond to. 68 */ 69export type Reducer<S = any, A extends Action = AnyAction> = (state: S | undefined, action: A) => S 70 71/** 72 * Object whose values correspond to different reducer functions. 73 * 74 * @template A The type of actions the reducers can potentially respond to. 75 */ 76export type ReducersMapObject<S = any, A extends Action = Action> = {[K in keyof S]: Reducer<S[K], A>} 77 78/** 79 * Turns an object whose values are different reducer functions, into a single 80 * reducer function. It will call every child reducer, and gather their results 81 * into a single state object, whose keys correspond to the keys of the passed 82 * reducer functions. 83 * 84 * @template S Combined state object type. 85 * 86 * @param reducers An object whose values correspond to different reducer 87 * functions that need to be combined into one. One handy way to obtain it 88 * is to use ES6 `import * as reducers` syntax. The reducers may never 89 * return undefined for any action. Instead, they should return their 90 * initial state if the state passed to them was undefined, and the current 91 * state for any unrecognized action. 92 * 93 * @returns A reducer function that invokes every reducer inside the passed 94 * object, and builds a state object with the same shape. 95 */ 96export function combineReducers<S>(reducers: ReducersMapObject<S, any>): Reducer<S> 97export function combineReducers<S, A extends Action = AnyAction>( 98 reducers: ReducersMapObject<S, A> 99): Reducer<S, A> 100 101/* store */ 102 103/** 104 * A *dispatching function* (or simply *dispatch function*) is a function that 105 * accepts an action or an async action; it then may or may not dispatch one 106 * or more actions to the store. 107 * 108 * We must distinguish between dispatching functions in general and the base 109 * `dispatch` function provided by the store instance without any middleware. 110 * 111 * The base dispatch function *always* synchronously sends an action to the 112 * store's reducer, along with the previous state returned by the store, to 113 * calculate a new state. It expects actions to be plain objects ready to be 114 * consumed by the reducer. 115 * 116 * Middleware wraps the base dispatch function. It allows the dispatch 117 * function to handle async actions in addition to actions. Middleware may 118 * transform, delay, ignore, or otherwise interpret actions or async actions 119 * before passing them to the next middleware. 120 * 121 * @template A The type of things (actions or otherwise) which may be 122 * dispatched. 123 */ 124export interface Dispatch<A extends Action = AnyAction> { 125 <T extends A>(action: T): void 126 // KB assume void // T 127} 128 129/** 130 * Function to remove listener added by `Store.subscribe()`. 131 */ 132export interface Unsubscribe { 133 (): void 134} 135 136/** 137 * A store is an object that holds the application's state tree. 138 * There should only be a single store in a Redux app, as the composition 139 * happens on the reducer level. 140 * 141 * @template S The type of state held by this store. 142 * @template A the type of actions which may be dispatched by this store. 143 */ 144export interface Store<S = TypedState, A extends Action = AnyAction> { 145 /** 146 * Dispatches an action. It is the only way to trigger a state change. 147 * 148 * The `reducer` function, used to create the store, will be called with the 149 * current state tree and the given `action`. Its return value will be 150 * considered the **next** state of the tree, and the change listeners will 151 * be notified. 152 * 153 * The base implementation only supports plain object actions. If you want 154 * to dispatch a Promise, an Observable, a thunk, or something else, you 155 * need to wrap your store creating function into the corresponding 156 * middleware. For example, see the documentation for the `redux-thunk` 157 * package. Even the middleware will eventually dispatch plain object 158 * actions using this method. 159 * 160 * @param action A plain object representing “what changed”. It is a good 161 * idea to keep actions serializable so you can record and replay user 162 * sessions, or use the time travelling `redux-devtools`. An action must 163 * have a `type` property which may not be `undefined`. It is a good idea 164 * to use string constants for action types. 165 * 166 * @returns For convenience, the same action object you dispatched. 167 * 168 * Note that, if you use a custom middleware, it may wrap `dispatch()` to 169 * return something else (for example, a Promise you can await). 170 */ 171 dispatch: Dispatch<A> 172 173 /** 174 * Reads the state tree managed by the store. 175 * 176 * @returns The current state tree of your application. 177 */ 178 getState(): S 179 180 /** 181 * Adds a change listener. It will be called any time an action is 182 * dispatched, and some part of the state tree may potentially have changed. 183 * You may then call `getState()` to read the current state tree inside the 184 * callback. 185 * 186 * You may call `dispatch()` from a change listener, with the following 187 * caveats: 188 * 189 * 1. The subscriptions are snapshotted just before every `dispatch()` call. 190 * If you subscribe or unsubscribe while the listeners are being invoked, 191 * this will not have any effect on the `dispatch()` that is currently in 192 * progress. However, the next `dispatch()` call, whether nested or not, 193 * will use a more recent snapshot of the subscription list. 194 * 195 * 2. The listener should not expect to see all states changes, as the state 196 * might have been updated multiple times during a nested `dispatch()` before 197 * the listener is called. It is, however, guaranteed that all subscribers 198 * registered before the `dispatch()` started will be called with the latest 199 * state by the time it exits. 200 * 201 * @param listener A callback to be invoked on every dispatch. 202 * @returns A function to remove this change listener. 203 */ 204 subscribe(listener: () => void): Unsubscribe 205 206 /** 207 * Replaces the reducer currently used by the store to calculate the state. 208 * 209 * You might need this if your app implements code splitting and you want to 210 * load some of the reducers dynamically. You might also need this if you 211 * implement a hot reloading mechanism for Redux. 212 * 213 * @param nextReducer The reducer for the store to use instead. 214 */ 215 replaceReducer(nextReducer: Reducer<S, A>): void 216} 217 218export type DeepPartial<T> = {[K in keyof T]?: DeepPartial<T[K]>} 219 220/** 221 * A store creator is a function that creates a Redux store. Like with 222 * dispatching function, we must distinguish the base store creator, 223 * `createStore(reducer, preloadedState)` exported from the Redux package, from 224 * store creators that are returned from the store enhancers. 225 * 226 * @template S The type of state to be held by the store. 227 * @template A The type of actions which may be dispatched. 228 * @template Ext Store extension that is mixed in to the Store type. 229 * @template StateExt State extension that is mixed into the state type. 230 */ 231export interface StoreCreator { 232 <S, A extends Action, Ext, StateExt>( 233 reducer: Reducer<S, A>, 234 enhancer?: StoreEnhancer<Ext, StateExt> 235 ): Store<S & StateExt, A> & Ext 236 <S, A extends Action, Ext, StateExt>( 237 reducer: Reducer<S, A>, 238 preloadedState?: DeepPartial<S>, 239 enhancer?: StoreEnhancer<Ext> 240 ): Store<S & StateExt, A> & Ext 241} 242 243/** 244 * Creates a Redux store that holds the state tree. 245 * The only way to change the data in the store is to call `dispatch()` on it. 246 * 247 * There should only be a single store in your app. To specify how different 248 * parts of the state tree respond to actions, you may combine several 249 * reducers 250 * into a single reducer function by using `combineReducers`. 251 * 252 * @template S State object type. 253 * 254 * @param reducer A function that returns the next state tree, given the 255 * current state tree and the action to handle. 256 * 257 * @param [preloadedState] The initial state. You may optionally specify it to 258 * hydrate the state from the server in universal apps, or to restore a 259 * previously serialized user session. If you use `combineReducers` to 260 * produce the root reducer function, this must be an object with the same 261 * shape as `combineReducers` keys. 262 * 263 * @param [enhancer] The store enhancer. You may optionally specify it to 264 * enhance the store with third-party capabilities such as middleware, time 265 * travel, persistence, etc. The only store enhancer that ships with Redux 266 * is `applyMiddleware()`. 267 * 268 * @returns A Redux store that lets you read the state, dispatch actions and 269 * subscribe to changes. 270 */ 271export const createStore: StoreCreator 272 273/** 274 * A store enhancer is a higher-order function that composes a store creator 275 * to return a new, enhanced store creator. This is similar to middleware in 276 * that it allows you to alter the store interface in a composable way. 277 * 278 * Store enhancers are much the same concept as higher-order components in 279 * React, which are also occasionally called “component enhancers”. 280 * 281 * Because a store is not an instance, but rather a plain-object collection of 282 * functions, copies can be easily created and modified without mutating the 283 * original store. There is an example in `compose` documentation 284 * demonstrating that. 285 * 286 * Most likely you'll never write a store enhancer, but you may use the one 287 * provided by the developer tools. It is what makes time travel possible 288 * without the app being aware it is happening. Amusingly, the Redux 289 * middleware implementation is itself a store enhancer. 290 * 291 * @template Ext Store extension that is mixed into the Store type. 292 * @template StateExt State extension that is mixed into the state type. 293 */ 294export type StoreEnhancer<Ext = {}, StateExt = {}> = ( 295 next: StoreEnhancerStoreCreator 296) => StoreEnhancerStoreCreator<Ext, StateExt> 297export type StoreEnhancerStoreCreator<Ext = {}, StateExt = {}> = <S = any, A extends Action = AnyAction>( 298 reducer: Reducer<S, A>, 299 preloadedState?: DeepPartial<S> 300) => Store<S & StateExt, A> & Ext 301 302/* middleware */ 303 304export interface MiddlewareAPI<D extends Dispatch = Dispatch, S = TypedState> { 305 dispatch: D 306 getState(): S 307} 308 309/** 310 * A middleware is a higher-order function that composes a dispatch function 311 * to return a new dispatch function. It often turns async actions into 312 * actions. 313 * 314 * Middleware is composable using function composition. It is useful for 315 * logging actions, performing side effects like routing, or turning an 316 * asynchronous API call into a series of synchronous actions. 317 * 318 * @template DispatchExt Extra Dispatch signature added by this middleware. 319 * @template S The type of the state supported by this middleware. 320 * @template D The type of Dispatch of the store where this middleware is 321 * installed. 322 */ 323export interface Middleware<DispatchExt = {}, S = TypedState, D extends Dispatch = Dispatch> { 324 (api: MiddlewareAPI<D, S>): (next: Dispatch<AnyAction>) => (action: any) => any 325} 326 327/** 328 * Creates a store enhancer that applies middleware to the dispatch method 329 * of the Redux store. This is handy for a variety of tasks, such as 330 * expressing asynchronous actions in a concise manner, or logging every 331 * action payload. 332 * 333 * See `redux-thunk` package as an example of the Redux middleware. 334 * 335 * Because middleware is potentially asynchronous, this should be the first 336 * store enhancer in the composition chain. 337 * 338 * Note that each middleware will be given the `dispatch` and `getState` 339 * functions as named arguments. 340 * 341 * @param middlewares The middleware chain to be applied. 342 * @returns A store enhancer applying the middleware. 343 * 344 * @template Ext Dispatch signature added by a middleware. 345 * @template S The type of the state supported by a middleware. 346 */ 347export function applyMiddleware(): StoreEnhancer 348export function applyMiddleware<Ext1, S>( 349 middleware1: Middleware<Ext1, S, any> 350): StoreEnhancer<{dispatch: Ext1}> 351export function applyMiddleware<Ext1, Ext2, S>( 352 middleware1: Middleware<Ext1, S, any>, 353 middleware2: Middleware<Ext2, S, any> 354): StoreEnhancer<{dispatch: Ext1 & Ext2}> 355export function applyMiddleware<Ext1, Ext2, Ext3, S>( 356 middleware1: Middleware<Ext1, S, any>, 357 middleware2: Middleware<Ext2, S, any>, 358 middleware3: Middleware<Ext3, S, any> 359): StoreEnhancer<{dispatch: Ext1 & Ext2 & Ext3}> 360export function applyMiddleware<Ext1, Ext2, Ext3, Ext4, S>( 361 middleware1: Middleware<Ext1, S, any>, 362 middleware2: Middleware<Ext2, S, any>, 363 middleware3: Middleware<Ext3, S, any>, 364 middleware4: Middleware<Ext4, S, any> 365): StoreEnhancer<{dispatch: Ext1 & Ext2 & Ext3 & Ext4}> 366export function applyMiddleware<Ext1, Ext2, Ext3, Ext4, Ext5, S>( 367 middleware1: Middleware<Ext1, S, any>, 368 middleware2: Middleware<Ext2, S, any>, 369 middleware3: Middleware<Ext3, S, any>, 370 middleware4: Middleware<Ext4, S, any>, 371 middleware5: Middleware<Ext5, S, any> 372): StoreEnhancer<{dispatch: Ext1 & Ext2 & Ext3 & Ext4 & Ext5}> 373export function applyMiddleware<Ext, S = any>( 374 ...middlewares: Middleware<any, S, any>[] 375): StoreEnhancer<{dispatch: Ext}> 376 377/* action creators */ 378 379/** 380 * An *action creator* is, quite simply, a function that creates an action. Do 381 * not confuse the two terms—again, an action is a payload of information, and 382 * an action creator is a factory that creates an action. 383 * 384 * Calling an action creator only produces an action, but does not dispatch 385 * it. You need to call the store's `dispatch` function to actually cause the 386 * mutation. Sometimes we say *bound action creators* to mean functions that 387 * call an action creator and immediately dispatch its result to a specific 388 * store instance. 389 * 390 * If an action creator needs to read the current state, perform an API call, 391 * or cause a side effect, like a routing transition, it should return an 392 * async action instead of an action. 393 * 394 * @template A Returned action type. 395 */ 396export interface ActionCreator<A> { 397 (...args: any[]): A 398} 399 400/** 401 * Object whose values are action creator functions. 402 */ 403export interface ActionCreatorsMapObject<A = any> { 404 [key: string]: ActionCreator<A> 405} 406 407/** 408 * Turns an object whose values are action creators, into an object with the 409 * same keys, but with every function wrapped into a `dispatch` call so they 410 * may be invoked directly. This is just a convenience method, as you can call 411 * `store.dispatch(MyActionCreators.doSomething())` yourself just fine. 412 * 413 * For convenience, you can also pass a single function as the first argument, 414 * and get a function in return. 415 * 416 * @param actionCreator An object whose values are action creator functions. 417 * One handy way to obtain it is to use ES6 `import * as` syntax. You may 418 * also pass a single function. 419 * 420 * @param dispatch The `dispatch` function available on your Redux store. 421 * 422 * @returns The object mimicking the original object, but with every action 423 * creator wrapped into the `dispatch` call. If you passed a function as 424 * `actionCreator`, the return value will also be a single function. 425 */ 426export function bindActionCreators<A, C extends ActionCreator<A>>(actionCreator: C, dispatch: Dispatch): C 427 428export function bindActionCreators<A extends ActionCreator<any>, B extends ActionCreator<any>>( 429 actionCreator: A, 430 dispatch: Dispatch 431): B 432 433export function bindActionCreators<A, M extends ActionCreatorsMapObject<A>>( 434 actionCreators: M, 435 dispatch: Dispatch 436): M 437 438export function bindActionCreators< 439 M extends ActionCreatorsMapObject<any>, 440 N extends ActionCreatorsMapObject<any> 441>(actionCreators: M, dispatch: Dispatch): N 442 443/* compose */ 444 445type Func0<R> = () => R 446type Func1<T1, R> = (a1: T1) => R 447type Func2<T1, T2, R> = (a1: T1, a2: T2) => R 448type Func3<T1, T2, T3, R> = (a1: T1, a2: T2, a3: T3, ...args: any[]) => R 449 450/** 451 * Composes single-argument functions from right to left. The rightmost 452 * function can take multiple arguments as it provides the signature for the 453 * resulting composite function. 454 * 455 * @param funcs The functions to compose. 456 * @returns R function obtained by composing the argument functions from right 457 * to left. For example, `compose(f, g, h)` is identical to doing 458 * `(...args) => f(g(h(...args)))`. 459 */ 460export function compose(): <R>(a: R) => R 461 462export function compose<F extends Function>(f: F): F 463 464/* two functions */ 465export function compose<A, R>(f1: (b: A) => R, f2: Func0<A>): Func0<R> 466export function compose<A, T1, R>(f1: (b: A) => R, f2: Func1<T1, A>): Func1<T1, R> 467export function compose<A, T1, T2, R>(f1: (b: A) => R, f2: Func2<T1, T2, A>): Func2<T1, T2, R> 468export function compose<A, T1, T2, T3, R>(f1: (b: A) => R, f2: Func3<T1, T2, T3, A>): Func3<T1, T2, T3, R> 469 470/* three functions */ 471export function compose<A, B, R>(f1: (b: B) => R, f2: (a: A) => B, f3: Func0<A>): Func0<R> 472export function compose<A, B, T1, R>(f1: (b: B) => R, f2: (a: A) => B, f3: Func1<T1, A>): Func1<T1, R> 473export function compose<A, B, T1, T2, R>( 474 f1: (b: B) => R, 475 f2: (a: A) => B, 476 f3: Func2<T1, T2, A> 477): Func2<T1, T2, R> 478export function compose<A, B, T1, T2, T3, R>( 479 f1: (b: B) => R, 480 f2: (a: A) => B, 481 f3: Func3<T1, T2, T3, A> 482): Func3<T1, T2, T3, R> 483 484/* four functions */ 485export function compose<A, B, C, R>(f1: (b: C) => R, f2: (a: B) => C, f3: (a: A) => B, f4: Func0<A>): Func0<R> 486export function compose<A, B, C, T1, R>( 487 f1: (b: C) => R, 488 f2: (a: B) => C, 489 f3: (a: A) => B, 490 f4: Func1<T1, A> 491): Func1<T1, R> 492export function compose<A, B, C, T1, T2, R>( 493 f1: (b: C) => R, 494 f2: (a: B) => C, 495 f3: (a: A) => B, 496 f4: Func2<T1, T2, A> 497): Func2<T1, T2, R> 498export function compose<A, B, C, T1, T2, T3, R>( 499 f1: (b: C) => R, 500 f2: (a: B) => C, 501 f3: (a: A) => B, 502 f4: Func3<T1, T2, T3, A> 503): Func3<T1, T2, T3, R> 504 505/* rest */ 506export function compose<R>(f1: (b: any) => R, ...funcs: Function[]): (...args: any[]) => R 507 508export function compose<R>(...funcs: Function[]): (...args: any[]) => R 509