index.d.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * Copyright (c) 2014-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. /**
  8. * Cursors
  9. * -------
  10. *
  11. * Cursors allow you to hold a reference to a path in a nested immutable data
  12. * structure, allowing you to pass smaller sections of a larger nested
  13. * collection to portions of your application while maintaining a central point
  14. * aware of changes to the entire data structure.
  15. *
  16. * This is particularly useful when used in conjuction with component-based UI
  17. * libraries like [React](http://facebook.github.io/react/) or to simulate
  18. * "state" throughout an application while maintaining a single flow of logic.
  19. *
  20. * Cursors provide a simple API for getting the value at that path
  21. * (the equivalent of `this.getIn(keyPath)`), updating the value at that path
  22. * (the equivalent of `this.updateIn(keyPath)`), and getting a sub-cursor
  23. * starting from that path.
  24. *
  25. * When updated, a new root collection is created and provided to the `onChange`
  26. * function provided to the first call to `Cursor(map, onChange)`.
  27. *
  28. * When this cursor's (or any of its sub-cursors') `update` method is called,
  29. * the resulting new data structure will be provided to the `onChange`
  30. * function. Use this callback to keep track of the most current value or
  31. * update the rest of your application.
  32. */
  33. /// <reference path='../../dist/immutable.d.ts'/>
  34. declare module __Cursor {
  35. export function from(
  36. collection: Immutable.Collection<any, any>,
  37. onChange?: (newValue: any, oldValue?: any, keyPath?: Array<any>) => any
  38. ): Cursor;
  39. export function from(
  40. collection: Immutable.Collection<any, any>,
  41. keyPath: Array<any>,
  42. onChange?: (newValue: any, oldValue?: any, keyPath?: Array<any>) => any
  43. ): Cursor;
  44. export function from(
  45. collection: Immutable.Collection<any, any>,
  46. key: any,
  47. onChange?: (newValue: any, oldValue?: any, keyPath?: Array<any>) => any
  48. ): Cursor;
  49. export interface Cursor extends Immutable.Seq<any, any> {
  50. /**
  51. * Returns a sub-cursor following the key-path starting from this cursor.
  52. */
  53. cursor(subKeyPath: Array<any>): Cursor;
  54. cursor(subKey: any): Cursor;
  55. /**
  56. * Returns the value at the cursor, if the cursor path does not yet exist,
  57. * returns `notSetValue`.
  58. */
  59. deref(notSetValue?: any): any;
  60. /**
  61. * Returns the value at the `key` in the cursor, or `notSetValue` if it
  62. * does not exist.
  63. *
  64. * If the key would return a collection, a new Cursor is returned.
  65. */
  66. get(key: any, notSetValue?: any): any;
  67. /**
  68. * Returns the value at the `keyPath` in the cursor, or `notSetValue` if it
  69. * does not exist.
  70. *
  71. * If the keyPath would return a collection, a new Cursor is returned.
  72. */
  73. getIn(keyPath: Array<any>, notSetValue?: any): any;
  74. getIn(keyPath: Immutable.Iterable<any, any>, notSetValue?: any): any;
  75. /**
  76. * Sets `value` at `key` in the cursor, returning a new cursor to the same
  77. * point in the new data.
  78. *
  79. * If only one parameter is provided, it is set directly as the cursor's value.
  80. */
  81. set(key: any, value: any): Cursor;
  82. set(value: any): Cursor;
  83. /**
  84. * Deletes `key` from the cursor, returning a new cursor to the same
  85. * point in the new data.
  86. *
  87. * Note: `delete` cannot be safely used in IE8
  88. * @alias remove
  89. */
  90. delete(key: any): Cursor;
  91. remove(key: any): Cursor;
  92. /**
  93. * Clears the value at this cursor, returning a new cursor to the same
  94. * point in the new data.
  95. */
  96. clear(): Cursor;
  97. /**
  98. * Updates the value in the data this cursor points to, triggering the
  99. * callback for the root cursor and returning a new cursor pointing to the
  100. * new data.
  101. */
  102. update(updater: (value: any) => any): Cursor;
  103. update(key: any, updater: (value: any) => any): Cursor;
  104. update(key: any, notSetValue: any, updater: (value: any) => any): Cursor;
  105. /**
  106. * @see `Map#merge`
  107. */
  108. merge(...iterables: Immutable.Iterable<any, any>[]): Cursor;
  109. merge(...iterables: {[key: string]: any}[]): Cursor;
  110. /**
  111. * @see `Map#mergeWith`
  112. */
  113. mergeWith(
  114. merger: (previous?: any, next?: any) => any,
  115. ...iterables: Immutable.Iterable<any, any>[]
  116. ): Cursor;
  117. mergeWith(
  118. merger: (previous?: any, next?: any) => any,
  119. ...iterables: {[key: string]: any}[]
  120. ): Cursor;
  121. /**
  122. * @see `Map#mergeDeep`
  123. */
  124. mergeDeep(...iterables: Immutable.Iterable<any, any>[]): Cursor;
  125. mergeDeep(...iterables: {[key: string]: any}[]): Cursor;
  126. /**
  127. * @see `Map#mergeDeepWith`
  128. */
  129. mergeDeepWith(
  130. merger: (previous?: any, next?: any) => any,
  131. ...iterables: Immutable.Iterable<any, any>[]
  132. ): Cursor;
  133. mergeDeepWith(
  134. merger: (previous?: any, next?: any) => any,
  135. ...iterables: {[key: string]: any}[]
  136. ): Cursor;
  137. // Deep persistent changes
  138. /**
  139. * Returns a new Cursor having set `value` at this `keyPath`. If any keys in
  140. * `keyPath` do not exist, a new immutable Map will be created at that key.
  141. */
  142. setIn(keyPath: Array<any>, value: any): Cursor;
  143. setIn(keyPath: Immutable.Iterable<any, any>, value: any): Cursor;
  144. /**
  145. * Returns a new Cursor with provided `values` appended
  146. */
  147. push(...values: Array<any>): Cursor;
  148. /**
  149. * Returns a new Cursor with a size ones less than this Cursor,
  150. * excluding the last index in this Cursor.
  151. */
  152. pop(): Cursor;
  153. /**
  154. * Returns a new Cursor with the provided `values` prepended,
  155. * shifting other values ahead to higher indices.
  156. */
  157. unshift(...values: Array<any>): Cursor;
  158. /**
  159. * Returns a new Cursor with a size ones less than this Cursor, excluding
  160. * the first index in this Cursor, shifting all other values to a lower index.
  161. */
  162. shift(): Cursor;
  163. /**
  164. * Returns a new Cursor having removed the value at this `keyPath`.
  165. *
  166. * @alias removeIn
  167. */
  168. deleteIn(keyPath: Array<any>): Cursor;
  169. deleteIn(keyPath: Immutable.Iterable<any, any>): Cursor;
  170. removeIn(keyPath: Array<any>): Cursor;
  171. removeIn(keyPath: Immutable.Iterable<any, any>): Cursor;
  172. /**
  173. * Returns a new Cursor having applied the `updater` to the value found at
  174. * the keyPath.
  175. *
  176. * If any keys in `keyPath` do not exist, new Immutable `Map`s will
  177. * be created at those keys. If the `keyPath` does not already contain a
  178. * value, the `updater` function will be called with `notSetValue`, if
  179. * provided, otherwise `undefined`.
  180. *
  181. * If the `updater` function returns the same value it was called with, then
  182. * no change will occur. This is still true if `notSetValue` is provided.
  183. */
  184. updateIn(
  185. keyPath: Array<any>,
  186. updater: (value: any) => any
  187. ): Cursor;
  188. updateIn(
  189. keyPath: Array<any>,
  190. notSetValue: any,
  191. updater: (value: any) => any
  192. ): Cursor;
  193. updateIn(
  194. keyPath: Immutable.Iterable<any, any>,
  195. updater: (value: any) => any
  196. ): Cursor;
  197. updateIn(
  198. keyPath: Immutable.Iterable<any, any>,
  199. notSetValue: any,
  200. updater: (value: any) => any
  201. ): Cursor;
  202. /**
  203. * A combination of `updateIn` and `merge`, returning a new Cursor, but
  204. * performing the merge at a point arrived at by following the keyPath.
  205. * In other words, these two lines are equivalent:
  206. *
  207. * x.updateIn(['a', 'b', 'c'], abc => abc.merge(y));
  208. * x.mergeIn(['a', 'b', 'c'], y);
  209. *
  210. */
  211. mergeIn(
  212. keyPath: Immutable.Iterable<any, any>,
  213. ...iterables: Immutable.Iterable<any, any>[]
  214. ): Cursor;
  215. mergeIn(
  216. keyPath: Array<any>,
  217. ...iterables: Immutable.Iterable<any, any>[]
  218. ): Cursor;
  219. mergeIn(
  220. keyPath: Array<any>,
  221. ...iterables: {[key: string]: any}[]
  222. ): Cursor;
  223. /**
  224. * A combination of `updateIn` and `mergeDeep`, returning a new Cursor, but
  225. * performing the deep merge at a point arrived at by following the keyPath.
  226. * In other words, these two lines are equivalent:
  227. *
  228. * x.updateIn(['a', 'b', 'c'], abc => abc.mergeDeep(y));
  229. * x.mergeDeepIn(['a', 'b', 'c'], y);
  230. *
  231. */
  232. mergeDeepIn(
  233. keyPath: Immutable.Iterable<any, any>,
  234. ...iterables: Immutable.Iterable<any, any>[]
  235. ): Cursor;
  236. mergeDeepIn(
  237. keyPath: Array<any>,
  238. ...iterables: Immutable.Iterable<any, any>[]
  239. ): Cursor;
  240. mergeDeepIn(
  241. keyPath: Array<any>,
  242. ...iterables: {[key: string]: any}[]
  243. ): Cursor;
  244. // Transient changes
  245. /**
  246. * Every time you call one of the above functions, a new immutable value is
  247. * created and the callback is triggered. If you need to apply a series of
  248. * mutations to a Cursor without triggering the callback repeatedly,
  249. * `withMutations()` creates a temporary mutable copy of the value which
  250. * can apply mutations in a highly performant manner. Afterwards the
  251. * callback is triggered with the final value.
  252. */
  253. withMutations(mutator: (mutable: any) => any): Cursor;
  254. }
  255. }
  256. declare module 'immutable/contrib/cursor' {
  257. export = __Cursor
  258. }