mutationObserver.mjs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { getDefaultState } from './mutation.mjs';
  2. import { notifyManager } from './notifyManager.mjs';
  3. import { Subscribable } from './subscribable.mjs';
  4. import { shallowEqualObjects } from './utils.mjs';
  5. // CLASS
  6. class MutationObserver extends Subscribable {
  7. constructor(client, options) {
  8. super();
  9. this.client = client;
  10. this.setOptions(options);
  11. this.bindMethods();
  12. this.updateResult();
  13. }
  14. bindMethods() {
  15. this.mutate = this.mutate.bind(this);
  16. this.reset = this.reset.bind(this);
  17. }
  18. setOptions(options) {
  19. var _this$currentMutation;
  20. const prevOptions = this.options;
  21. this.options = this.client.defaultMutationOptions(options);
  22. if (!shallowEqualObjects(prevOptions, this.options)) {
  23. this.client.getMutationCache().notify({
  24. type: 'observerOptionsUpdated',
  25. mutation: this.currentMutation,
  26. observer: this
  27. });
  28. }
  29. (_this$currentMutation = this.currentMutation) == null ? void 0 : _this$currentMutation.setOptions(this.options);
  30. }
  31. onUnsubscribe() {
  32. if (!this.hasListeners()) {
  33. var _this$currentMutation2;
  34. (_this$currentMutation2 = this.currentMutation) == null ? void 0 : _this$currentMutation2.removeObserver(this);
  35. }
  36. }
  37. onMutationUpdate(action) {
  38. this.updateResult(); // Determine which callbacks to trigger
  39. const notifyOptions = {
  40. listeners: true
  41. };
  42. if (action.type === 'success') {
  43. notifyOptions.onSuccess = true;
  44. } else if (action.type === 'error') {
  45. notifyOptions.onError = true;
  46. }
  47. this.notify(notifyOptions);
  48. }
  49. getCurrentResult() {
  50. return this.currentResult;
  51. }
  52. reset() {
  53. this.currentMutation = undefined;
  54. this.updateResult();
  55. this.notify({
  56. listeners: true
  57. });
  58. }
  59. mutate(variables, options) {
  60. this.mutateOptions = options;
  61. if (this.currentMutation) {
  62. this.currentMutation.removeObserver(this);
  63. }
  64. this.currentMutation = this.client.getMutationCache().build(this.client, { ...this.options,
  65. variables: typeof variables !== 'undefined' ? variables : this.options.variables
  66. });
  67. this.currentMutation.addObserver(this);
  68. return this.currentMutation.execute();
  69. }
  70. updateResult() {
  71. const state = this.currentMutation ? this.currentMutation.state : getDefaultState();
  72. const result = { ...state,
  73. isLoading: state.status === 'loading',
  74. isSuccess: state.status === 'success',
  75. isError: state.status === 'error',
  76. isIdle: state.status === 'idle',
  77. mutate: this.mutate,
  78. reset: this.reset
  79. };
  80. this.currentResult = result;
  81. }
  82. notify(options) {
  83. notifyManager.batch(() => {
  84. // First trigger the mutate callbacks
  85. if (this.mutateOptions && this.hasListeners()) {
  86. if (options.onSuccess) {
  87. var _this$mutateOptions$o, _this$mutateOptions, _this$mutateOptions$o2, _this$mutateOptions2;
  88. (_this$mutateOptions$o = (_this$mutateOptions = this.mutateOptions).onSuccess) == null ? void 0 : _this$mutateOptions$o.call(_this$mutateOptions, this.currentResult.data, this.currentResult.variables, this.currentResult.context);
  89. (_this$mutateOptions$o2 = (_this$mutateOptions2 = this.mutateOptions).onSettled) == null ? void 0 : _this$mutateOptions$o2.call(_this$mutateOptions2, this.currentResult.data, null, this.currentResult.variables, this.currentResult.context);
  90. } else if (options.onError) {
  91. var _this$mutateOptions$o3, _this$mutateOptions3, _this$mutateOptions$o4, _this$mutateOptions4;
  92. (_this$mutateOptions$o3 = (_this$mutateOptions3 = this.mutateOptions).onError) == null ? void 0 : _this$mutateOptions$o3.call(_this$mutateOptions3, this.currentResult.error, this.currentResult.variables, this.currentResult.context);
  93. (_this$mutateOptions$o4 = (_this$mutateOptions4 = this.mutateOptions).onSettled) == null ? void 0 : _this$mutateOptions$o4.call(_this$mutateOptions4, undefined, this.currentResult.error, this.currentResult.variables, this.currentResult.context);
  94. }
  95. } // Then trigger the listeners
  96. if (options.listeners) {
  97. this.listeners.forEach(({
  98. listener
  99. }) => {
  100. listener(this.currentResult);
  101. });
  102. }
  103. });
  104. }
  105. }
  106. export { MutationObserver };
  107. //# sourceMappingURL=mutationObserver.mjs.map