vue-use-query
Version:
vue use query
176 lines (175 loc) • 7.11 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import { getLogger } from './logger';
import { notifyManager } from './notifyManager';
import { Retryer } from './retryer';
import { noop } from './utils';
// CLASS
var Mutation = /** @class */ (function () {
function Mutation(config) {
this.options = __assign(__assign({}, config.defaultOptions), config.options);
this.mutationId = config.mutationId;
this.mutationCache = config.mutationCache;
this.observers = [];
this.state = config.state || getDefaultState();
}
Mutation.prototype.setState = function (state) {
this.dispatch({ type: 'setState', state: state });
};
Mutation.prototype.addObserver = function (observer) {
if (this.observers.indexOf(observer) === -1) {
this.observers.push(observer);
}
};
Mutation.prototype.removeObserver = function (observer) {
this.observers = this.observers.filter(function (x) { return x !== observer; });
};
Mutation.prototype.cancel = function () {
if (this.retryer) {
this.retryer.cancel();
return this.retryer.promise.then(noop).catch(noop);
}
return Promise.resolve();
};
Mutation.prototype.continue = function () {
if (this.retryer) {
this.retryer.continue();
return this.retryer.promise;
}
return this.execute();
};
Mutation.prototype.execute = function () {
var _this = this;
var data;
var restored = this.state.status === 'loading';
var promise = Promise.resolve();
if (!restored) {
this.dispatch({ type: 'loading', variables: this.options.variables });
promise = promise
.then(function () { var _a, _b; return (_b = (_a = _this.options).onMutate) === null || _b === void 0 ? void 0 : _b.call(_a, _this.state.variables); })
.then(function (context) {
if (context !== _this.state.context) {
_this.dispatch({
type: 'loading',
context: context,
variables: _this.state.variables,
});
}
});
}
return promise
.then(function () { return _this.executeMutation(); })
.then(function (result) {
data = result;
})
.then(function () {
var _a, _b;
return (_b = (_a = _this.options).onSuccess) === null || _b === void 0 ? void 0 : _b.call(_a, data, _this.state.variables, _this.state.context);
})
.then(function () {
var _a, _b;
return (_b = (_a = _this.options).onSettled) === null || _b === void 0 ? void 0 : _b.call(_a, data, null, _this.state.variables, _this.state.context);
})
.then(function () {
_this.dispatch({ type: 'success', data: data });
return data;
})
.catch(function (error) {
// Notify cache callback
if (_this.mutationCache.config.onError) {
_this.mutationCache.config.onError(error, _this.state.variables, _this.state.context, _this);
}
// Log error
getLogger().error(error);
return Promise.resolve()
.then(function () {
var _a, _b;
return (_b = (_a = _this.options).onError) === null || _b === void 0 ? void 0 : _b.call(_a, error, _this.state.variables, _this.state.context);
})
.then(function () {
var _a, _b;
return (_b = (_a = _this.options).onSettled) === null || _b === void 0 ? void 0 : _b.call(_a, undefined, error, _this.state.variables, _this.state.context);
})
.then(function () {
_this.dispatch({ type: 'error', error: error });
throw error;
});
});
};
Mutation.prototype.executeMutation = function () {
var _this = this;
var _a;
this.retryer = new Retryer({
fn: function () {
if (!_this.options.mutationFn) {
return Promise.reject('No mutationFn found');
}
return _this.options.mutationFn(_this.state.variables);
},
onFail: function () {
_this.dispatch({ type: 'failed' });
},
onPause: function () {
_this.dispatch({ type: 'pause' });
},
onContinue: function () {
_this.dispatch({ type: 'continue' });
},
retry: (_a = this.options.retry) !== null && _a !== void 0 ? _a : 0,
retryDelay: this.options.retryDelay,
});
return this.retryer.promise;
};
Mutation.prototype.dispatch = function (action) {
var _this = this;
this.state = reducer(this.state, action);
notifyManager.batch(function () {
_this.observers.forEach(function (observer) {
observer.onMutationUpdate(action);
});
_this.mutationCache.notify(_this);
});
};
return Mutation;
}());
export { Mutation };
export function getDefaultState() {
return {
context: undefined,
data: undefined,
error: null,
failureCount: 0,
isPaused: false,
status: 'idle',
variables: undefined,
};
}
function reducer(state, action) {
switch (action.type) {
case 'failed':
return __assign(__assign({}, state), { failureCount: state.failureCount + 1 });
case 'pause':
return __assign(__assign({}, state), { isPaused: true });
case 'continue':
return __assign(__assign({}, state), { isPaused: false });
case 'loading':
return __assign(__assign({}, state), { context: action.context, data: undefined, error: null, isPaused: false, status: 'loading', variables: action.variables });
case 'success':
return __assign(__assign({}, state), { data: action.data, error: null, status: 'success', isPaused: false });
case 'error':
return __assign(__assign({}, state), { data: undefined, error: action.error, failureCount: state.failureCount + 1, isPaused: false, status: 'error' });
case 'setState':
return __assign(__assign({}, state), action.state);
default:
return state;
}
}