UNPKG

@vue-async/resource-manager

Version:
64 lines 2.35 kB
import Vue from 'vue'; import warning from 'warning'; import { del, add, has } from './Suspense'; import { currentSuspenseInstance } from './currentInstance'; import findSuspenseInstance from './findSuspenseInstance'; export default function Lazy(asyncFactory, props) { return Vue.extend({ name: 'VueSuspenseLazy', props: props, created: function () { var _this = this; asyncFactory.suspenseInstance = currentSuspenseInstance || findSuspenseInstance(this); if (has(asyncFactory)) return; add(asyncFactory); if (asyncFactory.resolved) { asyncFactory.$$waiter.then(function () { del(asyncFactory); }); return; } var promise = asyncFactory(); asyncFactory.$$waiter = promise; promise .then(function (C) { // Compatible ES Module if (C.__esModule && C.default) { C = C.default; } asyncFactory.resolved = C; // Trigger update _this.$forceUpdate(); }) .catch(function (err) { warning(process.env.NODE_ENV === 'production', err.message); del(asyncFactory, err); }); }, updated: function () { del(asyncFactory); }, render: function (h) { var _this = this; // Fix context var slots = Object.keys(this.$slots) .reduce(function (arr, key) { return arr.concat(_this.$slots[key] || []); }, []) .map(function (vnode) { vnode.context = _this._self; return vnode; }); return asyncFactory.resolved ? h(asyncFactory.resolved, { on: this.$listeners, // If there is no props definition, fall back to `this.$attrs` props: props ? this.$props : this.$attrs, // Passthrough scopedSlots scopedSlots: this.$scopedSlots, attrs: this.$attrs, }, slots) : this._e(); }, }); } //# sourceMappingURL=lazy.js.map