Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

90 строки
3.4 KiB

  1. // @flow
  2. export interface Patch {
  3. op: "replace" | "remove" | "add",
  4. path: (string|number)[],
  5. value?: any
  6. }
  7. export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void
  8. interface IProduce {
  9. /**
  10. * Immer takes a state, and runs a function against it.
  11. * That function can freely mutate the state, as it will create copies-on-write.
  12. * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned.
  13. *
  14. * If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe
  15. * any time it is called with the current state.
  16. *
  17. * @param currentState - the state to start with
  18. * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified
  19. * @param initialState - if a curried function is created and this argument was given, it will be used as fallback if the curried function is called with a state of undefined
  20. * @returns The next state: a new state, or the current state if nothing was modified
  21. */
  22. <S>(
  23. currentState: S,
  24. recipe: (draftState: S) => S | void,
  25. patchListener?: PatchListener
  26. ): S;
  27. // curried invocations with inital state
  28. <S, A, B, C>(
  29. recipe: (draftState: S, a: A, b: B, c: C) => S | void,
  30. initialState: S
  31. ): (currentState: S | void, a: A, b: B, c: C) => S;
  32. <S, A, B>(
  33. recipe: (draftState: S, a: A, b: B) => S | void,
  34. initialState: S
  35. ): (currentState: S | void, a: A, b: B) => S;
  36. <S, A>(
  37. recipe: (draftState: S, a: A) => S | void,
  38. initialState: S
  39. ): (currentState: S | void, a: A) => S;
  40. <S>(
  41. recipe: (draftState: S) => S | void,
  42. initialState: S
  43. ): (currentState: S | void) => S;
  44. <S>(
  45. recipe: (draftState: S, ...extraArgs: any[]) => S | void,
  46. initialState: S
  47. ): (currentState: S | void, ...extraArgs: any[]) => S;
  48. // curried invocations without inital state
  49. <S, A, B, C>(
  50. recipe: (draftState: S, a: A, b: B, c: C) => S | void
  51. ): (currentState: S, a: A, b: B, c: C) => S;
  52. <S, A, B>(
  53. recipe: (draftState: S, a: A, b: B) => S | void
  54. ): (currentState: S, a: A, b: B) => S;
  55. <S, A>(
  56. recipe: (draftState: S, a: A) => S | void
  57. ): (currentState: S, a: A) => S;
  58. <S>(
  59. recipe: (draftState: S) => S | void
  60. ): (currentState: S) => S;
  61. <S>(
  62. recipe: (draftState: S, ...extraArgs: any[]) => S | void
  63. ): (currentState: S, ...extraArgs: any[]) => S;
  64. }
  65. declare export var produce: IProduce
  66. declare export default IProduce
  67. declare export var nothing: typeof undefined
  68. /**
  69. * Automatically freezes any state trees generated by immer.
  70. * This protects against accidental modifications of the state tree outside of an immer function.
  71. * This comes with a performance impact, so it is recommended to disable this option in production.
  72. * By default it is turned on during local development, and turned off in production.
  73. */
  74. declare export function setAutoFreeze(autoFreeze: boolean): void
  75. /**
  76. * Manually override whether proxies should be used.
  77. * By default done by using feature detection
  78. */
  79. declare export function setUseProxies(useProxies: boolean): void
  80. declare export function applyPatches<S>(state: S, patches: Patch[]): S
  81. declare export function original<S>(value: S): ?S