cdccfa6b6c94fe9cb201f7dd98f20154ecaaec94aec52f37203eaacd7755d54e1fbb3bf6faf4966d7fa151fcfdf46887d9dda67991ffbca8e478606949c928 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { ArrayLike } from 'zrender/lib/core/types.js';
  2. declare type DiffKeyGetter<CTX = unknown> = (this: DataDiffer<CTX>, value: unknown, index: number) => string;
  3. declare type DiffCallbackAdd = (newIndex: number) => void;
  4. declare type DiffCallbackUpdate = (newIndex: number, oldIndex: number) => void;
  5. declare type DiffCallbackRemove = (oldIndex: number) => void;
  6. declare type DiffCallbackUpdateManyToOne = (newIndex: number, oldIndex: number[]) => void;
  7. declare type DiffCallbackUpdateOneToMany = (newIndex: number[], oldIndex: number) => void;
  8. declare type DiffCallbackUpdateManyToMany = (newIndex: number[], oldIndex: number[]) => void;
  9. export declare type DataDiffMode = 'oneToOne' | 'multiple';
  10. declare class DataDiffer<CTX = unknown> {
  11. private _old;
  12. private _new;
  13. private _oldKeyGetter;
  14. private _newKeyGetter;
  15. private _add;
  16. private _update;
  17. private _updateManyToOne;
  18. private _updateOneToMany;
  19. private _updateManyToMany;
  20. private _remove;
  21. private _diffModeMultiple;
  22. readonly context: CTX;
  23. /**
  24. * @param context Can be visited by this.context in callback.
  25. */
  26. constructor(oldArr: ArrayLike<unknown>, newArr: ArrayLike<unknown>, oldKeyGetter?: DiffKeyGetter<CTX>, newKeyGetter?: DiffKeyGetter<CTX>, context?: CTX, diffMode?: DataDiffMode);
  27. /**
  28. * Callback function when add a data
  29. */
  30. add(func: DiffCallbackAdd): this;
  31. /**
  32. * Callback function when update a data
  33. */
  34. update(func: DiffCallbackUpdate): this;
  35. /**
  36. * Callback function when update a data and only work in `cbMode: 'byKey'`.
  37. */
  38. updateManyToOne(func: DiffCallbackUpdateManyToOne): this;
  39. /**
  40. * Callback function when update a data and only work in `cbMode: 'byKey'`.
  41. */
  42. updateOneToMany(func: DiffCallbackUpdateOneToMany): this;
  43. /**
  44. * Callback function when update a data and only work in `cbMode: 'byKey'`.
  45. */
  46. updateManyToMany(func: DiffCallbackUpdateManyToMany): this;
  47. /**
  48. * Callback function when remove a data
  49. */
  50. remove(func: DiffCallbackRemove): this;
  51. execute(): void;
  52. private _executeOneToOne;
  53. /**
  54. * For example, consider the case:
  55. * oldData: [o0, o1, o2, o3, o4, o5, o6, o7],
  56. * newData: [n0, n1, n2, n3, n4, n5, n6, n7, n8],
  57. * Where:
  58. * o0, o1, n0 has key 'a' (many to one)
  59. * o5, n4, n5, n6 has key 'b' (one to many)
  60. * o2, n1 has key 'c' (one to one)
  61. * n2, n3 has key 'd' (add)
  62. * o3, o4 has key 'e' (remove)
  63. * o6, o7, n7, n8 has key 'f' (many to many, treated as add and remove)
  64. * Then:
  65. * (The order of the following directives are not ensured.)
  66. * this._updateManyToOne(n0, [o0, o1]);
  67. * this._updateOneToMany([n4, n5, n6], o5);
  68. * this._update(n1, o2);
  69. * this._remove(o3);
  70. * this._remove(o4);
  71. * this._remove(o6);
  72. * this._remove(o7);
  73. * this._add(n2);
  74. * this._add(n3);
  75. * this._add(n7);
  76. * this._add(n8);
  77. */
  78. private _executeMultiple;
  79. private _performRestAdd;
  80. private _initIndexMap;
  81. }
  82. export default DataDiffer;