@vue-async/resource-manager
Version:
Resource Manager.
139 lines • 4.91 kB
JavaScript
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
import Vue from 'vue';
import warning from 'warning';
import { del, add } from './Suspense';
import { currentInstance, currentSuspenseInstance, setCurrentInstance } from './currentInstance';
import findSuspenseInstance from './findSuspenseInstance';
Vue.mixin({
created: function () {
setCurrentInstance(this);
},
});
//Vue.observable works in 2.6+
function observable(data) {
if (Vue.observable) {
return Vue.observable(data);
}
return new Vue({
data: function () {
return data;
},
}).$data;
}
export default function CreateResource(fetchFactory, options) {
var $res = observable({
$$result: null,
$$error: null,
$$loading: false,
$$loaded: false, // it has loaded data successed once more
});
var currInstance = currentInstance;
fetchFactory.suspenseInstance = currentSuspenseInstance
? currentSuspenseInstance
: findSuspenseInstance(currInstance);
var hasSuspenseInstance = !!fetchFactory.suspenseInstance;
var resourceResult = {
read: function () {
var input = [];
for (var _i = 0; _i < arguments.length; _i++) {
input[_i] = arguments[_i];
}
// prevent
if (options && options.prevent && $res.$$loading) {
return $res.$$promiser;
}
$res.$$loading = true;
// Because we don't need caching, this is just a unique identifier,
// and each call to .read() is a completely new request.
var uniqueWrapFactory = function () {
var i = [];
for (var _i = 0; _i < arguments.length; _i++) {
i[_i] = arguments[_i];
}
return fetchFactory.apply(void 0, __spreadArray([], __read(i), false));
};
if (hasSuspenseInstance) {
// Establish a relationship between the fetchFactory and the current component instance
uniqueWrapFactory.suspenseInstance = fetchFactory.suspenseInstance;
add(uniqueWrapFactory);
}
// Start fetching asynchronous data
var promise = ($res.$$promiser = uniqueWrapFactory.apply(void 0, __spreadArray([], __read(input), false)));
promise
.then(function (res) {
// Trigger update
$res.$$result = options && options.onSuccess ? options.onSuccess(res) : res;
if (!$res.$$loaded) {
$res.$$loaded = true;
}
if (hasSuspenseInstance) {
del(uniqueWrapFactory);
}
})
.catch(function (err) {
warning(process.env.NODE_ENV === 'production', err.message);
$res.$$error = options && options.onError ? options.onError(err) : err;
if (hasSuspenseInstance) {
del(uniqueWrapFactory, err);
}
})
.finally(function () {
$res.$$loading = false;
});
return promise;
},
get $result() {
return $res.$$result;
},
set $result(val) {
$res.$$result = val;
},
get $error() {
return $res.$$error;
},
set $error(val) {
$res.$$error = val;
},
get $loading() {
return $res.$$loading;
},
get $loaded() {
return $res.$$loaded;
},
fork: function () {
return CreateResource(function () {
var i = [];
for (var _i = 0; _i < arguments.length; _i++) {
i[_i] = arguments[_i];
}
return fetchFactory.apply(void 0, __spreadArray([], __read(i), false));
}, options);
},
};
return resourceResult;
}
//# sourceMappingURL=createResource.js.map