123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- import { getDefaultState } from './mutation.esm.js';
- import { notifyManager } from './notifyManager.esm.js';
- import { Subscribable } from './subscribable.esm.js';
- import { shallowEqualObjects } from './utils.esm.js';
- // CLASS
- class MutationObserver extends Subscribable {
- constructor(client, options) {
- super();
- this.client = client;
- this.setOptions(options);
- this.bindMethods();
- this.updateResult();
- }
- bindMethods() {
- this.mutate = this.mutate.bind(this);
- this.reset = this.reset.bind(this);
- }
- setOptions(options) {
- var _this$currentMutation;
- const prevOptions = this.options;
- this.options = this.client.defaultMutationOptions(options);
- if (!shallowEqualObjects(prevOptions, this.options)) {
- this.client.getMutationCache().notify({
- type: 'observerOptionsUpdated',
- mutation: this.currentMutation,
- observer: this
- });
- }
- (_this$currentMutation = this.currentMutation) == null ? void 0 : _this$currentMutation.setOptions(this.options);
- }
- onUnsubscribe() {
- if (!this.hasListeners()) {
- var _this$currentMutation2;
- (_this$currentMutation2 = this.currentMutation) == null ? void 0 : _this$currentMutation2.removeObserver(this);
- }
- }
- onMutationUpdate(action) {
- this.updateResult(); // Determine which callbacks to trigger
- const notifyOptions = {
- listeners: true
- };
- if (action.type === 'success') {
- notifyOptions.onSuccess = true;
- } else if (action.type === 'error') {
- notifyOptions.onError = true;
- }
- this.notify(notifyOptions);
- }
- getCurrentResult() {
- return this.currentResult;
- }
- reset() {
- this.currentMutation = undefined;
- this.updateResult();
- this.notify({
- listeners: true
- });
- }
- mutate(variables, options) {
- this.mutateOptions = options;
- if (this.currentMutation) {
- this.currentMutation.removeObserver(this);
- }
- this.currentMutation = this.client.getMutationCache().build(this.client, { ...this.options,
- variables: typeof variables !== 'undefined' ? variables : this.options.variables
- });
- this.currentMutation.addObserver(this);
- return this.currentMutation.execute();
- }
- updateResult() {
- const state = this.currentMutation ? this.currentMutation.state : getDefaultState();
- const result = { ...state,
- isLoading: state.status === 'loading',
- isSuccess: state.status === 'success',
- isError: state.status === 'error',
- isIdle: state.status === 'idle',
- mutate: this.mutate,
- reset: this.reset
- };
- this.currentResult = result;
- }
- notify(options) {
- notifyManager.batch(() => {
- // First trigger the mutate callbacks
- if (this.mutateOptions && this.hasListeners()) {
- if (options.onSuccess) {
- var _this$mutateOptions$o, _this$mutateOptions, _this$mutateOptions$o2, _this$mutateOptions2;
- (_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);
- (_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);
- } else if (options.onError) {
- var _this$mutateOptions$o3, _this$mutateOptions3, _this$mutateOptions$o4, _this$mutateOptions4;
- (_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);
- (_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);
- }
- } // Then trigger the listeners
- if (options.listeners) {
- this.listeners.forEach(({
- listener
- }) => {
- listener(this.currentResult);
- });
- }
- });
- }
- }
- export { MutationObserver };
- //# sourceMappingURL=mutationObserver.esm.js.map
|