Guest User

Untitled

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