@badrap/preload
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -1,2 +0,2 @@ | ||
const n=function(){function n(){}return n.prototype.then=function(e,r){const o=new n,i=this.s;if(i){const n=1&i?e:r;if(n){try{t(o,1,n(this.v))}catch(n){t(o,2,n)}return o}return this}return this.o=function(n){try{const i=n.v;1&n.s?t(o,1,e?e(i):i):r?t(o,1,r(i)):t(o,2,i)}catch(n){t(o,2,n)}},o},n}();function t(e,r,o){if(!e.s){if(o instanceof n){if(!o.s)return void(o.o=t.bind(null,e,r));1&r&&(r=o.s),o=o.v}if(o&&o.then)return void o.then(t.bind(null,e,r),t.bind(null,e,2));e.s=r,e.v=o;const i=e.o;i&&i(e)}}function e(t){return t instanceof n&&1&t.s}var r=function(n,t){return n.map(function(n){return t(Object.assign({},n,{children:n.children&&r(n.children,t)}))})},o={functional:!0,props:["status","error"],render:function(n,t){return n("div",{},[t.props.status+" "+t.props.error.message])}},i=Symbol(),u=Symbol(),c=function(n,t){return void 0===n&&(n=404),void 0===t&&(t="Not found"),{$type:i,status:n,error:t instanceof Error?t:{message:t}}},f=function(n){return{$type:u,to:n}},s=function(n){return"function"!=typeof n?Promise.resolve(n):new Promise(function(t,e){Promise.resolve(n(t,e)).then(t,e)}).then(function(n){return n.__esModule?n.default:n})};module.exports=function(a,h){void 0===h&&(h={});var v=h.context;void 0===v&&(v={});var d=h.errorComponent;void 0===d&&(d=o);var l,p=h.beforePreload,m=h.afterPreload,b=function(r){try{var o=!1;function a(n){if(o)return n;l={provide:function(){var n;return(n={})[y]=h,n},render:function(n){return n("router-view",{attrs:Object.assign({},this.$attrs)})}}}var h={};p&&p();var b=function(a,p){try{var m=(b=0,function(r,o,i){for(var u;;){var c=r();if(e(c)&&(c=c.v),!c)return f;if(c.then){u=0;break}var f=i();if(f&&f.then){if(!e(f)){u=1;break}f=f.s}if(o){var s=o();if(s&&s.then&&!e(s)){u=2;break}}}var a=new n,h=t.bind(null,a,2);return(0===u?c.then(d):1===u?f.then(v):s.then(l)).then(void 0,h),a;function v(n){f=n;do{if(o&&(s=o())&&s.then&&!e(s))return void s.then(l).then(void 0,h);if(!(c=r())||e(c)&&!c.v)return void t(a,1,f);if(c.then)return void c.then(d).then(void 0,h);e(f=i())&&(f=f.v)}while(!f||!f.then);f.then(v).then(void 0,h)}function d(n){n?(f=i())&&f.then?f.then(v).then(void 0,h):v(f):t(a,1,f)}function l(){(c=r())?c.then?c.then(d).then(void 0,h):d(c):t(a,1,f)}}(function(){return!o&&b<r.matched.length},function(){return b++},function(){var a=r.matched[b],p=Object.keys(a.components);return function(m,b,P){var j,g,$=-1;return function b(k){try{for(;++$<m.length&&(!P||!P());)if((k=Promise.resolve(s(a.components[p[$]])).then(function(n){if(n&&n[y]){var t=n[y],e=t.key;return Promise.resolve(Promise.resolve((0,t.preload)(Object.assign({},{route:r,redirect:f,error:c},v)))).then(function(n){return n&&n.$type===u?(o=!0,n.to):n&&n.$type===i?(l={render:function(t){return t(d,{props:{status:n.status,error:n.error}})}},void(o=!0)):void(h[e]=n)})}}))&&k.then){if(!e(k))return void k.then(b,g||(g=t.bind(null,j=new n,2)));k=k.v}j?t(j,1,k):j=k}catch(n){t(j||(j=new Pact),2,n)}}(),j}(p,0,function(){return o})}))}catch(n){return p(!0,n)}var b;return m&&m.then?m.then(p.bind(null,!1),p.bind(null,!0)):p(!1,value)}(0,function(n,t){if(m&&m(),n)throw t;return t});return Promise.resolve(b&&b.then?b.then(a):a(b))}catch(n){return Promise.reject(n)}},y=Symbol(),P=function(n){var t=null;return function(){return t||(t=s(n).then(function(n){var t;if(!n.preload)return n;var e=Symbol();return(t={extends:n})[y]={key:e,preload:n.preload},t.inject={$preload:y},t.data=function(){return Object.assign({},this.$preload[e])},t},function(n){return t=null,Promise.reject(n)})),t}};return[{path:"",component:{beforeRouteEnter:function(n,t,e){return b(n).then(e,e)},beforeRouteUpdate:function(n,t,e){return b(n).then(e,e)},render:function(n){return n(l,{key:this.$route.fullPath,attrs:this.$attrs})}},children:r(a,function(n){var t=Object.assign({},n);if(t.component&&(t.component=P(t.component)),t.components){var e={};Object.keys(t.components).forEach(function(n){e[n]=P(t.components[n])}),t.components=e}return t})}]}; | ||
function e(e,r){return new Promise(function(n,t){!function o(u){try{u>=e.length?n({break:!1}):Promise.resolve(r(e[u])).then(function(e){e&&e.break?n(e):o(u+1)},t)}catch(e){t(e)}}(0)})}var r=function(e,n){return e.map(function(e){return n(Object.assign({},e,{children:e.children&&r(e.children,n)}))})},n={functional:!0,props:["status","error"],render:function(e,r){return e("div",{},[r.props.status+" "+r.props.error.message])}},t=Symbol(),o=Symbol(),u=function(e,r){return void 0===e&&(e=404),void 0===r&&(r="Not found"),{$type:t,status:e,error:r instanceof Error?r:{message:r}}},i=function(e){return{$type:o,to:e}},c=function(e){return"function"!=typeof e?Promise.resolve(e):new Promise(function(r,n){Promise.resolve(e(r,n)).then(r,n)}).then(function(e){return e.__esModule?e.default:e})};module.exports=function(s,a){void 0===a&&(a={});var f=a.context;void 0===f&&(f={});var p=a.errorComponent;void 0===p&&(p=n);var d,l=a.beforePreload,m=a.afterPreload,v=Symbol(),h=function(e){var r=null;return function(){return r||(r=c(e).then(function(e){var r;if(!e.preload)return e;var n=Symbol();return(r={extends:e})[v]={key:n,preload:e.preload},r.inject={$preload:v},r.data=function(){return Object.assign({},this.$preload[n])},r},function(e){return r=null,Promise.reject(e)})),r}};function b(r,n,s){var a={};return l&&l(),e(r.matched,function(n){return e(Object.keys(n.components),function(e){return c(n.components[e]).then(function(e){if(e&&e[v]){var n=e[v],c=n.key;return Promise.resolve((0,n.preload)(Object.assign({},{route:r,redirect:i,error:u},f))).then(function(e){return e&&e.$type===o?{break:!0,value:e.to}:e&&e.$type===t?(d={render:function(r){return r(p,{props:{status:e.status,error:e.error}})}},{break:!0}):void(a[c]=e)})}})})}).then(function(e){if(m&&m(),e.break)return e.value;d={provide:function(){var e;return(e={})[v]=a,e},render:function(e){return e("router-view",{attrs:Object.assign({},this.$attrs)})}}},function(e){throw m&&m(),e}).then(s,s)}return[{path:"",component:{beforeRouteEnter:b,beforeRouteUpdate:b,render:function(e){return e(d,{key:this.$route.fullPath,attrs:this.$attrs})}},children:r(s,function(e){var r=Object.assign({},e);if(r.component&&(r.component=h(r.component)),r.components){var n={};Object.keys(r.components).forEach(function(e){n[e]=h(r.components[e])}),r.components=n}return r})}]}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@badrap/preload", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Data preloading for vue-router, similar to Sapper's preload or Nuxt.js's asyncData", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
145
src/index.js
@@ -0,1 +1,34 @@ | ||
// Execute `callback` for each `array` element asynchronously, | ||
// without building a memory-hogging promise chain. | ||
// | ||
// The callback may return a promise/thenable, in which case the | ||
// value will then be resolved before moving on to the next element. | ||
// To facilitate early exits, the callback's return value may resolve | ||
// to an object with a truthy property `break`. | ||
// | ||
// Return a promise that resolves to either the `callback`'s early | ||
// exit value or `{ break: false }` otherwise. | ||
function asyncFor(array, callback) { | ||
return new Promise((resolve, reject) => { | ||
function step(index) { | ||
try { | ||
if (index >= array.length) { | ||
resolve({ break: false }); | ||
} else { | ||
Promise.resolve(callback(array[index])).then(result => { | ||
if (result && result.break) { | ||
resolve(result); | ||
} else { | ||
step(index + 1); | ||
} | ||
}, reject); | ||
} | ||
} catch (err) { | ||
reject(err); | ||
} | ||
} | ||
step(0); | ||
}); | ||
} | ||
const mapRoutes = (routes, func) => { | ||
@@ -47,5 +80,4 @@ return routes.map(route => { | ||
export default function preload( | ||
routes, | ||
{ | ||
export default function preload(routes, options = {}) { | ||
const { | ||
context = {}, | ||
@@ -55,4 +87,4 @@ errorComponent = defaultErrorComponent, | ||
afterPreload | ||
} = {} | ||
) { | ||
} = options; | ||
let component; | ||
@@ -110,3 +142,3 @@ const preloadKey = Symbol(); | ||
async function beforeRoute(to) { | ||
function beforeRoute(to, _, next) { | ||
const datas = {}; | ||
@@ -118,53 +150,62 @@ | ||
try { | ||
for (let i = 0; i < to.matched.length; i++) { | ||
const route = to.matched[i]; | ||
const keys = Object.keys(route.components); | ||
for (let j = 0; j < keys.length; j++) { | ||
const comp = await componentPromise(route.components[keys[j]]); | ||
return asyncFor(to.matched, route => { | ||
return asyncFor(Object.keys(route.components), key => { | ||
return componentPromise(route.components[key]).then(comp => { | ||
if (!comp || !comp[preloadKey]) { | ||
continue; | ||
return; | ||
} | ||
const { key, preload } = comp[preloadKey]; | ||
const data = await Promise.resolve( | ||
return Promise.resolve( | ||
preload({ route: to, redirect, error, ...context }) | ||
); | ||
if (data && data.$type === ACTION_REDIRECT) { | ||
return data.to; | ||
).then(data => { | ||
if (data && data.$type === ACTION_REDIRECT) { | ||
return { break: true, value: data.to }; | ||
} | ||
if (data && data.$type === ACTION_ERROR) { | ||
component = { | ||
render(h) { | ||
return h(errorComponent, { | ||
props: { | ||
status: data.status, | ||
error: data.error | ||
} | ||
}); | ||
} | ||
}; | ||
return { break: true }; | ||
} | ||
datas[key] = data; | ||
}); | ||
}); | ||
}); | ||
}) | ||
.then( | ||
result => { | ||
if (afterPreload) { | ||
afterPreload(); | ||
} | ||
if (data && data.$type === ACTION_ERROR) { | ||
component = { | ||
render(h) { | ||
return h(errorComponent, { | ||
props: { | ||
status: data.status, | ||
error: data.error | ||
} | ||
}); | ||
} | ||
}; | ||
return; | ||
if (result.break) { | ||
return result.value; | ||
} | ||
datas[key] = data; | ||
component = { | ||
provide() { | ||
return { | ||
[preloadKey]: datas | ||
}; | ||
}, | ||
render(h) { | ||
return h("router-view", { | ||
attrs: { ...this.$attrs } | ||
}); | ||
} | ||
}; | ||
}, | ||
err => { | ||
if (afterPreload) { | ||
afterPreload(); | ||
} | ||
throw err; | ||
} | ||
} | ||
} finally { | ||
if (afterPreload) { | ||
afterPreload(); | ||
} | ||
} | ||
component = { | ||
provide() { | ||
return { | ||
[preloadKey]: datas | ||
}; | ||
}, | ||
render(h) { | ||
return h("router-view", { | ||
attrs: { ...this.$attrs } | ||
}); | ||
} | ||
}; | ||
) | ||
.then(next, next); | ||
} | ||
@@ -176,4 +217,4 @@ | ||
component: { | ||
beforeRouteEnter: (to, _, next) => beforeRoute(to).then(next, next), | ||
beforeRouteUpdate: (to, _, next) => beforeRoute(to).then(next, next), | ||
beforeRouteEnter: beforeRoute, | ||
beforeRouteUpdate: beforeRoute, | ||
render(h) { | ||
@@ -180,0 +221,0 @@ return h(component, { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
231
35292