@vue-async/resource-manager
Version:
Resource Manager.
145 lines • 5.37 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));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var vue_1 = __importDefault(require("vue"));
var warning_1 = __importDefault(require("warning"));
var Suspense_1 = require("./Suspense");
var currentInstance_1 = require("./currentInstance");
var findSuspenseInstance_1 = __importDefault(require("./findSuspenseInstance"));
vue_1.default.mixin({
created: function () {
(0, currentInstance_1.setCurrentInstance)(this);
},
});
//Vue.observable works in 2.6+
function observable(data) {
if (vue_1.default.observable) {
return vue_1.default.observable(data);
}
return new vue_1.default({
data: function () {
return data;
},
}).$data;
}
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_1.currentInstance;
fetchFactory.suspenseInstance = currentInstance_1.currentSuspenseInstance
? currentInstance_1.currentSuspenseInstance
: (0, findSuspenseInstance_1.default)(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;
(0, Suspense_1.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) {
(0, Suspense_1.del)(uniqueWrapFactory);
}
})
.catch(function (err) {
(0, warning_1.default)(process.env.NODE_ENV === 'production', err.message);
$res.$$error = options && options.onError ? options.onError(err) : err;
if (hasSuspenseInstance) {
(0, Suspense_1.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;
}
exports.default = CreateResource;
//# sourceMappingURL=createResource.js.map
;